Skip to content

Commit

Permalink
fix: handle empty/windows paths correctly in base-path-aware path fix…
Browse files Browse the repository at this point in the history
…er (#984)
  • Loading branch information
matt-codecov authored Dec 30, 2024
1 parent 0026a62 commit 82c4f5c
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
18 changes: 16 additions & 2 deletions services/path_fixer/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import logging
import os.path
from pathlib import PurePath
from pathlib import PurePosixPath, PureWindowsPath
from typing import Sequence

import sentry_sdk
Expand Down Expand Up @@ -126,7 +126,19 @@ def __init__(self, original_path_fixer, base_path) -> None:
# base_path argument is the file path after the "# path=" in the report containing report location, if provided.
# to get the base path we use, strip the coverage report from the path to get the base path
# e.g.: "path/to/coverage.xml" --> "path/to/"
self.base_path = [PurePath(base_path).parent] if base_path is not None else []

self.base_path = []

if base_path:
# We want to use a `PurePath`, but we have to handle both Windows
# and POSIX paths. The cleanest way to do that is:
# - start by assuming it's a Windows path
# - if it doesn't have a drive letter like C:\, convert to POSIX
pure_path = PureWindowsPath(base_path)
if not pure_path.drive:
pure_path = PurePosixPath(pure_path.as_posix())

self.base_path = [pure_path.parent]

def _try_fix_path(self, path: str, bases_to_try: Sequence[str]) -> str | None:
original_path_fixer_result = self.original_path_fixer(path)
Expand All @@ -153,6 +165,8 @@ def _try_fix_path(self, path: str, bases_to_try: Sequence[str]) -> str | None:
def __call__(
self, path: str, bases_to_try: Sequence[str] | None = None
) -> str | None:
if not path:
return None
bases_to_try = bases_to_try or tuple()
key = (path, bases_to_try)

Expand Down
28 changes: 28 additions & 0 deletions services/path_fixer/tests/unit/test_path_fixer.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from pathlib import PurePosixPath, PureWindowsPath

from shared.yaml import UserYaml

from services.path_fixer import PathFixer, invert_pattern
Expand Down Expand Up @@ -128,6 +130,32 @@ def test_basepath_uses_own_result_if_main_is_none_multuple_base_paths(self):
== "project/__init__.py"
)

def test_basepath_does_not_resolve_empty_paths(self):
toc = ["project/__init__.py", "tests/__init__.py", "tests/test_project.py"]
pf = PathFixer.init_from_user_yaml({}, toc, [])
coverage_file = "/some/coverage.xml"
base_aware_pf = pf.get_relative_path_aware_pathfixer(coverage_file)

assert base_aware_pf("") is None

def test_basepath_with_win_and_posix_paths(self):
toc = ["project/__init__.py", "tests/__init__.py", "tests/test_project.py"]
pf = PathFixer.init_from_user_yaml({}, toc, [])

posix_coverage_file_path = "/posix_base_path/coverage.xml"
posix_base_aware_pf = pf.get_relative_path_aware_pathfixer(
posix_coverage_file_path
)
assert posix_base_aware_pf.base_path == [PurePosixPath("/posix_base_path")]

windows_coverage_file_path = "C:\\windows_base_path\\coverage.xml"
windows_base_aware_pf = pf.get_relative_path_aware_pathfixer(
windows_coverage_file_path
)
assert windows_base_aware_pf.base_path == [
PureWindowsPath("C:\\windows_base_path")
]


def test_ambiguous_paths():
toc = [
Expand Down

0 comments on commit 82c4f5c

Please sign in to comment.