Home | History | Annotate | Download | only in tests
      1 """Tests for distutils.sysconfig."""
      2 import os
      3 import test
      4 import unittest
      5 import shutil
      6 
      7 from distutils import sysconfig
      8 from distutils.tests import support
      9 from test.test_support import TESTFN
     10 
     11 class SysconfigTestCase(support.EnvironGuard,
     12                         unittest.TestCase):
     13     def setUp(self):
     14         super(SysconfigTestCase, self).setUp()
     15         self.makefile = None
     16 
     17     def tearDown(self):
     18         if self.makefile is not None:
     19             os.unlink(self.makefile)
     20         self.cleanup_testfn()
     21         super(SysconfigTestCase, self).tearDown()
     22 
     23     def cleanup_testfn(self):
     24         path = test.test_support.TESTFN
     25         if os.path.isfile(path):
     26             os.remove(path)
     27         elif os.path.isdir(path):
     28             shutil.rmtree(path)
     29 
     30     def test_get_python_lib(self):
     31         lib_dir = sysconfig.get_python_lib()
     32         # XXX doesn't work on Linux when Python was never installed before
     33         #self.assertTrue(os.path.isdir(lib_dir), lib_dir)
     34         # test for pythonxx.lib?
     35         self.assertNotEqual(sysconfig.get_python_lib(),
     36                             sysconfig.get_python_lib(prefix=TESTFN))
     37         _sysconfig = __import__('sysconfig')
     38         res = sysconfig.get_python_lib(True, True)
     39         self.assertEqual(_sysconfig.get_path('platstdlib'), res)
     40 
     41     def test_get_python_inc(self):
     42         inc_dir = sysconfig.get_python_inc()
     43         # This is not much of a test.  We make sure Python.h exists
     44         # in the directory returned by get_python_inc() but we don't know
     45         # it is the correct file.
     46         #Broken after issue 7712(r78136) : add a temp_cwd context manager to test_support ...
     47         #NOTE: Its fail on platforms without root directory support(like windows)
     48         #where temp and current working directories may stay on different drivers.
     49         old_wd = os.getcwd()
     50         os.chdir(SAVEDCWD)
     51         self.assertTrue(os.path.isdir(inc_dir), inc_dir)
     52         python_h = os.path.join(inc_dir, "Python.h")
     53         self.assertTrue(os.path.isfile(python_h), python_h)
     54         os.chdir(old_wd)
     55 
     56     def test_parse_makefile_base(self):
     57         self.makefile = test.test_support.TESTFN
     58         fd = open(self.makefile, 'w')
     59         try:
     60             fd.write(r"CONFIG_ARGS=  '--arg1=optarg1' 'ENV=LIB'" '\n')
     61             fd.write('VAR=$OTHER\nOTHER=foo')
     62         finally:
     63             fd.close()
     64         d = sysconfig.parse_makefile(self.makefile)
     65         self.assertEqual(d, {'CONFIG_ARGS': "'--arg1=optarg1' 'ENV=LIB'",
     66                              'OTHER': 'foo'})
     67 
     68     def test_parse_makefile_literal_dollar(self):
     69         self.makefile = test.test_support.TESTFN
     70         fd = open(self.makefile, 'w')
     71         try:
     72             fd.write(r"CONFIG_ARGS=  '--arg1=optarg1' 'ENV=\$$LIB'" '\n')
     73             fd.write('VAR=$OTHER\nOTHER=foo')
     74         finally:
     75             fd.close()
     76         d = sysconfig.parse_makefile(self.makefile)
     77         self.assertEqual(d, {'CONFIG_ARGS': r"'--arg1=optarg1' 'ENV=\$LIB'",
     78                              'OTHER': 'foo'})
     79 
     80 
     81     def test_sysconfig_module(self):
     82         import sysconfig as global_sysconfig
     83         self.assertEqual(global_sysconfig.get_config_var('CFLAGS'), sysconfig.get_config_var('CFLAGS'))
     84         self.assertEqual(global_sysconfig.get_config_var('LDFLAGS'), sysconfig.get_config_var('LDFLAGS'))
     85 
     86     @unittest.skipIf(sysconfig.get_config_var('CUSTOMIZED_OSX_COMPILER'),'compiler flags customized')
     87     def test_sysconfig_compiler_vars(self):
     88         # On OS X, binary installers support extension module building on
     89         # various levels of the operating system with differing Xcode
     90         # configurations.  This requires customization of some of the
     91         # compiler configuration directives to suit the environment on
     92         # the installed machine.  Some of these customizations may require
     93         # running external programs and, so, are deferred until needed by
     94         # the first extension module build.  With Python 3.3, only
     95         # the Distutils version of sysconfig is used for extension module
     96         # builds, which happens earlier in the Distutils tests.  This may
     97         # cause the following tests to fail since no tests have caused
     98         # the global version of sysconfig to call the customization yet.
     99         # The solution for now is to simply skip this test in this case.
    100         # The longer-term solution is to only have one version of sysconfig.
    101 
    102         import sysconfig as global_sysconfig
    103         if sysconfig.get_config_var('CUSTOMIZED_OSX_COMPILER'):
    104             return
    105         self.assertEqual(global_sysconfig.get_config_var('LDSHARED'), sysconfig.get_config_var('LDSHARED'))
    106         self.assertEqual(global_sysconfig.get_config_var('CC'), sysconfig.get_config_var('CC'))
    107 
    108 
    109 
    110 def test_suite():
    111     suite = unittest.TestSuite()
    112     suite.addTest(unittest.makeSuite(SysconfigTestCase))
    113     return suite
    114 
    115 
    116 if __name__ == '__main__':
    117     test.test_support.run_unittest(test_suite())
    118