summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_importlib/resources/util.py
diff options
context:
space:
mode:
Diffstat (limited to 'Lib/test/test_importlib/resources/util.py')
-rw-r--r--Lib/test/test_importlib/resources/util.py72
1 files changed, 57 insertions, 15 deletions
diff --git a/Lib/test/test_importlib/resources/util.py b/Lib/test/test_importlib/resources/util.py
index d4bf3e6..893dda6 100644
--- a/Lib/test/test_importlib/resources/util.py
+++ b/Lib/test/test_importlib/resources/util.py
@@ -6,10 +6,10 @@ import types
import pathlib
import contextlib
-from . import data01
from importlib.resources.abc import ResourceReader
from test.support import import_helper, os_helper
from . import zip as zip_
+from . import _path
from importlib.machinery import ModuleSpec
@@ -68,7 +68,7 @@ def create_package(file=None, path=None, is_package=True, contents=()):
)
-class CommonTests(metaclass=abc.ABCMeta):
+class CommonTestsBase(metaclass=abc.ABCMeta):
"""
Tests shared by test_open, test_path, and test_read.
"""
@@ -84,34 +84,34 @@ class CommonTests(metaclass=abc.ABCMeta):
"""
Passing in the package name should succeed.
"""
- self.execute(data01.__name__, 'utf-8.file')
+ self.execute(self.data.__name__, 'utf-8.file')
def test_package_object(self):
"""
Passing in the package itself should succeed.
"""
- self.execute(data01, 'utf-8.file')
+ self.execute(self.data, 'utf-8.file')
def test_string_path(self):
"""
Passing in a string for the path should succeed.
"""
path = 'utf-8.file'
- self.execute(data01, path)
+ self.execute(self.data, path)
def test_pathlib_path(self):
"""
Passing in a pathlib.PurePath object for the path should succeed.
"""
path = pathlib.PurePath('utf-8.file')
- self.execute(data01, path)
+ self.execute(self.data, path)
def test_importing_module_as_side_effect(self):
"""
The anchor package can already be imported.
"""
- del sys.modules[data01.__name__]
- self.execute(data01.__name__, 'utf-8.file')
+ del sys.modules[self.data.__name__]
+ self.execute(self.data.__name__, 'utf-8.file')
def test_missing_path(self):
"""
@@ -141,24 +141,66 @@ class CommonTests(metaclass=abc.ABCMeta):
self.execute(package, 'utf-8.file')
-class ZipSetupBase:
- ZIP_MODULE = 'data01'
-
+fixtures = dict(
+ data01={
+ '__init__.py': '',
+ 'binary.file': bytes(range(4)),
+ 'utf-16.file': 'Hello, UTF-16 world!\n'.encode('utf-16'),
+ 'utf-8.file': 'Hello, UTF-8 world!\n'.encode('utf-8'),
+ 'subdirectory': {
+ '__init__.py': '',
+ 'binary.file': bytes(range(4, 8)),
+ },
+ },
+ data02={
+ '__init__.py': '',
+ 'one': {'__init__.py': '', 'resource1.txt': 'one resource'},
+ 'two': {'__init__.py': '', 'resource2.txt': 'two resource'},
+ 'subdirectory': {'subsubdir': {'resource.txt': 'a resource'}},
+ },
+ namespacedata01={
+ 'binary.file': bytes(range(4)),
+ 'utf-16.file': 'Hello, UTF-16 world!\n'.encode('utf-16'),
+ 'utf-8.file': 'Hello, UTF-8 world!\n'.encode('utf-8'),
+ 'subdirectory': {
+ 'binary.file': bytes(range(12, 16)),
+ },
+ },
+)
+
+
+class ModuleSetup:
def setUp(self):
self.fixtures = contextlib.ExitStack()
self.addCleanup(self.fixtures.close)
self.fixtures.enter_context(import_helper.isolated_modules())
+ self.data = self.load_fixture(self.MODULE)
+
+ def load_fixture(self, module):
+ self.tree_on_path({module: fixtures[module]})
+ return importlib.import_module(module)
+
+
+class ZipSetup(ModuleSetup):
+ MODULE = 'data01'
+ def tree_on_path(self, spec):
temp_dir = self.fixtures.enter_context(os_helper.temp_dir())
modules = pathlib.Path(temp_dir) / 'zipped modules.zip'
- src_path = pathlib.Path(__file__).parent.joinpath(self.ZIP_MODULE)
self.fixtures.enter_context(
- import_helper.DirsOnSysPath(str(zip_.make_zip_file(src_path, modules)))
+ import_helper.DirsOnSysPath(str(zip_.make_zip_file(spec, modules)))
)
- self.data = importlib.import_module(self.ZIP_MODULE)
+
+class DiskSetup(ModuleSetup):
+ MODULE = 'data01'
+
+ def tree_on_path(self, spec):
+ temp_dir = self.fixtures.enter_context(os_helper.temp_dir())
+ _path.build(spec, pathlib.Path(temp_dir))
+ self.fixtures.enter_context(import_helper.DirsOnSysPath(temp_dir))
-class ZipSetup(ZipSetupBase):
+class CommonTests(DiskSetup, CommonTestsBase):
pass