Skip to content

Commit

Permalink
fix: lcov report indexeerror for some Jinja2 files. #1553
Browse files Browse the repository at this point in the history
  • Loading branch information
nedbat committed May 14, 2023
1 parent aefde53 commit 1ced74b
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 0 deletions.
2 changes: 2 additions & 0 deletions coverage/lcovreport.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ def get_lcov(self, fr: FileReporter, analysis: Analysis, outfile: IO[str]) -> No
# characters of the encoding ("==") are removed from the hash to
# allow genhtml to run on the resulting lcov file.
if source_lines:
if covered-1 >= len(source_lines):
break
line = source_lines[covered-1]
else:
line = ""
Expand Down
84 changes: 84 additions & 0 deletions tests/test_report_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,3 +156,87 @@ def test_map_paths_during_lcov_report(self) -> None:
cov.lcov_report()
contains("coverage.lcov", os_sep("src/program.py"))
doesnt_contain("coverage.lcov", os_sep("ver1/program.py"), os_sep("ver2/program.py"))


class ReportWithJinjaTest(CoverageTest):
"""Tests of Jinja-like behavior.
Jinja2 compiles a template (template.j2) into Python code, and then runs the
Python code to render the template. But during rendering, it uses "template.j2"
as the file name. During reporting, we will try to parse template.j2 as
Python code.
See https://github.com/nedbat/coveragepy/issues/1553 for more detail.
"""

def make_files(self) -> None:
# A Jinja2 file that is syntactically acceptable Python (though it wont run).
self.make_file("good.j2", """\
{{ data }}
line2
line3
""")
# A Jinja2 file that is a Python syntax error.
self.make_file("bad.j2", """\
This is data: {{ data }}.
line 2
line 3
""")
self.make_data_file(
lines={
abs_file("good.j2"): [1, 3, 5, 7, 9],
abs_file("bad.j2"): [1, 3, 5, 7, 9],
}
)

def test_report(self) -> None:
self.make_files()
cov = coverage.Coverage()
cov.load()
cov.report(show_missing=True)
expected = textwrap.dedent("""\
Name Stmts Miss Cover Missing
---------------------------------------
good.j2 3 1 67% 2
---------------------------------------
TOTAL 3 1 67%
""")
assert expected == self.stdout()

def test_html(self) -> None:
self.make_files()
cov = coverage.Coverage()
cov.load()
cov.html_report()
contains("htmlcov/index.html", "good.j2")
doesnt_contain("htmlcov/index.html", "bad.j2")

def test_xml(self) -> None:
self.make_files()
cov = coverage.Coverage()
cov.load()
cov.xml_report()
contains("coverage.xml", "good.j2")
doesnt_contain("coverage.xml", "bad.j2")

def test_json(self) -> None:
self.make_files()
cov = coverage.Coverage()
cov.load()
cov.json_report()
contains("coverage.json", "good.j2")
doesnt_contain("coverage.json", "bad.j2")

def test_lcov(self) -> None:
self.make_files()
cov = coverage.Coverage()
cov.load()
cov.lcov_report()
contains("coverage.lcov",
"SF:good.j2",
"DA:1,1,FHs1rDakj9p/NAzMCu3Kgw",
"DA:3,1,DGOyp8LEgI+3CcdFYw9uKQ",
"DA:2,0,5iUbzxp9w7peeTPjJbvmBQ",
)
doesnt_contain("coverage.lcov", "SF:bad.j2")

0 comments on commit 1ced74b

Please sign in to comment.