test_base_commands.py 3.5 KB

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