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

Fix output file update on dry-run compile #842

Merged
merged 1 commit into from
Jul 5, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
7 changes: 2 additions & 5 deletions piptools/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from itertools import chain, groupby

import six
from click.utils import LazyFile
from six.moves import shlex_quote

from ._compat import install_req_from_line
Expand Down Expand Up @@ -339,11 +340,7 @@ def get_compile_command(click_ctx):
continue

# Use a file name for file-like objects
if (
hasattr(value, "write")
and hasattr(value, "read")
and hasattr(value, "name")
):
if isinstance(value, LazyFile):
value = value.name

# Convert value to the list
Expand Down
71 changes: 71 additions & 0 deletions tests/test_cli_compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -722,3 +722,74 @@ def test_pre_option(runner, cli_option, infile_option, expected_package):

assert out.exit_code == 0, out.output
assert expected_package in out.output.splitlines(), out.output


@pytest.mark.parametrize(
"add_options",
[
[],
["--output-file", "requirements.txt"],
["--upgrade"],
["--upgrade", "--output-file", "requirements.txt"],
["--upgrade-package", "small-fake-a"],
["--upgrade-package", "small-fake-a", "--output-file", "requirements.txt"],
],
)
def test_dry_run_option(runner, add_options):
"""
Tests pip-compile doesn't create requirements.txt file on dry-run.
"""
with open("requirements.in", "w") as req_in:
req_in.write("small-fake-a\n")

out = runner.invoke(
cli, ["--dry-run", "--find-links", MINIMAL_WHEELS_PATH] + add_options
)

assert out.exit_code == 0, out.output
assert "small-fake-a==0.2" in out.output.splitlines()
assert not os.path.exists("requirements.txt")


@pytest.mark.parametrize(
"add_options, expected_cli_output_package",
[
([], "small-fake-a==0.1"),
(["--output-file", "requirements.txt"], "small-fake-a==0.1"),
(["--upgrade"], "small-fake-a==0.2"),
(["--upgrade", "--output-file", "requirements.txt"], "small-fake-a==0.2"),
(["--upgrade-package", "small-fake-a"], "small-fake-a==0.2"),
(
["--upgrade-package", "small-fake-a", "--output-file", "requirements.txt"],
"small-fake-a==0.2",
),
],
)
def test_dry_run_doesnt_touch_output_file(
runner, add_options, expected_cli_output_package
):
"""
Tests pip-compile doesn't touch requirements.txt file on dry-run.
"""
with open("requirements.in", "w") as req_in:
req_in.write("small-fake-a\n")

with open("requirements.txt", "w") as req_txt:
req_txt.write("small-fake-a==0.1\n")

before_compile_mtime = os.stat("requirements.txt").st_mtime

out = runner.invoke(
cli, ["--dry-run", "--find-links", MINIMAL_WHEELS_PATH] + add_options
)

assert out.exit_code == 0, out.output
assert expected_cli_output_package in out.output.splitlines()

# The package version must NOT be updated in the output file
with open("requirements.txt", "r") as req_txt:
assert "small-fake-a==0.1" in req_txt.read().splitlines()

# The output file must not be touched
after_compile_mtime = os.stat("requirements.txt").st_mtime
assert after_compile_mtime == before_compile_mtime