diff --git a/tests/test_colorize.py b/tests/test_colorize.py index 0d7546e..56f1ef0 100644 --- a/tests/test_colorize.py +++ b/tests/test_colorize.py @@ -15,12 +15,14 @@ # """Unit tests for th_cli/colorize.py.""" +import click import pytest from th_cli.colorize import ( ColorConfig, HierarchyEnum, TextTypeEnum, + colorize_change, colorize_cmd_help, colorize_dump, colorize_error, @@ -436,3 +438,35 @@ def test_non_empty_with_colors_enabled(self): set_colors_enabled(True) result = italic("hello") assert "hello" in result + + +# --------------------------------------------------------------------------- +# colorize_change +# --------------------------------------------------------------------------- + + +@pytest.mark.unit +class TestColorizeChange: + def test_plain_when_colors_disabled(self): + set_colors_enabled(False) + assert colorize_change("added", " + TC_A_1_1.py") == " + TC_A_1_1.py" + + @pytest.mark.parametrize("kind", ["added", "modified", "removed"]) + def test_styled_text_keeps_the_content_when_colors_enabled(self, kind): + set_colors_enabled(True) + result = colorize_change(kind, " ~ TC_A_1_1.py") + assert result != " ~ TC_A_1_1.py" + assert click.unstyle(result) == " ~ TC_A_1_1.py" + + def test_each_kind_has_its_own_color(self): + set_colors_enabled(True) + results = {kind: colorize_change(kind, "x") for kind in ("added", "modified", "removed")} + assert len(set(results.values())) == 3 + + def test_get_change_color(self): + cfg = ColorConfig() + assert cfg.get_change_color("added") == "green" + assert cfg.get_change_color("MODIFIED") == "yellow" + assert cfg.get_change_color("removed") == "red" + assert cfg.get_change_color("unknown") == "white" + diff --git a/tests/test_rescan_tests.py b/tests/test_rescan_tests.py index a61f905..d7dd5a0 100644 --- a/tests/test_rescan_tests.py +++ b/tests/test_rescan_tests.py @@ -27,6 +27,19 @@ from th_cli.exceptions import ConfigurationError +def _rescan_response(test_collections: api_models.TestCollections, **changes: object) -> api_models.TestCollections: + changes_by_kind: dict[str, object] = {"added": [], "modified": [], "removed": [], "errors": []} + changes_by_kind.update(changes) + return api_models.TestCollections( + test_collections=test_collections.test_collections, + changes=api_models.RescanChanges(collections=list(test_collections.test_collections), **changes_by_kind), + ) + + +def _change(collection: str, test_case: str) -> api_models.TestCaseChange: + return api_models.TestCaseChange(collection=collection, test_suite="suite", test_case=test_case) + + @pytest.mark.unit @pytest.mark.cli class TestRescanTestsCommand: @@ -42,7 +55,7 @@ def test_rescan_tests_success( """Test successful rescan of test collections.""" # Arrange api = mock_sync_apis.test_collections_api.rescan_test_collections_api_v1_test_collections_rescan_post - api.return_value = sample_test_collections + api.return_value = _rescan_response(sample_test_collections) with patch("th_cli.commands.rescan_tests.get_client", return_value=mock_api_client): with patch("th_cli.commands.rescan_tests.SyncApis", return_value=mock_sync_apis): @@ -52,9 +65,189 @@ def test_rescan_tests_success( # Assert assert result.exit_code == 0 assert "Rescanned test collections successfully" in result.output + assert "No changes detected." in result.output api.assert_called_once() mock_api_client.close.assert_called_once() + def _invoke( + self, + cli_runner: CliRunner, + mock_sync_apis: Mock, + mock_api_client: Mock, + response: api_models.TestCollections, + ) -> str: + api = mock_sync_apis.test_collections_api.rescan_test_collections_api_v1_test_collections_rescan_post + api.return_value = response + + with patch("th_cli.commands.rescan_tests.get_client", return_value=mock_api_client): + with patch("th_cli.commands.rescan_tests.SyncApis", return_value=mock_sync_apis): + result = cli_runner.invoke(rescan_tests) + + assert result.exit_code == 0 + return result.output + + def test_rescan_tests_reports_changes_grouped_by_collection( + self, + cli_runner: CliRunner, + mock_sync_apis: Mock, + mock_api_client: Mock, + sample_test_collections: api_models.TestCollections, + ) -> None: + """Added, modified and removed test cases are listed under their collection, + and the collections without changes are summarized.""" + names = list(sample_test_collections.test_collections) + changed, unchanged = names[0], names[1:] + response = _rescan_response( + sample_test_collections, + added=[_change(changed, "TC-FAKE-1.1-custom")], + modified=[_change(changed, "TC-ACE-1.3-custom")], + removed=[_change(changed, "TC-OLD-1.1-custom")], + ) + + output = self._invoke(cli_runner, mock_sync_apis, mock_api_client, response) + + assert f"Rescanned test collections successfully ({len(names)} found)" in output + assert "No changes detected" not in output + lines = output.splitlines() + assert changed in lines + assert any(line.startswith(" + TC-FAKE-1.1-custom") and line.endswith("added") for line in lines) + assert any(line.startswith(" ~ TC-ACE-1.3-custom") and line.endswith("modified") for line in lines) + assert any(line.startswith(" - TC-OLD-1.1-custom") and line.endswith("removed") for line in lines) + if unchanged: + assert f"No changes in: {', '.join(unchanged)}" in output + + def test_rescan_tests_no_changes( + self, + cli_runner: CliRunner, + mock_sync_apis: Mock, + mock_api_client: Mock, + sample_test_collections: api_models.TestCollections, + ) -> None: + output = self._invoke(cli_runner, mock_sync_apis, mock_api_client, _rescan_response(sample_test_collections)) + + count = len(sample_test_collections.test_collections) + assert f"Rescanned test collections successfully ({count} found). No changes detected." in output + assert "Scripts that could not be parsed" not in output + + def test_rescan_tests_reports_parse_errors( + self, + cli_runner: CliRunner, + mock_sync_apis: Mock, + mock_api_client: Mock, + sample_test_collections: api_models.TestCollections, + ) -> None: + """Scripts that failed to parse are listed with the reason, also when nothing else changed.""" + response = _rescan_response( + sample_test_collections, + errors=[ + api_models.ScriptError(script="TC_BAD_1_1.py", reason="Syntax error: invalid syntax (line 3)"), + api_models.ScriptError(reason="Failed running command:\n boom"), + ], + ) + + output = self._invoke(cli_runner, mock_sync_apis, mock_api_client, response) + + assert "No changes detected." in output + assert "Scripts that could not be parsed (skipped):" in output + assert " ! TC_BAD_1_1.py: Syntax error: invalid syntax (line 3)" in output + assert " ! Failed running command: boom" in output + + def test_rescan_tests_reports_custom_file_changes( + self, + cli_runner: CliRunner, + mock_sync_apis: Mock, + mock_api_client: Mock, + sample_test_collections: api_models.TestCollections, + ) -> None: + """Added, modified and removed files in custom/ are listed, flagging the ones that replace an SDK module.""" + response = _rescan_response( + sample_test_collections, + custom_files=api_models.CustomFileChanges( + added=[api_models.CustomFileChange(name="TC_SUTestBase.py", replaces_sdk=True)], + modified=[api_models.CustomFileChange(name="TC_ACE_1_3.py")], + removed=[api_models.CustomFileChange(name="TC_OLD_1_1.py")], + ), + ) + + output = self._invoke(cli_runner, mock_sync_apis, mock_api_client, response) + + assert "Custom files" in output + assert " + TC_SUTestBase.py" in output + assert "added (replaces sdk/TC_SUTestBase.py)" in output + assert " ~ TC_ACE_1_3.py" in output + assert "modified" in output + assert " - TC_OLD_1_1.py" in output + assert "removed" in output + # Only files that replace an SDK module get the note. + assert "sdk/TC_ACE_1_3.py" not in output + assert "sdk/TC_OLD_1_1.py" not in output + + def test_rescan_tests_custom_file_changes_alone_are_not_reported_as_no_changes( + self, + cli_runner: CliRunner, + mock_sync_apis: Mock, + mock_api_client: Mock, + sample_test_collections: api_models.TestCollections, + ) -> None: + """A changed helper has no test case, but the rescan did pick up a change.""" + response = _rescan_response( + sample_test_collections, + custom_files=api_models.CustomFileChanges(modified=[api_models.CustomFileChange(name="Helper.py")]), + ) + + output = self._invoke(cli_runner, mock_sync_apis, mock_api_client, response) + + count = len(sample_test_collections.test_collections) + assert f"Rescanned test collections successfully ({count} found)" in output + assert "No changes detected" not in output + assert " ~ Helper.py" in output + + def test_rescan_tests_removed_file_that_replaced_an_sdk_module( + self, + cli_runner: CliRunner, + mock_sync_apis: Mock, + mock_api_client: Mock, + sample_test_collections: api_models.TestCollections, + ) -> None: + response = _rescan_response( + sample_test_collections, + custom_files=api_models.CustomFileChanges( + removed=[api_models.CustomFileChange(name="Helper.py", replaces_sdk=True)] + ), + ) + + output = self._invoke(cli_runner, mock_sync_apis, mock_api_client, response) + + assert "removed (was replacing sdk/Helper.py)" in output + + def test_rescan_tests_no_custom_file_changes_prints_nothing_extra( + self, + cli_runner: CliRunner, + mock_sync_apis: Mock, + mock_api_client: Mock, + sample_test_collections: api_models.TestCollections, + ) -> None: + response = _rescan_response(sample_test_collections, custom_files=api_models.CustomFileChanges()) + + output = self._invoke(cli_runner, mock_sync_apis, mock_api_client, response) + + assert "No changes detected." in output + assert "Custom files" not in output + + def test_rescan_tests_backend_without_change_report( + self, + cli_runner: CliRunner, + mock_sync_apis: Mock, + mock_api_client: Mock, + sample_test_collections: api_models.TestCollections, + ) -> None: + """A response without `changes` (older backend) falls back to the plain summary.""" + output = self._invoke(cli_runner, mock_sync_apis, mock_api_client, sample_test_collections) + + count = len(sample_test_collections.test_collections) + assert f"Rescanned test collections successfully ({count} found)" in output + assert "No changes detected" not in output + def test_rescan_tests_no_response( self, cli_runner: CliRunner, diff --git a/th_cli/api_lib_autogen/models.py b/th_cli/api_lib_autogen/models.py index 2c26b3b..9c75a01 100644 --- a/th_cli/api_lib_autogen/models.py +++ b/th_cli/api_lib_autogen/models.py @@ -1,6 +1,6 @@ # generated by datamodel-codegen: -# filename: tmp6b_blt9g.json -# timestamp: 2026-10-28T16:52:46+00:00 +# filename: tmpjqwdpe1o.json +# timestamp: 2026-11-07T00:09:37+00:00 from __future__ import annotations @@ -46,6 +46,17 @@ class Name(RootModel[str]): root: Annotated[str, Field(title="Name")] +class CustomFileChange(BaseModel): + name: Annotated[str, Field(title="Name")] + replaces_sdk: Annotated[bool | None, Field(title="Replaces Sdk")] = False + + +class CustomFileChanges(BaseModel): + added: Annotated[list[CustomFileChange] | None, Field(default_factory=list, title="Added")] + modified: Annotated[list[CustomFileChange] | None, Field(default_factory=list, title="Modified")] + removed: Annotated[list[CustomFileChange] | None, Field(default_factory=list, title="Removed")] + + class Id(RootModel[int]): root: Annotated[int, Field(title="Id")] @@ -76,6 +87,11 @@ class PICSItem(BaseModel): enabled: Annotated[bool, Field(title="Enabled")] +class ScriptError(BaseModel): + script: Annotated[str | None, Field(title="Script")] = None + reason: Annotated[str, Field(title="Reason")] + + class THConfig(BaseModel): prompt_timeout_seconds: Annotated[int | None, Field(title="Prompt Timeout Seconds")] = 60 enable_realtime_python_test_logs: Annotated[bool | None, Field(title="Enable Realtime Python Test Logs")] = None @@ -83,6 +99,12 @@ class THConfig(BaseModel): sdk_container_apps_dir: Annotated[str | None, Field(title="Sdk Container Apps Dir")] = None +class TestCaseChange(BaseModel): + collection: Annotated[str, Field(title="Collection")] + test_suite: Annotated[str, Field(title="Test Suite Id")] + test_case: Annotated[str, Field(title="Test Case Id")] + + class PublicId(RootModel[str]): root: Annotated[str, Field(title="Public Id")] @@ -279,6 +301,21 @@ class PICSCluster(BaseModel): ] +class RescanChanges(BaseModel): + added: Annotated[list[TestCaseChange], Field(title="Added")] + modified: Annotated[list[TestCaseChange], Field(title="Modified")] + removed: Annotated[list[TestCaseChange], Field(title="Removed")] + errors: Annotated[list[ScriptError], Field(title="Errors")] + collections: Annotated[list[str], Field(title="Collections")] + custom_files: Annotated[ + CustomFileChanges | None, + Field( + default_factory=lambda: CustomFileChanges.model_validate({"added": [], "modified": [], "removed": []}), + title="Custom Files", + ), + ] + + class TestCase(BaseModel): metadata: TestMetadata @@ -431,6 +468,7 @@ class TestCollection(BaseModel): class TestCollections(BaseModel): test_collections: Annotated[dict[str, TestCollection], Field(title="Test Collections")] + changes: RescanChanges | None = None class TestRunExecutionToExport(BaseModel): diff --git a/th_cli/colorize.py b/th_cli/colorize.py index 724043a..c4ed773 100644 --- a/th_cli/colorize.py +++ b/th_cli/colorize.py @@ -71,6 +71,13 @@ class ColorConfig: HierarchyEnum.TEST_STEP.value: "bright_black", } + # Colors for the kind of change reported by rescan-tests + CHANGE_COLORS: dict[str, str] = { + "added": "green", + "modified": "yellow", + "removed": "red", + } + # Default colors for logs TEXT_COLORS: dict[str, str] = { TextTypeEnum.CMD.value: "bright_blue", @@ -109,6 +116,10 @@ def get_hierarchy_color(self, level: str) -> str: """Get color for a hierarchy level.""" return self.TEST_HIERARCHY_COLORS.get(level.lower(), "white") + def get_change_color(self, kind: str) -> str: + """Get color for a kind of change (added, modified, removed).""" + return self.CHANGE_COLORS.get(kind.lower(), "white") + # Global color configuration instance color_config = ColorConfig() @@ -153,6 +164,23 @@ def colorize_state(state_name: str) -> str: return click.style(state_text, fg=color, bold=True) +def colorize_change(kind: str, text: str) -> str: + """ + Colorize a line describing an added, modified or removed item. + + Args: + kind: The kind of change: "added", "modified" or "removed" + text: The text to colorize + + Returns: + Colored string if colors are enabled, plain string otherwise + """ + if not color_config.colors_enabled: + return text + + return click.style(text, fg=color_config.get_change_color(kind)) + + def colorize_runner_state(runner_state_name: str) -> str: """ Colorize test runner state based on semantic meaning. diff --git a/th_cli/commands/rescan_tests.py b/th_cli/commands/rescan_tests.py index 6990bab..12a7fb6 100644 --- a/th_cli/commands/rescan_tests.py +++ b/th_cli/commands/rescan_tests.py @@ -17,10 +17,11 @@ import click from httpx import Timeout, TimeoutException +from th_cli.api_lib_autogen import models as api_models from th_cli.api_lib_autogen.api_client import SyncApis from th_cli.api_lib_autogen.exceptions import ResponseHandlingException, UnexpectedResponse from th_cli.client import get_client -from th_cli.colorize import colorize_cmd_help, colorize_help, colorize_success +from th_cli.colorize import colorize_change, colorize_cmd_help, colorize_help, colorize_success, colorize_warning from th_cli.exceptions import CLIError, handle_api_error # Rescanning regenerates the Python test JSON files via the SDK container, @@ -28,6 +29,87 @@ RESCAN_TIMEOUT = Timeout(120.0, connect=10.0) # 120s total, 10s connect +def _render_changes(changes: api_models.RescanChanges) -> list[str]: + """Render the test case changes grouped by collection, followed by the collections + that did not change.""" + markers = (("+", "added", changes.added), ("~", "modified", changes.modified), ("-", "removed", changes.removed)) + + lines_by_collection: dict[str, list[str]] = {} + for marker, label, test_cases in markers: + for change in test_cases: + line = f" {marker} {change.test_case:<30} {label}" + lines_by_collection.setdefault(change.collection, []).append(colorize_change(label, line)) + + lines: list[str] = [] + for collection, collection_lines in lines_by_collection.items(): + lines.append("") + lines.append(collection) + lines.extend(collection_lines) + + unchanged = [name for name in changes.collections if name not in lines_by_collection] + if unchanged: + lines.append(f"No changes in: {', '.join(unchanged)}") + return lines + + +def _render_errors(errors: list[api_models.ScriptError]) -> list[str]: + lines = ["", "Scripts that could not be parsed (skipped):"] + for error in errors: + reason = " ".join(error.reason.split()) + lines.append(f" ! {error.script}: {reason}" if error.script else f" ! {reason}") + return lines + + +def _render_custom_files(custom_files: api_models.CustomFileChanges) -> list[str]: + """Render the files added, modified or removed in custom/, flagging the ones that + replace an SDK module.""" + markers = ( + ("+", "added", custom_files.added), + ("~", "modified", custom_files.modified), + ("-", "removed", custom_files.removed), + ) + + lines: list[str] = [] + for marker, label, files in markers: + for file in files or []: + replaces = "" + if file.replaces_sdk: + replaces = f" ({'was replacing' if label == 'removed' else 'replaces'} sdk/{file.name})" + lines.append(colorize_change(label, f" {marker} {file.name:<30} {label}{replaces}")) + if lines: + lines.insert(0, "") + lines.insert(1, "Custom files") + return lines + + +def print_rescan_report(response: api_models.TestCollections) -> None: + """Print the outcome of a rescan, including what it changed.""" + collection_count = len(response.test_collections) + changes = response.changes + + summary = f"Rescanned test collections successfully ({collection_count} found)" + if changes is None: + # Backend that predates the change report + click.echo(colorize_success(summary)) + return + + test_cases_changed = bool(changes.added or changes.modified or changes.removed) + custom_file_lines = _render_custom_files(changes.custom_files) if changes.custom_files else [] + + if test_cases_changed or custom_file_lines: + click.echo(colorize_success(summary)) + if test_cases_changed: + for line in _render_changes(changes): + click.echo(line) + for line in custom_file_lines: + click.echo(line) + else: + click.echo(colorize_success(f"{summary}. No changes detected.")) + + if changes.errors: + click.echo(colorize_warning("\n".join(_render_errors(changes.errors)))) + + @click.command( short_help=colorize_help("Rescan available test collections"), help=colorize_cmd_help( @@ -48,8 +130,7 @@ def rescan_tests() -> None: if test_collections is None: raise CLIError("Server did not return test_collections") - collection_count = len(test_collections.test_collections) - click.echo(colorize_success(f"Rescanned test collections successfully ({collection_count} found)")) + print_rescan_report(test_collections) except CLIError: raise # Re-raise CLI Errors as-is except ResponseHandlingException as e: diff --git a/th_cli/shared_constants.py b/th_cli/shared_constants.py index 788c02b..ea818ec 100644 --- a/th_cli/shared_constants.py +++ b/th_cli/shared_constants.py @@ -17,14 +17,14 @@ # limitations under the License. # -from enum import Enum +from enum import StrEnum """ Shared constants across the application. """ -class TestStateEnum(str, Enum): +class TestStateEnum(StrEnum): __test__ = False # Needed to indicate to PyTest that this is not a "test" PENDING = "pending" EXECUTING = "executing" @@ -37,7 +37,7 @@ class TestStateEnum(str, Enum): # Enum Keys for different types of messages currently supported by the tool -class MessageTypeEnum(str, Enum): +class MessageTypeEnum(StrEnum): PROMPT_REQUEST = "prompt_request" PROMPT_RESPONSE = "prompt_response" OPTIONS_REQUEST = "options_request" @@ -54,13 +54,13 @@ class MessageTypeEnum(str, Enum): # Enum keys used with messages at the top level -class MessageKeysEnum(str, Enum): +class MessageKeysEnum(StrEnum): TYPE = "type" PAYLOAD = "payload" # Enum for DUT Pairing Modes -class DutPairingModeEnum(str, Enum): +class DutPairingModeEnum(StrEnum): ON_NETWORK = "onnetwork" BLE_WIFI = "ble-wifi" NFC_WIFI = "nfc-wifi"