Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 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`.

### Project Edit

Run `th-cli project edit --id {id}` to interactively edit a project's config in your
Expand Down
69 changes: 69 additions & 0 deletions tests/test_project_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 "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 still prints the header."""
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 "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."""
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
42 changes: 42 additions & 0 deletions th_cli/commands/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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

# 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)


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(
Expand Down
Loading