Files
gyxx-flow/tests/test_runtime_env_loading.py
T

78 lines
2.3 KiB
Python

from __future__ import annotations
import argparse
from pathlib import Path
import pytest
from gyxx_flow.cli import (
CliConfigurationError,
_load_runtime_environment_files,
build_parser,
)
def test_console_and_scheduler_accept_explicit_repeatable_env_files() -> None:
parser = build_parser()
console = parser.parse_args(
["console", "--env-file", "database.env", "--env-file", "notify.env"]
)
scheduler = parser.parse_args(
["schedule", "run", "--env-file", "production.env"]
)
assert console.env_file == [Path("database.env"), Path("notify.env")]
assert scheduler.env_file == [Path("production.env")]
def test_runtime_env_files_load_values_without_overriding_process_env(
tmp_path: Path,
monkeypatch: pytest.MonkeyPatch,
) -> None:
first = tmp_path / "database.env"
second = tmp_path / "notifications.env"
first.write_text(
"GYXX_RUNTIME_ENV_TEST_DB=cloud\nGYXX_RUNTIME_ENV_TEST_KEEP=file\n",
encoding="utf-8",
)
second.write_text("GYXX_RUNTIME_ENV_TEST_NOTIFY=owner\n", encoding="utf-8")
monkeypatch.delenv("GYXX_RUNTIME_ENV_TEST_DB", raising=False)
monkeypatch.delenv("GYXX_RUNTIME_ENV_TEST_NOTIFY", raising=False)
monkeypatch.setenv("GYXX_RUNTIME_ENV_TEST_KEEP", "process")
_load_runtime_environment_files(
argparse.Namespace(env_file=[first, second])
)
import os
assert os.environ["GYXX_RUNTIME_ENV_TEST_DB"] == "cloud"
assert os.environ["GYXX_RUNTIME_ENV_TEST_NOTIFY"] == "owner"
assert os.environ["GYXX_RUNTIME_ENV_TEST_KEEP"] == "process"
def test_runtime_env_file_fills_an_empty_process_placeholder(
tmp_path: Path,
monkeypatch: pytest.MonkeyPatch,
) -> None:
env_file = tmp_path / "production.env"
env_file.write_text(
"GYXX_RUNTIME_ENV_TEST_EMPTY=from-file\n",
encoding="utf-8",
)
monkeypatch.setenv("GYXX_RUNTIME_ENV_TEST_EMPTY", "")
_load_runtime_environment_files(argparse.Namespace(env_file=[env_file]))
import os
assert os.environ["GYXX_RUNTIME_ENV_TEST_EMPTY"] == "from-file"
def test_runtime_env_file_must_exist(tmp_path: Path) -> None:
missing = tmp_path / "missing.env"
with pytest.raises(CliConfigurationError, match="runtime environment file not found"):
_load_runtime_environment_files(argparse.Namespace(env_file=[missing]))