diff --git a/coverage/files.py b/coverage/files.py index 507496ffe..b5895cfca 100644 --- a/coverage/files.py +++ b/coverage/files.py @@ -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. diff --git a/tests/test_files.py b/tests/test_files.py index 5588c373d..28a98fb84 100644 --- a/tests/test_files.py +++ b/tests/test_files.py @@ -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 @@ -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"),