feat: implemented declarative schema-based settings system

This commit is contained in:
2026-01-06 04:55:52 +01:00
parent 58408d0dbe
commit 92f42ffce3
14 changed files with 3193 additions and 376 deletions
+427
View File
@@ -0,0 +1,427 @@
"""Tests for settings bootstrap."""
import pytest
from alfred.settings_bootstrap import (
ConfigSource,
SettingsBootstrap,
extract_python_version,
generate_secret,
get_nested_value,
)
@pytest.fixture
def test_pyproject_content():
"""Test pyproject.toml content with poetry metadata."""
return """
[tool.poetry]
name = "test"
version = "1.0.0"
[tool.poetry.dependencies]
python = "==3.14.2"
[tool.alfred.settings]
runner = "poetry"
image_name = "test_image"
"""
@pytest.fixture
def test_settings_content():
"""Test settings.toml content with schema."""
return """
[tool.alfred.settings_schema.TEST_FROM_TOML]
type = "string"
source = "toml"
toml_path = "tool.poetry.version"
description = "Version from TOML"
category = "test"
[tool.alfred.settings_schema.TEST_FROM_ENV]
type = "string"
source = "env"
default = "default_value"
description = "Value from env"
category = "test"
[tool.alfred.settings_schema.TEST_SECRET]
type = "secret"
source = "generated"
secret_rule = "16:hex"
description = "Generated secret"
category = "security"
[tool.alfred.settings_schema.TEST_COMPUTED]
type = "computed"
source = "computed"
compute_from = ["TEST_FROM_TOML", "TEST_FROM_ENV"]
compute_template = "{TEST_FROM_TOML}-{TEST_FROM_ENV}"
description = "Computed value"
category = "test"
[tool.alfred.settings_schema.PYTHON_VERSION]
type = "string"
source = "toml"
toml_path = "tool.poetry.dependencies.python"
transform = "extract_python_version_full"
description = "Python version"
category = "build"
"""
@pytest.fixture
def config_source(tmp_path):
"""Create a ConfigSource for testing."""
return ConfigSource.from_base_dir(tmp_path)
@pytest.fixture
def create_test_env(tmp_path, test_pyproject_content, test_settings_content):
"""Create a complete test environment."""
# Create pyproject.toml
pyproject_path = tmp_path / "pyproject.toml"
pyproject_path.write_text(test_pyproject_content)
# Create settings.toml
settings_path = tmp_path / "settings.toml"
settings_path.write_text(test_settings_content)
# Create .env.example
env_example = tmp_path / ".env.example"
env_example.write_text("""
TEST_FROM_TOML=
TEST_FROM_ENV=
TEST_SECRET=
TEST_COMPUTED=
PYTHON_VERSION=
""")
return ConfigSource.from_base_dir(tmp_path)
class TestExtractPythonVersion:
"""Test Python version extraction."""
def test_exact_version(self):
"""Test exact version format."""
full, short = extract_python_version("==3.14.2")
assert full == "3.14.2"
assert short == "3.14"
def test_caret_version(self):
"""Test caret version format."""
full, short = extract_python_version("^3.14.2")
assert full == "3.14.2"
assert short == "3.14"
def test_invalid_version(self):
"""Test invalid version raises error."""
with pytest.raises(ValueError, match="Invalid Python version"):
extract_python_version("3")
class TestGenerateSecret:
"""Test secret generation."""
def test_generate_b64(self):
"""Test base64 secret generation."""
secret = generate_secret("32:b64")
assert isinstance(secret, str)
assert len(secret) > 0
def test_generate_hex(self):
"""Test hex secret generation."""
secret = generate_secret("16:hex")
assert isinstance(secret, str)
assert len(secret) == 32 # 16 bytes = 32 hex chars
assert all(c in "0123456789abcdef" for c in secret)
def test_invalid_format(self):
"""Test invalid format raises error."""
with pytest.raises(ValueError, match="Invalid security format"):
generate_secret("32:invalid")
def test_invalid_rule(self):
"""Test invalid rule format raises error."""
with pytest.raises(ValueError, match="Invalid security rule format"):
generate_secret("32")
class TestGetNestedValue:
"""Test nested value extraction."""
def test_simple_path(self):
"""Test simple path."""
data = {"key": "value"}
assert get_nested_value(data, "key") == "value"
def test_nested_path(self):
"""Test nested path."""
data = {"a": {"b": {"c": "value"}}}
assert get_nested_value(data, "a.b.c") == "value"
def test_missing_key(self):
"""Test missing key raises error."""
data = {"a": {"b": "value"}}
with pytest.raises(KeyError):
get_nested_value(data, "a.c")
def test_non_dict_value(self):
"""Test accessing non-dict raises error."""
data = {"a": "not a dict"}
with pytest.raises(KeyError, match="non-dict"):
get_nested_value(data, "a.b")
class TestSettingsBootstrap:
"""Test settings bootstrap."""
def test_load_sources(self, create_test_env):
"""Test loading TOML and env sources."""
bootstrapper = SettingsBootstrap(create_test_env)
bootstrapper._load_sources()
assert bootstrapper.toml_data is not None
assert "tool" in bootstrapper.toml_data
assert isinstance(bootstrapper.existing_env, dict)
def test_resolve_from_toml(self, create_test_env):
"""Test resolving setting from TOML."""
# Load schema from test env
from alfred.settings_schema import load_schema
schema = load_schema(create_test_env.base_dir)
bootstrapper = SettingsBootstrap(create_test_env, schema)
bootstrapper._load_sources()
# Get definition for TEST_FROM_TOML
definition = bootstrapper.schema.get("TEST_FROM_TOML")
value = bootstrapper._resolve_from_toml(definition)
assert value == "1.0.0" # From tool.poetry.version
def test_resolve_from_env_with_default(self, create_test_env):
"""Test resolving from env with default."""
from alfred.settings_schema import load_schema
schema = load_schema(create_test_env.base_dir)
bootstrapper = SettingsBootstrap(create_test_env, schema)
bootstrapper._load_sources()
definition = bootstrapper.schema.get("TEST_FROM_ENV")
value = bootstrapper._resolve_from_env(definition)
assert value == "default_value"
def test_resolve_from_env_existing(self, create_test_env):
"""Test resolving from existing env."""
# Create existing .env
create_test_env.env_path.write_text("TEST_FROM_ENV=existing_value\n")
from alfred.settings_schema import load_schema
schema = load_schema(create_test_env.base_dir)
bootstrapper = SettingsBootstrap(create_test_env, schema)
bootstrapper._load_sources()
definition = bootstrapper.schema.get("TEST_FROM_ENV")
value = bootstrapper._resolve_from_env(definition)
assert value == "existing_value"
def test_resolve_generated_new(self, create_test_env):
"""Test generating new secret."""
from alfred.settings_schema import load_schema
schema = load_schema(create_test_env.base_dir)
bootstrapper = SettingsBootstrap(create_test_env, schema)
bootstrapper._load_sources()
definition = bootstrapper.schema.get("TEST_SECRET")
value = bootstrapper._resolve_generated(definition)
assert isinstance(value, str)
assert len(value) == 32 # 16 hex = 32 chars
def test_resolve_generated_preserve_existing(self, create_test_env):
"""Test preserving existing secret."""
# Create existing .env with secret
create_test_env.env_path.write_text("TEST_SECRET=existing_secret\n")
from alfred.settings_schema import load_schema
schema = load_schema(create_test_env.base_dir)
bootstrapper = SettingsBootstrap(create_test_env, schema)
bootstrapper._load_sources()
definition = bootstrapper.schema.get("TEST_SECRET")
value = bootstrapper._resolve_generated(definition)
assert value == "existing_secret"
def test_resolve_computed(self, create_test_env):
"""Test resolving computed setting."""
from alfred.settings_schema import load_schema
schema = load_schema(create_test_env.base_dir)
bootstrapper = SettingsBootstrap(create_test_env, schema)
bootstrapper._load_sources()
# Resolve dependencies first
bootstrapper.resolved_settings["TEST_FROM_TOML"] = "1.0.0"
bootstrapper.resolved_settings["TEST_FROM_ENV"] = "test"
definition = bootstrapper.schema.get("TEST_COMPUTED")
value = bootstrapper._resolve_computed(definition)
assert value == "1.0.0-test"
def test_resolve_with_transform(self, create_test_env):
"""Test resolving with transform function."""
from alfred.settings_schema import load_schema
schema = load_schema(create_test_env.base_dir)
bootstrapper = SettingsBootstrap(create_test_env, schema)
bootstrapper._load_sources()
definition = bootstrapper.schema.get("PYTHON_VERSION")
value = bootstrapper._resolve_from_toml(definition)
assert value == "3.14.2" # Transformed from "==3.14.2"
def test_full_bootstrap(self, create_test_env):
"""Test complete bootstrap process."""
from alfred.settings_schema import load_schema
schema = load_schema(create_test_env.base_dir)
bootstrapper = SettingsBootstrap(create_test_env, schema)
bootstrapper.bootstrap()
# Check .env was created
assert create_test_env.env_path.exists()
# Check .env.make was created
env_make_path = create_test_env.base_dir / ".env.make"
assert env_make_path.exists()
# Check content
env_content = create_test_env.env_path.read_text()
assert "TEST_FROM_TOML=1.0.0" in env_content
assert "TEST_FROM_ENV=default_value" in env_content
assert "TEST_SECRET=" in env_content
assert "TEST_COMPUTED=1.0.0-default_value" in env_content
def test_bootstrap_preserves_secrets(self, create_test_env):
"""Test that bootstrap preserves existing secrets."""
# Create existing .env with secret
create_test_env.env_path.write_text("""
TEST_FROM_ENV=old_value
TEST_SECRET=my_secret_123
""")
from alfred.settings_schema import load_schema
schema = load_schema(create_test_env.base_dir)
bootstrapper = SettingsBootstrap(create_test_env, schema)
bootstrapper.bootstrap()
# Check secret was preserved
env_content = create_test_env.env_path.read_text()
assert "TEST_SECRET=my_secret_123" in env_content
def test_validation_error(
self, tmp_path, test_pyproject_content, test_settings_content
):
"""Test validation error is raised."""
# Create pyproject.toml
pyproject_path = tmp_path / "pyproject.toml"
pyproject_path.write_text(test_pyproject_content)
# Add a setting with validation to settings.toml
settings_with_validation = (
test_settings_content
+ """
[tool.alfred.settings_schema.TEST_VALIDATED]
type = "integer"
source = "env"
default = 150
validator = "range:1:100"
description = "Validated setting"
category = "test"
"""
)
settings_path = tmp_path / "settings.toml"
settings_path.write_text(settings_with_validation)
env_example = tmp_path / ".env.example"
env_example.write_text("TEST_VALIDATED=\n")
source = ConfigSource.from_base_dir(tmp_path)
from alfred.settings_schema import load_schema
schema = load_schema(tmp_path)
bootstrapper = SettingsBootstrap(source, schema)
with pytest.raises(ValueError, match="Validation errors"):
bootstrapper.bootstrap()
def test_write_env_make_only_exports(self, create_test_env):
"""Test that .env.make only contains export_to_env_make settings."""
# Add a setting with export_to_env_make to settings.toml
settings_path = create_test_env.base_dir / "settings.toml"
settings_content = settings_path.read_text()
settings_content += """
[tool.alfred.settings_schema.EXPORTED_VAR]
type = "string"
source = "env"
default = "exported"
export_to_env_make = true
category = "build"
"""
settings_path.write_text(settings_content)
# Recreate schema
from alfred.settings_schema import load_schema
schema = load_schema(create_test_env.base_dir)
bootstrapper = SettingsBootstrap(create_test_env, schema)
bootstrapper.bootstrap()
env_make_content = (create_test_env.base_dir / ".env.make").read_text()
assert "EXPORTED_VAR=exported" in env_make_content
# Non-exported vars should not be in .env.make
assert "TEST_FROM_ENV" not in env_make_content
class TestConfigSource:
"""Test ConfigSource dataclass."""
def test_from_base_dir(self, tmp_path):
"""Test creating ConfigSource from base dir."""
source = ConfigSource.from_base_dir(tmp_path)
assert source.base_dir == tmp_path
assert source.toml_path == tmp_path / "pyproject.toml"
assert source.env_path == tmp_path / ".env"
assert source.env_example_path == tmp_path / ".env.example"
def test_from_base_dir_default(self):
"""Test creating ConfigSource with default base dir."""
source = ConfigSource.from_base_dir()
assert source.base_dir.exists()
assert source.toml_path.name == "pyproject.toml"
+761
View File
@@ -0,0 +1,761 @@
"""Advanced tests for settings bootstrap - template preservation and edge cases."""
import pytest
from alfred.settings_bootstrap import ConfigSource, SettingsBootstrap
@pytest.fixture
def test_toml_with_all_types(tmp_path):
"""Create test TOML with all setting types."""
# Create pyproject.toml for poetry metadata
pyproject_content = """
[tool.poetry]
name = "test"
version = "1.0.0"
[tool.poetry.dependencies]
python = "^3.14"
"""
(tmp_path / "pyproject.toml").write_text(pyproject_content)
# Create settings.toml with schema
settings_content = """
[tool.alfred.settings_schema.STRING_VAR]
type = "string"
source = "env"
default = "default_string"
description = "String variable"
category = "test"
[tool.alfred.settings_schema.INT_VAR]
type = "integer"
source = "env"
default = 42
description = "Integer variable"
category = "test"
[tool.alfred.settings_schema.FLOAT_VAR]
type = "float"
source = "env"
default = 3.14
description = "Float variable"
category = "test"
[tool.alfred.settings_schema.BOOL_VAR]
type = "boolean"
source = "env"
default = true
description = "Boolean variable"
category = "test"
[tool.alfred.settings_schema.SECRET_VAR]
type = "secret"
source = "generated"
secret_rule = "16:hex"
description = "Secret variable"
category = "security"
[tool.alfred.settings_schema.COMPUTED_VAR]
type = "computed"
source = "computed"
compute_from = ["STRING_VAR", "INT_VAR"]
compute_template = "{STRING_VAR}_{INT_VAR}"
description = "Computed variable"
category = "test"
"""
(tmp_path / "settings.toml").write_text(settings_content)
# Create .env.example
env_example_content = """# Test configuration
STRING_VAR=
INT_VAR=
FLOAT_VAR=
# Boolean settings
BOOL_VAR=
# Security
SECRET_VAR=
# Computed values
COMPUTED_VAR=
# Custom variable (not in schema)
CUSTOM_VAR=custom_value
"""
(tmp_path / ".env.example").write_text(env_example_content)
return tmp_path
class TestTemplatePreservation:
"""Test that .env.example template structure is preserved."""
def test_preserves_comments(self, test_toml_with_all_types):
"""Test that comments from .env.example are preserved."""
from alfred.settings_schema import load_schema
schema = load_schema(test_toml_with_all_types)
source = ConfigSource.from_base_dir(test_toml_with_all_types)
bootstrapper = SettingsBootstrap(source, schema)
bootstrapper.bootstrap()
env_content = source.env_path.read_text()
# Check comments are preserved
assert "# Test configuration" in env_content
assert "# Boolean settings" in env_content
assert "# Security" in env_content
assert "# Computed values" in env_content
def test_preserves_empty_lines(self, test_toml_with_all_types):
"""Test that empty lines from .env.example are preserved."""
from alfred.settings_schema import load_schema
schema = load_schema(test_toml_with_all_types)
source = ConfigSource.from_base_dir(test_toml_with_all_types)
bootstrapper = SettingsBootstrap(source, schema)
bootstrapper.bootstrap()
env_content = source.env_path.read_text()
lines = env_content.split("\n")
# Check there are empty lines (structure preserved)
assert "" in lines
def test_preserves_variable_order(self, test_toml_with_all_types):
"""Test that variable order from .env.example is preserved."""
from alfred.settings_schema import load_schema
schema = load_schema(test_toml_with_all_types)
source = ConfigSource.from_base_dir(test_toml_with_all_types)
bootstrapper = SettingsBootstrap(source, schema)
bootstrapper.bootstrap()
env_content = source.env_path.read_text()
# Check order is preserved
string_pos = env_content.find("STRING_VAR=")
int_pos = env_content.find("INT_VAR=")
float_pos = env_content.find("FLOAT_VAR=")
bool_pos = env_content.find("BOOL_VAR=")
assert string_pos < int_pos < float_pos < bool_pos
class TestSecretPreservation:
"""Test that secrets are never overwritten."""
def test_preserves_existing_secrets(self, test_toml_with_all_types):
"""Test that existing secrets are preserved across multiple bootstraps."""
from alfred.settings_schema import load_schema
schema = load_schema(test_toml_with_all_types)
source = ConfigSource.from_base_dir(test_toml_with_all_types)
# First bootstrap - generates secret
bootstrapper1 = SettingsBootstrap(source, schema)
bootstrapper1.bootstrap()
env_content_1 = source.env_path.read_text()
secret_1 = [
line.split("=")[1]
for line in env_content_1.split("\n")
if line.startswith("SECRET_VAR=")
][0]
# Second bootstrap - should preserve secret
bootstrapper2 = SettingsBootstrap(source, schema)
bootstrapper2.bootstrap()
env_content_2 = source.env_path.read_text()
secret_2 = [
line.split("=")[1]
for line in env_content_2.split("\n")
if line.startswith("SECRET_VAR=")
][0]
assert secret_1 == secret_2
assert len(secret_1) == 32 # 16 hex bytes
def test_multiple_secrets_preserved(self, tmp_path):
"""Test that multiple secrets are all preserved."""
pyproject_content = """
[tool.poetry]
name = "test"
version = "1.0.0"
"""
(tmp_path / "pyproject.toml").write_text(pyproject_content)
settings_content = """
[tool.alfred.settings_schema.SECRET_1]
type = "secret"
source = "generated"
secret_rule = "16:hex"
category = "security"
[tool.alfred.settings_schema.SECRET_2]
type = "secret"
source = "generated"
secret_rule = "32:b64"
category = "security"
[tool.alfred.settings_schema.SECRET_3]
type = "secret"
source = "generated"
secret_rule = "8:hex"
category = "security"
"""
(tmp_path / "settings.toml").write_text(settings_content)
(tmp_path / ".env.example").write_text("SECRET_1=\nSECRET_2=\nSECRET_3=\n")
from alfred.settings_schema import load_schema
schema = load_schema(tmp_path)
source = ConfigSource.from_base_dir(tmp_path)
# First bootstrap
bootstrapper1 = SettingsBootstrap(source, schema)
bootstrapper1.bootstrap()
env_content_1 = source.env_path.read_text()
# Second bootstrap
bootstrapper2 = SettingsBootstrap(source, schema)
bootstrapper2.bootstrap()
env_content_2 = source.env_path.read_text()
# All secrets should be identical
assert env_content_1 == env_content_2
class TestCustomVariables:
"""Test that custom variables (not in schema) are preserved."""
def test_preserves_custom_variables_from_env(self, test_toml_with_all_types):
"""Test that custom variables added to .env are preserved."""
from alfred.settings_schema import load_schema
schema = load_schema(test_toml_with_all_types)
source = ConfigSource.from_base_dir(test_toml_with_all_types)
# First bootstrap
bootstrapper1 = SettingsBootstrap(source, schema)
bootstrapper1.bootstrap()
# Add custom variables to .env
with open(source.env_path, "a") as f:
f.write("\nMY_CUSTOM_VAR=custom_value\n")
f.write("ANOTHER_CUSTOM=another_value\n")
# Second bootstrap
bootstrapper2 = SettingsBootstrap(source, schema)
bootstrapper2.bootstrap()
env_content = source.env_path.read_text()
# Custom variables should be preserved
assert "MY_CUSTOM_VAR=custom_value" in env_content
assert "ANOTHER_CUSTOM=another_value" in env_content
def test_custom_variables_in_dedicated_section(self, test_toml_with_all_types):
"""Test that custom variables are placed in a dedicated section."""
from alfred.settings_schema import load_schema
schema = load_schema(test_toml_with_all_types)
source = ConfigSource.from_base_dir(test_toml_with_all_types)
# Create .env with custom variable
source.env_path.write_text("MY_CUSTOM_VAR=test\n")
bootstrapper = SettingsBootstrap(source, schema)
bootstrapper.bootstrap()
env_content = source.env_path.read_text()
# Check custom section exists
assert "# --- CUSTOM VARIABLES ---" in env_content
assert "MY_CUSTOM_VAR=test" in env_content
def test_preserves_custom_from_env_example(self, test_toml_with_all_types):
"""Test that custom variables in .env.example are preserved."""
from alfred.settings_schema import load_schema
schema = load_schema(test_toml_with_all_types)
source = ConfigSource.from_base_dir(test_toml_with_all_types)
bootstrapper = SettingsBootstrap(source, schema)
bootstrapper.bootstrap()
env_content = source.env_path.read_text()
# CUSTOM_VAR is in .env.example but not in schema
assert "CUSTOM_VAR=custom_value" in env_content
class TestBooleanHandling:
"""Test that booleans are handled correctly."""
def test_booleans_written_as_lowercase(self, test_toml_with_all_types):
"""Test that Python booleans are written as lowercase strings."""
from alfred.settings_schema import load_schema
schema = load_schema(test_toml_with_all_types)
source = ConfigSource.from_base_dir(test_toml_with_all_types)
bootstrapper = SettingsBootstrap(source, schema)
bootstrapper.bootstrap()
env_content = source.env_path.read_text()
# Boolean should be lowercase
assert "BOOL_VAR=true" in env_content
assert "BOOL_VAR=True" not in env_content
assert "BOOL_VAR=TRUE" not in env_content
def test_false_boolean_written_as_lowercase(self, tmp_path):
"""Test that False is written as 'false'."""
pyproject_content = """
[tool.poetry]
name = "test"
version = "1.0.0"
"""
(tmp_path / "pyproject.toml").write_text(pyproject_content)
settings_content = """
[tool.alfred.settings_schema.BOOL_FALSE]
type = "boolean"
source = "env"
default = false
category = "test"
"""
(tmp_path / "settings.toml").write_text(settings_content)
(tmp_path / ".env.example").write_text("BOOL_FALSE=\n")
from alfred.settings_schema import load_schema
schema = load_schema(tmp_path)
source = ConfigSource.from_base_dir(tmp_path)
bootstrapper = SettingsBootstrap(source, schema)
bootstrapper.bootstrap()
env_content = source.env_path.read_text()
assert "BOOL_FALSE=false" in env_content
assert "BOOL_FALSE=False" not in env_content
def test_boolean_parsing_from_env(self, tmp_path):
"""Test that various boolean formats are parsed correctly."""
pyproject_content = """
[tool.poetry]
name = "test"
version = "1.0.0"
"""
(tmp_path / "pyproject.toml").write_text(pyproject_content)
settings_content = """
[tool.alfred.settings_schema.BOOL_VAR]
type = "boolean"
source = "env"
default = false
category = "test"
"""
(tmp_path / "settings.toml").write_text(settings_content)
(tmp_path / ".env.example").write_text("BOOL_VAR=\n")
from alfred.settings_schema import load_schema
schema = load_schema(tmp_path)
source = ConfigSource.from_base_dir(tmp_path)
# Test different boolean formats
test_cases = [
("true", True),
("TRUE", True),
("True", True),
("1", True),
("yes", True),
("false", False),
("FALSE", False),
("False", False),
("0", False),
("no", False),
]
for input_val, expected in test_cases:
source.env_path.write_text(f"BOOL_VAR={input_val}\n")
bootstrapper = SettingsBootstrap(source, schema)
bootstrapper._load_sources()
bootstrapper._resolve_settings()
assert bootstrapper.resolved_settings["BOOL_VAR"] == expected
class TestComputedVariables:
"""Test that computed variables are calculated correctly."""
def test_computed_variables_written_to_env(self, test_toml_with_all_types):
"""Test that computed variables are written with their computed values."""
from alfred.settings_schema import load_schema
schema = load_schema(test_toml_with_all_types)
source = ConfigSource.from_base_dir(test_toml_with_all_types)
bootstrapper = SettingsBootstrap(source, schema)
bootstrapper.bootstrap()
env_content = source.env_path.read_text()
# Computed variable should have its computed value
assert "COMPUTED_VAR=default_string_42" in env_content
def test_computed_uri_example(self, tmp_path):
"""Test computed URI (like MONGO_URI) is written correctly."""
pyproject_content = """
[tool.poetry]
name = "test"
version = "1.0.0"
"""
(tmp_path / "pyproject.toml").write_text(pyproject_content)
settings_content = """
[tool.alfred.settings_schema.DB_HOST]
type = "string"
source = "env"
default = "localhost"
category = "database"
[tool.alfred.settings_schema.DB_PORT]
type = "integer"
source = "env"
default = 5432
category = "database"
[tool.alfred.settings_schema.DB_USER]
type = "string"
source = "env"
default = "user"
category = "database"
[tool.alfred.settings_schema.DB_PASSWORD]
type = "secret"
source = "generated"
secret_rule = "16:hex"
category = "security"
[tool.alfred.settings_schema.DB_URI]
type = "computed"
source = "computed"
compute_from = ["DB_USER", "DB_PASSWORD", "DB_HOST", "DB_PORT"]
compute_template = "postgresql://{DB_USER}:{DB_PASSWORD}@{DB_HOST}:{DB_PORT}/db"
category = "database"
"""
(tmp_path / "settings.toml").write_text(settings_content)
(tmp_path / ".env.example").write_text(
"DB_HOST=\nDB_PORT=\nDB_USER=\nDB_PASSWORD=\nDB_URI=\n"
)
from alfred.settings_schema import load_schema
schema = load_schema(tmp_path)
source = ConfigSource.from_base_dir(tmp_path)
bootstrapper = SettingsBootstrap(source, schema)
bootstrapper.bootstrap()
env_content = source.env_path.read_text()
# Check URI is computed and written
assert "DB_URI=postgresql://user:" in env_content
assert "@localhost:5432/db" in env_content
# Extract password from URI to verify it's the same as DB_PASSWORD
import re
uri_match = re.search(r"DB_URI=postgresql://user:([^@]+)@", env_content)
password_match = re.search(r"DB_PASSWORD=([^\n]+)", env_content)
assert uri_match and password_match
assert uri_match.group(1) == password_match.group(1)
class TestEdgeCases:
"""Test edge cases and error conditions."""
def test_missing_env_example(self, tmp_path):
"""Test that missing .env.example raises error."""
pyproject_content = """
[tool.poetry]
name = "test"
version = "1.0.0"
"""
(tmp_path / "pyproject.toml").write_text(pyproject_content)
settings_content = """
[tool.alfred.settings_schema.TEST_VAR]
type = "string"
source = "env"
default = "test"
category = "test"
"""
(tmp_path / "settings.toml").write_text(settings_content)
from alfred.settings_schema import load_schema
schema = load_schema(tmp_path)
source = ConfigSource.from_base_dir(tmp_path)
bootstrapper = SettingsBootstrap(source, schema)
with pytest.raises(FileNotFoundError, match=".env.example not found"):
bootstrapper.bootstrap()
def test_empty_env_example(self, tmp_path):
"""Test that empty .env.example works."""
pyproject_content = """
[tool.poetry]
name = "test"
version = "1.0.0"
"""
(tmp_path / "pyproject.toml").write_text(pyproject_content)
settings_content = """
[tool.alfred.settings_schema.TEST_VAR]
type = "string"
source = "env"
default = "test"
category = "test"
"""
(tmp_path / "settings.toml").write_text(settings_content)
(tmp_path / ".env.example").write_text("")
from alfred.settings_schema import load_schema
schema = load_schema(tmp_path)
source = ConfigSource.from_base_dir(tmp_path)
bootstrapper = SettingsBootstrap(source, schema)
bootstrapper.bootstrap()
# Should create .env even if .env.example is empty
assert source.env_path.exists()
def test_variable_with_equals_in_value(self, tmp_path):
"""Test that variables with '=' in their value are handled correctly."""
pyproject_content = """
[tool.poetry]
name = "test"
version = "1.0.0"
"""
(tmp_path / "pyproject.toml").write_text(pyproject_content)
settings_content = """
[tool.alfred.settings_schema.URL_VAR]
type = "string"
source = "env"
default = "http://example.com?key=value"
category = "test"
"""
(tmp_path / "settings.toml").write_text(settings_content)
(tmp_path / ".env.example").write_text("URL_VAR=\n")
from alfred.settings_schema import load_schema
schema = load_schema(tmp_path)
source = ConfigSource.from_base_dir(tmp_path)
bootstrapper = SettingsBootstrap(source, schema)
bootstrapper.bootstrap()
env_content = source.env_path.read_text()
assert "URL_VAR=http://example.com?key=value" in env_content
def test_preserves_existing_values_on_update(self, tmp_path):
"""Test that existing values are preserved when updating."""
pyproject_content = """
[tool.poetry]
name = "test"
version = "1.0.0"
"""
(tmp_path / "pyproject.toml").write_text(pyproject_content)
settings_content = """
[tool.alfred.settings_schema.VAR1]
type = "string"
source = "env"
default = "default1"
category = "test"
[tool.alfred.settings_schema.VAR2]
type = "string"
source = "env"
default = "default2"
category = "test"
"""
(tmp_path / "settings.toml").write_text(settings_content)
(tmp_path / ".env.example").write_text("VAR1=\nVAR2=\n")
from alfred.settings_schema import load_schema
schema = load_schema(tmp_path)
source = ConfigSource.from_base_dir(tmp_path)
# First bootstrap
bootstrapper1 = SettingsBootstrap(source, schema)
bootstrapper1.bootstrap()
# Modify values
source.env_path.write_text("VAR1=custom1\nVAR2=custom2\n")
# Second bootstrap
bootstrapper2 = SettingsBootstrap(source, schema)
bootstrapper2.bootstrap()
env_content = source.env_path.read_text()
# Custom values should be preserved
assert "VAR1=custom1" in env_content
assert "VAR2=custom2" in env_content
class TestIntegration:
"""Integration tests with realistic scenarios."""
def test_full_workflow_like_alfred(self, tmp_path):
"""Test a full workflow similar to Alfred's actual usage."""
pyproject_content = """
[tool.poetry]
name = "alfred"
version = "0.1.7"
[tool.poetry.dependencies]
python = "^3.14"
"""
(tmp_path / "pyproject.toml").write_text(pyproject_content)
settings_content = """
[tool.alfred.settings_schema.ALFRED_VERSION]
type = "string"
source = "toml"
toml_path = "tool.poetry.version"
category = "build"
export_to_env_make = true
[tool.alfred.settings_schema.HOST]
type = "string"
source = "env"
default = "0.0.0.0"
category = "app"
[tool.alfred.settings_schema.PORT]
type = "integer"
source = "env"
default = 3080
category = "app"
[tool.alfred.settings_schema.JWT_SECRET]
type = "secret"
source = "generated"
secret_rule = "32:hex"
category = "security"
[tool.alfred.settings_schema.MONGO_HOST]
type = "string"
source = "env"
default = "mongodb"
category = "database"
[tool.alfred.settings_schema.MONGO_PASSWORD]
type = "secret"
source = "generated"
secret_rule = "16:hex"
category = "security"
[tool.alfred.settings_schema.MONGO_URI]
type = "computed"
source = "computed"
compute_from = ["MONGO_HOST", "MONGO_PASSWORD"]
compute_template = "mongodb://user:{MONGO_PASSWORD}@{MONGO_HOST}:27017/db"
category = "database"
[tool.alfred.settings_schema.DEBUG_MODE]
type = "boolean"
source = "env"
default = false
category = "app"
"""
(tmp_path / "settings.toml").write_text(settings_content)
env_example_content = """# Application settings
HOST=0.0.0.0
PORT=3080
DEBUG_MODE=false
# Security
JWT_SECRET=
# Database
MONGO_HOST=mongodb
MONGO_PASSWORD=
MONGO_URI=
# Build info
ALFRED_VERSION=
"""
(tmp_path / ".env.example").write_text(env_example_content)
from alfred.settings_schema import load_schema
schema = load_schema(tmp_path)
source = ConfigSource.from_base_dir(tmp_path)
# First bootstrap
bootstrapper1 = SettingsBootstrap(source, schema)
bootstrapper1.bootstrap()
env_content_1 = source.env_path.read_text()
# Verify structure
assert "# Application settings" in env_content_1
assert "HOST=0.0.0.0" in env_content_1
assert "PORT=3080" in env_content_1
assert "DEBUG_MODE=false" in env_content_1 # lowercase!
assert "ALFRED_VERSION=0.1.7" in env_content_1
assert "JWT_SECRET=" in env_content_1
assert (
len(
[line for line in env_content_1.split("\n") if "JWT_SECRET=" in line][0]
)
> 20
)
assert "MONGO_URI=mongodb://user:" in env_content_1
# Second bootstrap - should preserve everything
bootstrapper2 = SettingsBootstrap(source, schema)
bootstrapper2.bootstrap()
env_content_2 = source.env_path.read_text()
# Everything should be identical
assert env_content_1 == env_content_2
# Add custom variable
with open(source.env_path, "a") as f:
f.write("\nMY_CUSTOM_SETTING=test123\n")
# Third bootstrap - should preserve custom
bootstrapper3 = SettingsBootstrap(source, schema)
bootstrapper3.bootstrap()
env_content_3 = source.env_path.read_text()
assert "MY_CUSTOM_SETTING=test123" in env_content_3
assert "# --- CUSTOM VARIABLES ---" in env_content_3
+332
View File
@@ -0,0 +1,332 @@
"""Tests for settings schema parser."""
import pytest
from alfred.settings_schema import (
SettingDefinition,
SettingSource,
SettingType,
load_schema,
validate_value,
)
@pytest.fixture
def minimal_schema_toml():
"""Minimal valid schema TOML."""
return """
[tool.alfred.settings_schema.TEST_STRING]
type = "string"
source = "env"
default = "test_value"
description = "Test string setting"
category = "test"
[tool.alfred.settings_schema.TEST_INTEGER]
type = "integer"
source = "env"
default = 42
description = "Test integer setting"
category = "test"
validator = "range:1:100"
[tool.alfred.settings_schema.TEST_SECRET]
type = "secret"
source = "generated"
secret_rule = "32:b64"
description = "Test secret"
category = "security"
required = true
[tool.alfred.settings_schema.TEST_COMPUTED]
type = "computed"
source = "computed"
compute_from = ["TEST_STRING", "TEST_INTEGER"]
compute_template = "{TEST_STRING}_{TEST_INTEGER}"
description = "Test computed"
category = "test"
[tool.alfred.settings_schema.TEST_OPTIONAL]
type = "string"
source = "env"
required = false
description = "Optional setting"
category = "test"
"""
@pytest.fixture
def create_schema_file(tmp_path):
"""Factory to create pyproject.toml with schema."""
def _create(content: str):
toml_path = tmp_path / "settings.toml"
full_content = f"""
[tool.poetry]
name = "test"
version = "1.0.0"
{content}
"""
toml_path.write_text(full_content)
return tmp_path
return _create
class TestSettingDefinition:
"""Test SettingDefinition dataclass."""
def test_create_definition(self):
"""Test creating a setting definition."""
definition = SettingDefinition(
name="TEST_SETTING",
type=SettingType.STRING,
source=SettingSource.ENV,
description="Test setting",
category="test",
default="default_value",
)
assert definition.name == "TEST_SETTING"
assert definition.type == SettingType.STRING
assert definition.source == SettingSource.ENV
assert definition.default == "default_value"
assert definition.required is True # Default
class TestSettingsSchema:
"""Test SettingsSchema parser."""
def test_parse_schema(self, create_schema_file, minimal_schema_toml):
"""Test parsing schema from TOML."""
base_dir = create_schema_file(minimal_schema_toml)
schema = load_schema(base_dir)
assert len(schema) == 5
assert "TEST_STRING" in schema.definitions
assert "TEST_INTEGER" in schema.definitions
assert "TEST_SECRET" in schema.definitions
assert "TEST_COMPUTED" in schema.definitions
assert "TEST_OPTIONAL" in schema.definitions
def test_get_definition(self, create_schema_file, minimal_schema_toml):
"""Test getting a definition by name."""
base_dir = create_schema_file(minimal_schema_toml)
schema = load_schema(base_dir)
definition = schema.get("TEST_STRING")
assert definition is not None
assert definition.name == "TEST_STRING"
assert definition.type == SettingType.STRING
assert definition.default == "test_value"
def test_get_by_category(self, create_schema_file, minimal_schema_toml):
"""Test getting definitions by category."""
base_dir = create_schema_file(minimal_schema_toml)
schema = load_schema(base_dir)
test_settings = schema.get_by_category("test")
assert len(test_settings) == 4
security_settings = schema.get_by_category("security")
assert len(security_settings) == 1
def test_get_by_source(self, create_schema_file, minimal_schema_toml):
"""Test getting definitions by source."""
base_dir = create_schema_file(minimal_schema_toml)
schema = load_schema(base_dir)
env_settings = schema.get_by_source(SettingSource.ENV)
assert len(env_settings) == 3
generated_settings = schema.get_by_source(SettingSource.GENERATED)
assert len(generated_settings) == 1
computed_settings = schema.get_by_source(SettingSource.COMPUTED)
assert len(computed_settings) == 1
def test_get_required(self, create_schema_file, minimal_schema_toml):
"""Test getting required settings."""
base_dir = create_schema_file(minimal_schema_toml)
schema = load_schema(base_dir)
required = schema.get_required()
# TEST_OPTIONAL is not required
assert len(required) == 4
def test_parse_types(self, create_schema_file):
"""Test parsing different setting types."""
schema_toml = """
[tool.alfred.settings_schema.STR_SETTING]
type = "string"
source = "env"
default = "text"
[tool.alfred.settings_schema.INT_SETTING]
type = "integer"
source = "env"
default = 42
[tool.alfred.settings_schema.FLOAT_SETTING]
type = "float"
source = "env"
default = 3.14
[tool.alfred.settings_schema.BOOL_SETTING]
type = "boolean"
source = "env"
default = true
"""
base_dir = create_schema_file(schema_toml)
schema = load_schema(base_dir)
assert schema.get("STR_SETTING").default == "text"
assert schema.get("INT_SETTING").default == 42
assert schema.get("FLOAT_SETTING").default == 3.14
assert schema.get("BOOL_SETTING").default is True
def test_missing_schema_section(self, tmp_path):
"""Test error when schema section is missing."""
toml_path = tmp_path / "settings.toml"
toml_path.write_text("""
[tool.poetry]
name = "test"
version = "1.0.0"
""")
with pytest.raises(KeyError, match="settings_schema"):
load_schema(tmp_path)
class TestValidateValue:
"""Test value validation."""
def test_validate_string(self):
"""Test validating string values."""
definition = SettingDefinition(
name="TEST",
type=SettingType.STRING,
source=SettingSource.ENV,
)
assert validate_value(definition, "test") is True
with pytest.raises(ValueError, match="must be string"):
validate_value(definition, 123)
def test_validate_integer(self):
"""Test validating integer values."""
definition = SettingDefinition(
name="TEST",
type=SettingType.INTEGER,
source=SettingSource.ENV,
)
assert validate_value(definition, 42) is True
with pytest.raises(ValueError, match="must be integer"):
validate_value(definition, "not an int")
def test_validate_float(self):
"""Test validating float values."""
definition = SettingDefinition(
name="TEST",
type=SettingType.FLOAT,
source=SettingSource.ENV,
)
assert validate_value(definition, 3.14) is True
assert validate_value(definition, 42) is True # int is ok for float
with pytest.raises(ValueError, match="must be float"):
validate_value(definition, "not a float")
def test_validate_required(self):
"""Test validating required settings."""
definition = SettingDefinition(
name="TEST",
type=SettingType.STRING,
source=SettingSource.ENV,
required=True,
)
with pytest.raises(ValueError, match="is required"):
validate_value(definition, None)
def test_validate_optional(self):
"""Test validating optional settings."""
definition = SettingDefinition(
name="TEST",
type=SettingType.STRING,
source=SettingSource.ENV,
required=False,
)
assert validate_value(definition, None) is True
def test_validate_range(self):
"""Test range validator."""
definition = SettingDefinition(
name="TEST",
type=SettingType.INTEGER,
source=SettingSource.ENV,
validator="range:1:100",
)
assert validate_value(definition, 50) is True
assert validate_value(definition, 1) is True
assert validate_value(definition, 100) is True
with pytest.raises(ValueError, match=r"must be between .* and .*, got"):
validate_value(definition, 0)
with pytest.raises(ValueError, match=r"must be between .* and .*, got"):
validate_value(definition, 101)
def test_validate_float_range(self):
"""Test range validator with floats."""
definition = SettingDefinition(
name="TEST",
type=SettingType.FLOAT,
source=SettingSource.ENV,
validator="range:0.0:2.0",
)
assert validate_value(definition, 1.5) is True
assert validate_value(definition, 0.0) is True
assert validate_value(definition, 2.0) is True
with pytest.raises(ValueError, match="must be between 0.0 and 2.0"):
validate_value(definition, -0.1)
with pytest.raises(ValueError, match="must be between 0.0 and 2.0"):
validate_value(definition, 2.1)
def test_invalid_validator(self):
"""Test unknown validator raises error."""
definition = SettingDefinition(
name="TEST",
type=SettingType.STRING,
source=SettingSource.ENV,
validator="unknown:validator",
)
with pytest.raises(ValueError, match="Unknown validator"):
validate_value(definition, "test")
class TestSchemaIteration:
"""Test schema iteration."""
def test_iterate_schema(self, create_schema_file, minimal_schema_toml):
"""Test iterating over schema definitions."""
base_dir = create_schema_file(minimal_schema_toml)
schema = load_schema(base_dir)
definitions = list(schema)
assert len(definitions) == 5
names = [d.name for d in definitions]
assert "TEST_STRING" in names
assert "TEST_INTEGER" in names