From 594e258f231c5817d9733839dd1c1139e396b34e Mon Sep 17 00:00:00 2001 From: Romulo Quidute Filho Date: Tue, 10 Nov 2026 09:39:13 -0300 Subject: [PATCH 1/2] Add 'project applicable-tests' command to list PICS-filtered test cases (#1206) Calls GET /api/v1/projects/{id}/applicable_test_cases, which the autogen client already exposed but no command used. Prints sorted test IDs one per line (count on stderr) or the raw response with --json. --- README.md | 6 +++ tests/test_project_commands.py | 69 ++++++++++++++++++++++++++++++++++ th_cli/commands/project.py | 42 +++++++++++++++++++++ 3 files changed, 117 insertions(+) diff --git a/README.md b/README.md index 363f71f..a375cd0 100644 --- a/README.md +++ b/README.md @@ -189,6 +189,12 @@ Run `th-cli project delete --id {id}` to delete a project. Run `th-cli project update --id {id} --config {config file}` to update a project. Both parameters are required. Config must be a full test environment config file. +### Project Applicable Tests + +Run `th-cli project applicable-tests --id {id}` to list the test cases that apply to a project, based on its PICS configuration and `dmp_test_skip` setting. Test IDs are printed one per line (sorted) on stdout and the count goes to stderr, so the output can be piped, e.g. `th-cli project applicable-tests --id 5 | grep TC-ACL`. For the raw JSON response, add `--json`. + +This differs from `th-cli available-tests`, which lists every test case known to the Test Harness regardless of project: a test that requires a PICS item disabled in the project appears in `available-tests` but not in `project applicable-tests`. + ### Project Edit Run `th-cli project edit --id {id}` to interactively edit a project's config in your diff --git a/tests/test_project_commands.py b/tests/test_project_commands.py index 0f7c91e..07f7e7f 100644 --- a/tests/test_project_commands.py +++ b/tests/test_project_commands.py @@ -1280,3 +1280,72 @@ def test_logs_help_message(self, cli_runner: CliRunner) -> None: assert "--id" in result.output assert "--grouped" in result.output assert "--output-file" in result.output + + +@pytest.mark.unit +@pytest.mark.cli +class TestApplicableTestsCommand: + """Test cases for the project applicable-tests command.""" + + @staticmethod + def _mock_response(mock_sync_apis: Mock, test_cases: list[str]) -> Mock: + api = mock_sync_apis.projects_api.applicable_test_cases_api_v1_projects__id__applicable_test_cases_get + api.return_value = api_models.PICSApplicableTestCases(test_cases=test_cases) + return api + + def test_applicable_tests_lists_sorted_ids(self, cli_runner: CliRunner, mock_sync_apis: Mock) -> None: + """Test IDs are printed one per line, sorted.""" + api = self._mock_response(mock_sync_apis, ["TC-OO-2.1", "TC-ACL-2.1"]) + + with patch("th_cli.commands.project.SyncApis", return_value=mock_sync_apis): + result = cli_runner.invoke(project, ["applicable-tests", "--id", "3"]) + + assert result.exit_code == 0 + assert result.stdout.splitlines() == ["TC-ACL-2.1", "TC-OO-2.1"] + assert "2 applicable test case(s) for project 3" in result.stderr + api.assert_called_once_with(id=3) + + def test_applicable_tests_empty(self, cli_runner: CliRunner, mock_sync_apis: Mock) -> None: + """An empty result prints no IDs and reports a zero count.""" + self._mock_response(mock_sync_apis, []) + + with patch("th_cli.commands.project.SyncApis", return_value=mock_sync_apis): + result = cli_runner.invoke(project, ["applicable-tests", "--id", "3"]) + + assert result.exit_code == 0 + assert result.stdout == "" + assert "0 applicable test case(s)" in result.stderr + + def test_applicable_tests_json(self, cli_runner: CliRunner, mock_sync_apis: Mock) -> None: + """--json prints the raw response.""" + self._mock_response(mock_sync_apis, ["TC-ACL-2.1"]) + + with patch("th_cli.commands.project.SyncApis", return_value=mock_sync_apis): + result = cli_runner.invoke(project, ["applicable-tests", "--id", "3", "--json"]) + + assert result.exit_code == 0 + assert json.loads(result.stdout) == {"test_cases": ["TC-ACL-2.1"]} + + def test_applicable_tests_api_error(self, cli_runner: CliRunner, mock_sync_apis: Mock) -> None: + """A backend error is reported with the project ID.""" + mock_sync_apis.projects_api.applicable_test_cases_api_v1_projects__id__applicable_test_cases_get.side_effect = ( + UnexpectedResponse(status_code=404, content=b"Not Found") + ) + + with patch("th_cli.commands.project.SyncApis", return_value=mock_sync_apis): + result = cli_runner.invoke(project, ["applicable-tests", "--id", "99"]) + + assert result.exit_code == 1 + assert "Failed to get applicable test cases for project ID '99' (Status: 404)" in result.output + + def test_applicable_tests_requires_id(self, cli_runner: CliRunner) -> None: + result = cli_runner.invoke(project, ["applicable-tests"]) + + assert result.exit_code != 0 + + def test_applicable_tests_help_message(self, cli_runner: CliRunner) -> None: + result = cli_runner.invoke(project, ["applicable-tests", "--help"]) + + assert result.exit_code == 0 + assert "--id" in result.output + assert "--json" in result.output diff --git a/th_cli/commands/project.py b/th_cli/commands/project.py index 7861540..3c22e02 100644 --- a/th_cli/commands/project.py +++ b/th_cli/commands/project.py @@ -281,6 +281,31 @@ def import_project(file: str) -> None: _import_project(sync_apis, file) +# Click command to list the test cases applicable to a project +@project.command( + "applicable-tests", + short_help=colorize_help("List test cases applicable to a project (filtered by its PICS)"), +) +@click.option( + "--id", + "-i", + type=int, + required=True, + help=colorize_help("Project ID"), +) +@click.option( + "--json", + "json_output", + is_flag=True, + default=False, + help=colorize_help("Print the raw JSON response from the backend"), +) +def applicable_tests(id: int, json_output: bool) -> None: + """List the test cases that apply to a project based on its PICS and DMP test skip configuration""" + with get_sync_apis("applicable-tests") as sync_apis: + _applicable_tests(sync_apis, id, json_output) + + # Click command to download all logs for a project @project.command( "logs", @@ -685,6 +710,23 @@ def _import_project(sync_apis: SyncApis, file: str) -> None: handle_api_error(e, f"import project from '{file}'") +def _applicable_tests(sync_apis: SyncApis, id: int, json_output: bool) -> None: + """List the test cases applicable to a project""" + try: + response = sync_apis.projects_api.applicable_test_cases_api_v1_projects__id__applicable_test_cases_get(id=id) + except UnexpectedResponse as e: + handle_api_error(e, f"get applicable test cases for project ID '{id}'") + + if json_output: + __print_json(response) + return + + # Count goes to stderr so the test IDs on stdout stay clean for piping (grep, wc -l, ...) + click.echo(f"{len(response.test_cases)} applicable test case(s) for project {id}", err=True) + for test_id in sorted(response.test_cases): + click.echo(test_id) + + def _download_project_logs(sync_apis: SyncApis, id: int, grouped: bool, output_file: str | None) -> None: """Download all logs for a project as a single zip archive""" click.echo( From 08c72b8d00420f985ade3b39859daefa174e72a1 Mon Sep 17 00:00:00 2001 From: Romulo Quidute Filho Date: Tue, 10 Nov 2026 09:52:43 -0300 Subject: [PATCH 2/2] project applicable-tests: print 'Applicable test case(s) for project :' header --- README.md | 2 +- tests/test_project_commands.py | 6 +++--- th_cli/commands/project.py | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index a375cd0..2982513 100644 --- a/README.md +++ b/README.md @@ -191,7 +191,7 @@ Run `th-cli project update --id {id} --config {config file}` to update a project ### Project Applicable Tests -Run `th-cli project applicable-tests --id {id}` to list the test cases that apply to a project, based on its PICS configuration and `dmp_test_skip` setting. Test IDs are printed one per line (sorted) on stdout and the count goes to stderr, so the output can be piped, e.g. `th-cli project applicable-tests --id 5 | grep TC-ACL`. For the raw JSON response, add `--json`. +Run `th-cli project applicable-tests --id {id}` to list the test cases that apply to a project, based on its PICS configuration and `dmp_test_skip` setting. Test IDs are printed one per line (sorted) on stdout and a header line goes to stderr, so the output can be piped, e.g. `th-cli project applicable-tests --id 5 | grep TC-ACL`. For the raw JSON response, add `--json`. This differs from `th-cli available-tests`, which lists every test case known to the Test Harness regardless of project: a test that requires a PICS item disabled in the project appears in `available-tests` but not in `project applicable-tests`. diff --git a/tests/test_project_commands.py b/tests/test_project_commands.py index 07f7e7f..247428e 100644 --- a/tests/test_project_commands.py +++ b/tests/test_project_commands.py @@ -1302,11 +1302,11 @@ def test_applicable_tests_lists_sorted_ids(self, cli_runner: CliRunner, mock_syn assert result.exit_code == 0 assert result.stdout.splitlines() == ["TC-ACL-2.1", "TC-OO-2.1"] - assert "2 applicable test case(s) for project 3" in result.stderr + assert "Applicable test case(s) for project 3:" in result.stderr api.assert_called_once_with(id=3) def test_applicable_tests_empty(self, cli_runner: CliRunner, mock_sync_apis: Mock) -> None: - """An empty result prints no IDs and reports a zero count.""" + """An empty result prints no IDs and still prints the header.""" self._mock_response(mock_sync_apis, []) with patch("th_cli.commands.project.SyncApis", return_value=mock_sync_apis): @@ -1314,7 +1314,7 @@ def test_applicable_tests_empty(self, cli_runner: CliRunner, mock_sync_apis: Moc assert result.exit_code == 0 assert result.stdout == "" - assert "0 applicable test case(s)" in result.stderr + assert "Applicable test case(s) for project 3:" in result.stderr def test_applicable_tests_json(self, cli_runner: CliRunner, mock_sync_apis: Mock) -> None: """--json prints the raw response.""" diff --git a/th_cli/commands/project.py b/th_cli/commands/project.py index 3c22e02..283846d 100644 --- a/th_cli/commands/project.py +++ b/th_cli/commands/project.py @@ -721,8 +721,8 @@ def _applicable_tests(sync_apis: SyncApis, id: int, json_output: bool) -> None: __print_json(response) return - # Count goes to stderr so the test IDs on stdout stay clean for piping (grep, wc -l, ...) - click.echo(f"{len(response.test_cases)} applicable test case(s) for project {id}", err=True) + # Header goes to stderr so the test IDs on stdout stay clean for piping (grep, wc -l, ...) + click.echo(f"Applicable test case(s) for project {id}:", err=True) for test_id in sorted(response.test_cases): click.echo(test_id)