From e7c8f646d28e66d92063428f785fccdcf654a7d6 Mon Sep 17 00:00:00 2001 From: David Reed Date: Tue, 14 Mar 2023 15:06:51 -0600 Subject: [PATCH 1/5] Log a warning with -P if the output file is empty --- piptools/scripts/compile.py | 9 +++++++++ tests/test_cli_compile.py | 18 ++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/piptools/scripts/compile.py b/piptools/scripts/compile.py index f578140d1..7cec4e8f1 100755 --- a/piptools/scripts/compile.py +++ b/piptools/scripts/compile.py @@ -441,6 +441,15 @@ def cli( # Proxy with a LocalRequirementsRepository if --upgrade is not specified # (= default invocation) if not upgrade and os.path.exists(output_file.name): + if upgrade_install_reqs and os.path.getsize(output_file.name) == 0: + log.warning( + f"WARNING: the output file {output_file.name} exists but is empty. " + "Pip-tools cannot upgrade only specific packages (using -P/--upgrade-package) " + "without an existing pin file to provide constraints. " + "This often occurs if you redirect standard output to your output file, " + "as any existing content is truncated." + ) + # Use a temporary repository to ensure outdated(removed) options from # existing requirements.txt wouldn't get into the current repository. tmp_repository = PyPIRepository(pip_args, cache_dir=cache_dir) diff --git a/tests/test_cli_compile.py b/tests/test_cli_compile.py index 693cff130..92c742bfc 100644 --- a/tests/test_cli_compile.py +++ b/tests/test_cli_compile.py @@ -858,6 +858,24 @@ def test_upgrade_packages_option_no_existing_file(pip_conf, runner): assert out.exit_code == 0 assert "small-fake-a==0.2" in out.stderr.splitlines() assert "small-fake-b==0.3" in out.stderr.splitlines() + assert "WARNING: the output file requirements.txt exists but is empty" not in out.stderr + +def test_upgrade_packages_option_empty_existing_file_warning(pip_conf, runner): + """ + piptools warns the user if --upgrade-package/-P is specified and the + output file exists, but is empty. + """ + with open("requirements.in", "w") as req_in: + req_in.write("small-fake-a==0.2") + with open("requirements.txt", "w") as req_txt: + req_txt.write("") + + out = runner.invoke(cli, ["--no-annotate", "-P", "small-fake-a"]) + + assert out.exit_code == 0 + assert "small-fake-a==0.2" in out.stderr.splitlines() + assert "WARNING: the output file requirements.txt exists but is empty" in out.stderr + @pytest.mark.parametrize( From caa8fdef3b8a57ac6daa04c33bdecb2791078cd9 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 14 Mar 2023 23:59:31 +0000 Subject: [PATCH 2/5] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- tests/test_cli_compile.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/tests/test_cli_compile.py b/tests/test_cli_compile.py index 92c742bfc..e70d0e39f 100644 --- a/tests/test_cli_compile.py +++ b/tests/test_cli_compile.py @@ -858,7 +858,11 @@ def test_upgrade_packages_option_no_existing_file(pip_conf, runner): assert out.exit_code == 0 assert "small-fake-a==0.2" in out.stderr.splitlines() assert "small-fake-b==0.3" in out.stderr.splitlines() - assert "WARNING: the output file requirements.txt exists but is empty" not in out.stderr + assert ( + "WARNING: the output file requirements.txt exists but is empty" + not in out.stderr + ) + def test_upgrade_packages_option_empty_existing_file_warning(pip_conf, runner): """ @@ -877,7 +881,6 @@ def test_upgrade_packages_option_empty_existing_file_warning(pip_conf, runner): assert "WARNING: the output file requirements.txt exists but is empty" in out.stderr - @pytest.mark.parametrize( ("current_package", "upgraded_package"), ( From 784d7e99345b70bf6c4168bbf2648f250bdbfc62 Mon Sep 17 00:00:00 2001 From: David Reed Date: Sat, 13 May 2023 11:41:19 -0600 Subject: [PATCH 3/5] Update tests/test_cli_compile.py Co-authored-by: chrysle --- tests/test_cli_compile.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_cli_compile.py b/tests/test_cli_compile.py index 28efd5efc..10b071761 100644 --- a/tests/test_cli_compile.py +++ b/tests/test_cli_compile.py @@ -864,7 +864,7 @@ def test_upgrade_packages_option_no_existing_file(pip_conf, runner): ) -def test_upgrade_packages_option_empty_existing_file_warning(pip_conf, runner): +def test_upgrade_packages_empty_target_file_warning(pip_conf, runner): """ piptools warns the user if --upgrade-package/-P is specified and the output file exists, but is empty. From aa6460dc1c98329c0f8bfe5d046c58fae74aa77c Mon Sep 17 00:00:00 2001 From: David Reed Date: Sun, 14 May 2023 20:05:00 -0600 Subject: [PATCH 4/5] Improve readability of conditional --- piptools/scripts/compile.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/piptools/scripts/compile.py b/piptools/scripts/compile.py index 889da6463..7797a8240 100755 --- a/piptools/scripts/compile.py +++ b/piptools/scripts/compile.py @@ -444,7 +444,8 @@ def cli( # Proxy with a LocalRequirementsRepository if --upgrade is not specified # (= default invocation) if not upgrade and os.path.exists(output_file.name): - if upgrade_install_reqs and os.path.getsize(output_file.name) == 0: + output_file_is_empty = os.path.getsize(output_file.name) == 0 + if upgrade_install_reqs and output_file_is_empty: log.warning( f"WARNING: the output file {output_file.name} exists but is empty. " "Pip-tools cannot upgrade only specific packages (using -P/--upgrade-package) " From 6bd95b9ea54c9f6b01615d15034c2430e20b45bf Mon Sep 17 00:00:00 2001 From: David Reed Date: Mon, 15 May 2023 09:06:02 -0600 Subject: [PATCH 5/5] Update piptools/scripts/compile.py Co-authored-by: chrysle --- piptools/scripts/compile.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/piptools/scripts/compile.py b/piptools/scripts/compile.py index 7797a8240..b36ef8b30 100755 --- a/piptools/scripts/compile.py +++ b/piptools/scripts/compile.py @@ -443,7 +443,8 @@ def cli( # Proxy with a LocalRequirementsRepository if --upgrade is not specified # (= default invocation) - if not upgrade and os.path.exists(output_file.name): + output_file_exists = os.path.exists(output_file.name) + if not upgrade and output_file_exists: output_file_is_empty = os.path.getsize(output_file.name) == 0 if upgrade_install_reqs and output_file_is_empty: log.warning(