-
Notifications
You must be signed in to change notification settings - Fork 1.6k
feat: read content of generate request #14113
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
fbb164e
feat: read content of generate request
ohmayr 0b2264f
test commit
ohmayr 456b3f7
add cloud build config
ohmayr 5d48e80
address build failure
ohmayr 7dfd7bf
address PR feedback
ohmayr ac7f6f2
address PR feedback
ohmayr cca01f6
address PR feedback
ohmayr 1130c70
address PR feedback
ohmayr 98fb3c9
clean up unit tests
ohmayr 647d70e
simplify testing logic
ohmayr 75a0931
clarify storage bucket comment
ohmayr f0e8bda
change print statements to logs
ohmayr d785d92
address PR feedback
ohmayr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| # Copyright 2025 Google LLC | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| pytest | ||
| pytest-mock |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,17 +12,105 @@ | |
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| from cli import handle_generate, handle_build, handle_configure | ||
| import os | ||
| import pytest | ||
| import json | ||
| import logging | ||
|
|
||
| from unittest.mock import mock_open | ||
|
|
||
| def test_handle_configure_dry_run(): | ||
| # This is a simple test to ensure that the dry run command succeeds. | ||
| handle_configure(dry_run=True) | ||
| from cli import ( | ||
| _read_json_file, | ||
| handle_generate, | ||
| handle_build, | ||
| handle_configure, | ||
| LIBRARIAN_DIR, | ||
| GENERATE_REQUEST_FILE, | ||
| ) | ||
|
|
||
| def test_handle_generate_dry_run(): | ||
| # This is a simple test to ensure that the dry run command succeeds. | ||
| handle_generate(dry_run=True) | ||
|
|
||
| def test_handle_build_dry_run(): | ||
| # This is a simple test to ensure that the dry run command succeeds. | ||
| handle_build(dry_run=True) | ||
| @pytest.fixture | ||
| def mock_generate_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/generate-request.json | ||
| request_path = f"{LIBRARIAN_DIR}/{GENERATE_REQUEST_FILE}" | ||
| request_dir = tmp_path / os.path.dirname(request_path) | ||
| request_dir.mkdir() | ||
| request_file = request_dir / os.path.basename(request_path) | ||
|
|
||
| request_content = { | ||
| "id": "google-cloud-language", | ||
| "apis": [{"path": "google/cloud/language/v1"}], | ||
| } | ||
| 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 | ||
|
|
||
|
|
||
| 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_handle_generate_success(caplog, mock_generate_request_file): | ||
| """ | ||
| Tests the successful execution path of handle_generate. | ||
| """ | ||
| caplog.set_level(logging.INFO) | ||
|
|
||
| handle_generate() | ||
|
|
||
| assert "google-cloud-language" in caplog.text | ||
| assert "'generate' command executed." in caplog.text | ||
|
|
||
|
|
||
| def test_handle_generate_fail(caplog): | ||
| """ | ||
| Tests the failed to read `librarian/generate-request.json` file in handle_generates. | ||
| """ | ||
| with pytest.raises(ValueError): | ||
| handle_generate() | ||
|
|
||
|
|
||
| def test_handle_build_success(caplog, mock_generate_request_file): | ||
| """ | ||
| Tests the successful execution path of handle_build. | ||
| """ | ||
| caplog.set_level(logging.INFO) | ||
|
|
||
| handle_build() | ||
|
|
||
| assert "'build' command executed." in caplog.text | ||
|
|
||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add a |
||
| def test_read_valid_json(mocker): | ||
| """Tests reading a valid JSON file.""" | ||
| mock_content = '{"key": "value"}' | ||
| mocker.patch("builtins.open", mocker.mock_open(read_data=mock_content)) | ||
| result = _read_json_file("fake/path.json") | ||
| assert result == {"key": "value"} | ||
|
|
||
|
|
||
| def test_file_not_found(mocker): | ||
| """Tests behavior when the file does not exist.""" | ||
| mocker.patch("builtins.open", side_effect=FileNotFoundError("No such file")) | ||
|
|
||
| with pytest.raises(FileNotFoundError): | ||
| _read_json_file("non/existent/path.json") | ||
|
|
||
|
|
||
| def test_invalid_json(mocker): | ||
| """Tests reading a file with malformed JSON.""" | ||
| mock_content = '{"key": "value",}' | ||
| mocker.patch("builtins.open", mocker.mock_open(read_data=mock_content)) | ||
|
|
||
| with pytest.raises(json.JSONDecodeError): | ||
| _read_json_file("fake/path.json") | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| # Copyright 2025 Google LLC | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| steps: | ||
ohmayr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| # This step builds the Docker image. | ||
| - name: 'gcr.io/cloud-builders/docker' | ||
ohmayr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| args: | ||
| - 'build' | ||
| - '-t' | ||
| - 'gcr.io/$PROJECT_ID/python-librarian-generator:latest' | ||
| - '-f' | ||
| - '.generator/Dockerfile' | ||
ohmayr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| - '.' | ||
|
|
||
| # This section automatically create a storage bucket for storing docker build logs. | ||
| options: | ||
| default_logs_bucket_behavior: REGIONAL_USER_OWNED_BUCKET | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The exceptions specify the file path? Cool.