]> git.rkrishnan.org Git - tahoe-lafs/tahoe-lafs.git/commitdiff
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>
Tue, 31 Mar 2015 16:52:38 +0000 (17:52 +0100)
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 5613a1d6af721ec018e87d4f272f019ec02d053f..1e59ec41e672f30109b24f1c85ba1ca637869af4 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, to_str, from_utf8_or_none, _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 d14b08f6be448782a454fefd1da22d6d8e9e45eb..bc72b9d16fc0a10c8ca14bfe34ae53c6bd496594 100644 (file)
@@ -95,6 +95,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.