test_egg_info.py 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. import os
  2. import glob
  3. import re
  4. import stat
  5. import sys
  6. from setuptools.command.egg_info import egg_info
  7. from setuptools.dist import Distribution
  8. from setuptools.extern.six.moves import map
  9. import pytest
  10. from . import environment
  11. from .files import build_files
  12. from .textwrap import DALS
  13. from . import contexts
  14. class Environment(str):
  15. pass
  16. class TestEggInfo(object):
  17. setup_script = DALS("""
  18. from setuptools import setup
  19. setup(
  20. name='foo',
  21. py_modules=['hello'],
  22. entry_points={'console_scripts': ['hi = hello.run']},
  23. zip_safe=False,
  24. )
  25. """)
  26. def _create_project(self):
  27. build_files({
  28. 'setup.py': self.setup_script,
  29. 'hello.py': DALS("""
  30. def run():
  31. print('hello')
  32. """)
  33. })
  34. @pytest.yield_fixture
  35. def env(self):
  36. with contexts.tempdir(prefix='setuptools-test.') as env_dir:
  37. env = Environment(env_dir)
  38. os.chmod(env_dir, stat.S_IRWXU)
  39. subs = 'home', 'lib', 'scripts', 'data', 'egg-base'
  40. env.paths = dict(
  41. (dirname, os.path.join(env_dir, dirname))
  42. for dirname in subs
  43. )
  44. list(map(os.mkdir, env.paths.values()))
  45. build_files({
  46. env.paths['home']: {
  47. '.pydistutils.cfg': DALS("""
  48. [egg_info]
  49. egg-base = %(egg-base)s
  50. """ % env.paths)
  51. }
  52. })
  53. yield env
  54. def test_egg_info_save_version_info_setup_empty(self, tmpdir_cwd, env):
  55. """
  56. When the egg_info section is empty or not present, running
  57. save_version_info should add the settings to the setup.cfg
  58. in a deterministic order, consistent with the ordering found
  59. on Python 2.6 and 2.7 with PYTHONHASHSEED=0.
  60. """
  61. setup_cfg = os.path.join(env.paths['home'], 'setup.cfg')
  62. dist = Distribution()
  63. ei = egg_info(dist)
  64. ei.initialize_options()
  65. ei.save_version_info(setup_cfg)
  66. with open(setup_cfg, 'r') as f:
  67. content = f.read()
  68. assert '[egg_info]' in content
  69. assert 'tag_build =' in content
  70. assert 'tag_date = 0' in content
  71. assert 'tag_svn_revision = 0' in content
  72. expected_order = 'tag_build', 'tag_date', 'tag_svn_revision'
  73. self._validate_content_order(content, expected_order)
  74. @staticmethod
  75. def _validate_content_order(content, expected):
  76. """
  77. Assert that the strings in expected appear in content
  78. in order.
  79. """
  80. if sys.version_info < (2, 7):
  81. # On Python 2.6, expect dict key order.
  82. expected = dict.fromkeys(expected).keys()
  83. pattern = '.*'.join(expected)
  84. flags = re.MULTILINE | re.DOTALL
  85. assert re.search(pattern, content, flags)
  86. def test_egg_info_save_version_info_setup_defaults(self, tmpdir_cwd, env):
  87. """
  88. When running save_version_info on an existing setup.cfg
  89. with the 'default' values present from a previous run,
  90. the file should remain unchanged, except on Python 2.6,
  91. where the order of the keys will be changed to match the
  92. order as found in a dictionary of those keys.
  93. """
  94. setup_cfg = os.path.join(env.paths['home'], 'setup.cfg')
  95. build_files({
  96. setup_cfg: DALS("""
  97. [egg_info]
  98. tag_build =
  99. tag_date = 0
  100. tag_svn_revision = 0
  101. """),
  102. })
  103. dist = Distribution()
  104. ei = egg_info(dist)
  105. ei.initialize_options()
  106. ei.save_version_info(setup_cfg)
  107. with open(setup_cfg, 'r') as f:
  108. content = f.read()
  109. assert '[egg_info]' in content
  110. assert 'tag_build =' in content
  111. assert 'tag_date = 0' in content
  112. assert 'tag_svn_revision = 0' in content
  113. expected_order = 'tag_build', 'tag_date', 'tag_svn_revision'
  114. self._validate_content_order(content, expected_order)
  115. def test_egg_base_installed_egg_info(self, tmpdir_cwd, env):
  116. self._create_project()
  117. self._run_install_command(tmpdir_cwd, env)
  118. actual = self._find_egg_info_files(env.paths['lib'])
  119. expected = [
  120. 'PKG-INFO',
  121. 'SOURCES.txt',
  122. 'dependency_links.txt',
  123. 'entry_points.txt',
  124. 'not-zip-safe',
  125. 'top_level.txt',
  126. ]
  127. assert sorted(actual) == expected
  128. def test_manifest_template_is_read(self, tmpdir_cwd, env):
  129. self._create_project()
  130. build_files({
  131. 'MANIFEST.in': DALS("""
  132. recursive-include docs *.rst
  133. """),
  134. 'docs': {
  135. 'usage.rst': "Run 'hi'",
  136. }
  137. })
  138. self._run_install_command(tmpdir_cwd, env)
  139. egg_info_dir = self._find_egg_info_files(env.paths['lib']).base
  140. sources_txt = os.path.join(egg_info_dir, 'SOURCES.txt')
  141. assert 'docs/usage.rst' in open(sources_txt).read().split('\n')
  142. def _setup_script_with_requires(self, requires_line):
  143. setup_script = DALS("""
  144. from setuptools import setup
  145. setup(
  146. name='foo',
  147. %s
  148. zip_safe=False,
  149. )
  150. """ % requires_line)
  151. build_files({
  152. 'setup.py': setup_script,
  153. })
  154. def test_install_requires_with_markers(self, tmpdir_cwd, env):
  155. self._setup_script_with_requires(
  156. """install_requires=["barbazquux;python_version<'2'"],""")
  157. self._run_install_command(tmpdir_cwd, env)
  158. egg_info_dir = self._find_egg_info_files(env.paths['lib']).base
  159. requires_txt = os.path.join(egg_info_dir, 'requires.txt')
  160. assert "barbazquux;python_version<'2'" in open(
  161. requires_txt).read().split('\n')
  162. assert glob.glob(os.path.join(env.paths['lib'], 'barbazquux*')) == []
  163. def test_setup_requires_with_markers(self, tmpdir_cwd, env):
  164. self._setup_script_with_requires(
  165. """setup_requires=["barbazquux;python_version<'2'"],""")
  166. self._run_install_command(tmpdir_cwd, env)
  167. assert glob.glob(os.path.join(env.paths['lib'], 'barbazquux*')) == []
  168. def test_tests_require_with_markers(self, tmpdir_cwd, env):
  169. self._setup_script_with_requires(
  170. """tests_require=["barbazquux;python_version<'2'"],""")
  171. self._run_install_command(
  172. tmpdir_cwd, env, cmd=['test'], output="Ran 0 tests in")
  173. assert glob.glob(os.path.join(env.paths['lib'], 'barbazquux*')) == []
  174. def test_extra_requires_with_markers(self, tmpdir_cwd, env):
  175. self._setup_script_with_requires(
  176. """extra_requires={":python_version<'2'": ["barbazquux"]},""")
  177. self._run_install_command(tmpdir_cwd, env)
  178. assert glob.glob(os.path.join(env.paths['lib'], 'barbazquux*')) == []
  179. def _run_install_command(self, tmpdir_cwd, env, cmd=None, output=None):
  180. environ = os.environ.copy().update(
  181. HOME=env.paths['home'],
  182. )
  183. if cmd is None:
  184. cmd = [
  185. 'install',
  186. '--home', env.paths['home'],
  187. '--install-lib', env.paths['lib'],
  188. '--install-scripts', env.paths['scripts'],
  189. '--install-data', env.paths['data'],
  190. ]
  191. code, data = environment.run_setup_py(
  192. cmd=cmd,
  193. pypath=os.pathsep.join([env.paths['lib'], str(tmpdir_cwd)]),
  194. data_stream=1,
  195. env=environ,
  196. )
  197. if code:
  198. raise AssertionError(data)
  199. if output:
  200. assert output in data
  201. def _find_egg_info_files(self, root):
  202. class DirList(list):
  203. def __init__(self, files, base):
  204. super(DirList, self).__init__(files)
  205. self.base = base
  206. results = (
  207. DirList(filenames, dirpath)
  208. for dirpath, dirnames, filenames in os.walk(root)
  209. if os.path.basename(dirpath) == 'EGG-INFO'
  210. )
  211. # expect exactly one result
  212. result, = results
  213. return result