test_base_commands.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. """Focused regression tests for module base commands."""
  2. from __future__ import annotations
  3. from types import SimpleNamespace
  4. from cli.core.module.base_commands import GenerationConfig, generate_template, list_templates
  5. class _DisplayCapture:
  6. def __init__(self) -> None:
  7. self.lines: list[str] = []
  8. self.templates = SimpleNamespace(
  9. render_template_header=lambda *args, **kwargs: None,
  10. render_file_tree=lambda *args, **kwargs: None,
  11. )
  12. self.variables = SimpleNamespace(render_variables_table=lambda *args, **kwargs: None)
  13. def text(self, value: str, style: str | None = None) -> None:
  14. del style
  15. self.lines.append(value)
  16. def success(self, value: str, *args, **kwargs) -> None:
  17. del args, kwargs
  18. self.lines.append(value)
  19. def warning(self, value: str, *args, **kwargs) -> None:
  20. del args, kwargs
  21. self.lines.append(value)
  22. def error(self, value: str, *args, **kwargs) -> None:
  23. del args, kwargs
  24. self.lines.append(value)
  25. def data_table(self, *args, **kwargs) -> None:
  26. del args, kwargs
  27. raise AssertionError("data_table should not be used for raw output")
  28. def info(self, *args, **kwargs) -> None:
  29. del args, kwargs
  30. raise AssertionError("info should not be used when templates exist")
  31. def test_list_templates_raw_outputs_tab_separated_rows() -> None:
  32. """Raw listing should emit one tab-separated row per template."""
  33. template = SimpleNamespace(
  34. id="whoami",
  35. metadata=SimpleNamespace(
  36. name="Whoami",
  37. tags=["docker", "test"],
  38. version=SimpleNamespace(name="1.0.0"),
  39. library="default",
  40. library_type="git",
  41. ),
  42. )
  43. display = _DisplayCapture()
  44. module_instance = SimpleNamespace(
  45. name="compose",
  46. display=display,
  47. _load_all_templates=lambda: [template],
  48. )
  49. returned_templates = list_templates(module_instance, raw=True)
  50. assert returned_templates == [template]
  51. assert display.lines == ["whoami\tWhoami\tdocker,test\t1.0.0\tdefault"]
  52. def test_generate_template_dry_run_skips_destination_prompt_and_overwrite_check(
  53. monkeypatch,
  54. ) -> None:
  55. """Dry runs without explicit destinations should not ask where to write or confirm overwrites."""
  56. display = _DisplayCapture()
  57. template = SimpleNamespace(id="whoami", slug="whoami")
  58. module_instance = SimpleNamespace(name="compose", display=display)
  59. monkeypatch.setattr("cli.core.module.base_commands._prepare_template", lambda *args, **kwargs: template)
  60. monkeypatch.setattr(
  61. "cli.core.module.base_commands._render_template",
  62. lambda *args, **kwargs: ({"compose.yaml": "services:\n"}, {}),
  63. )
  64. monkeypatch.setattr(
  65. "cli.core.module.base_commands.prompt_generation_destination",
  66. lambda slug: (_ for _ in ()).throw(AssertionError(f"prompt_generation_destination called for {slug}")),
  67. )
  68. monkeypatch.setattr(
  69. "cli.core.module.base_commands.check_output_directory",
  70. lambda *args, **kwargs: (_ for _ in ()).throw(AssertionError("check_output_directory should not run")),
  71. )
  72. generate_template(
  73. module_instance,
  74. GenerationConfig(
  75. id="whoami",
  76. interactive=True,
  77. dry_run=True,
  78. ),
  79. )
  80. assert any("boilerplate rendered successfully" in line for line in display.lines)
  81. assert any("preview only" in line for line in display.lines)