Skip to content

Commit

Permalink
Removed reliance on on-disk fixtures.
Browse files Browse the repository at this point in the history
Instead, generate on-disk and zip file fixtures on demand in temporary directories.

This approach makes it easier to compose different scenarios and mix-in behavior for zip files. It also makes the disk and zip scenarios more symmetric.

Re-wrote `test_reader` to utilize these new fixtures instead of assuming the files were present on disk in the test suite.
  • Loading branch information
jaraco committed Aug 14, 2024
1 parent f77da58 commit 3faf336
Show file tree
Hide file tree
Showing 26 changed files with 156 additions and 143 deletions.
Empty file.
Binary file removed importlib_resources/tests/data01/binary.file
Binary file not shown.
Empty file.
1 change: 0 additions & 1 deletion importlib_resources/tests/data01/subdirectory/binary.file

This file was deleted.

Binary file removed importlib_resources/tests/data01/utf-16.file
Binary file not shown.
1 change: 0 additions & 1 deletion importlib_resources/tests/data01/utf-8.file

This file was deleted.

Empty file.
Empty file.
1 change: 0 additions & 1 deletion importlib_resources/tests/data02/one/resource1.txt

This file was deleted.

This file was deleted.

Empty file.
1 change: 0 additions & 1 deletion importlib_resources/tests/data02/two/resource2.txt

This file was deleted.

Binary file not shown.

This file was deleted.

Binary file removed importlib_resources/tests/namespacedata01/utf-16.file
Binary file not shown.
1 change: 0 additions & 1 deletion importlib_resources/tests/namespacedata01/utf-8.file

This file was deleted.

15 changes: 5 additions & 10 deletions importlib_resources/tests/test_contents.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import unittest
import importlib_resources as resources

from . import data01
from . import util


Expand All @@ -19,25 +18,21 @@ def test_contents(self):
assert self.expected <= contents


class ContentsDiskTests(ContentsTests, unittest.TestCase):
def setUp(self):
self.data = data01
class ContentsDiskTests(ContentsTests, util.DiskSetup, unittest.TestCase):
pass


class ContentsZipTests(ContentsTests, util.ZipSetup, unittest.TestCase):
pass


class ContentsNamespaceTests(ContentsTests, unittest.TestCase):
class ContentsNamespaceTests(ContentsTests, util.DiskSetup, unittest.TestCase):
MODULE = 'namespacedata01'

expected = {
# no __init__ because of namespace design
'binary.file',
'subdirectory',
'utf-16.file',
'utf-8.file',
}

def setUp(self):
from . import namespacedata01

self.data = namespacedata01
13 changes: 4 additions & 9 deletions importlib_resources/tests/test_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

import importlib_resources as resources
from ..abc import Traversable
from . import data01
from . import util
from . import _path
from .compat.py39 import os_helper
Expand Down Expand Up @@ -48,20 +47,16 @@ def test_old_parameter(self):
resources.files(package=self.data)


class OpenDiskTests(FilesTests, unittest.TestCase):
def setUp(self):
self.data = data01
class OpenDiskTests(FilesTests, util.DiskSetup, unittest.TestCase):
pass


class OpenZipTests(FilesTests, util.ZipSetup, unittest.TestCase):
pass


class OpenNamespaceTests(FilesTests, unittest.TestCase):
def setUp(self):
from . import namespacedata01

self.data = namespacedata01
class OpenNamespaceTests(FilesTests, util.DiskSetup, unittest.TestCase):
MODULE = 'namespacedata01'


class OpenNamespaceZipTests(FilesTests, util.ZipSetup, unittest.TestCase):
Expand Down
30 changes: 21 additions & 9 deletions importlib_resources/tests/test_functional.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import unittest
import os
import contextlib
import importlib

try:
from test.support.warnings_helper import ignore_warnings, check_warnings
Expand All @@ -10,22 +11,33 @@

import importlib_resources as resources

from . import util

# Since the functional API forwards to Traversable, we only test
# filesystem resources here -- not zip files, namespace packages etc.
# We do test for two kinds of Anchor, though.


class StringAnchorMixin:
anchor01 = 'importlib_resources.tests.data01'
anchor02 = 'importlib_resources.tests.data02'
anchor01 = 'data01'
anchor02 = 'data02'


class ModuleAnchorMixin:
from . import data01 as anchor01
from . import data02 as anchor02
@property
def anchor01(self):
return importlib.import_module('data01')

@property
def anchor02(self):
return importlib.import_module('data02')


class FunctionalAPIBase(util.DiskSetup):
def setUp(self):
super().setUp()
self.load_fixture('data02')

class FunctionalAPIBase:
def _gen_resourcetxt_path_parts(self):
"""Yield various names of a text file in anchor02, each in a subTest"""
for path_parts in (
Expand Down Expand Up @@ -227,16 +239,16 @@ def test_text_errors(self):


class FunctionalAPITest_StringAnchor(
unittest.TestCase,
FunctionalAPIBase,
StringAnchorMixin,
FunctionalAPIBase,
unittest.TestCase,
):
pass


class FunctionalAPITest_ModuleAnchor(
unittest.TestCase,
FunctionalAPIBase,
ModuleAnchorMixin,
FunctionalAPIBase,
unittest.TestCase,
):
pass
15 changes: 5 additions & 10 deletions importlib_resources/tests/test_open.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import unittest

import importlib_resources as resources
from . import data01
from . import util


Expand Down Expand Up @@ -65,24 +64,20 @@ def test_open_text_FileNotFoundError(self):
target.open(encoding='utf-8')


class OpenDiskTests(OpenTests, unittest.TestCase):
def setUp(self):
self.data = data01

class OpenDiskTests(OpenTests, util.DiskSetup, unittest.TestCase):
pass

class OpenDiskNamespaceTests(OpenTests, unittest.TestCase):
def setUp(self):
from . import namespacedata01

self.data = namespacedata01
class OpenDiskNamespaceTests(OpenTests, util.DiskSetup, unittest.TestCase):
MODULE = 'namespacedata01'


class OpenZipTests(OpenTests, util.ZipSetup, unittest.TestCase):
pass


class OpenNamespaceZipTests(OpenTests, util.ZipSetup, unittest.TestCase):
ZIP_MODULE = 'namespacedata01'
MODULE = 'namespacedata01'


if __name__ == '__main__':
Expand Down
5 changes: 1 addition & 4 deletions importlib_resources/tests/test_path.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import unittest

import importlib_resources as resources
from . import data01
from . import util


Expand All @@ -25,9 +24,7 @@ def test_reading(self):
self.assertEqual('Hello, UTF-8 world!\n', path.read_text(encoding='utf-8'))


class PathDiskTests(PathTests, unittest.TestCase):
data = data01

class PathDiskTests(PathTests, util.DiskSetup, unittest.TestCase):
def test_natural_path(self):
"""
Guarantee the internal implementation detail that
Expand Down
14 changes: 5 additions & 9 deletions importlib_resources/tests/test_read.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import unittest
import importlib_resources as resources

from . import data01
from . import util
from importlib import import_module

Expand Down Expand Up @@ -52,8 +51,8 @@ def test_read_text_with_errors(self):
)


class ReadDiskTests(ReadTests, unittest.TestCase):
data = data01
class ReadDiskTests(ReadTests, util.DiskSetup, unittest.TestCase):
pass


class ReadZipTests(ReadTests, util.ZipSetup, unittest.TestCase):
Expand All @@ -69,15 +68,12 @@ def test_read_submodule_resource_by_name(self):
self.assertEqual(result, bytes(range(4, 8)))


class ReadNamespaceTests(ReadTests, unittest.TestCase):
def setUp(self):
from . import namespacedata01

self.data = namespacedata01
class ReadNamespaceTests(ReadTests, util.DiskSetup, unittest.TestCase):
MODULE = 'namespacedata01'


class ReadNamespaceZipTests(ReadTests, util.ZipSetup, unittest.TestCase):
ZIP_MODULE = 'namespacedata01'
MODULE = 'namespacedata01'

def test_read_submodule_resource(self):
submodule = import_module('namespacedata01.subdirectory')
Expand Down
48 changes: 20 additions & 28 deletions importlib_resources/tests/test_reader.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
import os.path
import sys
import pathlib
import unittest

from importlib import import_module
from importlib_resources.readers import MultiplexedPath, NamespaceReader

from . import util

class MultiplexedPathTest(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.folder = pathlib.Path(__file__).parent / 'namespacedata01'

class MultiplexedPathTest(util.DiskSetup, unittest.TestCase):
MODULE = 'namespacedata01'

def setUp(self):
super().setUp()
self.folder = pathlib.Path(self.data.__path__[0])
self.data01 = pathlib.Path(self.load_fixture('data01').__file__).parent
self.data02 = pathlib.Path(self.load_fixture('data02').__file__).parent

def test_init_no_paths(self):
with self.assertRaises(FileNotFoundError):
Expand All @@ -31,9 +36,8 @@ def test_iterdir(self):
)

def test_iterdir_duplicate(self):
data01 = pathlib.Path(__file__).parent.joinpath('data01')
contents = {
path.name for path in MultiplexedPath(self.folder, data01).iterdir()
path.name for path in MultiplexedPath(self.folder, self.data01).iterdir()
}
for remove in ('__pycache__', '__init__.pyc'):
try:
Expand Down Expand Up @@ -61,9 +65,8 @@ def test_open_file(self):
path.open()

def test_join_path(self):
data01 = pathlib.Path(__file__).parent.joinpath('data01')
prefix = str(data01.parent)
path = MultiplexedPath(self.folder, data01)
prefix = str(self.folder.parent)
path = MultiplexedPath(self.folder, self.data01)
self.assertEqual(
str(path.joinpath('binary.file'))[len(prefix) + 1 :],
os.path.join('namespacedata01', 'binary.file'),
Expand All @@ -83,10 +86,8 @@ def test_join_path_compound(self):
assert not path.joinpath('imaginary/foo.py').exists()

def test_join_path_common_subdir(self):
data01 = pathlib.Path(__file__).parent.joinpath('data01')
data02 = pathlib.Path(__file__).parent.joinpath('data02')
prefix = str(data01.parent)
path = MultiplexedPath(data01, data02)
prefix = str(self.data02.parent)
path = MultiplexedPath(self.data01, self.data02)
self.assertIsInstance(path.joinpath('subdirectory'), MultiplexedPath)
self.assertEqual(
str(path.joinpath('subdirectory', 'subsubdir'))[len(prefix) + 1 :],
Expand All @@ -106,16 +107,8 @@ def test_name(self):
)


class NamespaceReaderTest(unittest.TestCase):
site_dir = str(pathlib.Path(__file__).parent)

@classmethod
def setUpClass(cls):
sys.path.append(cls.site_dir)

@classmethod
def tearDownClass(cls):
sys.path.remove(cls.site_dir)
class NamespaceReaderTest(util.DiskSetup, unittest.TestCase):
MODULE = 'namespacedata01'

def test_init_error(self):
with self.assertRaises(ValueError):
Expand All @@ -125,7 +118,7 @@ def test_resource_path(self):
namespacedata01 = import_module('namespacedata01')
reader = NamespaceReader(namespacedata01.__spec__.submodule_search_locations)

root = os.path.abspath(os.path.join(__file__, '..', 'namespacedata01'))
root = self.data.__path__[0]
self.assertEqual(
reader.resource_path('binary.file'), os.path.join(root, 'binary.file')
)
Expand All @@ -134,9 +127,8 @@ def test_resource_path(self):
)

def test_files(self):
namespacedata01 = import_module('namespacedata01')
reader = NamespaceReader(namespacedata01.__spec__.submodule_search_locations)
root = os.path.abspath(os.path.join(__file__, '..', 'namespacedata01'))
reader = NamespaceReader(self.data.__spec__.submodule_search_locations)
root = self.data.__path__[0]
self.assertIsInstance(reader.files(), MultiplexedPath)
self.assertEqual(repr(reader.files()), f"MultiplexedPath('{root}')")

Expand Down
Loading

0 comments on commit 3faf336

Please sign in to comment.