refactor template
This commit is contained in:
22
{{cookiecutter.project_slug}}/tests/conftest.py
Normal file
22
{{cookiecutter.project_slug}}/tests/conftest.py
Normal file
@@ -0,0 +1,22 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import pytest
|
||||
|
||||
from {{ cookiecutter.package_name }}.config import clear_config_cache
|
||||
from {{ cookiecutter.package_name }}.config.paths import ENV_CONFIG_VAR
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def _isolate_xdg_paths(monkeypatch, tmp_path_factory) -> None:
|
||||
temp_root = tmp_path_factory.mktemp("xdg")
|
||||
monkeypatch.setenv("XDG_CONFIG_HOME", str(temp_root / "config"))
|
||||
monkeypatch.setenv("XDG_DATA_HOME", str(temp_root / "data"))
|
||||
monkeypatch.delenv(ENV_CONFIG_VAR, raising=False)
|
||||
yield
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def _clear_config_cache() -> None:
|
||||
clear_config_cache()
|
||||
yield
|
||||
clear_config_cache()
|
||||
32
{{cookiecutter.project_slug}}/tests/test_internals.py
Normal file
32
{{cookiecutter.project_slug}}/tests/test_internals.py
Normal file
@@ -0,0 +1,32 @@
|
||||
from __future__ import annotations
|
||||
|
||||
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
|
||||
|
||||
|
||||
def test_env_config_override(tmp_path, monkeypatch) -> None:
|
||||
config_path = tmp_path / "config.toml"
|
||||
config_path.write_text("greeting = \"Hi\"\n")
|
||||
monkeypatch.setenv(ENV_CONFIG_VAR, str(config_path))
|
||||
|
||||
settings = get_config()
|
||||
|
||||
assert settings.greeting == "Hi"
|
||||
assert get_config_source() == config_path
|
||||
|
||||
|
||||
def test_missing_env_config_raises(tmp_path, monkeypatch) -> None:
|
||||
missing = tmp_path / "missing.toml"
|
||||
monkeypatch.setenv(ENV_CONFIG_VAR, str(missing))
|
||||
|
||||
try:
|
||||
get_config()
|
||||
except FileNotFoundError as exc:
|
||||
assert str(missing) in str(exc)
|
||||
else:
|
||||
raise AssertionError("Expected FileNotFoundError")
|
||||
|
||||
|
||||
def test_format_greeting() -> None:
|
||||
assert format_greeting("Ada") == "Hello, Ada!"
|
||||
13
{{cookiecutter.project_slug}}/tests/test_public.py
Normal file
13
{{cookiecutter.project_slug}}/tests/test_public.py
Normal file
@@ -0,0 +1,13 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from {{ cookiecutter.package_name }} import __version__
|
||||
from {{ cookiecutter.package_name }}.config import get_config
|
||||
|
||||
|
||||
def test_public_version_is_string() -> None:
|
||||
assert isinstance(__version__, str)
|
||||
|
||||
|
||||
def test_get_config_defaults() -> None:
|
||||
settings = get_config()
|
||||
assert settings.greeting == "Hello"
|
||||
@@ -1,24 +0,0 @@
|
||||
"""Tests for {{ cookiecutter.package_name }}."""
|
||||
|
||||
from io import StringIO
|
||||
import sys
|
||||
|
||||
from {{ cookiecutter.package_name }}.core import say_hello
|
||||
|
||||
|
||||
def test_say_hello():
|
||||
"""Test say_hello function."""
|
||||
captured_output = StringIO()
|
||||
sys.stdout = captured_output
|
||||
say_hello()
|
||||
sys.stdout = sys.__stdout__
|
||||
assert captured_output.getvalue() == "Hello, World!\n"
|
||||
|
||||
|
||||
def test_say_hello_with_name():
|
||||
"""Test say_hello function with custom name."""
|
||||
captured_output = StringIO()
|
||||
sys.stdout = captured_output
|
||||
say_hello("Alice")
|
||||
sys.stdout = sys.__stdout__
|
||||
assert captured_output.getvalue() == "Hello, Alice!\n"
|
||||
Reference in New Issue
Block a user