1515
1616import unittest
1717from test import support
18+ from test .support .script_helper import assert_python_ok
1819
1920# http://bugs.python.org/issue4373
2021# Don't load the xx module more than once.
@@ -26,11 +27,8 @@ class BuildExtTestCase(TempdirManager,
2627 unittest .TestCase ):
2728 def setUp (self ):
2829 # Create a simple test environment
29- # Note that we're making changes to sys.path
3030 super (BuildExtTestCase , self ).setUp ()
3131 self .tmp_dir = self .mkdtemp ()
32- self .sys_path = sys .path , sys .path [:]
33- sys .path .append (self .tmp_dir )
3432 import site
3533 self .old_user_base = site .USER_BASE
3634 site .USER_BASE = self .mkdtemp ()
@@ -40,15 +38,11 @@ def setUp(self):
4038 # bpo-30132: On Windows, a .pdb file may be created in the current
4139 # working directory. Create a temporary working directory to cleanup
4240 # everything at the end of the test.
43- self . temp_cwd = support .temp_cwd ( )
44- self . temp_cwd .__enter__ ()
45- self .addCleanup (self . temp_cwd .__exit__ , None , None , None )
41+ change_cwd = support .change_cwd ( self . tmp_dir )
42+ change_cwd .__enter__ ()
43+ self .addCleanup (change_cwd .__exit__ , None , None , None )
4644
4745 def tearDown (self ):
48- # Get everything back to normal
49- support .unload ('xx' )
50- sys .path = self .sys_path [0 ]
51- sys .path [:] = self .sys_path [1 ]
5246 import site
5347 site .USER_BASE = self .old_user_base
5448 from distutils .command import build_ext
@@ -88,19 +82,34 @@ def test_build_ext(self):
8882 else :
8983 ALREADY_TESTED = type (self ).__name__
9084
91- import xx
85+ code = textwrap .dedent (f"""
86+ tmp_dir = { self .tmp_dir !r}
9287
93- for attr in ('error' , 'foo' , 'new' , 'roj' ):
94- self .assertTrue (hasattr (xx , attr ))
88+ import sys
89+ import unittest
90+ from test import support
9591
96- self .assertEqual (xx .foo (2 , 5 ), 7 )
97- self .assertEqual (xx .foo (13 ,15 ), 28 )
98- self .assertEqual (xx .new ().demo (), None )
99- if support .HAVE_DOCSTRINGS :
100- doc = 'This is a template module just for instruction.'
101- self .assertEqual (xx .__doc__ , doc )
102- self .assertIsInstance (xx .Null (), xx .Null )
103- self .assertIsInstance (xx .Str (), xx .Str )
92+ sys.path.insert(0, tmp_dir)
93+ import xx
94+
95+ class Tests(unittest.TestCase):
96+ def test_xx(self):
97+ for attr in ('error', 'foo', 'new', 'roj'):
98+ self.assertTrue(hasattr(xx, attr))
99+
100+ self.assertEqual(xx.foo(2, 5), 7)
101+ self.assertEqual(xx.foo(13,15), 28)
102+ self.assertEqual(xx.new().demo(), None)
103+ if support.HAVE_DOCSTRINGS:
104+ doc = 'This is a template module just for instruction.'
105+ self.assertEqual(xx.__doc__, doc)
106+ self.assertIsInstance(xx.Null(), xx.Null)
107+ self.assertIsInstance(xx.Str(), xx.Str)
108+
109+
110+ unittest.main()
111+ """ )
112+ assert_python_ok ('-c' , code )
104113
105114 def test_solaris_enable_shared (self ):
106115 dist = Distribution ({'name' : 'xx' })
0 commit comments