Skip to content

Commit 474c1f4

Browse files
marcosbogerionelmc
authored andcommitted
Move markdown dest files check to StoreReport for earlier error and parser.error style output
1 parent 7b21833 commit 474c1f4

File tree

3 files changed

+19
-15
lines changed

3 files changed

+19
-15
lines changed

src/pytest_cov/engine.py

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
import coverage
1818
from coverage.data import CoverageData
19-
from coverage.misc import CoverageException
2019
from coverage.sqldata import filename_suffix
2120

2221
from . import CentralCovContextWarning
@@ -256,26 +255,17 @@ def summary(self, stream):
256255
total = self.cov.json_report(ignore_errors=True, outfile=output)
257256
stream.write('Coverage JSON written to file %s\n' % (self.cov.config.json_output if output is None else output))
258257

259-
# Check that markdown and markdown-append don't point to same file
260-
if all(x in self.cov_report for x in ['markdown', 'markdown-append']):
261-
markdown_file = self.cov_report['markdown'] or 'coverage.md'
262-
markdown_append_file = self.cov_report['markdown-append'] or 'coverage.md'
263-
if markdown_file == markdown_append_file:
264-
error_message = f"markdown and markdown-append options cannot point to the same file: '{markdown_file}'."
265-
error_message += ' Please redirect one of them using :DEST (e.g. --cov-report=markdown:dest_file.md)'
266-
raise CoverageException(error_message)
267-
268258
# Produce Markdown report if wanted.
269259
if 'markdown' in self.cov_report:
270-
output = self.cov_report['markdown'] or 'coverage.md'
260+
output = self.cov_report['markdown']
271261
with _backup(self.cov, 'config'):
272262
with Path(output).open('w') as output_file:
273263
total = self.cov.report(ignore_errors=True, file=output_file, output_format='markdown')
274264
stream.write(f'Coverage Markdown information written to file {output}\n')
275265

276266
# Produce Markdown report if wanted, appending to output file
277267
if 'markdown-append' in self.cov_report:
278-
output = self.cov_report['markdown-append'] or 'coverage.md'
268+
output = self.cov_report['markdown-append']
279269
with _backup(self.cov, 'config'):
280270
with Path(output).open('a') as output_file:
281271
total = self.cov.report(ignore_errors=True, file=output_file, output_format='markdown')

src/pytest_cov/plugin.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,20 @@ def __call__(self, parser, namespace, values, option_string=None):
8282
report_type, file = values
8383
namespace.cov_report[report_type] = file
8484

85+
# coverage.py doesn't set a default file for markdown output_format
86+
if report_type in ['markdown', 'markdown-append'] and file is None:
87+
namespace.cov_report[report_type] = 'coverage.md'
88+
if all(x in namespace.cov_report for x in ['markdown', 'markdown-append']):
89+
self._validate_markdown_dest_files(namespace.cov_report, parser)
90+
91+
def _validate_markdown_dest_files(self, cov_report_options, parser):
92+
markdown_file = cov_report_options['markdown']
93+
markdown_append_file = cov_report_options['markdown-append']
94+
if markdown_file == markdown_append_file:
95+
error_message = f"markdown and markdown-append options cannot point to the same file: '{markdown_file}'."
96+
error_message += ' Please redirect one of them using :DEST (e.g. --cov-report=markdown:dest_file.md)'
97+
parser.error(error_message)
98+
8599

86100
def pytest_addoption(parser):
87101
"""Add options to control coverage."""

tests/test_pytest_cov.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,7 @@ def test_markdown_and_markdown_append_work_together(testdir):
417417
assert result.ret == 0
418418

419419

420-
def test_markdown_and_markdown_append_pointing_to_same_file_throws_warning(testdir):
420+
def test_markdown_and_markdown_append_pointing_to_same_file_throws_error(testdir):
421421
script = testdir.makepyfile(SCRIPT)
422422

423423
result = testdir.runpytest(
@@ -428,8 +428,8 @@ def test_markdown_and_markdown_append_pointing_to_same_file_throws_warning(testd
428428
script,
429429
)
430430

431-
result.stdout.fnmatch_lines(['*WARNING: Failed to generate report: *', '*_ coverage: platform *, python * _*'])
432-
assert result.ret == 0
431+
result.stderr.fnmatch_lines(['* error: markdown and markdown-append options cannot point to the same file*'])
432+
assert result.ret == 4
433433

434434

435435
@pytest.mark.skipif('coverage.version_info < (6, 3)')

0 commit comments

Comments
 (0)