• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1"""Tests for distutils.command.config."""
2import unittest
3import os
4import sys
5import sysconfig
6from test.support import run_unittest, missing_compiler_executable
7
8from distutils.command.config import dump_file, config
9from distutils.tests import support
10from distutils import log
11
12class ConfigTestCase(support.LoggingSilencer,
13                     support.TempdirManager,
14                     unittest.TestCase):
15
16    def _info(self, msg, *args):
17        for line in msg.splitlines():
18            self._logs.append(line)
19
20    def setUp(self):
21        super(ConfigTestCase, self).setUp()
22        self._logs = []
23        self.old_log = log.info
24        log.info = self._info
25        self.old_config_vars = dict(sysconfig._CONFIG_VARS)
26
27    def tearDown(self):
28        log.info = self.old_log
29        sysconfig._CONFIG_VARS.clear()
30        sysconfig._CONFIG_VARS.update(self.old_config_vars)
31        super(ConfigTestCase, self).tearDown()
32
33    def test_dump_file(self):
34        this_file = os.path.splitext(__file__)[0] + '.py'
35        f = open(this_file)
36        try:
37            numlines = len(f.readlines())
38        finally:
39            f.close()
40
41        dump_file(this_file, 'I am the header')
42        self.assertEqual(len(self._logs), numlines+1)
43
44    @unittest.skipIf(sys.platform == 'win32', "can't test on Windows")
45    def test_search_cpp(self):
46        cmd = missing_compiler_executable(['preprocessor'])
47        if cmd is not None:
48            self.skipTest('The %r command is not found' % cmd)
49        pkg_dir, dist = self.create_dist()
50        cmd = config(dist)
51        cmd._check_compiler()
52        compiler = cmd.compiler
53        if sys.platform[:3] == "aix" and "xlc" in compiler.preprocessor[0].lower():
54            self.skipTest('xlc: The -E option overrides the -P, -o, and -qsyntaxonly options')
55
56        # simple pattern searches
57        match = cmd.search_cpp(pattern='xxx', body='/* xxx */')
58        self.assertEqual(match, 0)
59
60        match = cmd.search_cpp(pattern='_configtest', body='/* xxx */')
61        self.assertEqual(match, 1)
62
63    def test_finalize_options(self):
64        # finalize_options does a bit of transformation
65        # on options
66        pkg_dir, dist = self.create_dist()
67        cmd = config(dist)
68        cmd.include_dirs = 'one%stwo' % os.pathsep
69        cmd.libraries = 'one'
70        cmd.library_dirs = 'three%sfour' % os.pathsep
71        cmd.ensure_finalized()
72
73        self.assertEqual(cmd.include_dirs, ['one', 'two'])
74        self.assertEqual(cmd.libraries, ['one'])
75        self.assertEqual(cmd.library_dirs, ['three', 'four'])
76
77    def test_clean(self):
78        # _clean removes files
79        tmp_dir = self.mkdtemp()
80        f1 = os.path.join(tmp_dir, 'one')
81        f2 = os.path.join(tmp_dir, 'two')
82
83        self.write_file(f1, 'xxx')
84        self.write_file(f2, 'xxx')
85
86        for f in (f1, f2):
87            self.assertTrue(os.path.exists(f))
88
89        pkg_dir, dist = self.create_dist()
90        cmd = config(dist)
91        cmd._clean(f1, f2)
92
93        for f in (f1, f2):
94            self.assertFalse(os.path.exists(f))
95
96def test_suite():
97    return unittest.makeSuite(ConfigTestCase)
98
99if __name__ == "__main__":
100    run_unittest(test_suite())
101