From f487308f7b88ec66eb9a570a15e8930616739b2f Mon Sep 17 00:00:00 2001 From: Albert Tugushev Date: Tue, 20 Aug 2019 03:39:58 +0300 Subject: [PATCH] Parse extras_require when compile setup.py --- piptools/scripts/compile.py | 12 +++++++++ tests/test_cli_compile.py | 49 +++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+) diff --git a/piptools/scripts/compile.py b/piptools/scripts/compile.py index 24d50b662..3fb50409e 100755 --- a/piptools/scripts/compile.py +++ b/piptools/scripts/compile.py @@ -303,6 +303,18 @@ def cli( dist = run_setup(src_file) tmpfile.write("\n".join(dist.install_requires)) + + # Parse extras_require of the form + # "extra:{marker}": ["package"] to + # "package ; {marker}" if extra is empty. + for extra_and_marker, packages in dist.extras_require.items(): + extra, marker = extra_and_marker.split(":") + # TODO: non-empty extras will be ignored, perhaps we need + # an option like --with-extra to specify one. See GH-625. + if extra: + continue + for package in packages: + tmpfile.write("{} ; {}\n".format(package, marker)) else: tmpfile.write(sys.stdin.read()) tmpfile.flush() diff --git a/tests/test_cli_compile.py b/tests/test_cli_compile.py index 0dce21b73..6e64f2e88 100644 --- a/tests/test_cli_compile.py +++ b/tests/test_cli_compile.py @@ -122,6 +122,55 @@ def test_command_line_setuptools_output_file(pip_conf, options, expected_output_ assert os.path.exists(expected_output_file) +@fail_below_pip9 +@pytest.mark.parametrize( + "str_attrs", + [ + # Test with markers in install_requires + """install_requires=['small-fake-a==0.1; python_version >= "2.7"']""", + # Test with markers in extras_require + """extras_require={':python_version >= "2.7"': "small-fake-a==0.1"}""", + ], +) +def test_setuptools_with_env_markers(pip_conf, runner, str_attrs): + """ + Test pip-compile handles environment markers (PEP496) + in setup.py in install_requires/extras_require. + """ + with open("setup.py", "w") as setup: + setup.write("from setuptools import setup\nsetup({})".format(str_attrs)) + + out = runner.invoke(cli) + + expected_line = 'small-fake-a==0.1 ; python_version >= "2.7"' + assert out.exit_code == 0 + assert expected_line in out.stderr.splitlines(), out.stderr + + +@fail_below_pip9 +@pytest.mark.parametrize( + "str_attrs", + [ + # Test invalid marker + """install_requires=['small-fake-a==0.1; python_version < "2.7"']""", + # Test non-empty extra + """extras_require={'dev:python_version >= "2.7"': "small-fake-a==0.1"}""", + ], +) +def test_setuptools_skips_packages(pip_conf, runner, str_attrs): + """ + Test pip-compile skips packages in install_requires/extras_require + due to environment markers (PEP496) or non-empty extras. + """ + with open("setup.py", "w") as setup: + setup.write("from setuptools import setup\nsetup({})".format(str_attrs)) + + out = runner.invoke(cli) + + assert out.exit_code == 0 + assert "small-fake-a" not in out.stderr, out.stderr + + def test_find_links_option(runner): with open("requirements.in", "w") as req_in: req_in.write("-f ./libs3")