blob: e6d4c9b3608ae4d2b2e477e2e0300af1cf5cd3d9 [file] [log] [blame]
Fred Drake46c58c12008-04-04 05:41:30 +00001"""Tests for distutils.core."""
2
3import StringIO
4import distutils.core
5import os
Fred Drakefe705622008-04-04 11:31:14 +00006import shutil
7import sys
Fred Drake46c58c12008-04-04 05:41:30 +00008import test.test_support
Éric Araujo54274ad2011-02-03 00:12:18 +00009from test.test_support import captured_stdout, run_unittest
Fred Drake46c58c12008-04-04 05:41:30 +000010import unittest
Tarek Ziadé2b06d422009-10-18 09:28:26 +000011from distutils.tests import support
Serhiy Storchaka0d649402015-11-12 19:46:23 +020012from distutils import log
Fred Drake46c58c12008-04-04 05:41:30 +000013
14# setup script that uses __file__
15setup_using___file__ = """\
16
17__file__
18
19from distutils.core import setup
20setup()
21"""
22
Fred Drakefe705622008-04-04 11:31:14 +000023setup_prints_cwd = """\
24
25import os
26print os.getcwd()
27
28from distutils.core import setup
29setup()
30"""
31
Fred Drake46c58c12008-04-04 05:41:30 +000032
Tarek Ziadé2b06d422009-10-18 09:28:26 +000033class CoreTestCase(support.EnvironGuard, unittest.TestCase):
Fred Drake46c58c12008-04-04 05:41:30 +000034
Fred Drakefe705622008-04-04 11:31:14 +000035 def setUp(self):
Tarek Ziadé2b06d422009-10-18 09:28:26 +000036 super(CoreTestCase, self).setUp()
Fred Drakefe705622008-04-04 11:31:14 +000037 self.old_stdout = sys.stdout
38 self.cleanup_testfn()
Tarek Ziadé2b06d422009-10-18 09:28:26 +000039 self.old_argv = sys.argv, sys.argv[:]
Serhiy Storchaka0d649402015-11-12 19:46:23 +020040 self.addCleanup(log.set_threshold, log._global_log.threshold)
Fred Drake46c58c12008-04-04 05:41:30 +000041
Fred Drakefe705622008-04-04 11:31:14 +000042 def tearDown(self):
43 sys.stdout = self.old_stdout
44 self.cleanup_testfn()
Tarek Ziadé2b06d422009-10-18 09:28:26 +000045 sys.argv = self.old_argv[0]
46 sys.argv[:] = self.old_argv[1]
47 super(CoreTestCase, self).tearDown()
Fred Drakefe705622008-04-04 11:31:14 +000048
49 def cleanup_testfn(self):
50 path = test.test_support.TESTFN
51 if os.path.isfile(path):
52 os.remove(path)
53 elif os.path.isdir(path):
54 shutil.rmtree(path)
55
56 def write_setup(self, text, path=test.test_support.TESTFN):
Éric Araujod1feff72010-11-06 04:06:18 +000057 f = open(path, "w")
58 try:
59 f.write(text)
60 finally:
61 f.close()
Fred Drakefe705622008-04-04 11:31:14 +000062 return path
Fred Drake46c58c12008-04-04 05:41:30 +000063
64 def test_run_setup_provides_file(self):
65 # Make sure the script can use __file__; if that's missing, the test
66 # setup.py script will raise NameError.
Fred Drakefe705622008-04-04 11:31:14 +000067 distutils.core.run_setup(
68 self.write_setup(setup_using___file__))
69
70 def test_run_setup_uses_current_dir(self):
71 # This tests that the setup script is run with the current directory
Mark Dickinson3e4caeb2009-02-21 20:27:01 +000072 # as its own current directory; this was temporarily broken by a
Fred Drakefe705622008-04-04 11:31:14 +000073 # previous patch when TESTFN did not use the current directory.
74 sys.stdout = StringIO.StringIO()
75 cwd = os.getcwd()
76
77 # Create a directory and write the setup.py file there:
78 os.mkdir(test.test_support.TESTFN)
79 setup_py = os.path.join(test.test_support.TESTFN, "setup.py")
80 distutils.core.run_setup(
81 self.write_setup(setup_prints_cwd, path=setup_py))
82
83 output = sys.stdout.getvalue()
Fred Drakefe705622008-04-04 11:31:14 +000084 if output.endswith("\n"):
85 output = output[:-1]
86 self.assertEqual(cwd, output)
Fred Drake46c58c12008-04-04 05:41:30 +000087
Tarek Ziadé6d2db372009-09-21 12:19:07 +000088 def test_debug_mode(self):
89 # this covers the code called when DEBUG is set
90 sys.argv = ['setup.py', '--name']
91 with captured_stdout() as stdout:
92 distutils.core.setup(name='bar')
93 stdout.seek(0)
Ezio Melotti2623a372010-11-21 13:34:58 +000094 self.assertEqual(stdout.read(), 'bar\n')
Tarek Ziadé6d2db372009-09-21 12:19:07 +000095
96 distutils.core.DEBUG = True
97 try:
98 with captured_stdout() as stdout:
99 distutils.core.setup(name='bar')
100 finally:
101 distutils.core.DEBUG = False
102 stdout.seek(0)
103 wanted = "options (after parsing config files):\n"
Ezio Melotti2623a372010-11-21 13:34:58 +0000104 self.assertEqual(stdout.readlines()[0], wanted)
Fred Drake46c58c12008-04-04 05:41:30 +0000105
106def test_suite():
107 return unittest.makeSuite(CoreTestCase)
108
109if __name__ == "__main__":
Éric Araujo54274ad2011-02-03 00:12:18 +0000110 run_unittest(test_suite())