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