1
1
from .. import abc
2
+ import os .path
2
3
from .. import util
3
4
4
5
machinery = util .import_importlib ('importlib.machinery' )
@@ -13,34 +14,86 @@ class FindSpecTests(abc.FinderTests):
13
14
14
15
"""Test finding frozen modules."""
15
16
16
- def find (self , name , path = None ):
17
+ def find (self , name , ** kwargs ):
17
18
finder = self .machinery .FrozenImporter
18
19
with import_helper .frozen_modules ():
19
- return finder .find_spec (name , path )
20
+ return finder .find_spec (name , ** kwargs )
20
21
21
- def test_module (self ):
22
- name = '__hello__'
23
- spec = self .find ( name )
22
+ def check (self , spec , name ):
23
+ self . assertEqual ( spec . name , name )
24
+ self . assertIs ( spec . loader , self .machinery . FrozenImporter )
24
25
self .assertEqual (spec .origin , 'frozen' )
26
+ self .assertFalse (spec .has_location )
25
27
26
- def test_package (self ):
27
- spec = self .find ('__phello__' )
28
- self .assertIsNotNone (spec )
29
-
30
- def test_module_in_package (self ):
31
- spec = self .find ('__phello__.spam' , ['__phello__' ])
32
- self .assertIsNotNone (spec )
28
+ def test_module (self ):
29
+ names = [
30
+ '__hello__' ,
31
+ '__hello_alias__' ,
32
+ '__hello_only__' ,
33
+ '__phello__.__init__' ,
34
+ '__phello__.spam' ,
35
+ '__phello__.ham.__init__' ,
36
+ '__phello__.ham.eggs' ,
37
+ ]
38
+ for name in names :
39
+ with self .subTest (name ):
40
+ spec = self .find (name )
41
+ self .check (spec , name )
42
+ self .assertEqual (spec .submodule_search_locations , None )
33
43
34
- # No frozen package within another package to test with.
44
+ def test_package (self ):
45
+ names = [
46
+ '__phello__' ,
47
+ '__phello__.ham' ,
48
+ '__phello_alias__' ,
49
+ ]
50
+ for name in names :
51
+ with self .subTest (name ):
52
+ spec = self .find (name )
53
+ self .check (spec , name )
54
+ self .assertEqual (spec .submodule_search_locations , [])
55
+
56
+ # These are covered by test_module() and test_package().
57
+ test_module_in_package = None
35
58
test_package_in_package = None
36
59
37
60
# No easy way to test.
38
61
test_package_over_module = None
39
62
63
+ def test_path_ignored (self ):
64
+ for name in ('__hello__' , '__phello__' , '__phello__.spam' ):
65
+ actual = self .find (name )
66
+ for path in (None , object (), '' , 'eggs' , [], ['' ], ['eggs' ]):
67
+ with self .subTest ((name , path )):
68
+ spec = self .find (name , path = path )
69
+ self .assertEqual (spec , actual )
70
+
71
+ def test_target_ignored (self ):
72
+ imported = ('__hello__' , '__phello__' )
73
+ with import_helper .CleanImport (* imported , usefrozen = True ):
74
+ import __hello__ as match
75
+ import __phello__ as nonmatch
76
+ name = '__hello__'
77
+ actual = self .find (name )
78
+ for target in (None , match , nonmatch , object (), 'not-a-module-object' ):
79
+ with self .subTest (target ):
80
+ spec = self .find (name , target = target )
81
+ self .assertEqual (spec , actual )
82
+
40
83
def test_failure (self ):
41
84
spec = self .find ('<not real>' )
42
85
self .assertIsNone (spec )
43
86
87
+ def test_not_using_frozen (self ):
88
+ finder = self .machinery .FrozenImporter
89
+ with import_helper .frozen_modules (enabled = False ):
90
+ # both frozen and not frozen
91
+ spec1 = finder .find_spec ('__hello__' )
92
+ # only frozen
93
+ spec2 = finder .find_spec ('__hello_only__' )
94
+ self .assertIsNone (spec1 )
95
+ self .assertIsNone (spec2 )
96
+
44
97
45
98
(Frozen_FindSpecTests ,
46
99
Source_FindSpecTests
0 commit comments