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