-
Notifications
You must be signed in to change notification settings - Fork 7
[Feature] Add pics-export CLI command (#1092) #113
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
6 commits
Select commit
Hold shift + click to select a range
6ea2165
[Feature] Add pics-export CLI command (#1092)
rquidute 1f39848
Update pics-export for backend's 404-on-no-PICS change (#1092)
rquidute 68b7452
Pretty-print FastAPI error detail instead of raw dict repr (#1092)
rquidute d6197e1
Address review feedback: catch write OSError, document 404 (#1092)
rquidute e2acac9
Preserve field path (loc) in 422 validation error messages (#1092)
rquidute 73e246b
Fixes after generate_client script execution
rquidute 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,152 @@ | ||
| # | ||
| # Copyright (c) 2026 Project CHIP Authors | ||
| # | ||
| # 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. | ||
| # | ||
| """Tests for the `test-run-execution pics-export` command.""" | ||
|
|
||
| import os | ||
| from unittest.mock import Mock, patch | ||
|
|
||
| import pytest | ||
| from click.testing import CliRunner | ||
|
|
||
| from th_cli.api_lib_autogen import models as api_models | ||
| from th_cli.api_lib_autogen.exceptions import UnexpectedResponse | ||
| from th_cli.commands.test_run_execution import test_run_execution | ||
| from th_cli.exceptions import ConfigurationError | ||
|
|
||
|
|
||
| @pytest.mark.unit | ||
| @pytest.mark.cli | ||
| class TestPicsExportCommand: | ||
| """Test cases for the `test-run-execution pics-export` command.""" | ||
|
|
||
| def test_pics_export_writes_default_filename(self, cli_runner: CliRunner, mock_sync_apis: Mock) -> None: | ||
| """When no --output-file is given, the file is named after the execution title.""" | ||
| api = mock_sync_apis.test_run_executions_api | ||
| api.pics_export_api_v1_test_run_executions__id__pics_export_get.return_value = b"zip-bytes" | ||
| api.read_test_run_execution_api_v1_test_run_executions__id__get.return_value = api_models.TestRunExecution( | ||
| id=1, title="My Execution!", state=api_models.TestStateEnum.passed, project_id=1 | ||
| ) | ||
|
|
||
| with cli_runner.isolated_filesystem(): | ||
| with patch("th_cli.commands.test_run_execution.SyncApis", return_value=mock_sync_apis): | ||
| result = cli_runner.invoke(test_run_execution, ["pics-export", "--id", "1"]) | ||
|
|
||
| assert result.exit_code == 0 | ||
| assert os.path.exists("MyExecution-pics.zip") | ||
| with open("MyExecution-pics.zip", "rb") as f: | ||
| assert f.read() == b"zip-bytes" | ||
|
|
||
| api.pics_export_api_v1_test_run_executions__id__pics_export_get.assert_called_once_with(id=1) | ||
|
|
||
| def test_pics_export_writes_custom_output_file(self, cli_runner: CliRunner, mock_sync_apis: Mock) -> None: | ||
| """When --output-file is given, the export is written there and read_test_run_execution | ||
| is not called to derive a filename.""" | ||
| api = mock_sync_apis.test_run_executions_api | ||
| api.pics_export_api_v1_test_run_executions__id__pics_export_get.return_value = b"zip-bytes" | ||
|
|
||
| with cli_runner.isolated_filesystem(): | ||
| with patch("th_cli.commands.test_run_execution.SyncApis", return_value=mock_sync_apis): | ||
| result = cli_runner.invoke(test_run_execution, ["pics-export", "--id", "1", "--output-file", "out.zip"]) | ||
|
|
||
| assert result.exit_code == 0 | ||
| assert os.path.exists("out.zip") | ||
|
|
||
| api.read_test_run_execution_api_v1_test_run_executions__id__get.assert_not_called() | ||
|
|
||
| def test_pics_export_no_content(self, cli_runner: CliRunner, mock_sync_apis: Mock) -> None: | ||
| """If the API ever returns an empty-but-successful response, a message is | ||
| printed and no file is written (the backend normally 404s instead, see | ||
| test_pics_export_no_pics_used_api_error).""" | ||
| api = mock_sync_apis.test_run_executions_api | ||
| api.pics_export_api_v1_test_run_executions__id__pics_export_get.return_value = None | ||
|
|
||
| with patch("th_cli.commands.test_run_execution.SyncApis", return_value=mock_sync_apis): | ||
| result = cli_runner.invoke(test_run_execution, ["pics-export", "--id", "1"]) | ||
|
|
||
| assert result.exit_code == 0 | ||
| assert "No PICS content was returned for this test run execution." in result.output | ||
|
|
||
| def test_pics_export_no_pics_used_api_error(self, cli_runner: CliRunner, mock_sync_apis: Mock) -> None: | ||
| """When the execution used no PICS, the backend 404s with a JSON body | ||
| (as FastAPI does) and the CLI surfaces the plain detail message, | ||
| not the raw dict repr.""" | ||
| api = mock_sync_apis.test_run_executions_api | ||
| api.pics_export_api_v1_test_run_executions__id__pics_export_get.side_effect = UnexpectedResponse( | ||
| status_code=404, | ||
| content={"detail": "No PICS were used by this test run execution"}, | ||
| ) | ||
|
|
||
| with patch("th_cli.commands.test_run_execution.SyncApis", return_value=mock_sync_apis): | ||
| result = cli_runner.invoke(test_run_execution, ["pics-export", "--id", "1"]) | ||
|
|
||
| error_text = ( | ||
| "Error: Failed to fetch test run execution PICS export (Status: 404)" | ||
| " - No PICS were used by this test run execution" | ||
| ) | ||
| assert result.exit_code == 1 | ||
| assert error_text in result.output | ||
| assert "{" not in result.output | ||
|
|
||
| def test_pics_export_configuration_error(self, cli_runner: CliRunner) -> None: | ||
| """A ConfigurationError from get_client is surfaced to the user.""" | ||
| with patch( | ||
| "th_cli.commands.test_run_execution.get_client", | ||
| side_effect=ConfigurationError("Could not connect to server"), | ||
| ): | ||
| result = cli_runner.invoke(test_run_execution, ["pics-export", "--id", "1"]) | ||
|
|
||
| assert result.exit_code == 1 | ||
| assert "Error: Could not connect to server" in result.output | ||
|
|
||
| def test_pics_export_api_error(self, cli_runner: CliRunner, mock_sync_apis: Mock) -> None: | ||
| """An UnexpectedResponse from the API is surfaced to the user.""" | ||
| api = mock_sync_apis.test_run_executions_api | ||
| api.pics_export_api_v1_test_run_executions__id__pics_export_get.side_effect = UnexpectedResponse( | ||
| status_code=404, | ||
| content=b"Test run execution not found", | ||
| ) | ||
|
|
||
| with patch("th_cli.commands.test_run_execution.SyncApis", return_value=mock_sync_apis): | ||
| result = cli_runner.invoke(test_run_execution, ["pics-export", "--id", "999"]) | ||
|
|
||
| error_text = ( | ||
| "Error: Failed to fetch test run execution PICS export (Status: 404) - Test run execution not found" | ||
| ) | ||
| assert result.exit_code == 1 | ||
| assert error_text in result.output | ||
|
|
||
| def test_pics_export_requires_id(self, cli_runner: CliRunner) -> None: | ||
| """The --id parameter is required.""" | ||
| result = cli_runner.invoke(test_run_execution, ["pics-export"]) | ||
|
|
||
| assert result.exit_code != 0 | ||
| assert "Missing option" in result.output or "--id" in result.output | ||
|
|
||
| def test_pics_export_write_failure_raises_cli_error(self, cli_runner: CliRunner, mock_sync_apis: Mock) -> None: | ||
| """A file-write failure (e.g. unwritable directory) after a successful export | ||
| request is surfaced as a clean CLIError, not a raw traceback.""" | ||
| api = mock_sync_apis.test_run_executions_api | ||
| api.pics_export_api_v1_test_run_executions__id__pics_export_get.return_value = b"zip-bytes" | ||
|
|
||
| with patch("th_cli.commands.test_run_execution.SyncApis", return_value=mock_sync_apis): | ||
| with patch("builtins.open", side_effect=OSError("Permission denied")): | ||
| result = cli_runner.invoke( | ||
| test_run_execution, ["pics-export", "--id", "1", "--output-file", "/no/such/dir/out.zip"] | ||
| ) | ||
|
|
||
| assert result.exit_code == 1 | ||
| assert "Failed to write PICS export file '/no/such/dir/out.zip'" in result.output | ||
| assert "Permission denied" in result.output |
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.