diff --git a/AUTHORS.rst b/AUTHORS.rst index 561367d7..17386b93 100644 --- a/AUTHORS.rst +++ b/AUTHORS.rst @@ -2,6 +2,7 @@ Authors ======= +* anticipate218 - https://github.com/anticipate218 * GruffElixir - https://github.com/GruffElixir * Marc Schlaich - https://github.com/schlamar (\http://www.schlamar.org) * Rick van Hattem - http://wol.ph diff --git a/CHANGELOG.rst b/CHANGELOG.rst index cf403789..3ff62d89 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -5,6 +5,9 @@ Changelog Unreleased ---------- +* Write Markdown reports as UTF-8 so Unicode source filenames work on systems + with other default text encodings. + * Fixed no_cover markers and fixtures when coverage is disabled with --no-cov. Fixes #590. * Document the import-path difference between ``coverage run -m pytest`` and ``pytest --cov`` for users migrating from coverage.py. diff --git a/src/pytest_cov/engine.py b/src/pytest_cov/engine.py index 50b9c138..25b8af53 100644 --- a/src/pytest_cov/engine.py +++ b/src/pytest_cov/engine.py @@ -216,14 +216,14 @@ def summary(self, stream): # Produce Markdown report if wanted. if 'markdown' in self.cov_report: output = self.cov_report['markdown'] - with Path(output).open('w') as output_file: + with Path(output).open('w', encoding='utf-8') as output_file: self.cov.report(ignore_errors=True, file=output_file, output_format='markdown') stream.write(f'Coverage Markdown information written to file {output}\n') # Produce Markdown report if wanted, appending to output file if 'markdown-append' in self.cov_report: output = self.cov_report['markdown-append'] - with Path(output).open('a') as output_file: + with Path(output).open('a', encoding='utf-8') as output_file: self.cov.report(ignore_errors=True, file=output_file, output_format='markdown') stream.write(f'Coverage Markdown information appended to file {output}\n') diff --git a/tests/test_pytest_cov.py b/tests/test_pytest_cov.py index 098dc3fd..5fc45b99 100644 --- a/tests/test_pytest_cov.py +++ b/tests/test_pytest_cov.py @@ -386,6 +386,42 @@ def test_markdown_append_output_dir(testdir): assert result.ret == 0 +@pytest.mark.parametrize('report', ['markdown', 'markdown-append']) +def test_markdown_unicode_filenames(testdir, monkeypatch, report): + monkeypatch.setenv('PYTHONIOENCODING', 'utf-8') + script = testdir.makepyfile(test_unicode_é=SCRIPT2) + testdir.makeconftest( + """ +from pathlib import Path + +original_open = Path.open + +def ascii_default_open(self, mode='r', buffering=-1, encoding=None, errors=None, newline=None): + if self.name == 'coverage.md' and 'b' not in mode and encoding is None: + encoding = 'ascii' + return original_open(self, mode, buffering, encoding, errors, newline) + +def pytest_configure(): + Path.open = ascii_default_open + +def pytest_unconfigure(): + Path.open = original_open +""" + ) + output = Path(str(testdir.tmpdir)) / 'coverage.md' + prefix = 'Existing report\n' + if report == 'markdown-append': + output.write_text(prefix, encoding='utf-8') + + result = testdir.runpytest('-v', f'--cov={script.dirpath()}', f'--cov-report={report}:coverage.md', script) + + assert result.ret == 0 + content = output.read_text(encoding='utf-8') + assert r'test\_unicode\_é.py' in content + if report == 'markdown-append': + assert content.startswith(prefix) + + def test_markdown_and_markdown_append_work_together(testdir): script = testdir.makepyfile(SCRIPT)