test_test.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. # -*- coding: UTF-8 -*-
  2. from __future__ import unicode_literals
  3. import os
  4. import site
  5. from distutils.errors import DistutilsError
  6. import pytest
  7. from setuptools.command.test import test
  8. from setuptools.dist import Distribution
  9. from .textwrap import DALS
  10. from . import contexts
  11. SETUP_PY = DALS("""
  12. from setuptools import setup
  13. setup(name='foo',
  14. packages=['name', 'name.space', 'name.space.tests'],
  15. namespace_packages=['name'],
  16. test_suite='name.space.tests.test_suite',
  17. )
  18. """)
  19. NS_INIT = DALS("""
  20. # -*- coding: Latin-1 -*-
  21. # Söme Arbiträry Ünicode to test Distribute Issüé 310
  22. try:
  23. __import__('pkg_resources').declare_namespace(__name__)
  24. except ImportError:
  25. from pkgutil import extend_path
  26. __path__ = extend_path(__path__, __name__)
  27. """)
  28. TEST_PY = DALS("""
  29. import unittest
  30. class TestTest(unittest.TestCase):
  31. def test_test(self):
  32. print "Foo" # Should fail under Python 3 unless 2to3 is used
  33. test_suite = unittest.makeSuite(TestTest)
  34. """)
  35. @pytest.fixture
  36. def sample_test(tmpdir_cwd):
  37. os.makedirs('name/space/tests')
  38. # setup.py
  39. with open('setup.py', 'wt') as f:
  40. f.write(SETUP_PY)
  41. # name/__init__.py
  42. with open('name/__init__.py', 'wb') as f:
  43. f.write(NS_INIT.encode('Latin-1'))
  44. # name/space/__init__.py
  45. with open('name/space/__init__.py', 'wt') as f:
  46. f.write('#empty\n')
  47. # name/space/tests/__init__.py
  48. with open('name/space/tests/__init__.py', 'wt') as f:
  49. f.write(TEST_PY)
  50. @pytest.mark.skipif('hasattr(sys, "real_prefix")')
  51. @pytest.mark.usefixtures('user_override')
  52. @pytest.mark.usefixtures('sample_test')
  53. class TestTestTest:
  54. def test_test(self):
  55. params = dict(
  56. name='foo',
  57. packages=['name', 'name.space', 'name.space.tests'],
  58. namespace_packages=['name'],
  59. test_suite='name.space.tests.test_suite',
  60. use_2to3=True,
  61. )
  62. dist = Distribution(params)
  63. dist.script_name = 'setup.py'
  64. cmd = test(dist)
  65. cmd.user = 1
  66. cmd.ensure_finalized()
  67. cmd.install_dir = site.USER_SITE
  68. cmd.user = 1
  69. with contexts.quiet():
  70. # The test runner calls sys.exit
  71. with contexts.suppress_exceptions(SystemExit):
  72. cmd.run()