Skip to content

Commit

Permalink
Raise error if input and output filenames are matched (jazzband#1787)
Browse files Browse the repository at this point in the history
Co-authored-by: Sviatoslav Sydorenko <webknjaz@redhat.com>
  • Loading branch information
atugushev and webknjaz committed Dec 25, 2022
1 parent cee88ed commit 7357383
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
5 changes: 5 additions & 0 deletions piptools/scripts/compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,11 @@ def cli(
if isinstance(output_file, LazyFile): # pragma: no cover
ctx.call_on_close(safecall(output_file.close_intelligently))

if output_file.name != "-" and output_file.name in src_files:
raise click.BadArgumentUsage(
f"input and output filenames must not be matched: {output_file.name}"
)

if resolver_name == "legacy":
log.warning(
"WARNING: the legacy dependency resolver is deprecated and will be removed"
Expand Down
28 changes: 28 additions & 0 deletions tests/test_cli_compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -2776,3 +2776,31 @@ def test_print_deprecation_warning_if_using_legacy_resolver(runner, current_reso
assert expected_warning in out.stderr
else:
assert expected_warning not in out.stderr


@pytest.mark.parametrize(
"input_filenames",
(
pytest.param(("requirements.txt",), id="one file"),
pytest.param(("requirements.txt", "dev-requirements.in"), id="multiple files"),
),
)
def test_raise_error_when_input_and_output_filenames_are_matched(
runner, tmp_path, input_filenames
):
req_in_paths = []
for input_filename in input_filenames:
req_in = tmp_path / input_filename
req_in.touch()
req_in_paths.append(req_in.as_posix())

req_out = tmp_path / "requirements.txt"
req_out_path = req_out.as_posix()

out = runner.invoke(cli, req_in_paths + ["--output-file", req_out_path])
assert out.exit_code == 2

expected_error = (
f"Error: input and output filenames must not be matched: {req_out_path}"
)
assert expected_error in out.stderr.splitlines()

0 comments on commit 7357383

Please sign in to comment.