Skip to content

Commit

Permalink
fix: more relative_files=true fixes. #1280
Browse files Browse the repository at this point in the history
  • Loading branch information
nedbat committed Oct 30, 2022
1 parent f93c620 commit fd232a2
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 2 deletions.
9 changes: 8 additions & 1 deletion coverage/python.py
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
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
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 fd232a2

Please sign in to comment.