Skip to content

Commit 239ea3e

Browse files
committed
fix: fix relative git path when using working-directory
1 parent de6efd7 commit 239ea3e

File tree

3 files changed

+81
-6
lines changed

3 files changed

+81
-6
lines changed

src/pytest_codspeed/plugin.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
import pytest
1919
from _pytest.fixtures import FixtureManager
2020

21+
from pytest_codspeed.utils import get_git_relative_uri
22+
2123
from . import __version__
2224
from ._wrapper import get_lib
2325

@@ -169,7 +171,12 @@ def pytest_collection_modifyitems(
169171

170172

171173
def _run_with_instrumentation(
172-
lib: "LibType", nodeId: str, fn: Callable[..., Any], *args, **kwargs
174+
lib: "LibType",
175+
nodeId: str,
176+
config: "pytest.Config",
177+
fn: Callable[..., Any],
178+
*args,
179+
**kwargs,
173180
):
174181
is_gc_enabled = gc.isenabled()
175182
if is_gc_enabled:
@@ -185,12 +192,12 @@ def __codspeed_root_frame__():
185192
if SUPPORTS_PERF_TRAMPOLINE:
186193
# Warmup CPython performance map cache
187194
__codspeed_root_frame__()
188-
189195
lib.zero_stats()
190196
lib.start_instrumentation()
191197
__codspeed_root_frame__()
192198
lib.stop_instrumentation()
193-
lib.dump_stats_at(f"{nodeId}".encode("ascii"))
199+
uri = get_git_relative_uri(nodeId, config.rootpath)
200+
lib.dump_stats_at(uri.encode("ascii"))
194201
if is_gc_enabled:
195202
gc.enable()
196203

@@ -226,7 +233,9 @@ def pytest_runtest_protocol(item: "pytest.Item", nextitem: Union["pytest.Item",
226233
if setup_report.passed and not item.config.getoption("setuponly"):
227234
assert plugin.lib is not None
228235
runtest_call = pytest.CallInfo.from_call(
229-
lambda: _run_with_instrumentation(plugin.lib, item.nodeid, item.runtest),
236+
lambda: _run_with_instrumentation(
237+
plugin.lib, item.nodeid, item.config, item.runtest
238+
),
230239
"call",
231240
)
232241
runtest_report = ihook.pytest_runtest_makereport(item=item, call=runtest_call)
@@ -257,12 +266,13 @@ def __init__(self, request: "pytest.FixtureRequest"):
257266
self._request = request
258267

259268
def __call__(self, func: Callable[..., T], *args: Any, **kwargs: Any) -> T:
260-
plugin = get_plugin(self._request.config)
269+
config = self._request.config
270+
plugin = get_plugin(config)
261271
plugin.benchmark_count += 1
262272
if plugin.is_codspeed_enabled and plugin.should_measure:
263273
assert plugin.lib is not None
264274
return _run_with_instrumentation(
265-
plugin.lib, self._request.node.nodeid, func, *args, **kwargs
275+
plugin.lib, self._request.node.nodeid, config, func, *args, **kwargs
266276
)
267277
else:
268278
return func(*args, **kwargs)

src/pytest_codspeed/utils.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
from pathlib import Path
2+
3+
4+
def get_git_relative_path(abs_path: Path) -> Path:
5+
"""Get the path relative to the git root directory. If the path is not
6+
inside a git repository, the original path itself is returned.
7+
"""
8+
git_path = Path(abs_path).resolve()
9+
while (
10+
git_path != git_path.parent
11+
): # stops at root since parent of root is root itself
12+
if (git_path / ".git").exists():
13+
return abs_path.resolve().relative_to(git_path)
14+
git_path = git_path.parent
15+
return abs_path
16+
17+
18+
def get_git_relative_uri(uri: str, pytest_rootdir: Path) -> str:
19+
"""Get the benchmark uri relative to the git root dir.
20+
21+
Args:
22+
uri (str): the benchmark uri, for example:
23+
testing/test_excinfo.py::TestFormattedExcinfo::test_repr_source
24+
pytest_rootdir (str): the pytest root dir, for example:
25+
/home/user/gitrepo/folder
26+
27+
Returns:
28+
str: the benchmark uri relative to the git root dir, for example:
29+
folder/testing/test_excinfo.py::TestFormattedExcinfo::test_repr_source
30+
31+
"""
32+
file_path, function_path = uri.split("::", 1)
33+
absolute_file_path = pytest_rootdir / Path(file_path)
34+
relative_git_path = get_git_relative_path(absolute_file_path)
35+
return f"{str(relative_git_path)}::{function_path}"

tests/test_utils.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
from pathlib import Path
2+
from unittest.mock import patch
3+
4+
from pytest_codspeed.utils import get_git_relative_path, get_git_relative_uri
5+
6+
7+
def test_get_git_relative_path_found():
8+
with patch.object(
9+
Path, "exists", lambda self: str(self) == "/home/user/gitrepo/.git"
10+
):
11+
path = Path("/home/user/gitrepo/folder/nested_folder")
12+
assert get_git_relative_path(path) == Path("folder/nested_folder")
13+
14+
15+
def test_get_git_relative_path_not_found():
16+
with patch.object(Path, "exists", lambda self: False):
17+
path = Path("/home/user/gitrepo/folder")
18+
assert get_git_relative_path(path) == path
19+
20+
21+
def test_get_git_relative_uri():
22+
with patch.object(
23+
Path, "exists", lambda self: str(self) == "/home/user/gitrepo/.git"
24+
):
25+
pytest_rootdir = Path("/home/user/gitrepo/pytest_root")
26+
uri = "testing/test_excinfo.py::TestFormattedExcinfo::test_fn"
27+
assert (
28+
get_git_relative_uri(uri, pytest_rootdir)
29+
== "pytest_root/testing/test_excinfo.py::TestFormattedExcinfo::test_fn"
30+
)

0 commit comments

Comments
 (0)