Skip to content

Commit 98bbbbe

Browse files
gh-93616: Fix env changed issue in test_modulefinder (GH-93617)
(cherry picked from commit cffa4f7) Co-authored-by: Christian Heimes <christian@python.org>
1 parent 47a7855 commit 98bbbbe

File tree

2 files changed

+40
-37
lines changed

2 files changed

+40
-37
lines changed

Lib/test/test_modulefinder.py

Lines changed: 38 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@
1010

1111
import 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
@@ -23,9 +20,9 @@
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 in TEST_DIR, and TEST_DIR will be
23+
# Each package will be created in test_dir, and test_dir will be
2724
# 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
2926
# the standard Lib directory.
3027

3128
maybe_test = [
@@ -300,7 +297,7 @@ def open_file(path):
300297
return open(path, 'wb')
301298

302299

303-
def create_package(source):
300+
def create_package(test_dir, source):
304301
ofi = None
305302
try:
306303
for line in source.splitlines():
@@ -313,41 +310,45 @@ def create_package(source):
313310
ofi.close()
314311
if type(line) == bytes:
315312
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()))
317314
finally:
318315
if ofi:
319316
ofi.close()
320317

321318
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+
322326
def _do_test(self, info, report=False, debug=0, replace_paths=[], modulefinder_class=modulefinder.ModuleFinder):
323327
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)
351352

352353
def test_package(self):
353354
self._do_test(package_test)
@@ -380,7 +381,7 @@ def test_same_name_as_bad(self):
380381
self._do_test(same_name_as_bad_test)
381382

382383
def test_bytecode(self):
383-
base_path = os.path.join(TEST_DIR, 'a')
384+
base_path = os.path.join(self.test_dir, 'a')
384385
source_path = base_path + importlib.machinery.SOURCE_SUFFIXES[0]
385386
bytecode_path = base_path + importlib.machinery.BYTECODE_SUFFIXES[0]
386387
with open_file(source_path) as file:
@@ -390,8 +391,8 @@ def test_bytecode(self):
390391
self._do_test(bytecode_test)
391392

392393
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')
395396
with support.captured_stdout() as output:
396397
self._do_test(maybe_test, debug=2,
397398
replace_paths=[(old_path, new_path)])
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
``test_modulefinder`` now creates a temporary directory in
2+
``ModuleFinderTest.setUp()`` instead of module scope.

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy