1 """Tests for distutils.core.""" 2 3 import StringIO 4 import distutils.core 5 import os 6 import shutil 7 import sys 8 import test.test_support 9 from test.test_support import captured_stdout, run_unittest 10 import unittest 11 from distutils.tests import support 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 32 class CoreTestCase(support.EnvironGuard, unittest.TestCase): 33 34 def setUp(self): 35 super(CoreTestCase, self).setUp() 36 self.old_stdout = sys.stdout 37 self.cleanup_testfn() 38 self.old_argv = sys.argv, sys.argv[:] 39 40 def tearDown(self): 41 sys.stdout = self.old_stdout 42 self.cleanup_testfn() 43 sys.argv = self.old_argv[0] 44 sys.argv[:] = self.old_argv[1] 45 super(CoreTestCase, self).tearDown() 46 47 def cleanup_testfn(self): 48 path = test.test_support.TESTFN 49 if os.path.isfile(path): 50 os.remove(path) 51 elif os.path.isdir(path): 52 shutil.rmtree(path) 53 54 def write_setup(self, text, path=test.test_support.TESTFN): 55 f = open(path, "w") 56 try: 57 f.write(text) 58 finally: 59 f.close() 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 70 # as its own current directory; this was temporarily broken by a 71 # previous patch when TESTFN did not use the current directory. 72 sys.stdout = StringIO.StringIO() 73 cwd = os.getcwd() 74 75 # Create a directory and write the setup.py file there: 76 os.mkdir(test.test_support.TESTFN) 77 setup_py = os.path.join(test.test_support.TESTFN, "setup.py") 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 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) 92 self.assertEqual(stdout.read(), 'bar\n') 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" 102 self.assertEqual(stdout.readlines()[0], wanted) 103 104 def test_suite(): 105 return unittest.makeSuite(CoreTestCase) 106 107 if __name__ == "__main__": 108 run_unittest(test_suite()) 109