bpo-46125: Refactor tests to test traversable API directly. Includes changes from importlib_resources 5.4.0. (GH-30189)

This commit is contained in:
Jason R. Coombs 2021-12-18 21:28:49 -05:00 committed by GitHub
parent fe68486197
commit 9b52920173
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 152 additions and 211 deletions

View file

@ -6,13 +6,12 @@ import contextlib
import types import types
import importlib import importlib
from typing import Union, Any, Optional from typing import Union, Optional
from .abc import ResourceReader, Traversable from .abc import ResourceReader, Traversable
from ._adapters import wrap_spec from ._adapters import wrap_spec
Package = Union[types.ModuleType, str] Package = Union[types.ModuleType, str]
Resource = Union[str, os.PathLike]
def files(package): def files(package):
@ -23,19 +22,6 @@ def files(package):
return from_package(get_package(package)) return from_package(get_package(package))
def normalize_path(path):
# type: (Any) -> str
"""Normalize a path by ensuring it is a string.
If the resulting string contains path separators, an exception is raised.
"""
str_path = str(path)
parent, file_name = os.path.split(str_path)
if parent:
raise ValueError(f'{path!r} must be only a file name')
return file_name
def get_resource_reader(package): def get_resource_reader(package):
# type: (types.ModuleType) -> Optional[ResourceReader] # type: (types.ModuleType) -> Optional[ResourceReader]
""" """

View file

@ -4,12 +4,12 @@ import pathlib
import types import types
import warnings import warnings
from typing import Union, Iterable, ContextManager, BinaryIO, TextIO from typing import Union, Iterable, ContextManager, BinaryIO, TextIO, Any
from . import _common from . import _common
Package = Union[types.ModuleType, str] Package = Union[types.ModuleType, str]
Resource = Union[str, os.PathLike] Resource = str
def deprecated(func): def deprecated(func):
@ -27,16 +27,29 @@ def deprecated(func):
return wrapper return wrapper
def normalize_path(path):
# type: (Any) -> str
"""Normalize a path by ensuring it is a string.
If the resulting string contains path separators, an exception is raised.
"""
str_path = str(path)
parent, file_name = os.path.split(str_path)
if parent:
raise ValueError(f'{path!r} must be only a file name')
return file_name
@deprecated @deprecated
def open_binary(package: Package, resource: Resource) -> BinaryIO: def open_binary(package: Package, resource: Resource) -> BinaryIO:
"""Return a file-like object opened for binary reading of the resource.""" """Return a file-like object opened for binary reading of the resource."""
return (_common.files(package) / _common.normalize_path(resource)).open('rb') return (_common.files(package) / normalize_path(resource)).open('rb')
@deprecated @deprecated
def read_binary(package: Package, resource: Resource) -> bytes: def read_binary(package: Package, resource: Resource) -> bytes:
"""Return the binary contents of the resource.""" """Return the binary contents of the resource."""
return (_common.files(package) / _common.normalize_path(resource)).read_bytes() return (_common.files(package) / normalize_path(resource)).read_bytes()
@deprecated @deprecated
@ -47,7 +60,7 @@ def open_text(
errors: str = 'strict', errors: str = 'strict',
) -> TextIO: ) -> TextIO:
"""Return a file-like object opened for text reading of the resource.""" """Return a file-like object opened for text reading of the resource."""
return (_common.files(package) / _common.normalize_path(resource)).open( return (_common.files(package) / normalize_path(resource)).open(
'r', encoding=encoding, errors=errors 'r', encoding=encoding, errors=errors
) )
@ -85,7 +98,7 @@ def is_resource(package: Package, name: str) -> bool:
Directories are *not* resources. Directories are *not* resources.
""" """
resource = _common.normalize_path(name) resource = normalize_path(name)
return any( return any(
traversable.name == resource and traversable.is_file() traversable.name == resource and traversable.is_file()
for traversable in _common.files(package).iterdir() for traversable in _common.files(package).iterdir()
@ -105,4 +118,4 @@ def path(
raised if the file was deleted prior to the context manager raised if the file was deleted prior to the context manager
exiting). exiting).
""" """
return _common.as_file(_common.files(package) / _common.normalize_path(resource)) return _common.as_file(_common.files(package) / normalize_path(resource))

View file

@ -381,7 +381,7 @@ class Traversable(Protocol):
@abc.abstractmethod @abc.abstractmethod
def is_dir(self) -> bool: def is_dir(self) -> bool:
""" """
Return True if self is a dir Return True if self is a directory
""" """
@abc.abstractmethod @abc.abstractmethod

View file

@ -4,7 +4,6 @@ from ._common import (
as_file, as_file,
files, files,
Package, Package,
Resource,
) )
from ._legacy import ( from ._legacy import (
@ -15,6 +14,7 @@ from ._legacy import (
read_text, read_text,
is_resource, is_resource,
path, path,
Resource,
) )
from importlib.abc import ResourceReader from importlib.abc import ResourceReader

View file

@ -1,10 +1,8 @@
import abc import abc
import contextlib
import importlib import importlib
import io import io
import sys import sys
import types import types
import warnings
from pathlib import Path, PurePath from pathlib import Path, PurePath
from .. import data01 from .. import data01
@ -69,13 +67,6 @@ def create_package(file=None, path=None, is_package=True, contents=()):
) )
@contextlib.contextmanager
def suppress_known_deprecation():
with warnings.catch_warnings(record=True) as ctx:
warnings.simplefilter('default', category=DeprecationWarning)
yield ctx
class CommonTests(metaclass=abc.ABCMeta): class CommonTests(metaclass=abc.ABCMeta):
""" """
Tests shared by test_open, test_path, and test_read. Tests shared by test_open, test_path, and test_read.
@ -106,18 +97,6 @@ class CommonTests(metaclass=abc.ABCMeta):
path = PurePath('utf-8.file') path = PurePath('utf-8.file')
self.execute(data01, path) self.execute(data01, path)
def test_absolute_path(self):
# An absolute path is a ValueError.
path = Path(__file__)
full_path = path.parent / 'utf-8.file'
with self.assertRaises(ValueError):
self.execute(data01, full_path)
def test_relative_path(self):
# A reative path is a ValueError.
with self.assertRaises(ValueError):
self.execute(data01, '../data01/utf-8.file')
def test_importing_module_as_side_effect(self): def test_importing_module_as_side_effect(self):
# The anchor package can already be imported. # The anchor package can already be imported.
del sys.modules[data01.__name__] del sys.modules[data01.__name__]

View file

@ -15,8 +15,8 @@ class ContentsTests:
} }
def test_contents(self): def test_contents(self):
with util.suppress_known_deprecation(): contents = {path.name for path in resources.files(self.data).iterdir()}
assert self.expected <= set(resources.contents(self.data)) assert self.expected <= contents
class ContentsDiskTests(ContentsTests, unittest.TestCase): class ContentsDiskTests(ContentsTests, unittest.TestCase):

View file

@ -7,46 +7,43 @@ from .resources import util
class CommonBinaryTests(util.CommonTests, unittest.TestCase): class CommonBinaryTests(util.CommonTests, unittest.TestCase):
def execute(self, package, path): def execute(self, package, path):
with util.suppress_known_deprecation(): target = resources.files(package).joinpath(path)
with resources.open_binary(package, path): with target.open('rb'):
pass pass
class CommonTextTests(util.CommonTests, unittest.TestCase): class CommonTextTests(util.CommonTests, unittest.TestCase):
def execute(self, package, path): def execute(self, package, path):
with util.suppress_known_deprecation(): target = resources.files(package).joinpath(path)
with resources.open_text(package, path): with target.open():
pass pass
class OpenTests: class OpenTests:
def test_open_binary(self): def test_open_binary(self):
with util.suppress_known_deprecation(): target = resources.files(self.data) / 'binary.file'
with resources.open_binary(self.data, 'binary.file') as fp: with target.open('rb') as fp:
result = fp.read() result = fp.read()
self.assertEqual(result, b'\x00\x01\x02\x03') self.assertEqual(result, b'\x00\x01\x02\x03')
def test_open_text_default_encoding(self): def test_open_text_default_encoding(self):
with util.suppress_known_deprecation(): target = resources.files(self.data) / 'utf-8.file'
with resources.open_text(self.data, 'utf-8.file') as fp: with target.open() as fp:
result = fp.read() result = fp.read()
self.assertEqual(result, 'Hello, UTF-8 world!\n') self.assertEqual(result, 'Hello, UTF-8 world!\n')
def test_open_text_given_encoding(self): def test_open_text_given_encoding(self):
with util.suppress_known_deprecation(): target = resources.files(self.data) / 'utf-16.file'
with resources.open_text( with target.open(encoding='utf-16', errors='strict') as fp:
self.data, 'utf-16.file', 'utf-16', 'strict'
) as fp:
result = fp.read() result = fp.read()
self.assertEqual(result, 'Hello, UTF-16 world!\n') self.assertEqual(result, 'Hello, UTF-16 world!\n')
def test_open_text_with_errors(self): def test_open_text_with_errors(self):
# Raises UnicodeError without the 'errors' argument. # Raises UnicodeError without the 'errors' argument.
with util.suppress_known_deprecation(): target = resources.files(self.data) / 'utf-16.file'
with resources.open_text(self.data, 'utf-16.file', 'utf-8', 'strict') as fp: with target.open(encoding='utf-8', errors='strict') as fp:
self.assertRaises(UnicodeError, fp.read) self.assertRaises(UnicodeError, fp.read)
with util.suppress_known_deprecation(): with target.open(encoding='utf-8', errors='ignore') as fp:
with resources.open_text(self.data, 'utf-16.file', 'utf-8', 'ignore') as fp:
result = fp.read() result = fp.read()
self.assertEqual( self.assertEqual(
result, result,
@ -56,16 +53,12 @@ class OpenTests:
) )
def test_open_binary_FileNotFoundError(self): def test_open_binary_FileNotFoundError(self):
with util.suppress_known_deprecation(): target = resources.files(self.data) / 'does-not-exist'
self.assertRaises( self.assertRaises(FileNotFoundError, target.open, 'rb')
FileNotFoundError, resources.open_binary, self.data, 'does-not-exist'
)
def test_open_text_FileNotFoundError(self): def test_open_text_FileNotFoundError(self):
with util.suppress_known_deprecation(): target = resources.files(self.data) / 'does-not-exist'
self.assertRaises( self.assertRaises(FileNotFoundError, target.open)
FileNotFoundError, resources.open_text, self.data, 'does-not-exist'
)
class OpenDiskTests(OpenTests, unittest.TestCase): class OpenDiskTests(OpenTests, unittest.TestCase):

View file

@ -8,8 +8,7 @@ from .resources import util
class CommonTests(util.CommonTests, unittest.TestCase): class CommonTests(util.CommonTests, unittest.TestCase):
def execute(self, package, path): def execute(self, package, path):
with util.suppress_known_deprecation(): with resources.as_file(resources.files(package).joinpath(path)):
with resources.path(package, path):
pass pass
@ -18,8 +17,8 @@ class PathTests:
# Path should be readable. # Path should be readable.
# Test also implicitly verifies the returned object is a pathlib.Path # Test also implicitly verifies the returned object is a pathlib.Path
# instance. # instance.
with util.suppress_known_deprecation(): target = resources.files(self.data) / 'utf-8.file'
with resources.path(self.data, 'utf-8.file') as path: with resources.as_file(target) as path:
self.assertTrue(path.name.endswith("utf-8.file"), repr(path)) self.assertTrue(path.name.endswith("utf-8.file"), repr(path))
# pathlib.Path.read_text() was introduced in Python 3.5. # pathlib.Path.read_text() was introduced in Python 3.5.
with path.open('r', encoding='utf-8') as file: with path.open('r', encoding='utf-8') as file:
@ -34,8 +33,8 @@ class PathDiskTests(PathTests, unittest.TestCase):
# Guarantee the internal implementation detail that # Guarantee the internal implementation detail that
# file-system-backed resources do not get the tempdir # file-system-backed resources do not get the tempdir
# treatment. # treatment.
with util.suppress_known_deprecation(): target = resources.files(self.data) / 'utf-8.file'
with resources.path(self.data, 'utf-8.file') as path: with resources.as_file(target) as path:
assert 'data' in str(path) assert 'data' in str(path)
@ -54,8 +53,8 @@ class PathZipTests(PathTests, util.ZipSetup, unittest.TestCase):
def test_remove_in_context_manager(self): def test_remove_in_context_manager(self):
# It is not an error if the file that was temporarily stashed on the # It is not an error if the file that was temporarily stashed on the
# file system is removed inside the `with` stanza. # file system is removed inside the `with` stanza.
with util.suppress_known_deprecation(): target = resources.files(self.data) / 'utf-8.file'
with resources.path(self.data, 'utf-8.file') as path: with resources.as_file(target) as path:
path.unlink() path.unlink()

View file

@ -7,40 +7,36 @@ from .resources import util
class CommonBinaryTests(util.CommonTests, unittest.TestCase): class CommonBinaryTests(util.CommonTests, unittest.TestCase):
def execute(self, package, path): def execute(self, package, path):
with util.suppress_known_deprecation(): resources.files(package).joinpath(path).read_bytes()
resources.read_binary(package, path)
class CommonTextTests(util.CommonTests, unittest.TestCase): class CommonTextTests(util.CommonTests, unittest.TestCase):
def execute(self, package, path): def execute(self, package, path):
with util.suppress_known_deprecation(): resources.files(package).joinpath(path).read_text()
resources.read_text(package, path)
class ReadTests: class ReadTests:
def test_read_binary(self): def test_read_bytes(self):
with util.suppress_known_deprecation(): result = resources.files(self.data).joinpath('binary.file').read_bytes()
result = resources.read_binary(self.data, 'binary.file')
self.assertEqual(result, b'\0\1\2\3') self.assertEqual(result, b'\0\1\2\3')
def test_read_text_default_encoding(self): def test_read_text_default_encoding(self):
with util.suppress_known_deprecation(): result = resources.files(self.data).joinpath('utf-8.file').read_text()
result = resources.read_text(self.data, 'utf-8.file')
self.assertEqual(result, 'Hello, UTF-8 world!\n') self.assertEqual(result, 'Hello, UTF-8 world!\n')
def test_read_text_given_encoding(self): def test_read_text_given_encoding(self):
with util.suppress_known_deprecation(): result = (
result = resources.read_text(self.data, 'utf-16.file', encoding='utf-16') resources.files(self.data)
.joinpath('utf-16.file')
.read_text(encoding='utf-16')
)
self.assertEqual(result, 'Hello, UTF-16 world!\n') self.assertEqual(result, 'Hello, UTF-16 world!\n')
def test_read_text_with_errors(self): def test_read_text_with_errors(self):
# Raises UnicodeError without the 'errors' argument. # Raises UnicodeError without the 'errors' argument.
with util.suppress_known_deprecation(): target = resources.files(self.data) / 'utf-16.file'
self.assertRaises( self.assertRaises(UnicodeError, target.read_text, encoding='utf-8')
UnicodeError, resources.read_text, self.data, 'utf-16.file' result = target.read_text(encoding='utf-8', errors='ignore')
)
with util.suppress_known_deprecation():
result = resources.read_text(self.data, 'utf-16.file', errors='ignore')
self.assertEqual( self.assertEqual(
result, result,
'H\x00e\x00l\x00l\x00o\x00,\x00 ' 'H\x00e\x00l\x00l\x00o\x00,\x00 '
@ -56,13 +52,15 @@ class ReadDiskTests(ReadTests, unittest.TestCase):
class ReadZipTests(ReadTests, util.ZipSetup, unittest.TestCase): class ReadZipTests(ReadTests, util.ZipSetup, unittest.TestCase):
def test_read_submodule_resource(self): def test_read_submodule_resource(self):
submodule = import_module('ziptestdata.subdirectory') submodule = import_module('ziptestdata.subdirectory')
with util.suppress_known_deprecation(): result = resources.files(submodule).joinpath('binary.file').read_bytes()
result = resources.read_binary(submodule, 'binary.file')
self.assertEqual(result, b'\0\1\2\3') self.assertEqual(result, b'\0\1\2\3')
def test_read_submodule_resource_by_name(self): def test_read_submodule_resource_by_name(self):
with util.suppress_known_deprecation(): result = (
result = resources.read_binary('ziptestdata.subdirectory', 'binary.file') resources.files('ziptestdata.subdirectory')
.joinpath('binary.file')
.read_bytes()
)
self.assertEqual(result, b'\0\1\2\3') self.assertEqual(result, b'\0\1\2\3')

View file

@ -14,38 +14,18 @@ from test.support.os_helper import unlink
class ResourceTests: class ResourceTests:
# Subclasses are expected to set the `data` attribute. # Subclasses are expected to set the `data` attribute.
def test_is_resource_good_path(self): def test_is_file_exists(self):
with util.suppress_known_deprecation(): target = resources.files(self.data) / 'binary.file'
self.assertTrue(resources.is_resource(self.data, 'binary.file')) self.assertTrue(target.is_file())
def test_is_resource_missing(self): def test_is_file_missing(self):
with util.suppress_known_deprecation(): target = resources.files(self.data) / 'not-a-file'
self.assertFalse(resources.is_resource(self.data, 'not-a-file')) self.assertFalse(target.is_file())
def test_is_resource_subresource_directory(self): def test_is_dir(self):
# Directories are not resources. target = resources.files(self.data) / 'subdirectory'
with util.suppress_known_deprecation(): self.assertFalse(target.is_file())
self.assertFalse(resources.is_resource(self.data, 'subdirectory')) self.assertTrue(target.is_dir())
def test_contents(self):
with util.suppress_known_deprecation():
contents = set(resources.contents(self.data))
# There may be cruft in the directory listing of the data directory.
# It could have a __pycache__ directory,
# an artifact of the
# test suite importing these modules, which
# are not germane to this test, so just filter them out.
contents.discard('__pycache__')
self.assertEqual(
sorted(contents),
[
'__init__.py',
'binary.file',
'subdirectory',
'utf-16.file',
'utf-8.file',
],
)
class ResourceDiskTests(ResourceTests, unittest.TestCase): class ResourceDiskTests(ResourceTests, unittest.TestCase):
@ -57,34 +37,34 @@ class ResourceZipTests(ResourceTests, util.ZipSetup, unittest.TestCase):
pass pass
def names(traversable):
return {item.name for item in traversable.iterdir()}
class ResourceLoaderTests(unittest.TestCase): class ResourceLoaderTests(unittest.TestCase):
def test_resource_contents(self): def test_resource_contents(self):
package = util.create_package( package = util.create_package(
file=data01, path=data01.__file__, contents=['A', 'B', 'C'] file=data01, path=data01.__file__, contents=['A', 'B', 'C']
) )
with util.suppress_known_deprecation(): self.assertEqual(names(resources.files(package)), {'A', 'B', 'C'})
self.assertEqual(set(resources.contents(package)), {'A', 'B', 'C'})
def test_resource_is_resource(self): def test_is_file(self):
package = util.create_package( package = util.create_package(
file=data01, path=data01.__file__, contents=['A', 'B', 'C', 'D/E', 'D/F'] file=data01, path=data01.__file__, contents=['A', 'B', 'C', 'D/E', 'D/F']
) )
with util.suppress_known_deprecation(): self.assertTrue(resources.files(package).joinpath('B').is_file())
self.assertTrue(resources.is_resource(package, 'B'))
def test_resource_directory_is_not_resource(self): def test_is_dir(self):
package = util.create_package( package = util.create_package(
file=data01, path=data01.__file__, contents=['A', 'B', 'C', 'D/E', 'D/F'] file=data01, path=data01.__file__, contents=['A', 'B', 'C', 'D/E', 'D/F']
) )
with util.suppress_known_deprecation(): self.assertTrue(resources.files(package).joinpath('D').is_dir())
self.assertFalse(resources.is_resource(package, 'D'))
def test_resource_missing_is_not_resource(self): def test_resource_missing(self):
package = util.create_package( package = util.create_package(
file=data01, path=data01.__file__, contents=['A', 'B', 'C', 'D/E', 'D/F'] file=data01, path=data01.__file__, contents=['A', 'B', 'C', 'D/E', 'D/F']
) )
with util.suppress_known_deprecation(): self.assertFalse(resources.files(package).joinpath('Z').is_file())
self.assertFalse(resources.is_resource(package, 'Z'))
class ResourceCornerCaseTests(unittest.TestCase): class ResourceCornerCaseTests(unittest.TestCase):
@ -102,8 +82,7 @@ class ResourceCornerCaseTests(unittest.TestCase):
module.__file__ = '/path/which/shall/not/be/named' module.__file__ = '/path/which/shall/not/be/named'
module.__spec__.loader = module.__loader__ module.__spec__.loader = module.__loader__
module.__spec__.origin = module.__file__ module.__spec__.origin = module.__file__
with util.suppress_known_deprecation(): self.assertFalse(resources.files(module).joinpath('A').is_file())
self.assertFalse(resources.is_resource(module, 'A'))
class ResourceFromZipsTest01(util.ZipSetupBase, unittest.TestCase): class ResourceFromZipsTest01(util.ZipSetupBase, unittest.TestCase):
@ -111,26 +90,24 @@ class ResourceFromZipsTest01(util.ZipSetupBase, unittest.TestCase):
def test_is_submodule_resource(self): def test_is_submodule_resource(self):
submodule = import_module('ziptestdata.subdirectory') submodule = import_module('ziptestdata.subdirectory')
with util.suppress_known_deprecation(): self.assertTrue(resources.files(submodule).joinpath('binary.file').is_file())
self.assertTrue(resources.is_resource(submodule, 'binary.file'))
def test_read_submodule_resource_by_name(self): def test_read_submodule_resource_by_name(self):
with util.suppress_known_deprecation():
self.assertTrue( self.assertTrue(
resources.is_resource('ziptestdata.subdirectory', 'binary.file') resources.files('ziptestdata.subdirectory')
.joinpath('binary.file')
.is_file()
) )
def test_submodule_contents(self): def test_submodule_contents(self):
submodule = import_module('ziptestdata.subdirectory') submodule = import_module('ziptestdata.subdirectory')
with util.suppress_known_deprecation():
self.assertEqual( self.assertEqual(
set(resources.contents(submodule)), {'__init__.py', 'binary.file'} names(resources.files(submodule)), {'__init__.py', 'binary.file'}
) )
def test_submodule_contents_by_name(self): def test_submodule_contents_by_name(self):
with util.suppress_known_deprecation():
self.assertEqual( self.assertEqual(
set(resources.contents('ziptestdata.subdirectory')), names(resources.files('ziptestdata.subdirectory')),
{'__init__.py', 'binary.file'}, {'__init__.py', 'binary.file'},
) )
@ -143,14 +120,12 @@ class ResourceFromZipsTest02(util.ZipSetupBase, unittest.TestCase):
Test thata zip with two unrelated subpackages return Test thata zip with two unrelated subpackages return
distinct resources. Ref python/importlib_resources#44. distinct resources. Ref python/importlib_resources#44.
""" """
with util.suppress_known_deprecation():
self.assertEqual( self.assertEqual(
set(resources.contents('ziptestdata.one')), names(resources.files('ziptestdata.one')),
{'__init__.py', 'resource1.txt'}, {'__init__.py', 'resource1.txt'},
) )
with util.suppress_known_deprecation():
self.assertEqual( self.assertEqual(
set(resources.contents('ziptestdata.two')), names(resources.files('ziptestdata.two')),
{'__init__.py', 'resource2.txt'}, {'__init__.py', 'resource2.txt'},
) )
@ -192,47 +167,43 @@ class DeletingZipsTest(unittest.TestCase):
# If the test fails, this will probably fail too # If the test fails, this will probably fail too
pass pass
def test_contents_does_not_keep_open(self): def test_iterdir_does_not_keep_open(self):
with util.suppress_known_deprecation(): c = [item.name for item in resources.files('ziptestdata').iterdir()]
c = resources.contents('ziptestdata')
self.zip_path.unlink() self.zip_path.unlink()
del c del c
def test_is_resource_does_not_keep_open(self): def test_is_file_does_not_keep_open(self):
with util.suppress_known_deprecation(): c = resources.files('ziptestdata').joinpath('binary.file').is_file()
c = resources.is_resource('ziptestdata', 'binary.file')
self.zip_path.unlink() self.zip_path.unlink()
del c del c
def test_is_resource_failure_does_not_keep_open(self): def test_is_file_failure_does_not_keep_open(self):
with util.suppress_known_deprecation(): c = resources.files('ziptestdata').joinpath('not-present').is_file()
c = resources.is_resource('ziptestdata', 'not-present')
self.zip_path.unlink() self.zip_path.unlink()
del c del c
@unittest.skip("Desired but not supported.") @unittest.skip("Desired but not supported.")
def test_path_does_not_keep_open(self): def test_as_file_does_not_keep_open(self): # pragma: no cover
c = resources.path('ziptestdata', 'binary.file') c = resources.as_file(resources.files('ziptestdata') / 'binary.file')
self.zip_path.unlink() self.zip_path.unlink()
del c del c
def test_entered_path_does_not_keep_open(self): def test_entered_path_does_not_keep_open(self):
# This is what certifi does on import to make its bundle # This is what certifi does on import to make its bundle
# available for the process duration. # available for the process duration.
with util.suppress_known_deprecation(): c = resources.as_file(
c = resources.path('ziptestdata', 'binary.file').__enter__() resources.files('ziptestdata') / 'binary.file'
).__enter__()
self.zip_path.unlink() self.zip_path.unlink()
del c del c
def test_read_binary_does_not_keep_open(self): def test_read_binary_does_not_keep_open(self):
with util.suppress_known_deprecation(): c = resources.files('ziptestdata').joinpath('binary.file').read_bytes()
c = resources.read_binary('ziptestdata', 'binary.file')
self.zip_path.unlink() self.zip_path.unlink()
del c del c
def test_read_text_does_not_keep_open(self): def test_read_text_does_not_keep_open(self):
with util.suppress_known_deprecation(): c = resources.files('ziptestdata').joinpath('utf-8.file').read_text()
c = resources.read_text('ziptestdata', 'utf-8.file', encoding='utf-8')
self.zip_path.unlink() self.zip_path.unlink()
del c del c
@ -249,18 +220,19 @@ class ResourceFromNamespaceTest01(unittest.TestCase):
sys.path.remove(cls.site_dir) sys.path.remove(cls.site_dir)
def test_is_submodule_resource(self): def test_is_submodule_resource(self):
with util.suppress_known_deprecation():
self.assertTrue( self.assertTrue(
resources.is_resource(import_module('namespacedata01'), 'binary.file') resources.files(import_module('namespacedata01'))
.joinpath('binary.file')
.is_file()
) )
def test_read_submodule_resource_by_name(self): def test_read_submodule_resource_by_name(self):
with util.suppress_known_deprecation(): self.assertTrue(
self.assertTrue(resources.is_resource('namespacedata01', 'binary.file')) resources.files('namespacedata01').joinpath('binary.file').is_file()
)
def test_submodule_contents(self): def test_submodule_contents(self):
with util.suppress_known_deprecation(): contents = names(resources.files(import_module('namespacedata01')))
contents = set(resources.contents(import_module('namespacedata01')))
try: try:
contents.remove('__pycache__') contents.remove('__pycache__')
except KeyError: except KeyError:
@ -268,8 +240,7 @@ class ResourceFromNamespaceTest01(unittest.TestCase):
self.assertEqual(contents, {'binary.file', 'utf-8.file', 'utf-16.file'}) self.assertEqual(contents, {'binary.file', 'utf-8.file', 'utf-16.file'})
def test_submodule_contents_by_name(self): def test_submodule_contents_by_name(self):
with util.suppress_known_deprecation(): contents = names(resources.files('namespacedata01'))
contents = set(resources.contents('namespacedata01'))
try: try:
contents.remove('__pycache__') contents.remove('__pycache__')
except KeyError: except KeyError:

View file

@ -0,0 +1,2 @@
Refactor tests to test traversable API directly. Includes changes from
importlib 5.4.0.