Skip to content

Commit 7b21833

Browse files
marcosbogerionelmc
authored andcommitted
Default markdown-append to coverage.md and raise warning if both markdown options point to the same file
1 parent 3a15312 commit 7b21833

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

src/pytest_cov/engine.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
import coverage
1818
from coverage.data import CoverageData
19+
from coverage.misc import CoverageException
1920
from coverage.sqldata import filename_suffix
2021

2122
from . import CentralCovContextWarning
@@ -255,6 +256,15 @@ def summary(self, stream):
255256
total = self.cov.json_report(ignore_errors=True, outfile=output)
256257
stream.write('Coverage JSON written to file %s\n' % (self.cov.config.json_output if output is None else output))
257258

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+
258268
# Produce Markdown report if wanted.
259269
if 'markdown' in self.cov_report:
260270
output = self.cov_report['markdown'] or 'coverage.md'
@@ -265,7 +275,7 @@ def summary(self, stream):
265275

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

tests/test_pytest_cov.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,21 @@ 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):
421+
script = testdir.makepyfile(SCRIPT)
422+
423+
result = testdir.runpytest(
424+
'-v',
425+
'--cov=%s' % script.dirpath(),
426+
'--cov-report=markdown:' + MARKDOWN_REPORT_NAME,
427+
'--cov-report=markdown-append:' + MARKDOWN_REPORT_NAME,
428+
script,
429+
)
430+
431+
result.stdout.fnmatch_lines(['*WARNING: Failed to generate report: *', '*_ coverage: platform *, python * _*'])
432+
assert result.ret == 0
433+
434+
420435
@pytest.mark.skipif('coverage.version_info < (6, 3)')
421436
def test_lcov_output_dir(testdir):
422437
script = testdir.makepyfile(SCRIPT)

0 commit comments

Comments
 (0)