Skip to content

Commit 81e905d

Browse files
committed
add bazelisk command
1 parent 98f98e3 commit 81e905d

File tree

2 files changed

+42
-3
lines changed

2 files changed

+42
-3
lines changed

.generator/cli.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import logging
1818
import os
1919
import sys
20+
import subprocess
2021

2122
logger = logging.getLogger()
2223

@@ -66,7 +67,31 @@ def handle_generate():
6667
f"failed to read {LIBRARIAN_DIR}/{GENERATE_REQUEST_FILE}"
6768
) from e
6869

69-
logger.info(json.dumps(request_data, indent=2))
70+
library_id = request_data.get("id")
71+
if not library_id:
72+
raise ValueError("Request file is missing required 'id' field.")
73+
74+
for api in request_data.get("apis", []):
75+
api_path = api.get("path")
76+
if api_path:
77+
try:
78+
query = f'filter("-py$", kind("rule", //{api_path}/...:*))'
79+
command = ["bazelisk", "query", query]
80+
result = subprocess.run(
81+
command, capture_output=True, text=True, check=True
82+
)
83+
84+
bazel_rule = result.stdout.strip()
85+
if not bazel_rule:
86+
raise ValueError(
87+
f"Bazel query `{query}` returned an empty bazel rule."
88+
)
89+
90+
logger.info(f"Found Bazel rule: {bazel_rule}")
91+
except Exception as e:
92+
raise ValueError(f"Bazelisk query `{query}` failed") from e
93+
94+
logger.info(json.dumps(request_data, indent=2))
7095

7196
# TODO(https://github.com/googleapis/librarian/issues/448): Implement generate command and update docstring.
7297
logger.info("'generate' command executed.")

.generator/test_cli.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import json
1818
import logging
1919

20-
from unittest.mock import mock_open
20+
from unittest.mock import mock_open, MagicMock
2121

2222
from cli import (
2323
_read_json_file,
@@ -60,16 +60,30 @@ def test_handle_configure_success(caplog, mock_generate_request_file):
6060
assert "'configure' command executed." in caplog.text
6161

6262

63-
def test_handle_generate_success(caplog, mock_generate_request_file):
63+
def test_handle_generate_success(caplog, mock_generate_request_file, mocker):
6464
"""
6565
Tests the successful execution path of handle_generate.
6666
"""
6767
caplog.set_level(logging.INFO)
6868

69+
mock_query_result = MagicMock(
70+
stdout="//google/cloud/language/v1:google-cloud-language-v1-py\n", returncode=0
71+
)
72+
73+
mock_subprocess = mocker.patch(
74+
"cli.subprocess.run", side_effect=[mock_query_result]
75+
)
76+
6977
handle_generate()
7078

79+
# captured = capsys.readouterr()
7180
assert "google-cloud-language" in caplog.text
81+
assert (
82+
"Found Bazel rule: //google/cloud/language/v1:google-cloud-language-v1-py"
83+
in caplog.text
84+
)
7285
assert "'generate' command executed." in caplog.text
86+
assert mock_subprocess.call_count == 1
7387

7488

7589
def test_handle_generate_fail(caplog):

0 commit comments

Comments
 (0)