From e15d6d4b4e4557644955e15f5d95167349637c25 Mon Sep 17 00:00:00 2001 From: Arthur Rio Date: Tue, 14 Jun 2022 10:44:26 -0600 Subject: [PATCH 1/3] Fix paths when running coverage from root --- coverage/files.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/coverage/files.py b/coverage/files.py index 09f09da79..bf1647c9b 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 abs_curdir != 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. From 1272f1ae6ea5bab984948440ef7ef650e923ff12 Mon Sep 17 00:00:00 2001 From: Arthur Rio Date: Tue, 14 Jun 2022 11:44:12 -0600 Subject: [PATCH 2/3] Add simple tests --- coverage/files.py | 2 +- tests/test_files.py | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/coverage/files.py b/coverage/files.py index bf1647c9b..090ae0eb2 100644 --- a/coverage/files.py +++ b/coverage/files.py @@ -26,7 +26,7 @@ def set_relative_directory(): # The current directory abs_curdir = abs_file(os.curdir) - if abs_curdir != os.sep: + if not abs_curdir.endswith(os.sep): # Suffix with separator only if not at the system root abs_curdir = abs_curdir + os.sep diff --git a/tests/test_files.py b/tests/test_files.py index 5588c373d..35599116a 100644 --- a/tests/test_files.py +++ b/tests/test_files.py @@ -5,6 +5,7 @@ import os import os.path +from unittest import mock import pytest @@ -67,6 +68,21 @@ 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), + mock.patch.object(files.os, 'sep', new=sep), + 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"), From c6551ab16a93d9f886d5dc0762ff6a40cfd0a32b Mon Sep 17 00:00:00 2001 From: Arthur Rio Date: Tue, 14 Jun 2022 11:55:13 -0600 Subject: [PATCH 3/3] Use nested pattern for older python versions --- tests/test_files.py | 22 ++++++++-------------- 1 file changed, 8 insertions(+), 14 deletions(-) diff --git a/tests/test_files.py b/tests/test_files.py index 35599116a..28a98fb84 100644 --- a/tests/test_files.py +++ b/tests/test_files.py @@ -9,14 +9,10 @@ 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 @@ -75,13 +71,11 @@ def test_canonical_filename_ensure_cache_hit(self): ] ) def test_relative_dir_for_root(self, curdir, sep): - with ( - mock.patch.object(files.os, 'curdir', new=curdir), - mock.patch.object(files.os, 'sep', new=sep), - mock.patch('coverage.files.os.path.normcase', return_value=curdir), - ): - files.set_relative_directory() - assert files.relative_directory() == curdir + 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", [