Skip to content
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
3 changes: 3 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,9 @@ put it directly into ``pip``.
Version History
===============

* Preserve indented comments when updating requirements files.
See https://github.com/peterbe/hashin/issues/124

* Switch to GitHub Actions instead of TravisCI. And test ``tox`` in
Python 3.7 and 3.8 additionally as well as upgrading lint requirements.
See https://github.com/peterbe/hashin/pull/118
Expand Down
2 changes: 2 additions & 0 deletions hashin.py
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,8 @@ def is_different_lines(old_lines, new_lines, indent):
for line in requirements.splitlines():
if regex.search(line):
lines.append(line)
elif lines and line.startswith(indent + padding + "#"):
break
elif lines and line.startswith(indent + padding):
lines.append(line)
elif lines:
Expand Down
44 changes: 44 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,50 @@ def test_amend_requirements_content_replacement():
assert result == new_lines[2]


def test_amend_requirements_content_replacement_keep_indented_comments():
requirements = (
"""
autocompeter==1.2.2 \\
--hash=sha256:33a5d0145e82326e781ddee1ad375f92cb84f8cfafea56e9504682adff64a5ee
# some comment created
# by pip-compile
examplepackage==10.0.0 \\
--hash=sha256:fd54e979d3747be638f59de44a7f6523bed56d81961a438462b1346f49be5fe4 \\
--hash=sha256:12ce5c2ef718e7e31cef2e2a3bde771d9216f2cb014efba963e69cb709bcbbd1
# another comment
# indicating where this package comes from
""".strip()
+ "\n"
)

new_lines = (
"autocompeter",
"autocompeter",
"""
autocompeter==1.2.3 \\
--hash=sha256:4d64ed1b9e0e73095f5cfa87f0e97ddb4c840049e8efeb7e63b46118ba1d623a
""".strip()
+ "\n",
)

result = hashin.amend_requirements_content(requirements, [new_lines])
expect = (
"""
autocompeter==1.2.3 \\
--hash=sha256:4d64ed1b9e0e73095f5cfa87f0e97ddb4c840049e8efeb7e63b46118ba1d623a
# some comment created
# by pip-compile
examplepackage==10.0.0 \\
--hash=sha256:fd54e979d3747be638f59de44a7f6523bed56d81961a438462b1346f49be5fe4 \\
--hash=sha256:12ce5c2ef718e7e31cef2e2a3bde771d9216f2cb014efba963e69cb709bcbbd1
# another comment
# indicating where this package comes from
""".strip()
+ "\n"
)
assert result == expect


def test_amend_requirements_content_actually_not_replacement():
requirements = (
"""
Expand Down