from __future__ import annotations import json from typer.testing import CliRunner from {{ cookiecutter.package_name }}.cli.app import app from {{ cookiecutter.package_name }}.config.paths import ENV_CONFIG_VAR runner = CliRunner() def test_config_show_success_defaults() -> None: result = runner.invoke(app, ["config", "show"]) assert result.exit_code == 0 payload = json.loads(result.output) assert payload["source"] == "" assert payload["config"]["app"]["greeting"] == "Hello" def test_config_init_create_and_overwrite(tmp_path) -> None: path = tmp_path / "config.toml" create_result = runner.invoke(app, ["config", "init", "--path", str(path)]) assert create_result.exit_code == 0 assert path.exists() conflict_result = runner.invoke(app, ["config", "init", "--path", str(path)]) assert conflict_result.exit_code == 1 assert "Config already exists" in conflict_result.output overwrite_result = runner.invoke( app, ["config", "init", "--path", str(path), "--overwrite"], ) assert overwrite_result.exit_code == 0 def test_config_show_missing_env_file_exits_with_code_1(tmp_path, monkeypatch) -> None: missing = tmp_path / "missing.toml" monkeypatch.setenv(ENV_CONFIG_VAR, str(missing)) result = runner.invoke(app, ["config", "show"]) assert result.exit_code == 1 assert "Config file not found" in result.output def test_config_show_invalid_config_exits_with_code_1(tmp_path, monkeypatch) -> None: config_path = tmp_path / "config.toml" config_path.write_text('unknown = "value"\n') monkeypatch.setenv(ENV_CONFIG_VAR, str(config_path)) result = runner.invoke(app, ["config", "show"]) assert result.exit_code == 1 assert "Extra inputs are not permitted" in result.output def test_hello_cli_override_precedence(tmp_path, monkeypatch) -> None: config_path = tmp_path / "config.toml" config_path.write_text('[app]\ngreeting = "Hi"\n\n[database]\npath = "/tmp/example.db"\n') monkeypatch.setenv(ENV_CONFIG_VAR, str(config_path)) from_config = runner.invoke(app, ["hello", "say", "--name", "Ada"]) assert from_config.exit_code == 0 assert "Hi, Ada!" in from_config.output from_cli = runner.invoke( app, ["hello", "say", "--name", "Ada", "--greeting", "Yo"], ) assert from_cli.exit_code == 0 assert "Yo, Ada!" in from_cli.output