Add and make use of filepath_to_abspath. refs #2375
authorDaira Hopwood <daira@jacaranda.org>
Thu, 5 Feb 2015 21:53:10 +0000 (21:53 +0000)
committerDaira Hopwood <daira@jacaranda.org>
Thu, 5 Feb 2015 21:53:10 +0000 (21:53 +0000)
Signed-off-by: Daira Hopwood <daira@jacaranda.org>
src/allmydata/test/test_auth.py
src/allmydata/test/test_encodingutil.py
src/allmydata/util/encodingutil.py

index 2b52f00af1d711f4e558d23f26944e2f37f2694e..193f90fd0325648c737aa12f0313503aaa2b919d 100644 (file)
@@ -5,7 +5,7 @@ from twisted.conch import error as conch_error
 from twisted.conch.ssh import keys
 
 from allmydata.frontends import auth
-from allmydata.util.fileutil import abspath_expanduser_unicode
+from allmydata.util.encodingutil import filepath_to_abspath
 
 
 DUMMY_KEY = keys.Key.fromString("""\
@@ -39,7 +39,7 @@ class AccountFileCheckerKeyTests(unittest.TestCase):
     def setUp(self):
         self.account_file = filepath.FilePath(self.mktemp())
         self.account_file.setContent(DUMMY_ACCOUNTS)
-        abspath = abspath_expanduser_unicode(unicode(self.account_file.path))
+        abspath = filepath_to_abspath(self.account_file)
         self.checker = auth.AccountFileChecker(None, abspath)
 
     def test_unknown_user(self):
index 98156c6f19cd9c8204478ba48c7ad3cb36c7dbbf..682f3319232438e6a6d1b97c4126019b5e8d9a84 100644 (file)
@@ -62,13 +62,14 @@ import os, sys, locale
 
 from allmydata.test.common_util import ReallyEqualMixin
 from allmydata.util import encodingutil, fileutil
-from allmydata.util.encodingutil import argv_to_unicode, unicode_to_url, \
-    unicode_to_output, quote_output, quote_path, quote_local_unicode_path, \
+from allmydata.util.encodingutil import argv_to_unicode, filepath_to_abspath, \
+    unicode_to_url, unicode_to_output, quote_output, quote_path, quote_local_unicode_path, \
     unicode_platform, listdir_unicode, FilenameEncodingError, get_io_encoding, \
     get_filesystem_encoding, _reload
 from allmydata.dirnode import normalize
 
 from twisted.python import usage
+from twisted.python.filepath import FilePath
 
 class EncodingUtilErrors(ReallyEqualMixin, unittest.TestCase):
 
@@ -221,12 +222,7 @@ class EncodingUtil(ReallyEqualMixin):
         _reload()
         self.failUnlessReallyEqual(unicode_platform(), matrix[self.platform])
 
-    @patch('sys.getfilesystemencoding')
-    @patch('os.listdir')
-    def test_listdir_unicode(self, mock_listdir, mock_getfilesystemencoding):
-        if 'dirlist' not in dir(self):
-            return
-
+    def skip_if_cannot_encode_for_filesystem(self):
         try:
             u"test".encode(self.filesystem_encoding)
         except (LookupError, AttributeError):
@@ -234,6 +230,14 @@ class EncodingUtil(ReallyEqualMixin):
                                     "that we are testing for the benefit of a different platform."
                                     % (self.filesystem_encoding,))
 
+    @patch('sys.getfilesystemencoding')
+    @patch('os.listdir')
+    def test_listdir_unicode(self, mock_listdir, mock_getfilesystemencoding):
+        if 'dirlist' not in dir(self):
+            return
+
+        self.skip_if_cannot_encode_for_filesystem()
+
         mock_listdir.return_value = self.dirlist
         mock_getfilesystemencoding.return_value = self.filesystem_encoding
 
@@ -243,6 +247,17 @@ class EncodingUtil(ReallyEqualMixin):
         self.failUnlessEqual(set([normalize(fname) for fname in filenames]),
                              set(TEST_FILENAMES))
 
+    @patch('sys.getfilesystemencoding')
+    def test_filepath_to_abspath(self, mock_getfilesystemencoding):
+        self.skip_if_cannot_encode_for_filesystem()
+
+        mock_getfilesystemencoding.return_value = self.filesystem_encoding
+        _reload()
+
+        filename = lumiere_nfc
+        self.failUnlessReallyEqual(filepath_to_abspath(FilePath(filename)),
+                                   fileutil.abspath_expanduser_unicode(filename))
+
 
 class StdlibUnicode(unittest.TestCase):
     """This mainly tests that some of the stdlib functions support Unicode paths, but also that
index feafd8f5c46e6470981c68e14922e1b223af99c2..54f29c9c698883ec5b7317c1b1a5112e011c9558 100644 (file)
@@ -96,6 +96,16 @@ def argv_to_abspath(s):
     """
     return abspath_expanduser_unicode(argv_to_unicode(s))
 
+def filepath_to_abspath(fp):
+    """
+    Convenience function to extract an absolute path from a Twisted FilePath.
+    """
+    path = fp.path
+    if not isinstance(path, unicode):
+        path = path.decode(filesystem_encoding)
+
+    return abspath_expanduser_unicode(path)
+
 def unicode_to_argv(s, mangle=False):
     """
     Encode the given Unicode argument as a bytestring.