Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Raise error if input and output filenames are matched #1787

Merged
merged 4 commits into from
Dec 25, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions piptools/scripts/compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,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 @@ -2741,3 +2741,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(
webknjaz marked this conversation as resolved.
Show resolved Hide resolved
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()