jinjaturtle/tests/test_cli.py
Miguel Jacq 409824a3b5
All checks were successful
CI / test (push) Successful in 41s
Lint / test (push) Successful in 25s
Trivy / test (push) Successful in 23s
Fix tests
2025-11-25 17:51:36 +11:00

85 lines
2.1 KiB
Python

from __future__ import annotations
import sys
from pathlib import Path
import pytest
from jinjaturtle import cli
SAMPLES_DIR = Path(__file__).parent / "samples"
def test_cli_stdout_toml(capsys):
"""
Run the CLI on the TOML sample, printing to stdout.
Covers the no-output-path branches.
"""
cfg_path = SAMPLES_DIR / "tom.toml"
exit_code = cli._main([str(cfg_path), "-r", "jinjaturtle"])
assert exit_code
captured = capsys.readouterr()
out = captured.out
# Headers printed when not writing to files
assert "# defaults/main.yml" in out
assert "# config.j2" in out
# Should contain some variables
assert "jinjaturtle_" in out
assert captured.err == ""
def test_cli_writes_output_files(tmp_path, capsys):
"""
Run the CLI on the INI sample, writing to files instead of stdout.
Covers the --defaults-output and --template-output branches.
"""
cfg_path = SAMPLES_DIR / "php.ini"
defaults_path = tmp_path / "defaults.yml"
template_path = tmp_path / "config.j2"
exit_code = cli._main(
[
str(cfg_path),
"-r",
"php",
"--defaults-output",
str(defaults_path),
"--template-output",
str(template_path),
]
)
assert exit_code
assert defaults_path.is_file()
assert template_path.is_file()
defaults_text = defaults_path.read_text(encoding="utf-8")
template_text = template_path.read_text(encoding="utf-8")
assert "php_" in defaults_text
assert "php_" in template_text
captured = capsys.readouterr()
# When writing to files, we shouldn't print the big headers
assert "# defaults/main.yml" not in captured.out
assert "# config.j2" not in captured.out
def test_main_wrapper_exits_with_zero(monkeypatch):
"""
Cover the main() wrapper that raises SystemExit.
"""
cfg_path = SAMPLES_DIR / "tom.toml"
monkeypatch.setattr(
sys,
"argv",
["jinjaturtle", str(cfg_path), "-r", "jinjaturtle"],
)
with pytest.raises(SystemExit) as exc:
cli.main()
assert exc.value.code