Skip to content

Commit 7dfd7bf

Browse files
committed
address PR feedback
1 parent 5d48e80 commit 7dfd7bf

File tree

2 files changed

+36
-27
lines changed

2 files changed

+36
-27
lines changed

.generator/cli.py

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,64 +13,67 @@
1313
# limitations under the License.
1414

1515
import argparse
16-
import sys
1716
import json
18-
import os
1917
import logging
18+
import os
19+
import sys
2020

2121
logger = logging.getLogger()
2222

2323
LIBRARIAN = "/librarian"
2424

25+
2526
# Helper function that reads a json file path and returns the loaded json content.
26-
# Remove me.
2727
def _read_json_file(path):
28-
if not os.path.exists(path):
29-
raise FileNotFoundError(f"Request file not found at '{path}'")
30-
try:
31-
with open(path, 'r') as f:
32-
return json.load(f)
33-
except json.JSONDecodeError:
34-
raise ValueError(f"Invalid JSON in request file '{path}'")
35-
except IOError as e:
36-
raise IOError(f"Invalid JSON in request file '{path}': {e}")
28+
with open(path, "r") as f:
29+
return json.load(f)
3730

3831

3932
def handle_configure(dry_run=False):
4033
# TODO(https://github.com/googleapis/librarian/issues/466): Implement configure command.
4134
print("'configure' command executed.")
4235

36+
4337
def handle_generate(dry_run=False):
44-
# TODO(https://github.com/googleapis/librarian/issues/448): Implement generate command.
4538

4639
# Read a generate-request.json file
47-
request_path = f"{LIBRARIAN}/generate-request.json"
48-
try:
49-
request_data = _read_json_file(request_path)
50-
except Exception as e:
51-
logger.error(e)
52-
sys.exit(1)
40+
if not dry_run:
41+
request_path = f"{LIBRARIAN}/generate-request.json"
42+
try:
43+
request_data = _read_json_file(request_path)
44+
except Exception as e:
45+
logger.error(e)
46+
sys.exit(1)
47+
48+
# Print the data:
49+
print(json.dumps(request_data, indent=2))
5350

54-
# Print the data:
55-
print(json.dumps(request_data, indent=2))
51+
# TODO(https://github.com/googleapis/librarian/issues/448): Implement generate command.
5652
print("'generate' command executed.")
5753

54+
5855
def handle_build(dry_run=False):
5956
# TODO(https://github.com/googleapis/librarian/issues/450): Implement build command.
6057
print("'build' command executed.")
6158

6259

6360
if __name__ == "__main__":
6461
parser = argparse.ArgumentParser(description="A simple CLI tool.")
65-
subparsers = parser.add_subparsers(dest="command", required=True, help="Available commands")
62+
subparsers = parser.add_subparsers(
63+
dest="command", required=True, help="Available commands"
64+
)
6665

6766
# Define commands
6867
for command_name, help_text in [
6968
("configure", "Onboard a new library or an api path to Librarian workflow."),
7069
("generate", "generate a python client for an API."),
71-
("build", "Run unit tests via nox for the generated library.")
70+
("build", "Run unit tests via nox for the generated library."),
7271
]:
73-
handler_map = {"configure": handle_configure, "generate": handle_generate, "build": handle_build}
72+
handler_map = {
73+
"configure": handle_configure,
74+
"generate": handle_generate,
75+
"build": handle_build,
76+
}
7477
parser_cmd = subparsers.add_parser(command_name, help=help_text)
7578
parser_cmd.set_defaults(func=handler_map[command_name])
7679

.generator/test_cli.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,17 @@ def test_handle_configure_dry_run():
2222
# This is a simple test to ensure that the dry run command succeeds.
2323
handle_configure(dry_run=True)
2424

25+
2526
def test_handle_generate_dry_run():
2627
# This is a simple test to ensure that the dry run command succeeds.
2728
handle_generate(dry_run=True)
2829

30+
2931
def test_handle_build_dry_run():
3032
# This is a simple test to ensure that the dry run command succeeds.
3133
handle_build(dry_run=True)
3234

35+
3336
def test_read_valid_json(mocker):
3437
"""Tests reading a valid JSON file."""
3538
mock_content = '{"key": "value"}'
@@ -38,28 +41,31 @@ def test_read_valid_json(mocker):
3841
result = _read_json_file("fake/path.json")
3942
assert result == {"key": "value"}
4043

44+
4145
def test_file_not_found(mocker):
4246
"""Tests behavior when the file does not exist."""
4347
mocker.patch("os.path.exists", return_value=False)
4448

4549
with pytest.raises(FileNotFoundError):
4650
_read_json_file("non/existent/path.json")
4751

52+
4853
def test_invalid_json(mocker):
4954
"""Tests reading a file with malformed JSON."""
5055
mock_content = '{"key": "value",}'
5156
mocker.patch("os.path.exists", return_value=True)
5257
mocker.patch("builtins.open", mocker.mock_open(read_data=mock_content))
5358

54-
with pytest.raises(ValueError, match="Invalid JSON"):
59+
with pytest.raises(json.JSONDecodeError):
5560
_read_json_file("fake/path.json")
5661

62+
5763
def test_io_error_on_read(mocker):
5864
"""Tests for a generic IOError."""
5965
mocker.patch("os.path.exists", return_value=True)
6066
mocked_open = mocker.mock_open()
61-
mocked_open.side_effect = IOError("Permission denied")
67+
mocked_open.side_effect = IOError("permission denied")
6268
mocker.patch("builtins.open", mocked_open)
6369

64-
with pytest.raises(IOError, match="Permission denied"):
70+
with pytest.raises(IOError):
6571
_read_json_file("fake/path.json")

0 commit comments

Comments
 (0)