test_core.py 4.0 KB

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