blob: 215dc82df0b6f7ec32480d20dde97424dab153eb [file] [log] [blame]
Tarek Ziadé29bbb962009-05-10 12:02:35 +00001"""Tests for distutils.sysconfig."""
Christian Heimese7f9f462007-12-08 15:34:59 +00002import os
Tarek Ziadéabcc3f42009-06-11 08:31:17 +00003import test
Christian Heimese7f9f462007-12-08 15:34:59 +00004import unittest
5
Tarek Ziadé29bbb962009-05-10 12:02:35 +00006from distutils import sysconfig
Tarek Ziadé36797272010-07-22 12:50:05 +00007from distutils.ccompiler import get_default_compiler
Tarek Ziadé29bbb962009-05-10 12:02:35 +00008from distutils.tests import support
Tarek Ziadéabcc3f42009-06-11 08:31:17 +00009from test.support import TESTFN, run_unittest
Christian Heimese7f9f462007-12-08 15:34:59 +000010
Tarek Ziadé29bbb962009-05-10 12:02:35 +000011class SysconfigTestCase(support.EnvironGuard,
12 unittest.TestCase):
Tarek Ziadéabcc3f42009-06-11 08:31:17 +000013 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)
Tarek Ziadé430fb632009-10-18 11:34:51 +000020 self.cleanup_testfn()
Tarek Ziadéabcc3f42009-06-11 08:31:17 +000021 super(SysconfigTestCase, self).tearDown()
Tarek Ziadéd3409de2009-02-06 01:18:36 +000022
Tarek Ziadé430fb632009-10-18 11:34:51 +000023 def cleanup_testfn(self):
24 if os.path.isfile(TESTFN):
25 os.remove(TESTFN)
26 elif os.path.isdir(TESTFN):
27 shutil.rmtree(TESTFN)
28
Tarek Ziadé36797272010-07-22 12:50:05 +000029 def test_get_config_h_filename(self):
30 config_h = sysconfig.get_config_h_filename()
31 self.assertTrue(os.path.isfile(config_h), config_h)
32
Christian Heimese7f9f462007-12-08 15:34:59 +000033 def test_get_python_lib(self):
34 lib_dir = sysconfig.get_python_lib()
Christian Heimes0449f632007-12-15 01:27:15 +000035 # XXX doesn't work on Linux when Python was never installed before
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000036 #self.assertTrue(os.path.isdir(lib_dir), lib_dir)
Christian Heimese7f9f462007-12-08 15:34:59 +000037 # test for pythonxx.lib?
Tarek Ziadé7d5e9872009-02-10 12:36:33 +000038 self.assertNotEqual(sysconfig.get_python_lib(),
39 sysconfig.get_python_lib(prefix=TESTFN))
Christian Heimese7f9f462007-12-08 15:34:59 +000040
41 def test_get_python_inc(self):
Neil Schemenauerc03a2882009-02-05 16:35:04 +000042 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.
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000046 self.assertTrue(os.path.isdir(inc_dir), inc_dir)
Christian Heimese7f9f462007-12-08 15:34:59 +000047 python_h = os.path.join(inc_dir, "Python.h")
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000048 self.assertTrue(os.path.isfile(python_h), python_h)
Christian Heimese7f9f462007-12-08 15:34:59 +000049
Tarek Ziadé36797272010-07-22 12:50:05 +000050 def test_get_config_vars(self):
51 cvars = sysconfig.get_config_vars()
52 self.assertTrue(isinstance(cvars, dict))
53 self.assertTrue(cvars)
54
55 def test_customize_compiler(self):
56
57 # not testing if default compiler is not unix
58 if get_default_compiler() != 'unix':
59 return
60
61 os.environ['AR'] = 'my_ar'
62 os.environ['ARFLAGS'] = '-arflags'
63
64 # make sure AR gets caught
65 class compiler:
66 compiler_type = 'unix'
67
68 def set_executables(self, **kw):
69 self.exes = kw
70
71 comp = compiler()
72 sysconfig.customize_compiler(comp)
73 self.assertEquals(comp.exes['archiver'], 'my_ar -arflags')
74
Tarek Ziadéabcc3f42009-06-11 08:31:17 +000075 def test_parse_makefile_base(self):
76 self.makefile = TESTFN
77 fd = open(self.makefile, 'w')
Éric Araujobee5cef2010-11-05 23:51:56 +000078 try:
79 fd.write(r"CONFIG_ARGS= '--arg1=optarg1' 'ENV=LIB'" '\n')
80 fd.write('VAR=$OTHER\nOTHER=foo')
81 finally:
82 fd.close()
Tarek Ziadéabcc3f42009-06-11 08:31:17 +000083 d = sysconfig.parse_makefile(self.makefile)
84 self.assertEquals(d, {'CONFIG_ARGS': "'--arg1=optarg1' 'ENV=LIB'",
85 'OTHER': 'foo'})
86
87 def test_parse_makefile_literal_dollar(self):
88 self.makefile = TESTFN
89 fd = open(self.makefile, 'w')
Éric Araujobee5cef2010-11-05 23:51:56 +000090 try:
91 fd.write(r"CONFIG_ARGS= '--arg1=optarg1' 'ENV=\$$LIB'" '\n')
92 fd.write('VAR=$OTHER\nOTHER=foo')
93 finally:
94 fd.close()
Tarek Ziadéabcc3f42009-06-11 08:31:17 +000095 d = sysconfig.parse_makefile(self.makefile)
96 self.assertEquals(d, {'CONFIG_ARGS': r"'--arg1=optarg1' 'ENV=\$LIB'",
97 'OTHER': 'foo'})
98
Christian Heimese7f9f462007-12-08 15:34:59 +000099
Ronald Oussorene8d252d2010-07-23 09:43:17 +0000100 def test_sysconfig_module(self):
101 import sysconfig as global_sysconfig
102 self.assertEquals(global_sysconfig.get_config_var('CFLAGS'), sysconfig.get_config_var('CFLAGS'))
103 self.assertEquals(global_sysconfig.get_config_var('LDFLAGS'), sysconfig.get_config_var('LDFLAGS'))
104 self.assertEquals(global_sysconfig.get_config_var('LDSHARED'),sysconfig.get_config_var('LDSHARED'))
105 self.assertEquals(global_sysconfig.get_config_var('CC'), sysconfig.get_config_var('CC'))
106
107
108
Christian Heimese7f9f462007-12-08 15:34:59 +0000109def test_suite():
110 suite = unittest.TestSuite()
111 suite.addTest(unittest.makeSuite(SysconfigTestCase))
112 return suite
Tarek Ziadéabcc3f42009-06-11 08:31:17 +0000113
114
115if __name__ == '__main__':
116 run_unittest(test_suite())