Fix incorrect backslash before multi-lines string by LiuYinCarl · Pull Request #1828 · nedbat/coveragepy (original) (raw)
Tests added in dc819ff
I stumbled across this while reviewing #1838. I think the tests (and this fix) are incomplete.
def test_bug_1828(self) -> None: |
---|
# https://github.com/nedbat/coveragepy/pull/1828 |
self.make_file("backslashes.py", """\ |
a = ["aaa",\\ |
"bbb \\ |
ccc"] |
""") |
cov = coverage.Coverage() |
backslashes = self.start_import_stop(cov, "backslashes") |
cov.html_report(backslashes, directory="out") |
contains( |
"out/backslashes_py.html", |
# line 2 is `"bbb \` |
r'2' |
+ r' "bbb \', |
# line 3 is `ccc"]` |
r'3' |
+ r' ccc"]', |
) |
In lines 1146–1154, you are testing whether the HTML output correctly reproduces the backslash in the "bbb ccc"
string. However, the rendered text does not contain the backslash and linebreak within line 1137. This leads to a missing linebreak, and therefore to misaligned coverage information similar to #1836, just in the other direction.
def test_1828(self) -> None: |
---|
# https://github.com/nedbat/coveragepy/pull/1828 |
tokens = list(source_token_lines(textwrap.dedent(""" |
x = \ |
1 |
a = ["aaa",\\ |
"bbb \\ |
ccc"] |
"""))) |
assert tokens == [ |
[], |
[('nam', 'x'), ('ws', ' '), ('op', '='), ('ws', ' '), ('num', '1')], |
[('nam', 'a'), ('ws', ' '), ('op', '='), ('ws', ' '), |
('op', '['), ('str', '"aaa"'), ('op', ','), ('xx', '\\')], |
[('ws', ' '), ('str', '"bbb \\')], |
[('str', ' ccc"'), ('op', ']')], |
] |
Testing the same input, just with the tokenizer instead of the HTML report renderer. You can see in line 112 that there is a ws
token with many spaces (which doesn't occur in the tokenizer input) instead of two tokens with a backspace continuation (which does occur). Again, this leads to a missing linebreak, and to misaligned coverage info for all following lines.
Concerning this pull request in general:
- Python 3.11 (Linux) with coverage 7.6.1 (plus commit 01779db from fix backslash in f-string #1838 applied manually): I see the "before fix"/incorrect version for the
a
variable and the "after fix"/correct version for theb
,c
,d
andf
variables. The same happens whether I use single-quoted strings, triple-quoted strings, or triple-quoted f-strings. - Python 3.12 (Linux) with coverage 7.6.1 (plus commit 01779db): I see the incorrect version for the
a
variable if and only if I'm not using triple-quoted f-strings, and the correct version in all other cases/for all other variables.