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

Ensure pip-compile --no-header <blank requirements.in> creates/overwrites requirements.txt #909

Merged
merged 1 commit into from
Oct 1, 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
38 changes: 24 additions & 14 deletions piptools/writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,14 @@ def _iter_lines(
warn_uninstallable = False
has_hashes = hashes and any(hash for hash in hashes.values())

yielded = False

for line in self.write_header():
yield line
yielded = True
for line in self.write_flags():
yield line
yielded = True

unsafe_requirements = (
{r for r in results if r.name in UNSAFE_PACKAGES}
Expand All @@ -158,24 +162,26 @@ def _iter_lines(
)
packages = {r for r in results if r.name not in UNSAFE_PACKAGES}

packages = sorted(packages, key=self._sort_key)

for ireq in packages:
if has_hashes and not hashes.get(ireq):
yield MESSAGE_UNHASHED_PACKAGE
warn_uninstallable = True
line = self._format_requirement(
ireq,
reverse_dependencies,
primary_packages,
markers.get(key_from_ireq(ireq)),
hashes=hashes,
)
yield line
if packages:
packages = sorted(packages, key=self._sort_key)
for ireq in packages:
if has_hashes and not hashes.get(ireq):
yield MESSAGE_UNHASHED_PACKAGE
warn_uninstallable = True
line = self._format_requirement(
ireq,
reverse_dependencies,
primary_packages,
markers.get(key_from_ireq(ireq)),
hashes=hashes,
)
yield line
yielded = True

if unsafe_requirements:
unsafe_requirements = sorted(unsafe_requirements, key=self._sort_key)
yield ""
yielded = True
if has_hashes and not self.allow_unsafe:
yield MESSAGE_UNSAFE_PACKAGES_UNPINNED
warn_uninstallable = True
Expand All @@ -195,6 +201,10 @@ def _iter_lines(
else:
yield req

# Yield even when there's no real content, so that blank files are written
if not yielded:
yield ""

if warn_uninstallable:
log.warning(MESSAGE_UNINSTALLABLE)

Expand Down
26 changes: 26 additions & 0 deletions tests/test_cli_compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -801,3 +801,29 @@ def test_dry_run_doesnt_touch_output_file(
# The output file must not be touched
after_compile_mtime = os.stat("requirements.txt").st_mtime
assert after_compile_mtime == before_compile_mtime


@pytest.mark.parametrize(
"empty_input_pkg, prior_output_pkg",
[
("", ""),
("", "small-fake-a==0.1\n"),
("# Nothing to see here", ""),
("# Nothing to see here", "small-fake-a==0.1\n"),
],
)
def test_empty_input_file_no_header(runner, empty_input_pkg, prior_output_pkg):
"""
Tests pip-compile creates an empty requirements.txt file,
given --no-header and empty requirements.in
"""
with open("requirements.in", "w") as req_in:
req_in.write(empty_input_pkg) # empty input file

with open("requirements.txt", "w") as req_txt:
req_txt.write(prior_output_pkg)

runner.invoke(cli, ["--no-header", "requirements.in"])

with open("requirements.txt", "r") as req_txt:
assert req_txt.read().strip() == ""