1"""Tests for distutils.core.""" 2 3import StringIO 4import distutils.core 5import os 6import shutil 7import sys 8import test.test_support 9from test.test_support import captured_stdout, run_unittest 10import unittest 11from distutils.tests import support 12from distutils import log 13 14# setup script that uses __file__ 15setup_using___file__ = """\ 16 17__file__ 18 19from distutils.core import setup 20setup() 21""" 22 23setup_prints_cwd = """\ 24 25import os 26print os.getcwd() 27 28from distutils.core import setup 29setup() 30""" 31 32 33class CoreTestCase(support.EnvironGuard, unittest.TestCase): 34 35 def setUp(self): 36 super(CoreTestCase, self).setUp() 37 self.old_stdout = sys.stdout 38 self.cleanup_testfn() 39 self.old_argv = sys.argv, sys.argv[:] 40 self.addCleanup(log.set_threshold, log._global_log.threshold) 41 42 def tearDown(self): 43 sys.stdout = self.old_stdout 44 self.cleanup_testfn() 45 sys.argv = self.old_argv[0] 46 sys.argv[:] = self.old_argv[1] 47 super(CoreTestCase, self).tearDown() 48 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): 57 f = open(path, "w") 58 try: 59 f.write(text) 60 finally: 61 f.close() 62 return path 63 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. 67 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 72 # as its own current directory; this was temporarily broken by a 73 # 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() 84 if output.endswith("\n"): 85 output = output[:-1] 86 self.assertEqual(cwd, output) 87 88 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) 94 self.assertEqual(stdout.read(), 'bar\n') 95 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" 104 self.assertEqual(stdout.readlines()[0], wanted) 105 106def test_suite(): 107 return unittest.makeSuite(CoreTestCase) 108 109if __name__ == "__main__": 110 run_unittest(test_suite()) 111