Skip to content

Commit 535e477

Browse files
authored
Fixes bug when using diff-cover --format markdown:- (#516)
* Bugfix * Adds tests * Looking at this again, there is no need for re.compile anymore -- there is no benefit to it
1 parent a576ca6 commit 535e477

File tree

2 files changed

+43
-11
lines changed

2 files changed

+43
-11
lines changed

diff_cover/diff_cover_tool.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ def generate_coverage_report(
266266
if "markdown" in report_formats:
267267
markdown_report = report_formats["markdown"] or MARKDOWN_REPORT_DEFAULT_PATH
268268
reporter = MarkdownReportGenerator(coverage, diff)
269-
with open(markdown_report, "wb") as output_file:
269+
with open_file(markdown_report, "wb") as output_file:
270270
reporter.generate_report(output_file)
271271

272272
# Generate the report for stdout

tests/test_integration.py

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -96,21 +96,23 @@ def patch_diff(command, **kwargs):
9696
return helper
9797

9898

99+
def clean_html(html, clear_inline_css):
100+
if clear_inline_css:
101+
html = re.sub("<style>.*</style>", "", html, flags=re.DOTALL)
102+
return html.strip()
103+
104+
99105
def compare_html(expected_html_path, html_report_path, clear_inline_css=True):
100-
clean_content = re.compile("<style>.*</style>", flags=re.DOTALL)
101106
expected_file = open(expected_html_path, encoding="utf-8")
102107
html_report = open(html_report_path, encoding="utf-8")
103108

104109
with expected_file, html_report:
105-
html = html_report.read()
106-
expected = expected_file.read()
107-
if clear_inline_css:
108-
# The CSS is provided by pygments and changes fairly often.
109-
# Im ok with simply saying "There was css"
110-
# Perhaps I will eat these words
111-
html = clean_content.sub("", html)
112-
expected = clean_content.sub("", expected)
113-
assert expected.strip() == html.strip()
110+
# The CSS is provided by pygments and changes fairly often.
111+
# Im ok with simply saying "There was css"
112+
# Perhaps I will eat these words
113+
html = clean_html(html_report.read(), clear_inline_css)
114+
expected = clean_html(expected_file.read(), clear_inline_css)
115+
assert expected == html
114116

115117

116118
def compare_markdown(expected_file_path, actual_file_path):
@@ -179,6 +181,36 @@ def test_all_reports(self, runbin, patch_git_command, capsys):
179181
compare_json("add_json_report.json", "dummy/diff_coverage.json")
180182
compare_markdown("add_markdown_report.md", "dummy/diff_coverage.md")
181183

184+
def test_all_reports_with_stdout(self, runbin, patch_git_command, capsys):
185+
patch_git_command.set_stdout("git_diff_add.txt")
186+
assert (
187+
runbin(
188+
[
189+
"coverage.xml",
190+
"--format",
191+
"html:-,json:-,markdown:-",
192+
]
193+
)
194+
== 0
195+
)
196+
output = capsys.readouterr().out
197+
with open("add_console_report.txt", "r", encoding="utf-8") as f:
198+
actual = f.read()
199+
assert actual in output
200+
with open("add_html_report.html", "r", encoding="utf-8") as f:
201+
actual = clean_html(f.read(), clear_inline_css=True)
202+
# can't compare the output inside <head> since it changes
203+
# a lot so we just compare the body
204+
actual = actual[actual.find("</head>") :]
205+
assert len(actual) > 20
206+
assert actual in output
207+
with open("add_json_report.json", "r", encoding="utf-8") as f:
208+
actual = json.dumps(json.loads(f.read()))
209+
assert actual in output
210+
with open("add_markdown_report.md", "r", encoding="utf-8") as f:
211+
actual = f.read()
212+
assert actual in output
213+
182214
def test_added_file_console_lcov(self, runbin, patch_git_command, capsys):
183215
patch_git_command.set_stdout("git_diff_add.txt")
184216
assert runbin(["lcov.info"]) == 0

0 commit comments

Comments
 (0)