Skip to content

Commit 7b60921

Browse files
committed
feat: read content of generate request
1 parent b886e1d commit 7b60921

File tree

4 files changed

+95
-0
lines changed

4 files changed

+95
-0
lines changed

.generator/cli.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,44 @@
1414

1515
import argparse
1616
import sys
17+
import json
18+
import os
19+
import logging
20+
21+
logger = logging.getLogger()
22+
23+
LIBRARIAN = "/librarian"
24+
25+
# Helper function that reads a json file path and returns the loaded json content.
26+
def _read_json_file(path):
27+
if not os.path.exists(path):
28+
raise FileNotFoundError(f"Request file not found at '{path}'")
29+
try:
30+
with open(path, 'r') as f:
31+
return json.load(f)
32+
except json.JSONDecodeError:
33+
raise ValueError(f"Invalid JSON in request file '{path}'")
34+
except IOError as e:
35+
raise IOError(f"Invalid JSON in request file '{path}': {e}")
36+
1737

1838
def handle_configure(dry_run=False):
1939
# TODO(https://github.com/googleapis/librarian/issues/466): Implement configure command.
2040
print("'configure' command executed.")
2141

2242
def handle_generate(dry_run=False):
2343
# TODO(https://github.com/googleapis/librarian/issues/448): Implement generate command.
44+
45+
# Read a generate-request.json file
46+
request_path = f"{LIBRARIAN}/generate-request.json"
47+
try:
48+
request_data = _read_json_file(request_path)
49+
except Exception as e:
50+
logger.error(e)
51+
sys.exit(1)
52+
53+
# Print the data:
54+
print(json.dumps(request_data, indent=2))
2455
print("'generate' command executed.")
2556

2657
def handle_build(dry_run=False):

.generator/requirements-test.in

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Copyright 2025 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
pytest
16+
pytest-mock

.generator/test_cli.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,41 @@ def test_handle_generate_dry_run():
2626
def test_handle_build_dry_run():
2727
# This is a simple test to ensure that the dry run command succeeds.
2828
handle_build(dry_run=True)
29+
import pytest
30+
import json
31+
32+
from cli import _read_json_file
33+
34+
def test_read_valid_json(mocker):
35+
"""Tests reading a valid JSON file."""
36+
mock_content = '{"key": "value"}'
37+
mocker.patch("os.path.exists", return_value=True)
38+
mocker.patch("builtins.open", mocker.mock_open(read_data=mock_content))
39+
result = _read_json_file("fake/path.json")
40+
assert result == {"key": "value"}
41+
42+
def test_file_not_found(mocker):
43+
"""Tests behavior when the file does not exist."""
44+
mocker.patch("os.path.exists", return_value=False)
45+
46+
with pytest.raises(FileNotFoundError):
47+
_read_json_file("non/existent/path.json")
48+
49+
def test_invalid_json(mocker):
50+
"""Tests reading a file with malformed JSON."""
51+
mock_content = '{"key": "value",}'
52+
mocker.patch("os.path.exists", return_value=True)
53+
mocker.patch("builtins.open", mocker.mock_open(read_data=mock_content))
54+
55+
with pytest.raises(ValueError, match="Invalid JSON"):
56+
_read_json_file("fake/path.json")
57+
58+
def test_io_error_on_read(mocker):
59+
"""Tests for a generic IOError."""
60+
mocker.patch("os.path.exists", return_value=True)
61+
mocked_open = mocker.mock_open()
62+
mocked_open.side_effect = IOError("Permission denied")
63+
mocker.patch("builtins.open", mocked_open)
64+
65+
with pytest.raises(IOError, match="Permission denied"):
66+
_read_json_file("fake/path.json")

.librarian/generate-request.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"id": "google-cloud-language",
3+
"apis": [
4+
{
5+
"path": "google/cloud/language/v1",
6+
"service_config": "language.yaml"
7+
}
8+
]
9+
}
10+

0 commit comments

Comments
 (0)