From 1877bd38b9c4e06187b7e95c2cd67870acfb0b14 Mon Sep 17 00:00:00 2001 From: Brian Warner Date: Fri, 9 Sep 2016 00:25:21 -0700 Subject: [PATCH] consolidate skip_if_cannot_represent_filename() Remove duplicate copies of this utility, move it from a mixin/test-class method to being a simple function in common_util.py --- src/allmydata/test/cli/common.py | 15 +-------------- src/allmydata/test/cli/test_cli.py | 4 ++-- src/allmydata/test/cli/test_cp.py | 5 +++-- src/allmydata/test/cli/test_put.py | 3 ++- src/allmydata/test/common_util.py | 11 +++++++++++ src/allmydata/test/test_backupdb.py | 18 ++++-------------- src/allmydata/test/test_encodingutil.py | 12 ++---------- 7 files changed, 25 insertions(+), 43 deletions(-) diff --git a/src/allmydata/test/cli/common.py b/src/allmydata/test/cli/common.py index e41825df6..da9a708dc 100644 --- a/src/allmydata/test/cli/common.py +++ b/src/allmydata/test/cli/common.py @@ -1,10 +1,7 @@ from cStringIO import StringIO -from twisted.trial import unittest from twisted.internet import threads # CLI tests use deferToThread from ...util.assertutil import precondition -from ...util.encodingutil import (unicode_platform, - get_filesystem_encoding, - unicode_to_argv) +from ...util.encodingutil import unicode_to_argv from ...scripts import runner from ..common_util import ReallyEqualMixin @@ -36,13 +33,3 @@ class CLITestMixin(ReallyEqualMixin): return rc, stdout.getvalue(), stderr.getvalue() d.addCallback(_done) return d - - def skip_if_cannot_represent_filename(self, u): - precondition(isinstance(u, unicode)) - - enc = get_filesystem_encoding() - if not unicode_platform(): - try: - u.encode(enc) - except UnicodeEncodeError: - raise unittest.SkipTest("A non-ASCII filename could not be encoded on this platform.") diff --git a/src/allmydata/test/cli/test_cli.py b/src/allmydata/test/cli/test_cli.py index 932fae373..653a5c776 100644 --- a/src/allmydata/test/cli/test_cli.py +++ b/src/allmydata/test/cli/test_cli.py @@ -29,7 +29,7 @@ from allmydata.scripts.common import DEFAULT_ALIAS, get_aliases, get_alias, \ DefaultAliasMarker from allmydata.scripts import cli, debug, runner -from ..common_util import ReallyEqualMixin +from ..common_util import ReallyEqualMixin, skip_if_cannot_represent_filename from ..no_network import GridTestMixin from .common import CLITestMixin, parse_options from twisted.internet import threads # CLI tests use deferToThread @@ -493,7 +493,7 @@ class CLI(CLITestMixin, unittest.TestCase): filenames = [u'L\u00F4zane', u'Bern', u'Gen\u00E8ve'] # must be NFC for name in filenames: - self.skip_if_cannot_represent_filename(name) + skip_if_cannot_represent_filename(name) basedir = "cli/common/listdir_unicode_good" fileutil.make_dirs(basedir) diff --git a/src/allmydata/test/cli/test_cp.py b/src/allmydata/test/cli/test_cp.py index 08666ae85..20136e978 100644 --- a/src/allmydata/test/cli/test_cp.py +++ b/src/allmydata/test/cli/test_cp.py @@ -10,6 +10,7 @@ from allmydata.util.encodingutil import (quote_output, get_io_encoding, from allmydata.util.assertutil import _assert from ..no_network import GridTestMixin from .common import CLITestMixin +from ..common_util import skip_if_cannot_represent_filename timeout = 480 # deep_check takes 360s on Zandr's linksys box, others take > 240s @@ -30,7 +31,7 @@ class Cp(GridTestMixin, CLITestMixin, unittest.TestCase): except UnicodeEncodeError: raise unittest.SkipTest("A non-ASCII command argument could not be encoded on this platform.") - self.skip_if_cannot_represent_filename(fn1) + skip_if_cannot_represent_filename(fn1) self.set_up_grid(oneshare=True) @@ -198,7 +199,7 @@ class Cp(GridTestMixin, CLITestMixin, unittest.TestCase): except UnicodeEncodeError: raise unittest.SkipTest("A non-ASCII command argument could not be encoded on this platform.") - self.skip_if_cannot_represent_filename(fn1) + skip_if_cannot_represent_filename(fn1) self.set_up_grid(oneshare=True) diff --git a/src/allmydata/test/cli/test_put.py b/src/allmydata/test/cli/test_put.py index 99b6182ac..c62bdab85 100644 --- a/src/allmydata/test/cli/test_put.py +++ b/src/allmydata/test/cli/test_put.py @@ -6,6 +6,7 @@ from allmydata.util import fileutil from allmydata.scripts.common import get_aliases from allmydata.scripts import cli from ..no_network import GridTestMixin +from ..common_util import skip_if_cannot_represent_filename from allmydata.util.encodingutil import get_io_encoding, unicode_to_argv from allmydata.util.fileutil import abspath_expanduser_unicode from .common import CLITestMixin @@ -427,7 +428,7 @@ class Put(GridTestMixin, CLITestMixin, unittest.TestCase): except UnicodeEncodeError: raise unittest.SkipTest("A non-ASCII command argument could not be encoded on this platform.") - self.skip_if_cannot_represent_filename(u"\u00E0 trier.txt") + skip_if_cannot_represent_filename(u"\u00E0 trier.txt") self.basedir = "cli/Put/immutable_from_file_unicode" self.set_up_grid(oneshare=True) diff --git a/src/allmydata/test/common_util.py b/src/allmydata/test/common_util.py index 9d6bdf3f4..92e105b4b 100644 --- a/src/allmydata/test/common_util.py +++ b/src/allmydata/test/common_util.py @@ -3,10 +3,21 @@ from random import randrange from twisted.internet import reactor, defer from twisted.python import failure +from twisted.trial import unittest from allmydata.util import fileutil, log +from ..util.assertutil import precondition from allmydata.util.encodingutil import unicode_platform, get_filesystem_encoding +def skip_if_cannot_represent_filename(u): + precondition(isinstance(u, unicode)) + + enc = get_filesystem_encoding() + if not unicode_platform(): + try: + u.encode(enc) + except UnicodeEncodeError: + raise unittest.SkipTest("A non-ASCII filename could not be encoded on this platform.") class DevNullDictionary(dict): def __setitem__(self, key, value): diff --git a/src/allmydata/test/test_backupdb.py b/src/allmydata/test/test_backupdb.py index 835e25315..979c20016 100644 --- a/src/allmydata/test/test_backupdb.py +++ b/src/allmydata/test/test_backupdb.py @@ -4,9 +4,9 @@ from StringIO import StringIO from twisted.trial import unittest from allmydata.util import fileutil -from allmydata.util.encodingutil import listdir_unicode, get_filesystem_encoding, unicode_platform -from allmydata.util.assertutil import precondition +from allmydata.util.encodingutil import listdir_unicode from allmydata.scripts import backupdb +from .common_util import skip_if_cannot_represent_filename class BackupDB(unittest.TestCase): def create(self, dbfile): @@ -15,16 +15,6 @@ class BackupDB(unittest.TestCase): self.failUnless(bdb, "unable to create backupdb from %r" % (dbfile,)) return bdb - def skip_if_cannot_represent_filename(self, u): - precondition(isinstance(u, unicode)) - - enc = get_filesystem_encoding() - if not unicode_platform(): - try: - u.encode(enc) - except UnicodeEncodeError: - raise unittest.SkipTest("A non-ASCII filename could not be encoded on this platform.") - def test_basic(self): self.basedir = basedir = os.path.join("backupdb", "create") fileutil.make_dirs(basedir) @@ -222,8 +212,8 @@ class BackupDB(unittest.TestCase): self.failIf(r.was_created()) def test_unicode(self): - self.skip_if_cannot_represent_filename(u"f\u00f6\u00f6.txt") - self.skip_if_cannot_represent_filename(u"b\u00e5r.txt") + skip_if_cannot_represent_filename(u"f\u00f6\u00f6.txt") + skip_if_cannot_represent_filename(u"b\u00e5r.txt") self.basedir = basedir = os.path.join("backupdb", "unicode") fileutil.make_dirs(basedir) diff --git a/src/allmydata/test/test_encodingutil.py b/src/allmydata/test/test_encodingutil.py index 0632bea03..a5152c3e0 100644 --- a/src/allmydata/test/test_encodingutil.py +++ b/src/allmydata/test/test_encodingutil.py @@ -71,7 +71,7 @@ from allmydata.util.encodingutil import argv_to_unicode, unicode_to_url, \ get_io_encoding, get_filesystem_encoding, to_str, from_utf8_or_none, _reload, \ to_filepath, extend_filepath, unicode_from_filepath, unicode_segments_from from allmydata.dirnode import normalize - +from .common_util import skip_if_cannot_represent_filename from twisted.python import usage @@ -265,16 +265,8 @@ class StdlibUnicode(unittest.TestCase): """This mainly tests that some of the stdlib functions support Unicode paths, but also that listdir_unicode works for valid filenames.""" - def skip_if_cannot_represent_filename(self, u): - enc = get_filesystem_encoding() - if not unicode_platform(): - try: - u.encode(enc) - except UnicodeEncodeError: - raise unittest.SkipTest("A non-ASCII filename could not be encoded on this platform.") - def test_mkdir_open_exists_abspath_listdir_expanduser(self): - self.skip_if_cannot_represent_filename(lumiere_nfc) + skip_if_cannot_represent_filename(lumiere_nfc) try: os.mkdir(lumiere_nfc)