From 45cf7936ee605cfe06f7f5967a72a73198960120 Mon Sep 17 00:00:00 2001 From: Ned Batchelder Date: Mon, 24 Oct 2022 06:57:36 -0400 Subject: [PATCH] fix: more relative_files=true fixes. #1280 --- CHANGES.rst | 3 +++ coverage/python.py | 9 ++++++++- coverage/xmlreport.py | 3 ++- tests/test_api.py | 32 ++++++++++++++++++++++++++++++++ 4 files changed, 45 insertions(+), 2 deletions(-) diff --git a/CHANGES.rst b/CHANGES.rst index b0ea7bb6f..dd2c90144 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -34,6 +34,8 @@ Unreleased - A ``[paths]`` setting like ``*/foo`` will now match ``foo/bar.py`` so that relative file paths can be combined more easily. + - The setting is properly interpreted in more places, fixing `issue 1280`_. + - Fixed environment variable expansion in pyproject.toml files. It was overly broad, causing errors outside of coverage.py settings, as described in `issue 1481`_. This is now fixed, but in rare cases will require changing your @@ -43,6 +45,7 @@ Unreleased implementations other than CPython or PyPy (`issue 1474`_). .. _issue 991: https://github.com/nedbat/coveragepy/issues/991 +.. _issue 1280: https://github.com/nedbat/coveragepy/issues/1280 .. _issue 1407: https://github.com/nedbat/coveragepy/issues/1407 .. _issue 1474: https://github.com/nedbat/coveragepy/issues/1474 .. _issue 1481: https://github.com/nedbat/coveragepy/issues/1481 diff --git a/coverage/python.py b/coverage/python.py index da43e6e8b..c8b8e774b 100644 --- a/coverage/python.py +++ b/coverage/python.py @@ -151,7 +151,14 @@ def __init__(self, morf, coverage=None): filename = source_for_morf(morf) - super().__init__(canonical_filename(filename)) + fname = filename + canonicalize = True + if self.coverage is not None: + if self.coverage.config.relative_files: + canonicalize = False + if canonicalize: + fname = canonical_filename(filename) + super().__init__(fname) if hasattr(morf, '__name__'): name = morf.__name__.replace(".", os.sep) diff --git a/coverage/xmlreport.py b/coverage/xmlreport.py index 2c34cb546..5eb940bf6 100644 --- a/coverage/xmlreport.py +++ b/coverage/xmlreport.py @@ -149,7 +149,8 @@ def xml_file(self, fr, analysis, has_arcs): # are populated later. Note that a package == a directory. filename = fr.filename.replace("\\", "/") for source_path in self.source_paths: - source_path = files.canonical_filename(source_path) + if not self.config.relative_files: + source_path = files.canonical_filename(source_path) if filename.startswith(source_path.replace("\\", "/") + "/"): rel_name = filename[len(source_path)+1:] break diff --git a/tests/test_api.py b/tests/test_api.py index 07bd07f33..ce44b9b1c 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -1239,6 +1239,38 @@ def test_combine_no_suffix_multiprocessing(self): self.assert_file_count(".coverage.*", 0) self.assert_exists(".coverage") + def test_files_up_one_level(self): + # https://github.com/nedbat/coveragepy/issues/1280 + self.make_file("src/mycode.py", """\ + def foo(): + return 17 + """) + self.make_file("test/test_it.py", """\ + from src.mycode import foo + assert foo() == 17 + """) + self.make_file("test/.coveragerc", """\ + [run] + parallel = True + relative_files = True + + [paths] + source = + ../src/ + */src + """) + os.chdir("test") + sys.path.insert(0, "..") + cov1 = coverage.Coverage() + self.start_import_stop(cov1, "test_it") + cov1.save() + cov2 = coverage.Coverage() + cov2.combine() + cov3 = coverage.Coverage() + cov3.load() + report = self.get_report(cov3) + assert self.last_line_squeezed(report) == "TOTAL 4 0 100%" + class CombiningTest(CoverageTest): """More tests of combining data."""