Skip to content

Commit

Permalink
Merge pull request #1822 from davidmreed/warn-empty-output-file
Browse files Browse the repository at this point in the history
  • Loading branch information
webknjaz authored May 15, 2023
2 parents 244ac03 + 6bd95b9 commit 41f4b66
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
13 changes: 12 additions & 1 deletion piptools/scripts/compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,18 @@ 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(
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)
Expand Down
21 changes: 21 additions & 0 deletions tests/test_cli_compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -858,6 +858,27 @@ 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_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.
"""
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(
Expand Down

0 comments on commit 41f4b66

Please sign in to comment.