Skip to content

Commit dd1cfef

Browse files
authored
bpo-36235: Enhance distutils test_customize_compiler() (GH-12403) (GH-12415)
The test test_customize_compiler() now mocks all sysconfig variables and all environment variables used by customize_compiler(). (cherry picked from commit 72c7b37)
1 parent 6a7a9f1 commit dd1cfef

File tree

1 file changed

+79
-13
lines changed

1 file changed

+79
-13
lines changed

Lib/distutils/tests/test_sysconfig.py

Lines changed: 79 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
"""Tests for distutils.sysconfig."""
2+
import contextlib
23
import os
34
import shutil
45
import subprocess
@@ -74,29 +75,94 @@ def test_srcdir_independent_of_cwd(self):
7475
os.chdir(cwd)
7576
self.assertEqual(srcdir, srcdir2)
7677

77-
@unittest.skipUnless(get_default_compiler() == 'unix',
78-
'not testing if default compiler is not unix')
79-
def test_customize_compiler(self):
80-
os.environ['AR'] = 'my_ar'
81-
os.environ['CC'] = 'my_cc'
82-
os.environ['ARFLAGS'] = '--myarflags'
83-
os.environ['CFLAGS'] = '--mycflags'
84-
78+
def customize_compiler(self):
8579
# make sure AR gets caught
8680
class compiler:
8781
compiler_type = 'unix'
8882

8983
def set_executables(self, **kw):
9084
self.exes = kw
9185

92-
# Make sure that sysconfig._config_vars is initialized
93-
sysconfig.get_config_vars()
86+
sysconfig_vars = {
87+
'AR': 'sc_ar',
88+
'CC': 'sc_cc',
89+
'CXX': 'sc_cxx',
90+
'ARFLAGS': '--sc-arflags',
91+
'CFLAGS': '--sc-cflags',
92+
'CCSHARED': '--sc-ccshared',
93+
'LDSHARED': 'sc_ldshared',
94+
'SHLIB_SUFFIX': 'sc_shutil_suffix',
95+
}
9496

9597
comp = compiler()
96-
with swap_item(sysconfig._config_vars, 'CFLAGS', '--sysconfig-cflags'):
98+
with contextlib.ExitStack() as cm:
99+
for key, value in sysconfig_vars.items():
100+
cm.enter_context(swap_item(sysconfig._config_vars, key, value))
97101
sysconfig.customize_compiler(comp)
98-
self.assertEqual(comp.exes['archiver'], 'my_ar --myarflags')
99-
self.assertEqual(comp.exes['compiler'], 'my_cc --sysconfig-cflags --mycflags')
102+
103+
return comp
104+
105+
@unittest.skipUnless(get_default_compiler() == 'unix',
106+
'not testing if default compiler is not unix')
107+
def test_customize_compiler(self):
108+
# Make sure that sysconfig._config_vars is initialized
109+
sysconfig.get_config_vars()
110+
111+
os.environ['AR'] = 'env_ar'
112+
os.environ['CC'] = 'env_cc'
113+
os.environ['CPP'] = 'env_cpp'
114+
os.environ['CXX'] = 'env_cxx --env-cxx-flags'
115+
os.environ['LDSHARED'] = 'env_ldshared'
116+
os.environ['LDFLAGS'] = '--env-ldflags'
117+
os.environ['ARFLAGS'] = '--env-arflags'
118+
os.environ['CFLAGS'] = '--env-cflags'
119+
os.environ['CPPFLAGS'] = '--env-cppflags'
120+
121+
comp = self.customize_compiler()
122+
self.assertEqual(comp.exes['archiver'],
123+
'env_ar --env-arflags')
124+
self.assertEqual(comp.exes['preprocessor'],
125+
'env_cpp --env-cppflags')
126+
self.assertEqual(comp.exes['compiler'],
127+
'env_cc --sc-cflags --env-cflags --env-cppflags')
128+
self.assertEqual(comp.exes['compiler_so'],
129+
('env_cc --sc-cflags '
130+
'--env-cflags ''--env-cppflags --sc-ccshared'))
131+
self.assertEqual(comp.exes['compiler_cxx'],
132+
'env_cxx --env-cxx-flags')
133+
self.assertEqual(comp.exes['linker_exe'],
134+
'env_cc')
135+
self.assertEqual(comp.exes['linker_so'],
136+
('env_ldshared --env-ldflags --env-cflags'
137+
' --env-cppflags'))
138+
self.assertEqual(comp.shared_lib_extension, 'sc_shutil_suffix')
139+
140+
del os.environ['AR']
141+
del os.environ['CC']
142+
del os.environ['CPP']
143+
del os.environ['CXX']
144+
del os.environ['LDSHARED']
145+
del os.environ['LDFLAGS']
146+
del os.environ['ARFLAGS']
147+
del os.environ['CFLAGS']
148+
del os.environ['CPPFLAGS']
149+
150+
comp = self.customize_compiler()
151+
self.assertEqual(comp.exes['archiver'],
152+
'sc_ar --sc-arflags')
153+
self.assertEqual(comp.exes['preprocessor'],
154+
'sc_cc -E')
155+
self.assertEqual(comp.exes['compiler'],
156+
'sc_cc --sc-cflags')
157+
self.assertEqual(comp.exes['compiler_so'],
158+
'sc_cc --sc-cflags --sc-ccshared')
159+
self.assertEqual(comp.exes['compiler_cxx'],
160+
'sc_cxx')
161+
self.assertEqual(comp.exes['linker_exe'],
162+
'sc_cc')
163+
self.assertEqual(comp.exes['linker_so'],
164+
'sc_ldshared')
165+
self.assertEqual(comp.shared_lib_extension, 'sc_shutil_suffix')
100166

101167
def test_parse_makefile_base(self):
102168
self.makefile = TESTFN

0 commit comments

Comments
 (0)