15
15
16
16
import unittest
17
17
from test import support
18
+ from test .support .script_helper import assert_python_ok
18
19
19
20
# http://bugs.python.org/issue4373
20
21
# Don't load the xx module more than once.
@@ -26,11 +27,8 @@ class BuildExtTestCase(TempdirManager,
26
27
unittest .TestCase ):
27
28
def setUp (self ):
28
29
# Create a simple test environment
29
- # Note that we're making changes to sys.path
30
30
super (BuildExtTestCase , self ).setUp ()
31
31
self .tmp_dir = self .mkdtemp ()
32
- self .sys_path = sys .path , sys .path [:]
33
- sys .path .append (self .tmp_dir )
34
32
import site
35
33
self .old_user_base = site .USER_BASE
36
34
site .USER_BASE = self .mkdtemp ()
@@ -40,15 +38,11 @@ def setUp(self):
40
38
# bpo-30132: On Windows, a .pdb file may be created in the current
41
39
# working directory. Create a temporary working directory to cleanup
42
40
# 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 )
46
44
47
45
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 ]
52
46
import site
53
47
site .USER_BASE = self .old_user_base
54
48
from distutils .command import build_ext
@@ -88,19 +82,34 @@ def test_build_ext(self):
88
82
else :
89
83
ALREADY_TESTED = type (self ).__name__
90
84
91
- import xx
85
+ code = textwrap .dedent (f"""
86
+ tmp_dir = { self .tmp_dir !r}
92
87
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
95
91
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 )
104
113
105
114
def test_solaris_enable_shared (self ):
106
115
dist = Distribution ({'name' : 'xx' })
0 commit comments