Skip to content

Commit

Permalink
Correctly ignore comment lines
Browse files Browse the repository at this point in the history
Correctly ignore comment lines following a line continuation
character at the end of the previous line.

Fixes arkq#14 and closes arkq#15

Co-authored-by: Arkadiusz Bokowy <[email protected]>
  • Loading branch information
wwuck and arkq committed May 14, 2020
1 parent d83fa34 commit d38fc92
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
18 changes: 11 additions & 7 deletions src/flake8_requirements/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from .modules import STDLIB_PY3

# NOTE: Changing this number will alter package version as well.
__version__ = "1.3.1"
__version__ = "1.3.2"
__license__ = "MIT"

LOG = getLogger('flake8.plugin.requirements')
Expand Down Expand Up @@ -69,16 +69,20 @@ def project2module(project):


def joinlines(lines):
"""Strip comments and join line continuations."""
"""Join line continuations and strip comments."""
joined_line = ""
for line in map(lambda x: x.strip(), lines):
if not line or line.startswith("#"):
continue
if line.endswith("\\"):
comment = line.startswith("#")
if line.endswith("\\") and not comment:
joined_line += line[:-1]
continue
yield joined_line + line
joined_line = ""
if not comment:
joined_line += line
if joined_line:
yield joined_line
joined_line = ""
if joined_line:
yield joined_line


class ImportVisitor(ast.NodeVisitor):
Expand Down
8 changes: 8 additions & 0 deletions test/test_requirements.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,14 @@ def test_resolve_requirement_with_file_content_line_continuation(self):
["foo"],
)

def test_resolve_requirement_with_file_content_line_continuation_2(self):
content = "foo \\\n>= 1.0.0 \\\n# comment \\\nbar \\"
with mock.patch(builtins_open, mock.mock_open(read_data=content)):
self.assertEqual(
Flake8Checker.resolve_requirement("-r requirements.txt", 1),
["foo", "bar"],
)

def test_resolve_requirement_with_file_recursion_beyond_max_depth(self):
content = "-r requirements.txt\n"
with mock.patch(builtins_open, mock.mock_open(read_data=content)):
Expand Down

0 comments on commit d38fc92

Please sign in to comment.