Skip to content

Commit f3364a9

Browse files
committed
Add test capturing current behavior and missed expectation. Ref #203.
1 parent 3110294 commit f3364a9

File tree

4 files changed

+93
-1
lines changed

4 files changed

+93
-1
lines changed

.coveragerc

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ omit =
66
*/_itertools.py
77
*/_legacy.py
88
*/simple.py
9+
*/_path.py
910

1011
[report]
1112
show_missing = True

importlib_resources/tests/_compat.py

+14-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,20 @@
66
except ImportError:
77
# Python 3.9 and earlier
88
class import_helper: # type: ignore
9-
from test.support import modules_setup, modules_cleanup
9+
from test.support import (
10+
modules_setup,
11+
modules_cleanup,
12+
DirsOnSysPath,
13+
CleanImport,
14+
)
15+
16+
17+
try:
18+
from test.support import os_helper # type: ignore
19+
except ImportError:
20+
# Python 3.9 compat
21+
class os_helper: # type:ignore
22+
from test.support import temp_dir
1023

1124

1225
try:

importlib_resources/tests/_path.py

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import pathlib
2+
import functools
3+
4+
5+
####
6+
# from jaraco.path 3.4
7+
8+
9+
def build(spec, prefix=pathlib.Path()):
10+
"""
11+
Build a set of files/directories, as described by the spec.
12+
13+
Each key represents a pathname, and the value represents
14+
the content. Content may be a nested directory.
15+
16+
>>> spec = {
17+
... 'README.txt': "A README file",
18+
... "foo": {
19+
... "__init__.py": "",
20+
... "bar": {
21+
... "__init__.py": "",
22+
... },
23+
... "baz.py": "# Some code",
24+
... }
25+
... }
26+
>>> tmpdir = getfixture('tmpdir')
27+
>>> build(spec, tmpdir)
28+
"""
29+
for name, contents in spec.items():
30+
create(contents, pathlib.Path(prefix) / name)
31+
32+
33+
@functools.singledispatch
34+
def create(content, path):
35+
path.mkdir(exist_ok=True)
36+
build(content, prefix=path) # type: ignore
37+
38+
39+
@create.register
40+
def _(content: bytes, path):
41+
path.write_bytes(content)
42+
43+
44+
@create.register
45+
def _(content: str, path):
46+
path.write_text(content)
47+
48+
49+
# end from jaraco.path
50+
####

importlib_resources/tests/test_files.py

+28
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
import typing
22
import unittest
3+
import contextlib
34

45
import importlib_resources as resources
56
from importlib_resources.abc import Traversable
67
from . import data01
78
from . import util
9+
from . import _path
10+
from ._compat import os_helper, import_helper
811

912

1013
class FilesTests:
@@ -42,5 +45,30 @@ def setUp(self):
4245
self.data = namespacedata01
4346

4447

48+
class ModulesFilesTests(unittest.TestCase):
49+
def setUp(self):
50+
self.fixtures = contextlib.ExitStack()
51+
self.addCleanup(self.fixtures.close)
52+
self.site_dir = self.fixtures.enter_context(os_helper.temp_dir())
53+
self.fixtures.enter_context(import_helper.DirsOnSysPath(self.site_dir))
54+
self.fixtures.enter_context(import_helper.CleanImport())
55+
56+
def test_module_resources(self):
57+
"""
58+
A module can have resources found adjacent to the module.
59+
"""
60+
spec = {
61+
'mod.py': '',
62+
'res.txt': 'resources are the best',
63+
}
64+
_path.build(spec, self.site_dir)
65+
import mod
66+
67+
# currently a failure occurs; ref #203
68+
with self.assertRaisesRegex(TypeError, '.*mod.* is not a package'):
69+
actual = resources.files(mod).joinpath('res.txt').read_text()
70+
assert actual == spec['res.txt']
71+
72+
4573
if __name__ == '__main__':
4674
unittest.main()

0 commit comments

Comments
 (0)