From b7e82648ac4df3ce04cf51b6caeb5d8db1d5e707 Mon Sep 17 00:00:00 2001 From: Alex Date: Tue, 1 Dec 2026 05:31:50 -0400 Subject: [PATCH] fix(init): keep template list stdout clean for structured output --- .../src/deepctl_cmd_init/command.py | 18 +++--- .../tests/unit/test_init_list_output.py | 57 +++++++++++++++++++ 2 files changed, 68 insertions(+), 7 deletions(-) create mode 100644 packages/deepctl-cmd-init/tests/unit/test_init_list_output.py diff --git a/packages/deepctl-cmd-init/src/deepctl_cmd_init/command.py b/packages/deepctl-cmd-init/src/deepctl_cmd_init/command.py index 4b460f13..5a6f0e14 100644 --- a/packages/deepctl-cmd-init/src/deepctl_cmd_init/command.py +++ b/packages/deepctl-cmd-init/src/deepctl_cmd_init/command.py @@ -584,16 +584,20 @@ def output_result(self, result: Any, config: Config) -> None: def _handle_list(self, search: str | None) -> list[dict[str, Any]]: """List templates and return structured data.""" - with console.status("[blue]Fetching templates...[/blue]"): + from deepctl_core.output import get_output_format + + default_output = get_output_format() == "default" + progress_console = console if default_output else status_console + with progress_console.status("[blue]Fetching templates...[/blue]"): response = templates_api.list_templates(search=search) templates = response.items - if not templates: - console.print("[yellow]No templates found[/yellow]") - return [] - - self._print_templates_table(templates) - console.print(f"\n[dim]{len(templates)} template(s)[/dim]") + if default_output: + if templates: + self._print_templates_table(templates) + console.print(f"\n[dim]{len(templates)} template(s)[/dim]") + else: + console.print("[yellow]No templates found[/yellow]") return [t.model_dump() for t in templates] # ── Interactive picker ──────────────────────────────────────────────────── diff --git a/packages/deepctl-cmd-init/tests/unit/test_init_list_output.py b/packages/deepctl-cmd-init/tests/unit/test_init_list_output.py new file mode 100644 index 00000000..4ae24bb1 --- /dev/null +++ b/packages/deepctl-cmd-init/tests/unit/test_init_list_output.py @@ -0,0 +1,57 @@ +"""Tests for the output of dg init --list.""" + +import json +from unittest.mock import Mock, patch + +import pytest +from deepctl_cmd_init.command import InitCommand +from deepctl_cmd_init.models import TemplateInfo, TemplateListResponse +from deepctl_core import AuthManager, Config, DeepgramClient, output + + +@pytest.mark.parametrize( + ("format_type", "has_templates"), + [("json", True), ("json", False), ("default", True)], +) +@patch("deepctl_cmd_init.command.templates_api") +def test_list_output( + mock_api, + format_type, + has_templates, + capsys, + monkeypatch, +): + templates = ( + [ + TemplateInfo( + name="example", + title="Example", + description="Example template", + language="python", + ) + ] + if has_templates + else [] + ) + mock_api.list_templates.return_value = TemplateListResponse( + total=len(templates), items=templates + ) + monkeypatch.setitem(output._output_config, "format", format_type) + monkeypatch.setitem(output._output_config, "quiet", False) + + config = Mock(spec=Config) + init_command = InitCommand() + result = init_command.handle( + config=config, + auth_manager=Mock(spec=AuthManager), + client=Mock(spec=DeepgramClient), + list=True, + ) + init_command.output_result(result, config) + + stdout = capsys.readouterr().out + if format_type == "json": + assert json.loads(stdout) == [t.model_dump() for t in templates] + else: + assert "example" in stdout + assert "1 template(s)" in stdout