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