test_develop.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. """develop tests
  2. """
  3. import os
  4. import shutil
  5. import site
  6. import sys
  7. import tempfile
  8. import unittest
  9. from distutils.errors import DistutilsError
  10. from setuptools.command.develop import develop
  11. from setuptools.command import easy_install as easy_install_pkg
  12. from setuptools.compat import StringIO
  13. from setuptools.dist import Distribution
  14. SETUP_PY = """\
  15. from setuptools import setup
  16. setup(name='foo',
  17. packages=['foo'],
  18. use_2to3=True,
  19. )
  20. """
  21. INIT_PY = """print "foo"
  22. """
  23. class TestDevelopTest(unittest.TestCase):
  24. def setUp(self):
  25. if sys.version < "2.6" or hasattr(sys, 'real_prefix'):
  26. return
  27. # Directory structure
  28. self.dir = tempfile.mkdtemp()
  29. os.mkdir(os.path.join(self.dir, 'foo'))
  30. # setup.py
  31. setup = os.path.join(self.dir, 'setup.py')
  32. f = open(setup, 'w')
  33. f.write(SETUP_PY)
  34. f.close()
  35. self.old_cwd = os.getcwd()
  36. # foo/__init__.py
  37. init = os.path.join(self.dir, 'foo', '__init__.py')
  38. f = open(init, 'w')
  39. f.write(INIT_PY)
  40. f.close()
  41. os.chdir(self.dir)
  42. self.old_base = site.USER_BASE
  43. site.USER_BASE = tempfile.mkdtemp()
  44. self.old_site = site.USER_SITE
  45. site.USER_SITE = tempfile.mkdtemp()
  46. def tearDown(self):
  47. if sys.version < "2.6" or hasattr(sys, 'real_prefix') or (hasattr(sys, 'base_prefix') and sys.base_prefix != sys.prefix):
  48. return
  49. os.chdir(self.old_cwd)
  50. shutil.rmtree(self.dir)
  51. shutil.rmtree(site.USER_BASE)
  52. shutil.rmtree(site.USER_SITE)
  53. site.USER_BASE = self.old_base
  54. site.USER_SITE = self.old_site
  55. def test_develop(self):
  56. if sys.version < "2.6" or hasattr(sys, 'real_prefix'):
  57. return
  58. dist = Distribution(
  59. dict(name='foo',
  60. packages=['foo'],
  61. use_2to3=True,
  62. version='0.0',
  63. ))
  64. dist.script_name = 'setup.py'
  65. cmd = develop(dist)
  66. cmd.user = 1
  67. cmd.ensure_finalized()
  68. cmd.install_dir = site.USER_SITE
  69. cmd.user = 1
  70. old_stdout = sys.stdout
  71. #sys.stdout = StringIO()
  72. try:
  73. cmd.run()
  74. finally:
  75. sys.stdout = old_stdout
  76. # let's see if we got our egg link at the right place
  77. content = os.listdir(site.USER_SITE)
  78. content.sort()
  79. self.assertEqual(content, ['easy-install.pth', 'foo.egg-link'])
  80. # Check that we are using the right code.
  81. egg_link_file = open(os.path.join(site.USER_SITE, 'foo.egg-link'), 'rt')
  82. try:
  83. path = egg_link_file.read().split()[0].strip()
  84. finally:
  85. egg_link_file.close()
  86. init_file = open(os.path.join(path, 'foo', '__init__.py'), 'rt')
  87. try:
  88. init = init_file.read().strip()
  89. finally:
  90. init_file.close()
  91. if sys.version < "3":
  92. self.assertEqual(init, 'print "foo"')
  93. else:
  94. self.assertEqual(init, 'print("foo")')
  95. def notest_develop_with_setup_requires(self):
  96. wanted = ("Could not find suitable distribution for "
  97. "Requirement.parse('I-DONT-EXIST')")
  98. old_dir = os.getcwd()
  99. os.chdir(self.dir)
  100. try:
  101. try:
  102. dist = Distribution({'setup_requires': ['I_DONT_EXIST']})
  103. except DistutilsError:
  104. e = sys.exc_info()[1]
  105. error = str(e)
  106. if error == wanted:
  107. pass
  108. finally:
  109. os.chdir(old_dir)