1010
1111import modulefinder
1212
13- TEST_DIR = tempfile .mkdtemp ()
14- TEST_PATH = [TEST_DIR ,os .path .dirname (tempfile .__file__ )]
15-
1613# Each test description is a list of 5 items:
1714#
1815# 1. a module name that will be imported by modulefinder
2320# about because they MAY be not found
2421# 5. a string specifying packages to create; the format is obvious imo.
2522#
26- # Each package will be created inTEST_DIR , andTEST_DIR will be
23+ # Each package will be created intest_dir , andtest_dir will be
2724# removed after the tests again.
28- # Modulefinder searches in a path that containsTEST_DIR , plus
25+ # Modulefinder searches in a path that containstest_dir , plus
2926# the standard Lib directory.
3027
3128maybe_test = [
@@ -300,7 +297,7 @@ def open_file(path):
300297return open (path ,'wb' )
301298
302299
303- def create_package (source ):
300+ def create_package (test_dir , source ):
304301ofi = None
305302try :
306303for line in source .splitlines ():
@@ -313,41 +310,45 @@ def create_package(source):
313310ofi .close ()
314311if type (line )== bytes :
315312line = 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 ()))
317314finally :
318315if ofi :
319316ofi .close ()
320317
321318class 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+
322326def _do_test (self ,info ,report = False ,debug = 0 ,replace_paths = [],modulefinder_class = modulefinder .ModuleFinder ):
323327import_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 )
351352
352353def test_package (self ):
353354self ._do_test (package_test )
@@ -380,7 +381,7 @@ def test_same_name_as_bad(self):
380381self ._do_test (same_name_as_bad_test )
381382
382383def test_bytecode (self ):
383- base_path = os .path .join (TEST_DIR ,'a' )
384+ base_path = os .path .join (self . test_dir ,'a' )
384385source_path = base_path + importlib .machinery .SOURCE_SUFFIXES [0 ]
385386bytecode_path = base_path + importlib .machinery .BYTECODE_SUFFIXES [0 ]
386387with open_file (source_path )as file :
@@ -390,8 +391,8 @@ def test_bytecode(self):
390391self ._do_test (bytecode_test )
391392
392393def 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' )
395396with support .captured_stdout ()as output :
396397self ._do_test (maybe_test ,debug = 2 ,
397398replace_paths = [(old_path ,new_path )])