Skip to content

Commit

Permalink
Add test capturing current behavior and missed expectation. Ref #203.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaraco committed Jul 22, 2022
1 parent 3110294 commit 190f2b5
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 1 deletion.
15 changes: 14 additions & 1 deletion importlib_resources/tests/_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,20 @@
except ImportError:
# Python 3.9 and earlier
class import_helper: # type: ignore
from test.support import modules_setup, modules_cleanup
from test.support import (
modules_setup,
modules_cleanup,
DirsOnSysPath,
CleanImport,
)


try:
from test.support import os_helper # type: ignore
except ImportError:
# Python 3.9 compat
class os_helper: # type:ignore
from test.support import temp_dir


try:
Expand Down
50 changes: 50 additions & 0 deletions importlib_resources/tests/_path.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import pathlib
import functools


####
# from jaraco.path 3.4


def build(spec, prefix=pathlib.Path()):
"""
Build a set of files/directories, as described by the spec.
Each key represents a pathname, and the value represents
the content. Content may be a nested directory.
>>> spec = {
... 'README.txt': "A README file",
... "foo": {
... "__init__.py": "",
... "bar": {
... "__init__.py": "",
... },
... "baz.py": "# Some code",
... }
... }
>>> tmpdir = getfixture('tmpdir')
>>> build(spec, tmpdir)
"""
for name, contents in spec.items():
create(contents, pathlib.Path(prefix) / name)


@functools.singledispatch
def create(content, path):
path.mkdir(exist_ok=True)
build(content, prefix=path) # type: ignore


@create.register
def _(content: bytes, path):
path.write_bytes(content)


@create.register
def _(content: str, path):
path.write_text(content)


# end from jaraco.path
####
28 changes: 28 additions & 0 deletions importlib_resources/tests/test_files.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import typing
import unittest
import contextlib

import importlib_resources as resources
from importlib_resources.abc import Traversable
from . import data01
from . import util
from . import _path
from ._compat import os_helper, import_helper


class FilesTests:
Expand Down Expand Up @@ -42,5 +45,30 @@ def setUp(self):
self.data = namespacedata01


class ModulesFilesTests(unittest.TestCase):
def setUp(self):
self.fixtures = contextlib.ExitStack()
self.addCleanup(self.fixtures.close)
self.site_dir = self.fixtures.enter_context(os_helper.temp_dir())
self.fixtures.enter_context(import_helper.DirsOnSysPath(self.site_dir))
self.fixtures.enter_context(import_helper.CleanImport())

def test_module_resources(self):
"""
A module can have resources found adjacent to the module.
"""
spec = {
'mod.py': '',
'res.txt': 'resources are the best',
}
_path.build(spec, self.site_dir)
import mod

# currently a failure occurs; ref #203
with self.assertRaisesRegex(TypeError, '.*mod.* is not a package'):
actual = resources.files(mod).joinpath('res.txt').read_text()
assert actual == spec['res.txt']


if __name__ == '__main__':
unittest.main()

0 comments on commit 190f2b5

Please sign in to comment.