mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +00:00
bpo-34200: Fix non-determinism of test_pkg (GH-9248)
This causes the tearDown code to only unimport the test modules specifically created as part of each test via the self.mkhier method rather than abusing test.support.modules_setup() and the scary test.support.modules_cleanup() code. https://bugs.python.org/issue34200
This commit is contained in:
parent
ed709d5699
commit
4ae8ece5cd
2 changed files with 12 additions and 8 deletions
|
@ -5,7 +5,6 @@ import os
|
||||||
import tempfile
|
import tempfile
|
||||||
import textwrap
|
import textwrap
|
||||||
import unittest
|
import unittest
|
||||||
from test import support
|
|
||||||
|
|
||||||
|
|
||||||
# Helpers to create and destroy hierarchies.
|
# Helpers to create and destroy hierarchies.
|
||||||
|
@ -50,11 +49,13 @@ class TestPkg(unittest.TestCase):
|
||||||
self.root = None
|
self.root = None
|
||||||
self.pkgname = None
|
self.pkgname = None
|
||||||
self.syspath = list(sys.path)
|
self.syspath = list(sys.path)
|
||||||
self.modules_before = support.modules_setup()
|
self.modules_to_cleanup = set() # Populated by mkhier().
|
||||||
|
|
||||||
def tearDown(self):
|
def tearDown(self):
|
||||||
sys.path[:] = self.syspath
|
sys.path[:] = self.syspath
|
||||||
support.modules_cleanup(*self.modules_before)
|
for modulename in self.modules_to_cleanup:
|
||||||
|
if modulename in sys.modules:
|
||||||
|
del sys.modules[modulename]
|
||||||
if self.root: # Only clean if the test was actually run
|
if self.root: # Only clean if the test was actually run
|
||||||
cleanout(self.root)
|
cleanout(self.root)
|
||||||
|
|
||||||
|
@ -75,17 +76,17 @@ class TestPkg(unittest.TestCase):
|
||||||
os.mkdir(root)
|
os.mkdir(root)
|
||||||
for name, contents in descr:
|
for name, contents in descr:
|
||||||
comps = name.split()
|
comps = name.split()
|
||||||
|
self.modules_to_cleanup.add('.'.join(comps))
|
||||||
fullname = root
|
fullname = root
|
||||||
for c in comps:
|
for c in comps:
|
||||||
fullname = os.path.join(fullname, c)
|
fullname = os.path.join(fullname, c)
|
||||||
if contents is None:
|
if contents is None:
|
||||||
os.mkdir(fullname)
|
os.mkdir(fullname)
|
||||||
else:
|
else:
|
||||||
f = open(fullname, "w")
|
with open(fullname, "w") as f:
|
||||||
f.write(contents)
|
f.write(contents)
|
||||||
if contents and contents[-1] != '\n':
|
if not contents.endswith('\n'):
|
||||||
f.write('\n')
|
f.write('\n')
|
||||||
f.close()
|
|
||||||
self.root = root
|
self.root = root
|
||||||
# package name is the name of the first item
|
# package name is the name of the first item
|
||||||
self.pkgname = descr[0][0]
|
self.pkgname = descr[0][0]
|
||||||
|
|
|
@ -0,0 +1,3 @@
|
||||||
|
Fixed non-deterministic flakiness of test_pkg by not using the scary
|
||||||
|
test.support.module_cleanup() logic to save and restore sys.modules contents
|
||||||
|
between test cases.
|
Loading…
Add table
Add a link
Reference in a new issue