Skip to content

Commit

Permalink
fix: more relative_files=true fixes. nedbat#1280
Browse files Browse the repository at this point in the history
  • Loading branch information
nedbat committed Oct 30, 2022
1 parent ec6205a commit 45cf793
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 2 deletions.
3 changes: 3 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
9 changes: 8 additions & 1 deletion coverage/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion coverage/xmlreport.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
32 changes: 32 additions & 0 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down

0 comments on commit 45cf793

Please sign in to comment.