From 47aaaa015b34d23fb8a2bf3bc808d6e94d22dfa0 Mon Sep 17 00:00:00 2001 From: Brett Cannon Date: Fri, 27 Oct 2017 11:54:25 -0700 Subject: [PATCH] Factor out common tests --- importlib_resources/tests/test_open.py | 56 +++---------------------- importlib_resources/tests/test_path.py | 57 +++----------------------- importlib_resources/tests/test_read.py | 47 +++------------------ importlib_resources/tests/util.py | 54 ++++++++++++++++++++++++ 4 files changed, 71 insertions(+), 143 deletions(-) create mode 100644 importlib_resources/tests/util.py diff --git a/importlib_resources/tests/test_open.py b/importlib_resources/tests/test_open.py index f7a13cc8..b76d9334 100644 --- a/importlib_resources/tests/test_open.py +++ b/importlib_resources/tests/test_open.py @@ -5,7 +5,8 @@ import unittest import importlib_resources as resources -from importlib_resources.tests import data +from . import data +from . import util ZIP_DATA_PATH = None # type: Optional[pathlib.Path] @@ -29,56 +30,11 @@ def tearDownModule(): pass -class CommonTests(unittest.TestCase): - - def test_package_name(self): - # Passing in the package name should succeed. - with resources.open(data.__name__, 'utf-8.file') as file: - pass # No error. - - def test_package_object(self): - # Passing in the package itself should succeed. - with resources.open(data, 'utf-8.file') as file: - pass # No error. - - def test_string_path(self): - path = 'utf-8.file' - # Passing in a string for the path should succeed. - with resources.open(data, path) as file: - pass # No error. - - @unittest.skipIf(sys.version_info < (3, 6), 'requires os.PathLike support') - def test_pathlib_path(self): - # Passing in a pathlib.PurePath object for the path should succeed. - path = pathlib.PurePath('utf-8.file') - with resources.open(data, path) as file: - pass # No error. - - def test_absolute_path(self): - # An absolute path is a ValueError. - path = pathlib.Path(__file__) - full_path = path.parent/'utf-8.file' - with self.assertRaises(ValueError): - with resources.open(data, str(full_path)) as file: - pass - - def test_relative_path(self): - # A reative path is a ValueError. - with self.assertRaises(ValueError): - with resources.open(data, '../data/utf-8.file') as file: - pass +class CommonTests(util.CommonTests, unittest.TestCase): - def test_importing_module_as_side_effect(self): - # The anchor package can already be imported. - del sys.modules[data.__name__] - with resources.open(data.__name__, 'utf-8.file') as file: - pass # No Errors. - - def test_non_package(self): - # The anchor package cannot be a module. - with self.assertRaises(TypeError): - with resources.open(__spec__.name, 'utf-8.file') as file: - pass + def execute(self, package, path): + with resources.open(package, path): + pass class OpenTests: diff --git a/importlib_resources/tests/test_path.py b/importlib_resources/tests/test_path.py index fe1b3eb3..571b6df8 100644 --- a/importlib_resources/tests/test_path.py +++ b/importlib_resources/tests/test_path.py @@ -5,60 +5,15 @@ import unittest import importlib_resources as resources -from importlib_resources.tests import data +from . import data +from . import util -class CommonTests(unittest.TestCase): +class CommonTests(util.CommonTests, unittest.TestCase): - def test_package_name(self): - # Passing in the package name should succeed. - with resources.path(data.__name__, 'utf-8.file') as path: - pass # No error. - - def test_package_object(self): - # Passing in the package itself should succeed. - with resources.path(data, 'utf-8.file') as path: - pass # No error. - - def test_string_path(self): - path = 'utf-8.file' - # Passing in a string for the path should succeed. - with resources.path(data, path) as path: - pass # No error. - - @unittest.skipIf(sys.version_info < (3, 6), 'requires os.PathLike support') - def test_pathlib_path(self): - # Passing in a pathlib.PurePath object for the path should succeed. - path = pathlib.PurePath('utf-8.file') - with resources.path(data, path) as path: - pass # No error. - - # Don't fail if run under e.g. pytest. - def test_absolute_path(self): - # An absolute path is a ValueError. - path = pathlib.Path(__file__) - full_path = path.parent/'utf-8.file' - with self.assertRaises(ValueError): - with resources.path(data, str(full_path)) as path: - pass - - def test_relative_path(self): - # A reative path is a ValueError. - with self.assertRaises(ValueError): - with resources.path(data, '../data/utf-8.file') as path: - pass - - def test_importing_module_as_side_effect(self): - # The anchor package can already be imported. - del sys.modules[data.__name__] - with resources.path(data.__name__, 'utf-8.file') as path: - pass # No Errors. - - def test_non_package(self): - # The anchor package cannot be a module. - with self.assertRaises(TypeError): - with resources.path(__spec__.name, 'utf-8.file') as path: - pass + def execute(self, package, path): + with resources.path(package, path): + pass class PathTests(unittest.TestCase): diff --git a/importlib_resources/tests/test_read.py b/importlib_resources/tests/test_read.py index 51e05769..67f0d6a1 100644 --- a/importlib_resources/tests/test_read.py +++ b/importlib_resources/tests/test_read.py @@ -5,51 +5,14 @@ import unittest import importlib_resources as resources -from importlib_resources.tests import data +from . import data +from . import util -class CommonTests(unittest.TestCase): +class CommonTests(util.CommonTests, unittest.TestCase): - def test_package_name(self): - # Passing in the package name should succeed. - resources.read(data.__name__, 'utf-8.file') - - def test_package_object(self): - # Passing in the package itself should succeed. - resources.read(data, 'utf-8.file') - - def test_string_path(self): - path = 'utf-8.file' - # Passing in a string for the path should succeed. - resources.read(data, path) - - @unittest.skipIf(sys.version_info < (3, 6), 'requires os.PathLike support') - def test_pathlib_path(self): - # Passing in a pathlib.PurePath object for the path should succeed. - path = pathlib.PurePath('utf-8.file') - resources.read(data, path) - - def test_absolute_path(self): - # An absolute path is a ValueError. - path = pathlib.Path(__file__) - full_path = path.parent/'utf-8.file' - with self.assertRaises(ValueError): - resources.read(data, str(full_path)) - - def test_relative_path(self): - # A reative path is a ValueError. - with self.assertRaises(ValueError): - resources.read(data, '../data/utf-8.file') - - def test_importing_module_as_side_effect(self): - # The anchor package can already be imported. - del sys.modules[data.__name__] - resources.read(data.__name__, 'utf-8.file') - - def test_non_package(self): - # The anchor package cannot be a module. - with self.assertRaises(TypeError): - resources.read(__spec__.name, 'utf-8.file') + def execute(self, package, path): + resources.read(package, path) class ReadTests(unittest.TestCase): diff --git a/importlib_resources/tests/util.py b/importlib_resources/tests/util.py new file mode 100644 index 00000000..1bedcc98 --- /dev/null +++ b/importlib_resources/tests/util.py @@ -0,0 +1,54 @@ +import abc +import pathlib +import sys +import unittest + +from . import data + + +class CommonTests(abc.ABC): + + @abc.abstractmethod + def execute(self, package, path): + raise NotImplementedError + + def test_package_name(self): + # Passing in the package name should succeed. + self.execute(data.__name__, 'utf-8.file') + + def test_package_object(self): + # Passing in the package itself should succeed. + self.execute(data, 'utf-8.file') + + def test_string_path(self): + # Passing in a string for the path should succeed. + path = 'utf-8.file' + self.execute(data, path) + + @unittest.skipIf(sys.version_info < (3, 6), 'requires os.PathLike support') + def test_pathlib_path(self): + # Passing in a pathlib.PurePath object for the path should succeed. + path = pathlib.PurePath('utf-8.file') + self.execute(data, path) + + def test_absolute_path(self): + # An absolute path is a ValueError. + path = pathlib.Path(__file__) + full_path = path.parent/'utf-8.file' + with self.assertRaises(ValueError): + self.execute(data, full_path) + + def test_relative_path(self): + # A reative path is a ValueError. + with self.assertRaises(ValueError): + self.execute(data, '../data/utf-8.file') + + def test_importing_module_as_side_effect(self): + # The anchor package can already be imported. + del sys.modules[data.__name__] + self.execute(data.__name__, 'utf-8.file') + + def test_non_package(self): + # The anchor package cannot be a module. + with self.assertRaises(TypeError): + self.execute(__spec__.name, 'utf-8.file')