File tree 4 files changed +93
-1
lines changed
importlib_resources/tests
4 files changed +93
-1
lines changed Original file line number Diff line number Diff line change 6
6
*/_itertools.py
7
7
*/_legacy.py
8
8
*/simple.py
9
+ */_path.py
9
10
10
11
[report]
11
12
show_missing = True
Original file line number Diff line number Diff line change 6
6
except ImportError :
7
7
# Python 3.9 and earlier
8
8
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
10
23
11
24
12
25
try :
Original file line number Diff line number Diff line change
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
+ ####
Original file line number Diff line number Diff line change 1
1
import typing
2
2
import unittest
3
+ import contextlib
3
4
4
5
import importlib_resources as resources
5
6
from importlib_resources .abc import Traversable
6
7
from . import data01
7
8
from . import util
9
+ from . import _path
10
+ from ._compat import os_helper , import_helper
8
11
9
12
10
13
class FilesTests :
@@ -42,5 +45,30 @@ def setUp(self):
42
45
self .data = namespacedata01
43
46
44
47
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
+
45
73
if __name__ == '__main__' :
46
74
unittest .main ()
You can’t perform that action at this time.
0 commit comments