Skip to content

Commit

Permalink
bpo-36235: Enhance distutils test_customize_compiler() (pythonGH-12403)
Browse files Browse the repository at this point in the history
The test test_customize_compiler() now mocks all sysconfig variables
and all environment variables used by customize_compiler().
  • Loading branch information
vstinner authored Mar 18, 2019
1 parent 9e06d2b commit 72c7b37
Showing 1 changed file with 79 additions and 13 deletions.
92 changes: 79 additions & 13 deletions Lib/distutils/tests/test_sysconfig.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Tests for distutils.sysconfig."""
import contextlib
import os
import shutil
import subprocess
Expand Down Expand Up @@ -74,29 +75,94 @@ def test_srcdir_independent_of_cwd(self):
os.chdir(cwd)
self.assertEqual(srcdir, srcdir2)

@unittest.skipUnless(get_default_compiler() == 'unix',
'not testing if default compiler is not unix')
def test_customize_compiler(self):
os.environ['AR'] = 'my_ar'
os.environ['CC'] = 'my_cc'
os.environ['ARFLAGS'] = '--myarflags'
os.environ['CFLAGS'] = '--mycflags'

def customize_compiler(self):
# make sure AR gets caught
class compiler:
compiler_type = 'unix'

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

# Make sure that sysconfig._config_vars is initialized
sysconfig.get_config_vars()
sysconfig_vars = {
'AR': 'sc_ar',
'CC': 'sc_cc',
'CXX': 'sc_cxx',
'ARFLAGS': '--sc-arflags',
'CFLAGS': '--sc-cflags',
'CCSHARED': '--sc-ccshared',
'LDSHARED': 'sc_ldshared',
'SHLIB_SUFFIX': 'sc_shutil_suffix',
}

comp = compiler()
with swap_item(sysconfig._config_vars, 'CFLAGS', '--sysconfig-cflags'):
with contextlib.ExitStack() as cm:
for key, value in sysconfig_vars.items():
cm.enter_context(swap_item(sysconfig._config_vars, key, value))
sysconfig.customize_compiler(comp)
self.assertEqual(comp.exes['archiver'], 'my_ar --myarflags')
self.assertEqual(comp.exes['compiler'], 'my_cc --sysconfig-cflags --mycflags')

return comp

@unittest.skipUnless(get_default_compiler() == 'unix',
'not testing if default compiler is not unix')
def test_customize_compiler(self):
# Make sure that sysconfig._config_vars is initialized
sysconfig.get_config_vars()

os.environ['AR'] = 'env_ar'
os.environ['CC'] = 'env_cc'
os.environ['CPP'] = 'env_cpp'
os.environ['CXX'] = 'env_cxx --env-cxx-flags'
os.environ['LDSHARED'] = 'env_ldshared'
os.environ['LDFLAGS'] = '--env-ldflags'
os.environ['ARFLAGS'] = '--env-arflags'
os.environ['CFLAGS'] = '--env-cflags'
os.environ['CPPFLAGS'] = '--env-cppflags'

comp = self.customize_compiler()
self.assertEqual(comp.exes['archiver'],
'env_ar --env-arflags')
self.assertEqual(comp.exes['preprocessor'],
'env_cpp --env-cppflags')
self.assertEqual(comp.exes['compiler'],
'env_cc --sc-cflags --env-cflags --env-cppflags')
self.assertEqual(comp.exes['compiler_so'],
('env_cc --sc-cflags '
'--env-cflags ''--env-cppflags --sc-ccshared'))
self.assertEqual(comp.exes['compiler_cxx'],
'env_cxx --env-cxx-flags')
self.assertEqual(comp.exes['linker_exe'],
'env_cc')
self.assertEqual(comp.exes['linker_so'],
('env_ldshared --env-ldflags --env-cflags'
' --env-cppflags'))
self.assertEqual(comp.shared_lib_extension, 'sc_shutil_suffix')

del os.environ['AR']
del os.environ['CC']
del os.environ['CPP']
del os.environ['CXX']
del os.environ['LDSHARED']
del os.environ['LDFLAGS']
del os.environ['ARFLAGS']
del os.environ['CFLAGS']
del os.environ['CPPFLAGS']

comp = self.customize_compiler()
self.assertEqual(comp.exes['archiver'],
'sc_ar --sc-arflags')
self.assertEqual(comp.exes['preprocessor'],
'sc_cc -E')
self.assertEqual(comp.exes['compiler'],
'sc_cc --sc-cflags')
self.assertEqual(comp.exes['compiler_so'],
'sc_cc --sc-cflags --sc-ccshared')
self.assertEqual(comp.exes['compiler_cxx'],
'sc_cxx')
self.assertEqual(comp.exes['linker_exe'],
'sc_cc')
self.assertEqual(comp.exes['linker_so'],
'sc_ldshared')
self.assertEqual(comp.shared_lib_extension, 'sc_shutil_suffix')

def test_parse_makefile_base(self):
self.makefile = TESTFN
Expand Down

0 comments on commit 72c7b37

Please sign in to comment.