Neal Norwitz | f5c7c2e | 2008-04-05 04:47:45 +0000 | [diff] [blame] | 1 | """Tests for distutils.core.""" |
| 2 | |
| 3 | import io |
| 4 | import distutils.core |
| 5 | import os |
| 6 | import shutil |
| 7 | import sys |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 8 | import test.support |
Éric Araujo | 70ec44a | 2010-11-06 02:44:43 +0000 | [diff] [blame] | 9 | from test.support import captured_stdout, run_unittest |
Neal Norwitz | f5c7c2e | 2008-04-05 04:47:45 +0000 | [diff] [blame] | 10 | import unittest |
Tarek Ziadé | 430fb63 | 2009-10-18 11:34:51 +0000 | [diff] [blame] | 11 | from distutils.tests import support |
Neal Norwitz | f5c7c2e | 2008-04-05 04:47:45 +0000 | [diff] [blame] | 12 | |
| 13 | # setup script that uses __file__ |
| 14 | setup_using___file__ = """\ |
| 15 | |
| 16 | __file__ |
| 17 | |
| 18 | from distutils.core import setup |
| 19 | setup() |
| 20 | """ |
| 21 | |
| 22 | setup_prints_cwd = """\ |
| 23 | |
| 24 | import os |
| 25 | print(os.getcwd()) |
| 26 | |
| 27 | from distutils.core import setup |
| 28 | setup() |
| 29 | """ |
| 30 | |
| 31 | |
Tarek Ziadé | 430fb63 | 2009-10-18 11:34:51 +0000 | [diff] [blame] | 32 | class CoreTestCase(support.EnvironGuard, unittest.TestCase): |
Neal Norwitz | f5c7c2e | 2008-04-05 04:47:45 +0000 | [diff] [blame] | 33 | |
| 34 | def setUp(self): |
Tarek Ziadé | 430fb63 | 2009-10-18 11:34:51 +0000 | [diff] [blame] | 35 | super(CoreTestCase, self).setUp() |
Neal Norwitz | f5c7c2e | 2008-04-05 04:47:45 +0000 | [diff] [blame] | 36 | self.old_stdout = sys.stdout |
| 37 | self.cleanup_testfn() |
Tarek Ziadé | 430fb63 | 2009-10-18 11:34:51 +0000 | [diff] [blame] | 38 | self.old_argv = sys.argv, sys.argv[:] |
Neal Norwitz | f5c7c2e | 2008-04-05 04:47:45 +0000 | [diff] [blame] | 39 | |
| 40 | def tearDown(self): |
| 41 | sys.stdout = self.old_stdout |
| 42 | self.cleanup_testfn() |
Tarek Ziadé | 430fb63 | 2009-10-18 11:34:51 +0000 | [diff] [blame] | 43 | sys.argv = self.old_argv[0] |
| 44 | sys.argv[:] = self.old_argv[1] |
| 45 | super(CoreTestCase, self).tearDown() |
Neal Norwitz | f5c7c2e | 2008-04-05 04:47:45 +0000 | [diff] [blame] | 46 | |
| 47 | def cleanup_testfn(self): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 48 | path = test.support.TESTFN |
Neal Norwitz | f5c7c2e | 2008-04-05 04:47:45 +0000 | [diff] [blame] | 49 | if os.path.isfile(path): |
| 50 | os.remove(path) |
| 51 | elif os.path.isdir(path): |
| 52 | shutil.rmtree(path) |
| 53 | |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 54 | def write_setup(self, text, path=test.support.TESTFN): |
Éric Araujo | bee5cef | 2010-11-05 23:51:56 +0000 | [diff] [blame] | 55 | f = open(path, "w") |
| 56 | try: |
| 57 | f.write(text) |
| 58 | finally: |
| 59 | f.close() |
Neal Norwitz | f5c7c2e | 2008-04-05 04:47:45 +0000 | [diff] [blame] | 60 | return path |
| 61 | |
| 62 | def test_run_setup_provides_file(self): |
| 63 | # Make sure the script can use __file__; if that's missing, the test |
| 64 | # setup.py script will raise NameError. |
| 65 | distutils.core.run_setup( |
| 66 | self.write_setup(setup_using___file__)) |
| 67 | |
| 68 | def test_run_setup_uses_current_dir(self): |
| 69 | # This tests that the setup script is run with the current directory |
Mark Dickinson | 934896d | 2009-02-21 20:59:32 +0000 | [diff] [blame] | 70 | # as its own current directory; this was temporarily broken by a |
Neal Norwitz | f5c7c2e | 2008-04-05 04:47:45 +0000 | [diff] [blame] | 71 | # previous patch when TESTFN did not use the current directory. |
| 72 | sys.stdout = io.StringIO() |
| 73 | cwd = os.getcwd() |
| 74 | |
| 75 | # Create a directory and write the setup.py file there: |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 76 | os.mkdir(test.support.TESTFN) |
| 77 | setup_py = os.path.join(test.support.TESTFN, "setup.py") |
Neal Norwitz | f5c7c2e | 2008-04-05 04:47:45 +0000 | [diff] [blame] | 78 | distutils.core.run_setup( |
| 79 | self.write_setup(setup_prints_cwd, path=setup_py)) |
| 80 | |
| 81 | output = sys.stdout.getvalue() |
| 82 | if output.endswith("\n"): |
| 83 | output = output[:-1] |
| 84 | self.assertEqual(cwd, output) |
| 85 | |
Tarek Ziadé | 03d5d08 | 2009-09-21 13:01:54 +0000 | [diff] [blame] | 86 | def test_debug_mode(self): |
| 87 | # this covers the code called when DEBUG is set |
| 88 | sys.argv = ['setup.py', '--name'] |
| 89 | with captured_stdout() as stdout: |
| 90 | distutils.core.setup(name='bar') |
| 91 | stdout.seek(0) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 92 | self.assertEqual(stdout.read(), 'bar\n') |
Tarek Ziadé | 03d5d08 | 2009-09-21 13:01:54 +0000 | [diff] [blame] | 93 | |
| 94 | distutils.core.DEBUG = True |
| 95 | try: |
| 96 | with captured_stdout() as stdout: |
| 97 | distutils.core.setup(name='bar') |
| 98 | finally: |
| 99 | distutils.core.DEBUG = False |
| 100 | stdout.seek(0) |
| 101 | wanted = "options (after parsing config files):\n" |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 102 | self.assertEqual(stdout.readlines()[0], wanted) |
Neal Norwitz | f5c7c2e | 2008-04-05 04:47:45 +0000 | [diff] [blame] | 103 | |
| 104 | def test_suite(): |
| 105 | return unittest.makeSuite(CoreTestCase) |
| 106 | |
| 107 | if __name__ == "__main__": |
Éric Araujo | 70ec44a | 2010-11-06 02:44:43 +0000 | [diff] [blame] | 108 | run_unittest(test_suite()) |