From: Daira Hopwood Date: Thu, 5 Feb 2015 21:53:10 +0000 (+0000) Subject: Add and make use of filepath_to_abspath. refs #2375 X-Git-Url: https://git.rkrishnan.org/?a=commitdiff_plain;h=e812d9f5a3e5b3eba57846bb844ac38574b0447b;p=tahoe-lafs%2Ftahoe-lafs.git Add and make use of filepath_to_abspath. refs #2375 Signed-off-by: Daira Hopwood --- diff --git a/src/allmydata/test/test_auth.py b/src/allmydata/test/test_auth.py index 2b52f00a..193f90fd 100644 --- a/src/allmydata/test/test_auth.py +++ b/src/allmydata/test/test_auth.py @@ -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): diff --git a/src/allmydata/test/test_encodingutil.py b/src/allmydata/test/test_encodingutil.py index 5613a1d6..1e59ec41 100644 --- a/src/allmydata/test/test_encodingutil.py +++ b/src/allmydata/test/test_encodingutil.py @@ -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 diff --git a/src/allmydata/util/encodingutil.py b/src/allmydata/util/encodingutil.py index d14b08f6..bc72b9d1 100644 --- a/src/allmydata/util/encodingutil.py +++ b/src/allmydata/util/encodingutil.py @@ -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.