Tarek Ziadé | 0eb1304 | 2009-04-12 14:57:46 +0000 | [diff] [blame] | 1 | """Tests for distutils.command.config.""" |
| 2 | import unittest |
| 3 | import os |
Tarek Ziadé | f1f003c | 2009-04-13 12:59:03 +0000 | [diff] [blame] | 4 | import sys |
Éric Araujo | 70ec44a | 2010-11-06 02:44:43 +0000 | [diff] [blame] | 5 | from test.support import run_unittest |
Tarek Ziadé | 0eb1304 | 2009-04-12 14:57:46 +0000 | [diff] [blame] | 6 | |
Tarek Ziadé | fd39b7a | 2009-04-12 16:34:34 +0000 | [diff] [blame] | 7 | from distutils.command.config import dump_file, config |
Tarek Ziadé | 0eb1304 | 2009-04-12 14:57:46 +0000 | [diff] [blame] | 8 | from distutils.tests import support |
| 9 | from distutils import log |
| 10 | |
| 11 | class ConfigTestCase(support.LoggingSilencer, |
| 12 | support.TempdirManager, |
| 13 | unittest.TestCase): |
| 14 | |
Tarek Ziadé | fd39b7a | 2009-04-12 16:34:34 +0000 | [diff] [blame] | 15 | def _info(self, msg, *args): |
Tarek Ziadé | 0eb1304 | 2009-04-12 14:57:46 +0000 | [diff] [blame] | 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') |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 38 | self.assertEqual(len(self._logs), numlines+1) |
Tarek Ziadé | 0eb1304 | 2009-04-12 14:57:46 +0000 | [diff] [blame] | 39 | |
Serhiy Storchaka | 3c02ece | 2013-12-18 16:41:01 +0200 | [diff] [blame] | 40 | @unittest.skipIf(sys.platform == 'win32', "can't test on Windows") |
Tarek Ziadé | fd39b7a | 2009-04-12 16:34:34 +0000 | [diff] [blame] | 41 | def test_search_cpp(self): |
| 42 | pkg_dir, dist = self.create_dist() |
| 43 | cmd = config(dist) |
| 44 | |
| 45 | # simple pattern searches |
Éric Araujo | db95c7a | 2011-10-07 23:13:45 +0200 | [diff] [blame] | 46 | match = cmd.search_cpp(pattern='xxx', body='/* xxx */') |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 47 | self.assertEqual(match, 0) |
Tarek Ziadé | fd39b7a | 2009-04-12 16:34:34 +0000 | [diff] [blame] | 48 | |
Éric Araujo | db95c7a | 2011-10-07 23:13:45 +0200 | [diff] [blame] | 49 | match = cmd.search_cpp(pattern='_configtest', body='/* xxx */') |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 50 | self.assertEqual(match, 1) |
Tarek Ziadé | fd39b7a | 2009-04-12 16:34:34 +0000 | [diff] [blame] | 51 | |
Tarek Ziadé | bccb0c9 | 2009-04-12 16:49:20 +0000 | [diff] [blame] | 52 | def test_finalize_options(self): |
| 53 | # finalize_options does a bit of transformation |
| 54 | # on options |
| 55 | pkg_dir, dist = self.create_dist() |
| 56 | cmd = config(dist) |
| 57 | cmd.include_dirs = 'one%stwo' % os.pathsep |
| 58 | cmd.libraries = 'one' |
| 59 | cmd.library_dirs = 'three%sfour' % os.pathsep |
| 60 | cmd.ensure_finalized() |
| 61 | |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 62 | self.assertEqual(cmd.include_dirs, ['one', 'two']) |
| 63 | self.assertEqual(cmd.libraries, ['one']) |
| 64 | self.assertEqual(cmd.library_dirs, ['three', 'four']) |
Tarek Ziadé | bccb0c9 | 2009-04-12 16:49:20 +0000 | [diff] [blame] | 65 | |
Tarek Ziadé | 865a714 | 2009-04-12 17:04:39 +0000 | [diff] [blame] | 66 | def test_clean(self): |
| 67 | # _clean removes files |
| 68 | tmp_dir = self.mkdtemp() |
| 69 | f1 = os.path.join(tmp_dir, 'one') |
| 70 | f2 = os.path.join(tmp_dir, 'two') |
| 71 | |
| 72 | self.write_file(f1, 'xxx') |
| 73 | self.write_file(f2, 'xxx') |
| 74 | |
| 75 | for f in (f1, f2): |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 76 | self.assertTrue(os.path.exists(f)) |
Tarek Ziadé | 865a714 | 2009-04-12 17:04:39 +0000 | [diff] [blame] | 77 | |
| 78 | pkg_dir, dist = self.create_dist() |
| 79 | cmd = config(dist) |
| 80 | cmd._clean(f1, f2) |
| 81 | |
| 82 | for f in (f1, f2): |
Serhiy Storchaka | 3998915 | 2013-11-17 00:17:46 +0200 | [diff] [blame] | 83 | self.assertFalse(os.path.exists(f)) |
Tarek Ziadé | bccb0c9 | 2009-04-12 16:49:20 +0000 | [diff] [blame] | 84 | |
Tarek Ziadé | 0eb1304 | 2009-04-12 14:57:46 +0000 | [diff] [blame] | 85 | def test_suite(): |
| 86 | return unittest.makeSuite(ConfigTestCase) |
| 87 | |
| 88 | if __name__ == "__main__": |
Éric Araujo | 70ec44a | 2010-11-06 02:44:43 +0000 | [diff] [blame] | 89 | run_unittest(test_suite()) |