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

Support excluding multiline regexes (fix #996) #1807

Merged
merged 14 commits into from
Jul 4, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
18 changes: 13 additions & 5 deletions coverage/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,14 +105,22 @@ def lines_matching(self, regex: str) -> set[TLineNo]:
"""Find the lines matching a regex.

Returns a set of line numbers, the lines that contain a match for
`regex`. The entire line needn't match, just a part of it.
`regex`. The entire line needn't match, just a part of it.
Handles multiline regex patterns.

"""
regex_c = re.compile(regex)
regex_c = re.compile(regex, re.MULTILINE)
matches = set()
for i, ltext in enumerate(self.text.split("\n"), start=1):
if regex_c.search(ltext):
matches.add(self._multiline.get(i, i))

last_start = 0
last_start_line = 0
for match in regex_c.finditer(self.text):
start, end = match.span()
start_line = last_start_line + self.text.count('\n', last_start, start)
end_line = last_start_line + self.text.count('\n', last_start, end)
matches.update(self._multiline.get(i, i) for i in range(start_line + 1, end_line + 2))
last_start = start
last_start_line = start_line
return matches

def _raw_parse(self) -> None:
Expand Down
69 changes: 69 additions & 0 deletions tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -781,6 +781,75 @@ def function() -> int:
assert parser.raw_statements == {1, 3, 4, 5, 6, 8, 9}
assert parser.statements == {1, 8, 9}

def test_multiline_exclusion(self) -> None:
devdanzin marked this conversation as resolved.
Show resolved Hide resolved
regex = r"print\('.*'\)"
parser = self.parse_text("""\
def foo():
print('Hello, world!')
""", regex)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please indent the code strings like this:

Suggested change
parser = self.parse_text("""\
def foo():
print('Hello, world!')
""", regex)
parser = self.parse_text("""\
def foo():
print('Hello, world!')
""", regex)

This should match the way other code is included in tests.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! Done.

devdanzin marked this conversation as resolved.
Show resolved Hide resolved
assert parser.lines_matching(regex) == {2}
assert parser.raw_statements == {1, 2}
assert parser.statements == {1}

regex = r"if True:\n\s+print\('Hello, world!'\)"
parser = self.parse_text("""\
def foo():
if True:
print('Hello, world!')
print('This is a multiline regex test.')
""", regex)
devdanzin marked this conversation as resolved.
Show resolved Hide resolved
assert parser.lines_matching(regex) == {2, 3}
assert parser.raw_statements == {1, 2, 3, 4}
assert parser.statements == {1}
devdanzin marked this conversation as resolved.
Show resolved Hide resolved

regex = r"nonexistent"
parser = self.parse_text("""\
def foo():
print('Hello, world!')
""", regex)
devdanzin marked this conversation as resolved.
Show resolved Hide resolved
assert parser.lines_matching(regex) == set()
assert parser.raw_statements == {1, 2}
assert parser.statements == {1, 2}

regex = r"anything"
parser = PythonParser(text="", filename="dummy.py", exclude=regex)
assert parser.lines_matching(regex) == set()
assert parser.raw_statements == set()
assert parser.statements == set()

regex = r"print\('.*'\)"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't a multi-line regex?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, that was just a sanity check from when I was sketching this that slipped through. I'll rewrite the test so it covers multiple multi-line matches.

parser = self.parse_text("""\
def foo():
print('Hello, world!')
def bar():
print('Hello again!')
""", regex)
devdanzin marked this conversation as resolved.
Show resolved Hide resolved
assert parser.lines_matching(regex) == {2, 4}
assert parser.raw_statements == {1, 2, 3, 4}
assert parser.statements == {1, 3}

regex = r"print\('Hello, world!'\)\n\s+if True:"
parser = self.parse_text("""\
def foo():
print('Hello, world!')
if True:
print('This is a test.')
""", regex)
devdanzin marked this conversation as resolved.
Show resolved Hide resolved
assert parser.lines_matching(regex) == {2, 3}
assert parser.raw_statements == {1, 2, 3, 4}
assert parser.statements == {1}

regex = r"""def foo\(\):\n\s+print\('Hello, world!'\)\n\s+if True:\n\s+print\('This is a test\.'\)"""
parser = self.parse_text("""\
def foo():
print('Hello, world!')
if True:
print('This is a test.')
""", regex)
devdanzin marked this conversation as resolved.
Show resolved Hide resolved
assert parser.lines_matching(regex) == {1, 2, 3, 4}
assert parser.raw_statements == {1, 2, 3, 4}
assert parser.statements == set()


class ParserMissingArcDescriptionTest(PythonParserTestBase):
"""Tests for PythonParser.missing_arc_description."""
Expand Down
Loading