Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion .generator/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def _determine_bazel_rule(api_path):
str: The discovered Bazel rule.

Raises:
Exception: If the subprocess call fails or returns an empty result.
ValueError: If the subprocess call fails or returns an empty result.
"""
logger.info(f"Determining Bazel rule for api_path: '{api_path}'")
try:
Expand All @@ -82,6 +82,30 @@ def _determine_bazel_rule(api_path):
raise ValueError(f"Bazelisk query `{query}` failed") from e


def _build_bazel_target(bazel_rule):
"""Executes `bazelisk build` on a given Bazel rule.

Args:
bazel_rule (str): The Bazel rule to build.

Raises:
ValueError: If the subprocess call fails.
"""
logger.info(f"Executing build for rule: {bazel_rule}")
try:
command = ["bazelisk", "build", bazel_rule]
subprocess.run(
command,
cwd=f"{SOURCE_DIR}/googleapis",
capture_output=True,
text=True,
check=True,
)
logger.info(f"Bazel build for {bazel_rule} rule completed successfully.")
except Exception as e:
raise ValueError(f"Bazel build for {bazel_rule} rule failed.") from e


def handle_generate():
"""The main coordinator for the code generation process.

Expand All @@ -104,6 +128,7 @@ def handle_generate():
api_path = api.get("path")
if api_path:
bazel_rule = _determine_bazel_rule(api_path)
_build_bazel_target(bazel_rule)

logger.info(json.dumps(request_data, indent=2))
except Exception as e:
Expand Down
25 changes: 25 additions & 0 deletions .generator/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from cli import (
_read_json_file,
_determine_bazel_rule,
_build_bazel_target,
handle_generate,
handle_build,
handle_configure,
Expand Down Expand Up @@ -78,6 +79,29 @@ def test_determine_bazel_rule_success(mocker, caplog):
assert "Found Bazel rule" in caplog.text


def test_build_bazel_target_success(mocker, caplog):
"""
Tests that the build helper logs success when the command runs correctly.
"""
caplog.set_level(logging.INFO)
mocker.patch("cli.subprocess.run", return_value=MagicMock(returncode=0))
_build_bazel_target("mock/bazel:rule")
assert "Bazel build for mock/bazel:rule rule completed successfully" in caplog.text


def test_build_bazel_target_fails(mocker, caplog):
"""
Tests that ValueError is raised if the subprocess command fails.
"""
caplog.set_level(logging.ERROR)
mocker.patch(
"cli.subprocess.run",
side_effect=subprocess.CalledProcessError(1, "cmd", stderr="Build failed"),
)
with pytest.raises(ValueError):
_build_bazel_target("mock/bazel:rule")


def test_determine_bazel_rule_command_fails(mocker, caplog):
"""
Tests that an exception is raised if the subprocess command fails.
Expand All @@ -103,6 +127,7 @@ def test_handle_generate_success(caplog, mock_generate_request_file, mocker):
mock_determine_rule = mocker.patch(
"cli._determine_bazel_rule", return_value="mock-rule"
)
mock_build_target = mocker.patch("cli._build_bazel_target")

handle_generate()

Expand Down
Loading