Skip to content

Commit 6bc9627

Browse files
committed
Preserve indented comments
1 parent a47b6d9 commit 6bc9627

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed

README.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,9 @@ put it directly into ``pip``.
270270
Version History
271271
===============
272272

273+
* Preserve indented comments when updating requirements files.
274+
See https://github.com/peterbe/hashin/issues/124
275+
273276
* Switch to GitHub Actions instead of TravisCI. And test ``tox`` in
274277
Python 3.7 and 3.8 additionally as well as upgrading lint requirements.
275278
See https://github.com/peterbe/hashin/pull/118

hashin.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,8 @@ def is_different_lines(old_lines, new_lines, indent):
436436
for line in requirements.splitlines():
437437
if regex.search(line):
438438
lines.append(line)
439+
elif lines and line.startswith(indent + padding + "#"):
440+
break
439441
elif lines and line.startswith(indent + padding):
440442
lines.append(line)
441443
elif lines:

tests/test_cli.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,50 @@ def test_amend_requirements_content_replacement():
415415
assert result == new_lines[2]
416416

417417

418+
def test_amend_requirements_content_replacement_keep_indented_comments():
419+
requirements = (
420+
"""
421+
autocompeter==1.2.2 \\
422+
--hash=sha256:33a5d0145e82326e781ddee1ad375f92cb84f8cfafea56e9504682adff64a5ee
423+
# some comment created
424+
# by pip-compile
425+
examplepackage==10.0.0 \\
426+
--hash=sha256:fd54e979d3747be638f59de44a7f6523bed56d81961a438462b1346f49be5fe4 \\
427+
--hash=sha256:12ce5c2ef718e7e31cef2e2a3bde771d9216f2cb014efba963e69cb709bcbbd1
428+
# another comment
429+
# indicating where this package comes from
430+
""".strip()
431+
+ "\n"
432+
)
433+
434+
new_lines = (
435+
"autocompeter",
436+
"autocompeter",
437+
"""
438+
autocompeter==1.2.3 \\
439+
--hash=sha256:4d64ed1b9e0e73095f5cfa87f0e97ddb4c840049e8efeb7e63b46118ba1d623a
440+
""".strip()
441+
+ "\n",
442+
)
443+
444+
result = hashin.amend_requirements_content(requirements, [new_lines])
445+
expect = (
446+
"""
447+
autocompeter==1.2.3 \\
448+
--hash=sha256:4d64ed1b9e0e73095f5cfa87f0e97ddb4c840049e8efeb7e63b46118ba1d623a
449+
# some comment created
450+
# by pip-compile
451+
examplepackage==10.0.0 \\
452+
--hash=sha256:fd54e979d3747be638f59de44a7f6523bed56d81961a438462b1346f49be5fe4 \\
453+
--hash=sha256:12ce5c2ef718e7e31cef2e2a3bde771d9216f2cb014efba963e69cb709bcbbd1
454+
# another comment
455+
# indicating where this package comes from
456+
""".strip()
457+
+ "\n"
458+
)
459+
assert result == expect
460+
461+
418462
def test_amend_requirements_content_actually_not_replacement():
419463
requirements = (
420464
"""

0 commit comments

Comments
 (0)