Commit 558ed858 authored by Benoit Pierre's avatar Benoit Pierre Committed by GitHub

Merge pull request #1108 from benoit-pierre/fix_requires_handling,_again

fix requires handling when using setup.cfg
parents 28e2625c 096f3287
...@@ -349,14 +349,15 @@ class Distribution(Distribution_parse_config_files, _Distribution): ...@@ -349,14 +349,15 @@ class Distribution(Distribution_parse_config_files, _Distribution):
"setuptools, pip, and PyPI. Please see PEP 440 for more " "setuptools, pip, and PyPI. Please see PEP 440 for more "
"details." % self.metadata.version "details." % self.metadata.version
) )
if getattr(self, 'python_requires', None):
self.metadata.python_requires = self.python_requires
self._finalize_requires() self._finalize_requires()
def _finalize_requires(self): def _finalize_requires(self):
""" """
Fix environment markers in `install_requires` and `extras_require`. Set `metadata.python_requires` and fix environment markers
in `install_requires` and `extras_require`.
""" """
if getattr(self, 'python_requires', None):
self.metadata.python_requires = self.python_requires
self._convert_extras_requirements() self._convert_extras_requirements()
self._move_install_requirements_markers() self._move_install_requirements_markers()
...@@ -424,8 +425,7 @@ class Distribution(Distribution_parse_config_files, _Distribution): ...@@ -424,8 +425,7 @@ class Distribution(Distribution_parse_config_files, _Distribution):
_Distribution.parse_config_files(self, filenames=filenames) _Distribution.parse_config_files(self, filenames=filenames)
parse_configuration(self, self.command_options) parse_configuration(self, self.command_options)
if getattr(self, 'python_requires', None): self._finalize_requires()
self.metadata.python_requires = self.python_requires
def parse_command_line(self): def parse_command_line(self):
"""Process features after parsing command line options""" """Process features after parsing command line options"""
......
...@@ -333,7 +333,7 @@ class TestOptions: ...@@ -333,7 +333,7 @@ class TestOptions:
]) ])
assert dist.install_requires == ([ assert dist.install_requires == ([
'docutils>=0.3', 'docutils>=0.3',
'pack ==1.1, ==1.3', 'pack==1.1,==1.3',
'hey' 'hey'
]) ])
assert dist.setup_requires == ([ assert dist.setup_requires == ([
...@@ -403,7 +403,7 @@ class TestOptions: ...@@ -403,7 +403,7 @@ class TestOptions:
]) ])
assert dist.install_requires == ([ assert dist.install_requires == ([
'docutils>=0.3', 'docutils>=0.3',
'pack ==1.1, ==1.3', 'pack==1.1,==1.3',
'hey' 'hey'
]) ])
assert dist.setup_requires == ([ assert dist.setup_requires == ([
...@@ -508,7 +508,7 @@ class TestOptions: ...@@ -508,7 +508,7 @@ class TestOptions:
with get_dist(tmpdir) as dist: with get_dist(tmpdir) as dist:
assert dist.extras_require == { assert dist.extras_require == {
'pdf': ['ReportLab>=1.2', 'RXP'], 'pdf': ['ReportLab>=1.2', 'RXP'],
'rest': ['docutils>=0.3', 'pack ==1.1, ==1.3'] 'rest': ['docutils>=0.3', 'pack==1.1,==1.3']
} }
def test_entry_points(self, tmpdir): def test_entry_points(self, tmpdir):
......
import ast
import os import os
import glob import glob
import re import re
...@@ -165,19 +166,17 @@ class TestEggInfo(object): ...@@ -165,19 +166,17 @@ class TestEggInfo(object):
sources_txt = os.path.join(egg_info_dir, 'SOURCES.txt') sources_txt = os.path.join(egg_info_dir, 'SOURCES.txt')
assert 'docs/usage.rst' in open(sources_txt).read().split('\n') assert 'docs/usage.rst' in open(sources_txt).read().split('\n')
def _setup_script_with_requires(self, requires_line): def _setup_script_with_requires(self, requires, use_setup_cfg=False):
setup_script = DALS(""" setup_script = DALS(
'''
from setuptools import setup from setuptools import setup
setup( setup(name='foo', zip_safe=False, %s)
name='foo', '''
%s ) % ('' if use_setup_cfg else requires)
zip_safe=False, setup_config = requires if use_setup_cfg else ''
) build_files({'setup.py': setup_script,
""" % requires_line) 'setup.cfg': setup_config})
build_files({
'setup.py': setup_script,
})
mismatch_marker = "python_version<'{this_ver}'".format( mismatch_marker = "python_version<'{this_ver}'".format(
this_ver=sys.version_info[0], this_ver=sys.version_info[0],
...@@ -188,131 +187,198 @@ class TestEggInfo(object): ...@@ -188,131 +187,198 @@ class TestEggInfo(object):
) )
invalid_marker = "<=>++" invalid_marker = "<=>++"
def test_install_requires_with_marker(self, tmpdir_cwd, env): class RequiresTestHelper(object):
tmpl = 'install_requires=["barbazquux;{marker}"],'
req = tmpl.format(marker=self.mismatch_marker) @staticmethod
self._setup_script_with_requires(req) def parametrize(*test_list, **format_dict):
self._run_install_command(tmpdir_cwd, env) idlist = []
egg_info_dir = self._find_egg_info_files(env.paths['lib']).base argvalues = []
requires_txt = os.path.join(egg_info_dir, 'requires.txt') for test in test_list:
with open(requires_txt) as fp: test_params = test.lstrip().split('\n\n', 3)
install_requires = fp.read() name_kwargs = test_params.pop(0).split('\n')
expected_requires = DALS(''' if len(name_kwargs) > 1:
[:{marker}] install_cmd_kwargs = ast.literal_eval(name_kwargs[1].strip())
barbazquux else:
''').format(marker=self.mismatch_marker_alternate) install_cmd_kwargs = {}
assert install_requires.lstrip() == expected_requires name = name_kwargs[0].strip()
assert glob.glob(os.path.join(env.paths['lib'], 'barbazquux*')) == [] setup_py_requires, setup_cfg_requires, expected_requires = (
DALS(a).format(**format_dict) for a in test_params
)
for id_, requires, use_cfg in (
(name, setup_py_requires, False),
(name + '_in_setup_cfg', setup_cfg_requires, True),
):
idlist.append(id_)
marks = ()
if requires.startswith('@xfail\n'):
requires = requires[7:]
marks = pytest.mark.xfail
argvalues.append(pytest.param(requires, use_cfg,
expected_requires,
install_cmd_kwargs,
marks=marks))
return pytest.mark.parametrize('requires,use_setup_cfg,'
'expected_requires,install_cmd_kwargs',
argvalues, ids=idlist)
@RequiresTestHelper.parametrize(
# Format of a test:
#
# id
# install_cmd_kwargs [optional]
#
# requires block (when used in setup.py)
#
# requires block (when used in setup.cfg)
#
# expected contents of requires.txt
def test_install_requires_with_extra(self, tmpdir_cwd, env): '''
req = 'install_requires=["barbazquux [test]"],' install_requires_with_marker
self._setup_script_with_requires(req)
self._run_install_command(tmpdir_cwd, env, cmd=['egg_info']) install_requires=["barbazquux;{mismatch_marker}"],
egg_info_dir = os.path.join('.', 'foo.egg-info')
requires_txt = os.path.join(egg_info_dir, 'requires.txt') [options]
with open(requires_txt) as fp: install_requires =
install_requires = fp.read() barbazquux; {mismatch_marker}
expected_requires = DALS('''
barbazquux[test]
''')
assert install_requires.lstrip() == expected_requires
assert glob.glob(os.path.join(env.paths['lib'], 'barbazquux*')) == []
def test_install_requires_with_extra_and_marker(self, tmpdir_cwd, env): [:{mismatch_marker_alternate}]
tmpl = 'install_requires=["barbazquux [test]; {marker}"],' barbazquux
req = tmpl.format(marker=self.mismatch_marker) ''',
self._setup_script_with_requires(req)
self._run_install_command(tmpdir_cwd, env)
egg_info_dir = self._find_egg_info_files(env.paths['lib']).base
requires_txt = os.path.join(egg_info_dir, 'requires.txt')
with open(requires_txt) as fp:
install_requires = fp.read()
expected_requires = DALS('''
[:{marker}]
barbazquux[test]
''').format(marker=self.mismatch_marker_alternate)
assert install_requires.lstrip() == expected_requires
assert glob.glob(os.path.join(env.paths['lib'], 'barbazquux*')) == []
def test_setup_requires_with_markers(self, tmpdir_cwd, env): '''
tmpl = 'setup_requires=["barbazquux;{marker}"],' install_requires_with_extra
req = tmpl.format(marker=self.mismatch_marker) {'cmd': ['egg_info']}
self._setup_script_with_requires(req)
self._run_install_command(tmpdir_cwd, env)
assert glob.glob(os.path.join(env.paths['lib'], 'barbazquux*')) == []
def test_tests_require_with_markers(self, tmpdir_cwd, env): install_requires=["barbazquux [test]"],
tmpl = 'tests_require=["barbazquux;{marker}"],'
req = tmpl.format(marker=self.mismatch_marker)
self._setup_script_with_requires(req)
self._run_install_command(
tmpdir_cwd, env, cmd=['test'], output="Ran 0 tests in")
assert glob.glob(os.path.join(env.paths['lib'], 'barbazquux*')) == []
def test_extras_require_with_extra(self, tmpdir_cwd, env): [options]
req = 'extras_require={"extra": ["barbazquux [test]"]},' install_requires =
self._setup_script_with_requires(req) barbazquux [test]
self._run_install_command(tmpdir_cwd, env, cmd=['egg_info'])
egg_info_dir = os.path.join('.', 'foo.egg-info')
requires_txt = os.path.join(egg_info_dir, 'requires.txt')
with open(requires_txt) as fp:
install_requires = fp.read()
expected_requires = DALS('''
[extra]
barbazquux[test]
''')
assert install_requires.lstrip() == expected_requires
assert glob.glob(os.path.join(env.paths['lib'], 'barbazquux*')) == []
def test_extras_require_with_extra_and_marker_in_req(self, tmpdir_cwd, env): barbazquux[test]
tmpl = 'extras_require={{"extra": ["barbazquux [test]; {marker}"]}},' ''',
req = tmpl.format(marker=self.mismatch_marker)
self._setup_script_with_requires(req)
self._run_install_command(tmpdir_cwd, env)
egg_info_dir = self._find_egg_info_files(env.paths['lib']).base
requires_txt = os.path.join(egg_info_dir, 'requires.txt')
with open(requires_txt) as fp:
install_requires = fp.read()
expected_requires = DALS('''
[extra]
[extra:{marker}]
barbazquux[test]
''').format(marker=self.mismatch_marker_alternate)
assert install_requires.lstrip() == expected_requires
assert glob.glob(os.path.join(env.paths['lib'], 'barbazquux*')) == []
def test_extras_require_with_marker(self, tmpdir_cwd, env): '''
tmpl = 'extras_require={{":{marker}": ["barbazquux"]}},' install_requires_with_extra_and_marker
req = tmpl.format(marker=self.mismatch_marker)
self._setup_script_with_requires(req)
self._run_install_command(tmpdir_cwd, env)
egg_info_dir = self._find_egg_info_files(env.paths['lib']).base
requires_txt = os.path.join(egg_info_dir, 'requires.txt')
with open(requires_txt) as fp:
install_requires = fp.read()
expected_requires = DALS('''
[:{marker}]
barbazquux
''').format(marker=self.mismatch_marker)
assert install_requires.lstrip() == expected_requires
assert glob.glob(os.path.join(env.paths['lib'], 'barbazquux*')) == []
def test_extras_require_with_marker_in_req(self, tmpdir_cwd, env): install_requires=["barbazquux [test]; {mismatch_marker}"],
tmpl = 'extras_require={{"extra": ["barbazquux; {marker}"]}},'
req = tmpl.format(marker=self.mismatch_marker) [options]
self._setup_script_with_requires(req) install_requires =
self._run_install_command(tmpdir_cwd, env) barbazquux [test]; {mismatch_marker}
egg_info_dir = self._find_egg_info_files(env.paths['lib']).base
[:{mismatch_marker_alternate}]
barbazquux[test]
''',
'''
setup_requires_with_markers
setup_requires=["barbazquux;{mismatch_marker}"],
[options]
setup_requires =
barbazquux; {mismatch_marker}
''',
'''
tests_require_with_markers
{'cmd': ['test'], 'output': "Ran 0 tests in"}
tests_require=["barbazquux;{mismatch_marker}"],
[options]
tests_require =
barbazquux; {mismatch_marker}
''',
'''
extras_require_with_extra
{'cmd': ['egg_info']}
extras_require={{"extra": ["barbazquux [test]"]}},
[options.extras_require]
extra = barbazquux [test]
[extra]
barbazquux[test]
''',
'''
extras_require_with_extra_and_marker_in_req
extras_require={{"extra": ["barbazquux [test]; {mismatch_marker}"]}},
[options.extras_require]
extra =
barbazquux [test]; {mismatch_marker}
[extra]
[extra:{mismatch_marker_alternate}]
barbazquux[test]
''',
# FIXME: ConfigParser does not allow : in key names!
'''
extras_require_with_marker
extras_require={{":{mismatch_marker}": ["barbazquux"]}},
@xfail
[options.extras_require]
:{mismatch_marker} = barbazquux
[:{mismatch_marker}]
barbazquux
''',
'''
extras_require_with_marker_in_req
extras_require={{"extra": ["barbazquux; {mismatch_marker}"]}},
[options.extras_require]
extra =
barbazquux; {mismatch_marker}
[extra]
[extra:{mismatch_marker_alternate}]
barbazquux
''',
'''
extras_require_with_empty_section
extras_require={{"empty": []}},
[options.extras_require]
empty =
[empty]
''',
# Format arguments.
invalid_marker=invalid_marker,
mismatch_marker=mismatch_marker,
mismatch_marker_alternate=mismatch_marker_alternate,
)
def test_requires(self, tmpdir_cwd, env,
requires, use_setup_cfg,
expected_requires, install_cmd_kwargs):
self._setup_script_with_requires(requires, use_setup_cfg)
self._run_install_command(tmpdir_cwd, env, **install_cmd_kwargs)
egg_info_dir = os.path.join('.', 'foo.egg-info')
requires_txt = os.path.join(egg_info_dir, 'requires.txt') requires_txt = os.path.join(egg_info_dir, 'requires.txt')
with open(requires_txt) as fp: if os.path.exists(requires_txt):
install_requires = fp.read() with open(requires_txt) as fp:
expected_requires = DALS(''' install_requires = fp.read()
[extra] else:
install_requires = ''
[extra:{marker}]
barbazquux
''').format(marker=self.mismatch_marker_alternate)
assert install_requires.lstrip() == expected_requires assert install_requires.lstrip() == expected_requires
assert glob.glob(os.path.join(env.paths['lib'], 'barbazquux*')) == [] assert glob.glob(os.path.join(env.paths['lib'], 'barbazquux*')) == []
...@@ -332,20 +398,6 @@ class TestEggInfo(object): ...@@ -332,20 +398,6 @@ class TestEggInfo(object):
self._run_install_command(tmpdir_cwd, env) self._run_install_command(tmpdir_cwd, env)
assert glob.glob(os.path.join(env.paths['lib'], 'barbazquux*')) == [] assert glob.glob(os.path.join(env.paths['lib'], 'barbazquux*')) == []
def test_extras_require_with_empty_section(self, tmpdir_cwd, env):
tmpl = 'extras_require={{"empty": []}},'
req = tmpl.format(marker=self.invalid_marker)
self._setup_script_with_requires(req)
self._run_install_command(tmpdir_cwd, env)
egg_info_dir = self._find_egg_info_files(env.paths['lib']).base
requires_txt = os.path.join(egg_info_dir, 'requires.txt')
with open(requires_txt) as fp:
install_requires = fp.read()
expected_requires = DALS('''
[empty]
''').format(marker=self.mismatch_marker_alternate)
assert install_requires.lstrip() == expected_requires
def test_python_requires_egg_info(self, tmpdir_cwd, env): def test_python_requires_egg_info(self, tmpdir_cwd, env):
self._setup_script_with_requires( self._setup_script_with_requires(
"""python_requires='>=2.7.12',""") """python_requires='>=2.7.12',""")
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment