105 lines
3.1 KiB
Python
105 lines
3.1 KiB
Python
"""Unit tests for the .env.example generator."""
|
|
|
|
import pytest
|
|
|
|
from envschema.schema import Schema, EnvVar, EnvVarType
|
|
from envschema.generator import generate_env_example, generate_env_example_to_file
|
|
|
|
|
|
class TestGenerateEnvExample:
|
|
"""Tests for generate_env_example function."""
|
|
|
|
def test_empty_schema(self):
|
|
schema = Schema()
|
|
result = generate_env_example(schema)
|
|
assert "# Environment Variables Schema" in result
|
|
|
|
def test_basic_variable(self):
|
|
schema = Schema(
|
|
envvars=[
|
|
EnvVar(name="TEST_VAR", type=EnvVarType.STRING),
|
|
]
|
|
)
|
|
result = generate_env_example(schema)
|
|
assert "TEST_VAR=" in result
|
|
|
|
def test_required_variable(self):
|
|
schema = Schema(
|
|
envvars=[
|
|
EnvVar(name="REQUIRED_VAR", required=True),
|
|
]
|
|
)
|
|
result = generate_env_example(schema)
|
|
assert "# REQUIRED" in result
|
|
assert "REQUIRED_VAR=" in result
|
|
|
|
def test_variable_with_default(self):
|
|
schema = Schema(
|
|
envvars=[
|
|
EnvVar(name="VAR_WITH_DEFAULT", default="default_value"),
|
|
]
|
|
)
|
|
result = generate_env_example(schema)
|
|
assert "VAR_WITH_DEFAULT=default_value" in result
|
|
|
|
def test_variable_with_description(self):
|
|
schema = Schema(
|
|
envvars=[
|
|
EnvVar(
|
|
name="DESCRIBED_VAR",
|
|
description="This is a description",
|
|
),
|
|
]
|
|
)
|
|
result = generate_env_example(schema)
|
|
assert "# This is a description" in result
|
|
|
|
def test_variable_with_type(self):
|
|
schema = Schema(
|
|
envvars=[
|
|
EnvVar(name="INT_VAR", type=EnvVarType.INTEGER),
|
|
]
|
|
)
|
|
result = generate_env_example(schema)
|
|
assert "INT_VAR=" in result
|
|
|
|
def test_no_descriptions(self):
|
|
schema = Schema(
|
|
envvars=[
|
|
EnvVar(
|
|
name="VAR",
|
|
description="Some description",
|
|
),
|
|
]
|
|
)
|
|
result = generate_env_example(schema, include_descriptions=False)
|
|
assert "Some description" not in result
|
|
|
|
def test_multiple_variables(self):
|
|
schema = Schema(
|
|
envvars=[
|
|
EnvVar(name="VAR1", required=True, description="First var"),
|
|
EnvVar(name="VAR2", default="value"),
|
|
EnvVar(name="VAR3", type=EnvVarType.INTEGER),
|
|
]
|
|
)
|
|
result = generate_env_example(schema)
|
|
assert "VAR1=" in result
|
|
assert "VAR2=value" in result
|
|
assert "VAR3=" in result
|
|
|
|
|
|
class TestGenerateEnvExampleToFile:
|
|
"""Tests for generate_env_example_to_file function."""
|
|
|
|
def test_write_to_file(self, tmp_path):
|
|
schema = Schema(
|
|
envvars=[
|
|
EnvVar(name="TEST_VAR"),
|
|
]
|
|
)
|
|
output_path = tmp_path / ".env.example"
|
|
generate_env_example_to_file(schema, str(output_path))
|
|
|
|
content = output_path.read_text()
|
|
assert "TEST_VAR=" in content |