test_develop.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. """develop tests
  2. """
  3. import os
  4. import shutil
  5. import site
  6. import sys
  7. import tempfile
  8. from setuptools.command.develop import develop
  9. from setuptools.dist import Distribution
  10. SETUP_PY = """\
  11. from setuptools import setup
  12. setup(name='foo',
  13. packages=['foo'],
  14. use_2to3=True,
  15. )
  16. """
  17. INIT_PY = """print "foo"
  18. """
  19. class TestDevelopTest:
  20. def setup_method(self, method):
  21. if hasattr(sys, 'real_prefix'):
  22. return
  23. # Directory structure
  24. self.dir = tempfile.mkdtemp()
  25. os.mkdir(os.path.join(self.dir, 'foo'))
  26. # setup.py
  27. setup = os.path.join(self.dir, 'setup.py')
  28. f = open(setup, 'w')
  29. f.write(SETUP_PY)
  30. f.close()
  31. self.old_cwd = os.getcwd()
  32. # foo/__init__.py
  33. init = os.path.join(self.dir, 'foo', '__init__.py')
  34. f = open(init, 'w')
  35. f.write(INIT_PY)
  36. f.close()
  37. os.chdir(self.dir)
  38. self.old_base = site.USER_BASE
  39. site.USER_BASE = tempfile.mkdtemp()
  40. self.old_site = site.USER_SITE
  41. site.USER_SITE = tempfile.mkdtemp()
  42. def teardown_method(self, method):
  43. if hasattr(sys, 'real_prefix') or (hasattr(sys, 'base_prefix') and sys.base_prefix != sys.prefix):
  44. return
  45. os.chdir(self.old_cwd)
  46. shutil.rmtree(self.dir)
  47. shutil.rmtree(site.USER_BASE)
  48. shutil.rmtree(site.USER_SITE)
  49. site.USER_BASE = self.old_base
  50. site.USER_SITE = self.old_site
  51. def test_develop(self):
  52. if hasattr(sys, 'real_prefix'):
  53. return
  54. dist = Distribution(
  55. dict(name='foo',
  56. packages=['foo'],
  57. use_2to3=True,
  58. version='0.0',
  59. ))
  60. dist.script_name = 'setup.py'
  61. cmd = develop(dist)
  62. cmd.user = 1
  63. cmd.ensure_finalized()
  64. cmd.install_dir = site.USER_SITE
  65. cmd.user = 1
  66. old_stdout = sys.stdout
  67. #sys.stdout = StringIO()
  68. try:
  69. cmd.run()
  70. finally:
  71. sys.stdout = old_stdout
  72. # let's see if we got our egg link at the right place
  73. content = os.listdir(site.USER_SITE)
  74. content.sort()
  75. assert content == ['easy-install.pth', 'foo.egg-link']
  76. # Check that we are using the right code.
  77. egg_link_file = open(os.path.join(site.USER_SITE, 'foo.egg-link'), 'rt')
  78. try:
  79. path = egg_link_file.read().split()[0].strip()
  80. finally:
  81. egg_link_file.close()
  82. init_file = open(os.path.join(path, 'foo', '__init__.py'), 'rt')
  83. try:
  84. init = init_file.read().strip()
  85. finally:
  86. init_file.close()
  87. if sys.version < "3":
  88. assert init == 'print "foo"'
  89. else:
  90. assert init == 'print("foo")'