use Codex 5.3 to review template to spec
This commit is contained in:
75
{{cookiecutter.project_slug}}/tests/test_cli.py
Normal file
75
{{cookiecutter.project_slug}}/tests/test_cli.py
Normal file
@@ -0,0 +1,75 @@
|
||||
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"] == "<defaults>"
|
||||
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
|
||||
@@ -1,18 +1,22 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import pytest
|
||||
from pydantic import ValidationError
|
||||
|
||||
from {{ cookiecutter.package_name }}.commands import format_greeting
|
||||
from {{ cookiecutter.package_name }}.config import get_config, get_config_source
|
||||
from {{ cookiecutter.package_name }}.config.paths import ENV_CONFIG_VAR
|
||||
from {{ cookiecutter.package_name }}.config.paths import ENV_CONFIG_VAR, config_file_path
|
||||
|
||||
|
||||
def test_env_config_override(tmp_path, monkeypatch) -> None:
|
||||
config_path = tmp_path / "config.toml"
|
||||
config_path.write_text("greeting = \"Hi\"\n")
|
||||
config_path.write_text('[app]\ngreeting = "Hi"\n\n[database]\npath = "/tmp/app.db"\n')
|
||||
monkeypatch.setenv(ENV_CONFIG_VAR, str(config_path))
|
||||
|
||||
settings = get_config()
|
||||
|
||||
assert settings.greeting == "Hi"
|
||||
assert settings.app.greeting == "Hi"
|
||||
assert settings.database.path == "/tmp/app.db"
|
||||
assert get_config_source() == config_path
|
||||
|
||||
|
||||
@@ -20,13 +24,39 @@ def test_missing_env_config_raises(tmp_path, monkeypatch) -> None:
|
||||
missing = tmp_path / "missing.toml"
|
||||
monkeypatch.setenv(ENV_CONFIG_VAR, str(missing))
|
||||
|
||||
try:
|
||||
with pytest.raises(FileNotFoundError, match="Config file not found"):
|
||||
get_config()
|
||||
except FileNotFoundError as exc:
|
||||
assert str(missing) in str(exc)
|
||||
else:
|
||||
raise AssertionError("Expected FileNotFoundError")
|
||||
|
||||
|
||||
def test_xdg_default_config_is_loaded(monkeypatch) -> None:
|
||||
path = config_file_path()
|
||||
path.parent.mkdir(parents=True, exist_ok=True)
|
||||
path.write_text('[app]\ngreeting = "Hola"\n\n[database]\npath = "/tmp/default.db"\n')
|
||||
monkeypatch.delenv(ENV_CONFIG_VAR, raising=False)
|
||||
|
||||
settings = get_config()
|
||||
|
||||
assert settings.app.greeting == "Hola"
|
||||
assert settings.database.path == "/tmp/default.db"
|
||||
assert get_config_source() == path
|
||||
|
||||
|
||||
def test_unknown_key_raises_validation_error(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))
|
||||
|
||||
with pytest.raises(ValidationError):
|
||||
get_config()
|
||||
|
||||
|
||||
def test_settings_are_frozen() -> None:
|
||||
settings = get_config()
|
||||
|
||||
with pytest.raises(ValidationError, match="frozen"):
|
||||
settings.app.greeting = "Mutated"
|
||||
|
||||
|
||||
def test_format_greeting() -> None:
|
||||
assert format_greeting("Ada") == "Hello, Ada!"
|
||||
assert format_greeting("Ada", greeting="Hi") == "Hi, Ada!"
|
||||
|
||||
@@ -1,13 +1,19 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from {{ cookiecutter.package_name }} import __version__
|
||||
from {{ cookiecutter.package_name }}.config import get_config
|
||||
from {{ cookiecutter.package_name }} import __all__, __version__, get_config
|
||||
from {{ cookiecutter.package_name }}.config.settings import Settings
|
||||
|
||||
|
||||
def test_public_version_is_string() -> None:
|
||||
assert isinstance(__version__, str)
|
||||
|
||||
|
||||
def test_get_config_defaults() -> None:
|
||||
def test_public_exports_include_stable_symbols() -> None:
|
||||
assert set(__all__) >= {"__version__", "get_config", "Settings"}
|
||||
|
||||
|
||||
def test_get_config_defaults_shape() -> None:
|
||||
settings = get_config()
|
||||
assert settings.greeting == "Hello"
|
||||
assert isinstance(settings, Settings)
|
||||
assert settings.app.greeting == "Hello"
|
||||
assert settings.database.path.endswith("/data.db")
|
||||
|
||||
Reference in New Issue
Block a user