test_test.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. # -*- coding: UTF-8 -*-
  2. """develop tests
  3. """
  4. import os
  5. import shutil
  6. import site
  7. import sys
  8. import tempfile
  9. import unittest
  10. from distutils.errors import DistutilsError
  11. from setuptools.compat import StringIO
  12. from setuptools.command.test import test
  13. from setuptools.command import easy_install as easy_install_pkg
  14. from setuptools.dist import Distribution
  15. SETUP_PY = """\
  16. from setuptools import setup
  17. setup(name='foo',
  18. packages=['name', 'name.space', 'name.space.tests'],
  19. namespace_packages=['name'],
  20. test_suite='name.space.tests.test_suite',
  21. )
  22. """
  23. NS_INIT = """# -*- coding: Latin-1 -*-
  24. # Söme Arbiträry Ünicode to test Issüé 310
  25. try:
  26. __import__('pkg_resources').declare_namespace(__name__)
  27. except ImportError:
  28. from pkgutil import extend_path
  29. __path__ = extend_path(__path__, __name__)
  30. """
  31. # Make sure this is Latin-1 binary, before writing:
  32. if sys.version_info < (3,):
  33. NS_INIT = NS_INIT.decode('UTF-8')
  34. NS_INIT = NS_INIT.encode('Latin-1')
  35. TEST_PY = """import unittest
  36. class TestTest(unittest.TestCase):
  37. def test_test(self):
  38. print "Foo" # Should fail under Python 3 unless 2to3 is used
  39. test_suite = unittest.makeSuite(TestTest)
  40. """
  41. class TestTestTest(unittest.TestCase):
  42. def setUp(self):
  43. if sys.version < "2.6" or hasattr(sys, 'real_prefix'):
  44. return
  45. # Directory structure
  46. self.dir = tempfile.mkdtemp()
  47. os.mkdir(os.path.join(self.dir, 'name'))
  48. os.mkdir(os.path.join(self.dir, 'name', 'space'))
  49. os.mkdir(os.path.join(self.dir, 'name', 'space', 'tests'))
  50. # setup.py
  51. setup = os.path.join(self.dir, 'setup.py')
  52. f = open(setup, 'wt')
  53. f.write(SETUP_PY)
  54. f.close()
  55. self.old_cwd = os.getcwd()
  56. # name/__init__.py
  57. init = os.path.join(self.dir, 'name', '__init__.py')
  58. f = open(init, 'wb')
  59. f.write(NS_INIT)
  60. f.close()
  61. # name/space/__init__.py
  62. init = os.path.join(self.dir, 'name', 'space', '__init__.py')
  63. f = open(init, 'wt')
  64. f.write('#empty\n')
  65. f.close()
  66. # name/space/tests/__init__.py
  67. init = os.path.join(self.dir, 'name', 'space', 'tests', '__init__.py')
  68. f = open(init, 'wt')
  69. f.write(TEST_PY)
  70. f.close()
  71. os.chdir(self.dir)
  72. self.old_base = site.USER_BASE
  73. site.USER_BASE = tempfile.mkdtemp()
  74. self.old_site = site.USER_SITE
  75. site.USER_SITE = tempfile.mkdtemp()
  76. def tearDown(self):
  77. if sys.version < "2.6" or hasattr(sys, 'real_prefix'):
  78. return
  79. os.chdir(self.old_cwd)
  80. shutil.rmtree(self.dir)
  81. shutil.rmtree(site.USER_BASE)
  82. shutil.rmtree(site.USER_SITE)
  83. site.USER_BASE = self.old_base
  84. site.USER_SITE = self.old_site
  85. def test_test(self):
  86. if sys.version < "2.6" or hasattr(sys, 'real_prefix'):
  87. return
  88. dist = Distribution(dict(
  89. name='foo',
  90. packages=['name', 'name.space', 'name.space.tests'],
  91. namespace_packages=['name'],
  92. test_suite='name.space.tests.test_suite',
  93. use_2to3=True,
  94. ))
  95. dist.script_name = 'setup.py'
  96. cmd = test(dist)
  97. cmd.user = 1
  98. cmd.ensure_finalized()
  99. cmd.install_dir = site.USER_SITE
  100. cmd.user = 1
  101. old_stdout = sys.stdout
  102. sys.stdout = StringIO()
  103. try:
  104. try: # try/except/finally doesn't work in Python 2.4, so we need nested try-statements.
  105. cmd.run()
  106. except SystemExit: # The test runner calls sys.exit, stop that making an error.
  107. pass
  108. finally:
  109. sys.stdout = old_stdout