Skip to content

Commit

Permalink
refactor(test): Deduplicate workspace discovery function
Browse files Browse the repository at this point in the history
  • Loading branch information
martis42 committed Dec 16, 2024
1 parent 34bac11 commit e89c3dc
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 16 deletions.
7 changes: 1 addition & 6 deletions test/apply_fixes/execution_logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from tempfile import TemporaryDirectory
from typing import TYPE_CHECKING

from test.support.bazel import get_bazel_binary
from test.support.bazel import get_bazel_binary, get_current_workspace
from test.support.result import Error

if TYPE_CHECKING:
Expand Down Expand Up @@ -52,11 +52,6 @@ def dwyu_path_as_string(dwyu_path: Path) -> str:
return str(dwyu_path) if isinstance(dwyu_path, PosixPath) else str(dwyu_path).replace("\\", "\\\\")


def get_current_workspace(bazel_binary: Path) -> Path:
process = subprocess.run([bazel_binary, "info", "workspace"], check=True, capture_output=True, text=True)
return Path(process.stdout.strip())


@dataclass
class TestDefinition:
name: str
Expand Down
13 changes: 3 additions & 10 deletions test/aspect/execution_logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

from version import CompatibleVersions, TestedVersions

from test.support.bazel import get_bazel_binary
from test.support.bazel import get_bazel_binary, get_current_workspace
from test.support.result import Error

if TYPE_CHECKING:
Expand Down Expand Up @@ -44,13 +44,6 @@ def execute_test(
return succeeded


def get_current_workspace() -> Path:
process = subprocess.run(
["bazel", "--max_idle_secs=5", "info", "workspace"], check=True, capture_output=True, text=True
)
return Path(process.stdout.strip())


def get_bazel_rolling_version(bazel_bin: Path) -> str:
run_env = deepcopy(environ)
run_env["USE_BAZEL_VERSION"] = "rolling"
Expand Down Expand Up @@ -80,7 +73,8 @@ def main(
list_tests: bool = False,
only_default_version: bool = False,
) -> int:
workspace_path = get_current_workspace()
bazel_binary = get_bazel_binary()
workspace_path = get_current_workspace(bazel_binary)
test_files = sorted([Path(x) for x in workspace_path.glob("*/test_*.py")])

if list_tests:
Expand All @@ -103,7 +97,6 @@ def main(
else:
versions = tested_versions

bazel_binary = get_bazel_binary()
versions = set_rolling_bazel_version(versions=versions, bazel_bin=bazel_binary)

failed_tests = []
Expand Down
12 changes: 12 additions & 0 deletions test/support/bazel.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,15 @@ def get_bazel_binary() -> Path:
return Path(bazel)

raise RuntimeError("No bazelisk binary or bazel symlink towards bazelisk available on your system")


def get_current_workspace(bazel_bin: Path) -> Path:
cmd = [
str(bazel_bin),
# Make sure no idle server lives forever wasting RAM
"--max_idle_secs=10",
"info",
"workspace",
]
process = subprocess.run(cmd, shell=False, check=True, capture_output=True, text=True)
return Path(process.stdout.strip())

0 comments on commit e89c3dc

Please sign in to comment.