11from .. import abc
2+ import os .path
23from .. import util
34
45machinery = util .import_importlib ('importlib.machinery' )
@@ -13,34 +14,86 @@ class FindSpecTests(abc.FinderTests):
1314
1415 """Test finding frozen modules."""
1516
16- def find (self , name , path = None ):
17+ def find (self , name , ** kwargs ):
1718 finder = self .machinery .FrozenImporter
1819 with import_helper .frozen_modules ():
19- return finder .find_spec (name , path )
20+ return finder .find_spec (name , ** kwargs )
2021
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 )
2425 self .assertEqual (spec .origin , 'frozen' )
26+ self .assertFalse (spec .has_location )
2527
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 )
3343
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
3558 test_package_in_package = None
3659
3760 # No easy way to test.
3861 test_package_over_module = None
3962
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+
4083 def test_failure (self ):
4184 spec = self .find ('<not real>' )
4285 self .assertIsNone (spec )
4386
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+
4497
4598(Frozen_FindSpecTests ,
4699 Source_FindSpecTests
0 commit comments