Skip to content

Commit

Permalink
testlib.repo: Cache repository helper functions
Browse files Browse the repository at this point in the history
The repository related helper functions are relatively costly,
given that they are run repeatedly from many different tests.
As the output of those functions is more or less static during a
test run, this change adds caching so we do not have to execute
the external commands multiple times.

CMK-20561

Change-Id: I6a2070ea8de4a2120e5bcfb404cdfd14b049cc61
(cherry picked from commit 0da7e2d)
  • Loading branch information
rene-slowenski-checkmk committed Jan 7, 2025
1 parent 33cdb7e commit b059bca
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions tests/testlib/repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@
import subprocess
import sys
from contextlib import suppress
from functools import cache
from pathlib import Path
from typing import Callable, Iterator

logger = logging.getLogger(__name__)


@cache
def repo_path() -> Path:
"""Returns the checkout/worktree path (in contrast to the 'git-dir')
same as result of `git rev-parse --show-toplevel`, but repo_path is being executed
Expand Down Expand Up @@ -48,10 +50,12 @@ def add_python_paths() -> None:
sys.path.insert(0, os.path.join(repo_path(), "omd/packages/omd"))


@cache
def qa_test_data_path() -> Path:
return Path(__file__).parent.parent.resolve() / Path("qa-test-data")


@cache
def branch_from_env(*, env_var: str, fallback: str | Callable[[], str] | None = None) -> str:
if branch := os.environ.get(env_var):
return branch
Expand All @@ -60,6 +64,7 @@ def branch_from_env(*, env_var: str, fallback: str | Callable[[], str] | None =
raise RuntimeError(f"{env_var} environment variable, e.g. master, is missing")


@cache
def current_branch_version() -> str:
return subprocess.check_output(
[
Expand All @@ -73,6 +78,7 @@ def current_branch_version() -> str:
).strip()


@cache
def current_base_branch_name() -> str:
branch_name = current_branch_name()

Expand Down Expand Up @@ -118,13 +124,15 @@ def current_base_branch_name() -> str:
return branch_name


@cache
def current_branch_name() -> str:
branch_name = subprocess.check_output(
["git", "rev-parse", "--abbrev-ref", "HEAD"], encoding="utf-8"
)
return branch_name.split("\n", 1)[0]


@cache
def git_commit_id(path: Path | str) -> str:
"""Returns the git hash for given @path."""
return subprocess.check_output(
Expand All @@ -136,6 +144,7 @@ def git_commit_id(path: Path | str) -> str:
).strip("\n")


@cache
def git_essential_directories(checkout_dir: Path) -> Iterator[str]:
"""Yields paths to all directories needed to be accessible in order to run git operations
Note that if a directory is a subdirectory of checkout_dir it will be skipped"""
Expand Down Expand Up @@ -165,6 +174,7 @@ def git_essential_directories(checkout_dir: Path) -> Iterator[str]:
yield alternate.as_posix()


@cache
def find_git_rm_mv_files(dirpath: Path) -> list[str]:
del_files = []

Expand Down

0 comments on commit b059bca

Please sign in to comment.