Files
photoanalyzer/tests/characterization/test_pa_config.py

47 lines
1.6 KiB
Python

"""Characterize configuration behavior (donor: load_env_file, _env_str,
_env_int, _env_bool)."""
import os
import photo_analyzer as pa
def test_load_env_file_parsing(tmp_path, monkeypatch):
env = tmp_path / pa.ENV_FILE
env.write_text(
"# comment\n"
"PLAIN=value\n"
"export EXPORTED=yes\n"
'QUOTED="hash # kept"\n'
"INLINE=val # comment stripped\n"
"TOKEN=pass#word\n"
"PRESET=file-loses\n"
"NOEQUALS\n"
)
monkeypatch.chdir(tmp_path)
for k in ("PLAIN", "EXPORTED", "QUOTED", "INLINE", "TOKEN"):
monkeypatch.delenv(k, raising=False)
monkeypatch.setenv("PRESET", "shell-wins")
used = pa.load_env_file()
assert used == env
assert os.environ["PLAIN"] == "value"
assert os.environ["EXPORTED"] == "yes", "'export ' prefix accepted"
assert os.environ["QUOTED"] == "hash # kept", "quoted # not a comment"
assert os.environ["INLINE"] == "val", "inline comment stripped"
assert os.environ["TOKEN"] == "pass#word", "mid-token # kept"
assert os.environ["PRESET"] == "shell-wins", "shell env always wins"
def test_env_helpers(monkeypatch):
monkeypatch.setenv("S", "")
assert pa._env_str("S", "fb") == "fb", "empty string falls back"
monkeypatch.setenv("I", "not-int")
assert pa._env_int("I", 7) == 7, "bad int falls back with warning"
monkeypatch.setenv("I", "42")
assert pa._env_int("I", 7) == 42
for truthy in ("1", "true", "YES", "On"):
monkeypatch.setenv("B", truthy)
assert pa._env_bool("B") is True
monkeypatch.setenv("B", "0")
assert pa._env_bool("B") is False