Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
24 changes: 23 additions & 1 deletion .generator/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@

BUILD_REQUEST_FILE = "build-request.json"
GENERATE_REQUEST_FILE = "generate-request.json"
CONFIGURE_REQUEST_FILE = "configure-response.json"
RELEASE_INIT_REQUEST_FILE = "release-init-request.json"
STATE_YAML_FILE = "state.yaml"

Expand Down Expand Up @@ -144,7 +145,28 @@ def handle_configure(
Raises:
ValueError: If configuring a new library fails.
"""
# TODO(https://github.com/googleapis/librarian/issues/466): Implement configure command and update docstring.
try:
# configure-request.json contains the library definitions.
request_data = _read_json_file(f"{librarian}/{CONFIGURE_REQUEST_FILE}")
for library_config in request_data.get("libraries", []):
is_new_library_or_api = False
if "apis" in library_config:
for api in library_config["apis"]:
if api.get("status") == "new":
is_new_library_or_api = True
# Delete the status field since we don't need to include it in `configure-response.json`.
del api["status"]

if is_new_library_or_api:
library_id = _get_library_id(library_config)
# TODO: Add missing information for the configured library.
request_data[library_id] = library_config

# Write the new library configuration to configure-response.json.
_write_json_file(f"{librarian}/configure-response.json", request_data[library_id])

except Exception as e:
raise ValueError("Configuring a new library failed.") from e
logger.info("'configure' command executed.")


Expand Down
36 changes: 25 additions & 11 deletions .generator/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
from cli import (
GENERATE_REQUEST_FILE,
BUILD_REQUEST_FILE,
CONFIGURE_REQUEST_FILE,
RELEASE_INIT_REQUEST_FILE,
SOURCE_DIR,
STATE_YAML_FILE,
Expand Down Expand Up @@ -160,6 +161,30 @@ def mock_build_request_file(tmp_path, monkeypatch):
return request_file


@pytest.fixture
def mock_configure_request_file(tmp_path, monkeypatch):
"""Creates the mock request file at the correct path inside a temp dir."""
# Create the path as expected by the script: .librarian/configure-request.json
request_path = f"{LIBRARIAN_DIR}/{CONFIGURE_REQUEST_FILE}"
request_dir = tmp_path / os.path.dirname(request_path)
request_dir.mkdir(parents=True, exist_ok=True)
request_file = request_dir / os.path.basename(request_path)

request_content = {
"libraries": [
{
"id": "google-cloud-language",
"apis": [{"path": "google/cloud/language/v1", "status": "new"}],
}
]
}
request_file.write_text(json.dumps(request_content))

# Change the current working directory to the temp path for the test.
monkeypatch.chdir(tmp_path)
return request_file


@pytest.fixture
def mock_build_bazel_file(tmp_path, monkeypatch):
"""Creates the mock BUILD.bazel file at the correct path inside a temp dir."""
Expand Down Expand Up @@ -261,17 +286,6 @@ def test_get_library_id_empty_id():
_get_library_id(request_data)


def test_handle_configure_success(caplog, mock_generate_request_file):
"""
Tests the successful execution path of handle_configure.
"""
caplog.set_level(logging.INFO)

handle_configure()

assert "'configure' command executed." in caplog.text


def test_run_post_processor_success(mocker, caplog):
"""
Tests that the post-processor helper calls the correct command.
Expand Down
Loading