Files
romm/backend/tests/test_env_template.py
Spinnich 24c1c4fe26 fix(env): quote empty values in env.template so Compose reads them as empty (#5649)
* fix(env): quote empty values in env.template so Compose reads them as empty

Docker Compose's dotenv parser only treats `  #` as an inline comment
after a value. For `KEY=  # comment` it takes the comment text as the
value, both for env_file and for ${KEY:-default} interpolation in
docker-compose.yml. A .env copied from the template therefore hands 46
settings their description instead of an empty string, and the dev
backend fails at startup on OIDC_AUTH_PARAMS ("not valid JSON").

Quoting the empty values (`KEY=""  # comment`) parses as empty in both
Compose and python-dotenv. The docs generator already strips surrounding
quotes, so the env var reference renders unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* test(env): fail when env.template leaves an empty value unquoted

Docker Compose reads `KEY=  # comment` as the value "# comment", so a
new setting added in that shape would break .env files copied from the
template again.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(env): code-review pass on env.template quoting

Tell users to single-quote JSON values, since double-quoted JSON inside
"" breaks Compose's .env parsing. Point REDIS_SAVE_POLICY at "" for
disabling snapshots, and widen the guard test to catch KEY=#comment.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* docs(env): humanizer pass on env.template comments

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-18 18:50:13 -05:00

58 lines
1.9 KiB
Python

"""Guards env.template, which generates the docs' env var reference and seeds users' .env."""
import ast
import re
from pathlib import Path
import pytest
REPO_ROOT = Path(__file__).resolve().parents[2]
CONFIG_MODULE = REPO_ROOT / "backend" / "config" / "__init__.py"
ENV_TEMPLATE = REPO_ROOT / "env.template"
# The dev container only mounts backend/, so env.template isn't there.
pytestmark = pytest.mark.skipif(
not ENV_TEMPLATE.exists(), reason="env.template is not in this checkout"
)
ENV_READERS = {"_get_env", "_get_env_list", "_get_env_set"}
# Set by pytest itself, not by users.
NOT_USER_SETTINGS = {"PYTEST_VERSION"}
def _env_vars_read_by_config() -> set[str]:
names = set()
for node in ast.walk(ast.parse(CONFIG_MODULE.read_text())):
if (
isinstance(node, ast.Call)
and isinstance(node.func, ast.Name)
and node.func.id in ENV_READERS
and node.args
and isinstance(node.args[0], ast.Constant)
and isinstance(node.args[0].value, str)
):
names.add(node.args[0].value)
return names - NOT_USER_SETTINGS
def _env_vars_in_template() -> set[str]:
names = set()
for line in ENV_TEMPLATE.read_text().splitlines():
key, sep, _ = line.lstrip("# ").partition("=")
if sep and key.isupper():
names.add(key)
return names
def test_env_template_lists_every_env_var_the_config_reads() -> None:
missing = _env_vars_read_by_config() - _env_vars_in_template()
assert not missing, f"env.template is missing {sorted(missing)}"
# Docker Compose reads `KEY= # comment` as the value "# comment".
def test_env_template_quotes_empty_values() -> None:
unquoted = re.findall(
r"^([A-Z][A-Z0-9_]*)=[ \t]*#", ENV_TEMPLATE.read_text(), re.MULTILINE
)
assert not unquoted, f'env.template leaves {unquoted} empty without ""'