10
10
11
11
import modulefinder
12
12
13
- TEST_DIR = tempfile .mkdtemp ()
14
- TEST_PATH = [TEST_DIR , os .path .dirname (tempfile .__file__ )]
15
-
16
13
# Each test description is a list of 5 items:
17
14
#
18
15
# 1. a module name that will be imported by modulefinder
23
20
# about because they MAY be not found
24
21
# 5. a string specifying packages to create; the format is obvious imo.
25
22
#
26
- # Each package will be created in TEST_DIR , and TEST_DIR will be
23
+ # Each package will be created in test_dir , and test_dir will be
27
24
# removed after the tests again.
28
- # Modulefinder searches in a path that contains TEST_DIR , plus
25
+ # Modulefinder searches in a path that contains test_dir , plus
29
26
# the standard Lib directory.
30
27
31
28
maybe_test = [
@@ -300,7 +297,7 @@ def open_file(path):
300
297
return open (path , 'wb' )
301
298
302
299
303
- def create_package (source ):
300
+ def create_package (test_dir , source ):
304
301
ofi = None
305
302
try :
306
303
for line in source .splitlines ():
@@ -313,41 +310,45 @@ def create_package(source):
313
310
ofi .close ()
314
311
if type (line ) == bytes :
315
312
line = line .decode ('utf-8' )
316
- ofi = open_file (os .path .join (TEST_DIR , line .strip ()))
313
+ ofi = open_file (os .path .join (test_dir , line .strip ()))
317
314
finally :
318
315
if ofi :
319
316
ofi .close ()
320
317
321
318
class ModuleFinderTest (unittest .TestCase ):
319
+ def setUp (self ):
320
+ self .test_dir = tempfile .mkdtemp ()
321
+ self .test_path = [self .test_dir , os .path .dirname (tempfile .__file__ )]
322
+
323
+ def tearDown (self ):
324
+ shutil .rmtree (self .test_dir )
325
+
322
326
def _do_test (self , info , report = False , debug = 0 , replace_paths = [], modulefinder_class = modulefinder .ModuleFinder ):
323
327
import_this , modules , missing , maybe_missing , source = info
324
- create_package (source )
325
- try :
326
- mf = modulefinder_class (path = TEST_PATH , debug = debug ,
327
- replace_paths = replace_paths )
328
- mf .import_hook (import_this )
329
- if report :
330
- mf .report ()
331
- ## # This wouldn't work in general when executed several times:
332
- ## opath = sys.path[:]
333
- ## sys.path = TEST_PATH
334
- ## try:
335
- ## __import__(import_this)
336
- ## except:
337
- ## import traceback; traceback.print_exc()
338
- ## sys.path = opath
339
- ## return
340
- modules = sorted (set (modules ))
341
- found = sorted (mf .modules )
342
- # check if we found what we expected, not more, not less
343
- self .assertEqual (found , modules )
344
-
345
- # check for missing and maybe missing modules
346
- bad , maybe = mf .any_missing_maybe ()
347
- self .assertEqual (bad , missing )
348
- self .assertEqual (maybe , maybe_missing )
349
- finally :
350
- shutil .rmtree (TEST_DIR )
328
+ create_package (self .test_dir , source )
329
+ mf = modulefinder_class (path = self .test_path , debug = debug ,
330
+ replace_paths = replace_paths )
331
+ mf .import_hook (import_this )
332
+ if report :
333
+ mf .report ()
334
+ ## # This wouldn't work in general when executed several times:
335
+ ## opath = sys.path[:]
336
+ ## sys.path = self.test_path
337
+ ## try:
338
+ ## __import__(import_this)
339
+ ## except:
340
+ ## import traceback; traceback.print_exc()
341
+ ## sys.path = opath
342
+ ## return
343
+ modules = sorted (set (modules ))
344
+ found = sorted (mf .modules )
345
+ # check if we found what we expected, not more, not less
346
+ self .assertEqual (found , modules )
347
+
348
+ # check for missing and maybe missing modules
349
+ bad , maybe = mf .any_missing_maybe ()
350
+ self .assertEqual (bad , missing )
351
+ self .assertEqual (maybe , maybe_missing )
351
352
352
353
def test_package (self ):
353
354
self ._do_test (package_test )
@@ -380,7 +381,7 @@ def test_same_name_as_bad(self):
380
381
self ._do_test (same_name_as_bad_test )
381
382
382
383
def test_bytecode (self ):
383
- base_path = os .path .join (TEST_DIR , 'a' )
384
+ base_path = os .path .join (self . test_dir , 'a' )
384
385
source_path = base_path + importlib .machinery .SOURCE_SUFFIXES [0 ]
385
386
bytecode_path = base_path + importlib .machinery .BYTECODE_SUFFIXES [0 ]
386
387
with open_file (source_path ) as file :
@@ -390,8 +391,8 @@ def test_bytecode(self):
390
391
self ._do_test (bytecode_test )
391
392
392
393
def test_replace_paths (self ):
393
- old_path = os .path .join (TEST_DIR , 'a' , 'module.py' )
394
- new_path = os .path .join (TEST_DIR , 'a' , 'spam.py' )
394
+ old_path = os .path .join (self . test_dir , 'a' , 'module.py' )
395
+ new_path = os .path .join (self . test_dir , 'a' , 'spam.py' )
395
396
with support .captured_stdout () as output :
396
397
self ._do_test (maybe_test , debug = 2 ,
397
398
replace_paths = [(old_path , new_path )])
0 commit comments