Skip to content

Commit 41602b5

Browse files
authored
fix: paths were wrong when running from root (#1403)
* Fix paths when running coverage from root * Add simple tests * Use nested pattern for older python versions
1 parent eaf5592 commit 41602b5

File tree

2 files changed

+24
-8
lines changed

2 files changed

+24
-8
lines changed

coverage/files.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,14 @@ def set_relative_directory():
2424
"""Set the directory that `relative_filename` will be relative to."""
2525
global RELATIVE_DIR, CANONICAL_FILENAME_CACHE
2626

27+
# The current directory
28+
abs_curdir = abs_file(os.curdir)
29+
if not abs_curdir.endswith(os.sep):
30+
# Suffix with separator only if not at the system root
31+
abs_curdir = abs_curdir + os.sep
32+
2733
# The absolute path to our current directory.
28-
RELATIVE_DIR = os.path.normcase(abs_file(os.curdir) + os.sep)
34+
RELATIVE_DIR = os.path.normcase(abs_curdir)
2935

3036
# Cache of results of calling the canonical_filename() method, to
3137
# avoid duplicating work.

tests/test_files.py

+17-7
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,14 @@
55

66
import os
77
import os.path
8+
from unittest import mock
89

910
import pytest
1011

11-
from coverage import env
12-
from coverage import files
12+
from coverage import env, files
1313
from coverage.exceptions import ConfigError
14-
from coverage.files import (
15-
TreeMatcher, FnmatchMatcher, ModuleMatcher, PathAliases,
16-
find_python_files, abs_file, actual_path, flat_rootname, fnmatches_to_regex,
17-
)
18-
14+
from coverage.files import (FnmatchMatcher, ModuleMatcher, PathAliases, TreeMatcher, abs_file,
15+
actual_path, find_python_files, flat_rootname, fnmatches_to_regex)
1916
from tests.coveragetest import CoverageTest
2017

2118

@@ -67,6 +64,19 @@ def test_canonical_filename_ensure_cache_hit(self):
6764
assert 'sub/proj1/file1.py' in files.CANONICAL_FILENAME_CACHE
6865
assert files.canonical_filename('sub/proj1/file1.py') == self.abs_path('file1.py')
6966

67+
@pytest.mark.parametrize(
68+
["curdir", "sep"], [
69+
("/", "/"),
70+
("X:\\", "\\"),
71+
]
72+
)
73+
def test_relative_dir_for_root(self, curdir, sep):
74+
with mock.patch.object(files.os, 'curdir', new=curdir):
75+
with mock.patch.object(files.os, 'sep', new=sep):
76+
with mock.patch('coverage.files.os.path.normcase', return_value=curdir):
77+
files.set_relative_directory()
78+
assert files.relative_directory() == curdir
79+
7080

7181
@pytest.mark.parametrize("original, flat", [
7282
("abc.py", "abc_py"),

0 commit comments

Comments
 (0)