Skip to content

Commit

Permalink
fix: paths were wrong when running from root (#1403)
Browse files Browse the repository at this point in the history
* Fix paths when running coverage from root

* Add simple tests

* Use nested pattern for older python versions
  • Loading branch information
arthurio authored Aug 6, 2022
1 parent eaf5592 commit 41602b5
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
8 changes: 7 additions & 1 deletion coverage/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,14 @@ def set_relative_directory():
"""Set the directory that `relative_filename` will be relative to."""
global RELATIVE_DIR, CANONICAL_FILENAME_CACHE

# The current directory
abs_curdir = abs_file(os.curdir)
if not abs_curdir.endswith(os.sep):
# Suffix with separator only if not at the system root
abs_curdir = abs_curdir + os.sep

# The absolute path to our current directory.
RELATIVE_DIR = os.path.normcase(abs_file(os.curdir) + os.sep)
RELATIVE_DIR = os.path.normcase(abs_curdir)

# Cache of results of calling the canonical_filename() method, to
# avoid duplicating work.
Expand Down
24 changes: 17 additions & 7 deletions tests/test_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,14 @@

import os
import os.path
from unittest import mock

import pytest

from coverage import env
from coverage import files
from coverage import env, files
from coverage.exceptions import ConfigError
from coverage.files import (
TreeMatcher, FnmatchMatcher, ModuleMatcher, PathAliases,
find_python_files, abs_file, actual_path, flat_rootname, fnmatches_to_regex,
)

from coverage.files import (FnmatchMatcher, ModuleMatcher, PathAliases, TreeMatcher, abs_file,
actual_path, find_python_files, flat_rootname, fnmatches_to_regex)
from tests.coveragetest import CoverageTest


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

@pytest.mark.parametrize(
["curdir", "sep"], [
("/", "/"),
("X:\\", "\\"),
]
)
def test_relative_dir_for_root(self, curdir, sep):
with mock.patch.object(files.os, 'curdir', new=curdir):
with mock.patch.object(files.os, 'sep', new=sep):
with mock.patch('coverage.files.os.path.normcase', return_value=curdir):
files.set_relative_directory()
assert files.relative_directory() == curdir


@pytest.mark.parametrize("original, flat", [
("abc.py", "abc_py"),
Expand Down

0 comments on commit 41602b5

Please sign in to comment.