| 1 | n/a | """Tests for distutils.core.""" |
|---|
| 2 | n/a | |
|---|
| 3 | n/a | import io |
|---|
| 4 | n/a | import distutils.core |
|---|
| 5 | n/a | import os |
|---|
| 6 | n/a | import shutil |
|---|
| 7 | n/a | import sys |
|---|
| 8 | n/a | import test.support |
|---|
| 9 | n/a | from test.support import captured_stdout, run_unittest |
|---|
| 10 | n/a | import unittest |
|---|
| 11 | n/a | from distutils.tests import support |
|---|
| 12 | n/a | from distutils import log |
|---|
| 13 | n/a | |
|---|
| 14 | n/a | # setup script that uses __file__ |
|---|
| 15 | n/a | setup_using___file__ = """\ |
|---|
| 16 | n/a | |
|---|
| 17 | n/a | __file__ |
|---|
| 18 | n/a | |
|---|
| 19 | n/a | from distutils.core import setup |
|---|
| 20 | n/a | setup() |
|---|
| 21 | n/a | """ |
|---|
| 22 | n/a | |
|---|
| 23 | n/a | setup_prints_cwd = """\ |
|---|
| 24 | n/a | |
|---|
| 25 | n/a | import os |
|---|
| 26 | n/a | print(os.getcwd()) |
|---|
| 27 | n/a | |
|---|
| 28 | n/a | from distutils.core import setup |
|---|
| 29 | n/a | setup() |
|---|
| 30 | n/a | """ |
|---|
| 31 | n/a | |
|---|
| 32 | n/a | setup_does_nothing = """\ |
|---|
| 33 | n/a | from distutils.core import setup |
|---|
| 34 | n/a | setup() |
|---|
| 35 | n/a | """ |
|---|
| 36 | n/a | |
|---|
| 37 | n/a | |
|---|
| 38 | n/a | setup_defines_subclass = """\ |
|---|
| 39 | n/a | from distutils.core import setup |
|---|
| 40 | n/a | from distutils.command.install import install as _install |
|---|
| 41 | n/a | |
|---|
| 42 | n/a | class install(_install): |
|---|
| 43 | n/a | sub_commands = _install.sub_commands + ['cmd'] |
|---|
| 44 | n/a | |
|---|
| 45 | n/a | setup(cmdclass={'install': install}) |
|---|
| 46 | n/a | """ |
|---|
| 47 | n/a | |
|---|
| 48 | n/a | class CoreTestCase(support.EnvironGuard, unittest.TestCase): |
|---|
| 49 | n/a | |
|---|
| 50 | n/a | def setUp(self): |
|---|
| 51 | n/a | super(CoreTestCase, self).setUp() |
|---|
| 52 | n/a | self.old_stdout = sys.stdout |
|---|
| 53 | n/a | self.cleanup_testfn() |
|---|
| 54 | n/a | self.old_argv = sys.argv, sys.argv[:] |
|---|
| 55 | n/a | self.addCleanup(log.set_threshold, log._global_log.threshold) |
|---|
| 56 | n/a | |
|---|
| 57 | n/a | def tearDown(self): |
|---|
| 58 | n/a | sys.stdout = self.old_stdout |
|---|
| 59 | n/a | self.cleanup_testfn() |
|---|
| 60 | n/a | sys.argv = self.old_argv[0] |
|---|
| 61 | n/a | sys.argv[:] = self.old_argv[1] |
|---|
| 62 | n/a | super(CoreTestCase, self).tearDown() |
|---|
| 63 | n/a | |
|---|
| 64 | n/a | def cleanup_testfn(self): |
|---|
| 65 | n/a | path = test.support.TESTFN |
|---|
| 66 | n/a | if os.path.isfile(path): |
|---|
| 67 | n/a | os.remove(path) |
|---|
| 68 | n/a | elif os.path.isdir(path): |
|---|
| 69 | n/a | shutil.rmtree(path) |
|---|
| 70 | n/a | |
|---|
| 71 | n/a | def write_setup(self, text, path=test.support.TESTFN): |
|---|
| 72 | n/a | f = open(path, "w") |
|---|
| 73 | n/a | try: |
|---|
| 74 | n/a | f.write(text) |
|---|
| 75 | n/a | finally: |
|---|
| 76 | n/a | f.close() |
|---|
| 77 | n/a | return path |
|---|
| 78 | n/a | |
|---|
| 79 | n/a | def test_run_setup_provides_file(self): |
|---|
| 80 | n/a | # Make sure the script can use __file__; if that's missing, the test |
|---|
| 81 | n/a | # setup.py script will raise NameError. |
|---|
| 82 | n/a | distutils.core.run_setup( |
|---|
| 83 | n/a | self.write_setup(setup_using___file__)) |
|---|
| 84 | n/a | |
|---|
| 85 | n/a | def test_run_setup_preserves_sys_argv(self): |
|---|
| 86 | n/a | # Make sure run_setup does not clobber sys.argv |
|---|
| 87 | n/a | argv_copy = sys.argv.copy() |
|---|
| 88 | n/a | distutils.core.run_setup( |
|---|
| 89 | n/a | self.write_setup(setup_does_nothing)) |
|---|
| 90 | n/a | self.assertEqual(sys.argv, argv_copy) |
|---|
| 91 | n/a | |
|---|
| 92 | n/a | def test_run_setup_defines_subclass(self): |
|---|
| 93 | n/a | # Make sure the script can use __file__; if that's missing, the test |
|---|
| 94 | n/a | # setup.py script will raise NameError. |
|---|
| 95 | n/a | dist = distutils.core.run_setup( |
|---|
| 96 | n/a | self.write_setup(setup_defines_subclass)) |
|---|
| 97 | n/a | install = dist.get_command_obj('install') |
|---|
| 98 | n/a | self.assertIn('cmd', install.sub_commands) |
|---|
| 99 | n/a | |
|---|
| 100 | n/a | def test_run_setup_uses_current_dir(self): |
|---|
| 101 | n/a | # This tests that the setup script is run with the current directory |
|---|
| 102 | n/a | # as its own current directory; this was temporarily broken by a |
|---|
| 103 | n/a | # previous patch when TESTFN did not use the current directory. |
|---|
| 104 | n/a | sys.stdout = io.StringIO() |
|---|
| 105 | n/a | cwd = os.getcwd() |
|---|
| 106 | n/a | |
|---|
| 107 | n/a | # Create a directory and write the setup.py file there: |
|---|
| 108 | n/a | os.mkdir(test.support.TESTFN) |
|---|
| 109 | n/a | setup_py = os.path.join(test.support.TESTFN, "setup.py") |
|---|
| 110 | n/a | distutils.core.run_setup( |
|---|
| 111 | n/a | self.write_setup(setup_prints_cwd, path=setup_py)) |
|---|
| 112 | n/a | |
|---|
| 113 | n/a | output = sys.stdout.getvalue() |
|---|
| 114 | n/a | if output.endswith("\n"): |
|---|
| 115 | n/a | output = output[:-1] |
|---|
| 116 | n/a | self.assertEqual(cwd, output) |
|---|
| 117 | n/a | |
|---|
| 118 | n/a | def test_debug_mode(self): |
|---|
| 119 | n/a | # this covers the code called when DEBUG is set |
|---|
| 120 | n/a | sys.argv = ['setup.py', '--name'] |
|---|
| 121 | n/a | with captured_stdout() as stdout: |
|---|
| 122 | n/a | distutils.core.setup(name='bar') |
|---|
| 123 | n/a | stdout.seek(0) |
|---|
| 124 | n/a | self.assertEqual(stdout.read(), 'bar\n') |
|---|
| 125 | n/a | |
|---|
| 126 | n/a | distutils.core.DEBUG = True |
|---|
| 127 | n/a | try: |
|---|
| 128 | n/a | with captured_stdout() as stdout: |
|---|
| 129 | n/a | distutils.core.setup(name='bar') |
|---|
| 130 | n/a | finally: |
|---|
| 131 | n/a | distutils.core.DEBUG = False |
|---|
| 132 | n/a | stdout.seek(0) |
|---|
| 133 | n/a | wanted = "options (after parsing config files):\n" |
|---|
| 134 | n/a | self.assertEqual(stdout.readlines()[0], wanted) |
|---|
| 135 | n/a | |
|---|
| 136 | n/a | def test_suite(): |
|---|
| 137 | n/a | return unittest.makeSuite(CoreTestCase) |
|---|
| 138 | n/a | |
|---|
| 139 | n/a | if __name__ == "__main__": |
|---|
| 140 | n/a | run_unittest(test_suite()) |
|---|