test_test.py 2.4 KB

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