From 2cf198054a0ccefaa8f3f60516aa1b8cb0f4b16b Mon Sep 17 00:00:00 2001 From: Nicolas Trangez Date: Mon, 22 Nov 2021 23:37:18 +0000 Subject: [PATCH] Python: Honour `--strip-extras` flag of `pip-compile` The `--strip-extras` flag of `pip-compile` is useful to generate contstraints files (instead of the more traditional requirements files) from some `constraints.in` file. Constraints files can't have extras markers on the packages (this wouldn't make much sense indeed), hence they need to be stripped. Since `pip-tools` 6.2.0 this is supported by using the `--strip-extras` flag. This commit adds this as a 'recognized' flag: if it's found in an existing `foo.txt` file, it will be passed to `pip-compile` when some `foo.in` file is updated. See: https://github.com/dependabot/dependabot-core/pull/3974#issuecomment-866660246 See: https://github.com/dependabot/dependabot-core/pull/3974#issuecomment-975953752 See: https://github.com/jazzband/pip-tools/issues/1300#issuecomment-860020431 --- .../file_updater/pip_compile_file_updater.rb | 3 +++ .../pip_compile_file_updater_spec.rb | 16 ++++++++++++++ .../pip_compile_files/strip_extras.in | 1 + .../requirements/pip_compile_strip_extras.txt | 22 +++++++++++++++++++ 4 files changed, 42 insertions(+) create mode 100644 python/spec/fixtures/pip_compile_files/strip_extras.in create mode 100644 python/spec/fixtures/requirements/pip_compile_strip_extras.txt diff --git a/python/lib/dependabot/python/file_updater/pip_compile_file_updater.rb b/python/lib/dependabot/python/file_updater/pip_compile_file_updater.rb index 6b5d5dab932..e02e9ec65e5 100644 --- a/python/lib/dependabot/python/file_updater/pip_compile_file_updater.rb +++ b/python/lib/dependabot/python/file_updater/pip_compile_file_updater.rb @@ -430,6 +430,9 @@ def pip_compile_options_from_compiled_file(requirements_file) options << "--no-header" unless requirements_file.content.include?("autogenerated by pip-c") options << "--pre" if requirements_file.content.include?("--pre") + + options << "--strip-extras" if requirements_file.content.include?("--strip-extras") + options end diff --git a/python/spec/dependabot/python/file_updater/pip_compile_file_updater_spec.rb b/python/spec/dependabot/python/file_updater/pip_compile_file_updater_spec.rb index 3bf187500ba..f65c16340df 100644 --- a/python/spec/dependabot/python/file_updater/pip_compile_file_updater_spec.rb +++ b/python/spec/dependabot/python/file_updater/pip_compile_file_updater_spec.rb @@ -505,5 +505,21 @@ end end end + + context "with stripped extras" do + let(:manifest_fixture_name) { "strip_extras.in" } + let(:generated_fixture_name) { "pip_compile_strip_extras.txt" } + let(:dependency_name) { "cachecontrol" } + let(:dependency_version) { "0.12.10" } + let(:dependency_previous_version) { "0.12.9" } + + it "doesn't add an extras annotation on cachecontrol" do + expect(updated_files.count).to eq(1) + expect(updated_files.first.content).to include("--strip-extras") + expect(updated_files.first.content).to include("cachecontrol==0.12.10") + expect(updated_files.first.content). + to_not include("cachecontrol[filecache]==") + end + end end end diff --git a/python/spec/fixtures/pip_compile_files/strip_extras.in b/python/spec/fixtures/pip_compile_files/strip_extras.in new file mode 100644 index 00000000000..b1db984ed70 --- /dev/null +++ b/python/spec/fixtures/pip_compile_files/strip_extras.in @@ -0,0 +1 @@ +cachecontrol[filecache] diff --git a/python/spec/fixtures/requirements/pip_compile_strip_extras.txt b/python/spec/fixtures/requirements/pip_compile_strip_extras.txt new file mode 100644 index 00000000000..871e3b538b2 --- /dev/null +++ b/python/spec/fixtures/requirements/pip_compile_strip_extras.txt @@ -0,0 +1,22 @@ +# +# This file is autogenerated by pip-compile with python 3.10 +# To update, run: +# +# pip-compile --strip-extras --output-file=constraints.txt constraints.in +# +cachecontrol==0.12.10 + # via -r constraints.in +certifi==2021.10.8 + # via requests +charset-normalizer==2.0.7 + # via requests +idna==3.3 + # via requests +lockfile==0.12.2 + # via cachecontrol +msgpack==1.0.2 + # via cachecontrol +requests==2.26.0 + # via cachecontrol +urllib3==1.26.7 + # via requests