Skip to content

Commit 1283a48

Browse files
committed
feat: run post processor in cli
1 parent 4e034a5 commit 1283a48

File tree

2 files changed

+55
-3
lines changed

2 files changed

+55
-3
lines changed

.generator/cli.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,26 @@ def _locate_and_extract_artifact(bazel_rule: str, library_id: str):
153153
) from e
154154

155155

156+
def _run_post_processor():
157+
"""Runs the synthtool post-processor on the output directory.
158+
159+
Raises:
160+
ValueError: If the subprocess call fails.
161+
"""
162+
try:
163+
import synthtool
164+
from synthtool import gcp
165+
166+
logger.info("Running Python post-processor...")
167+
command = ["python3", "-m", "synthtool.languages.python_mono_repo"]
168+
os.chdir(OUTPUT_DIR)
169+
subprocess.run(command, cwd=OUTPUT_DIR, text=True, check=True)
170+
logger.info("Python post-processor ran successfully.")
171+
172+
except Exception as e:
173+
raise ValueError("Python post-processor failed...") from e
174+
175+
156176
def handle_generate():
157177
"""The main coordinator for the code generation process.
158178
@@ -164,8 +184,8 @@ def handle_generate():
164184
ValueError: If the `generate-request.json` file is not found or read.
165185
"""
166186

167-
# Read a generate-request.json file
168187
try:
188+
# Read a generate-request.json file
169189
request_data = _read_json_file(f"{LIBRARIAN_DIR}/{GENERATE_REQUEST_FILE}")
170190
library_id = request_data.get("id")
171191
if not library_id:
@@ -177,8 +197,8 @@ def handle_generate():
177197
bazel_rule = _determine_bazel_rule(api_path)
178198
_build_bazel_target(bazel_rule)
179199
_locate_and_extract_artifact(bazel_rule, library_id)
200+
_run_post_processor()
180201

181-
logger.info(json.dumps(request_data, indent=2))
182202
except Exception as e:
183203
raise ValueError("Generation failed.") from e
184204

.generator/test_cli.py

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
_determine_bazel_rule,
2626
_build_bazel_target,
2727
_locate_and_extract_artifact,
28+
_run_post_processor,
2829
handle_generate,
2930
handle_build,
3031
handle_configure,
@@ -165,6 +166,37 @@ def test_locate_and_extract_artifact_fails(mocker, caplog):
165166
)
166167

167168

169+
def test_run_post_processor_success(mocker, caplog):
170+
"""
171+
Tests that the post-processor helper calls the correct command.
172+
"""
173+
caplog.set_level(logging.INFO)
174+
mocker.patch("cli.synthtool", create=True)
175+
mock_chdir = mocker.patch("cli.os.chdir")
176+
mock_subprocess = mocker.patch("cli.subprocess.run")
177+
178+
_run_post_processor()
179+
180+
mock_subprocess.assert_called_once()
181+
182+
assert mock_subprocess.call_args.kwargs["cwd"] == "output"
183+
assert "Python post-processor ran successfully." in caplog.text
184+
185+
186+
def test_locate_and_extract_artifact_fails(mocker, caplog):
187+
"""
188+
Tests that an exception is raised if the subprocess command fails.
189+
"""
190+
caplog.set_level(logging.INFO)
191+
mocker.patch(
192+
"cli.subprocess.run",
193+
side_effect=subprocess.CalledProcessError(1, "cmd", stderr="Python error"),
194+
)
195+
196+
with pytest.raises(ValueError):
197+
_run_post_processor()
198+
199+
168200
def test_handle_generate_success(caplog, mock_generate_request_file, mocker):
169201
"""
170202
Tests the successful execution path of handle_generate.
@@ -175,8 +207,8 @@ def test_handle_generate_success(caplog, mock_generate_request_file, mocker):
175207
"cli._determine_bazel_rule", return_value="mock-rule"
176208
)
177209
mock_build_target = mocker.patch("cli._build_bazel_target")
178-
179210
mock_locate_and_extract_artifact = mocker.patch("cli._locate_and_extract_artifact")
211+
mock_run_post_processor = mocker.patch("cli._run_post_processor")
180212

181213
handle_generate()
182214

0 commit comments

Comments
 (0)