Files
cpl/test/core/environment/environment_test.py
clu 27205022a5
Some checks failed
Test before pr merge / test-lint (pull_request) Successful in 8s
Test before pr merge / test (pull_request) Failing after 36s
test(core): extend coverage — console, errors, log, service, time, benchmark
Add missing test modules for previously untested core areas:
- console: ForegroundColorEnum, BackgroundColorEnum, Console methods
- errors: dependency_error, module_dependency_error
- log: LogLevel ordering/values, LogSettings, Logger (should_log, format, file write, fatal)
- service: HostedService, StartupTask, CronjobABC (start/stop/loop/task cancellation)
- time: TimeFormatSettings properties and setters
- utils: Benchmark.time / .memory / .all call-count and output

Also fix existing test files: environment cleanup, cron exception specificity,
json_processor kwargs bug doc, configuration_model_abc to_dict bug doc.
All 199 tests pass, black clean.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-13 19:34:52 +02:00

105 lines
2.6 KiB
Python

import os
import pytest
from cpl.core.environment.environment import Environment
from cpl.core.environment.environment_enum import EnvironmentEnum
@pytest.fixture(autouse=True)
def restore_env():
"""Restore touched env vars after each test to prevent pollution."""
original = dict(os.environ)
yield
for key in list(os.environ.keys()):
if key not in original:
del os.environ[key]
elif os.environ[key] != original[key]:
os.environ[key] = original[key]
def test_set_and_get_environment_valid():
for env in EnvironmentEnum:
Environment.set_environment(env.value)
assert Environment.get_environment() == env.value
def test_set_environment_invalid():
with pytest.raises(AssertionError):
Environment.set_environment("invalid_env")
def test_set_environment_empty():
with pytest.raises(AssertionError):
Environment.set_environment("")
def test_set_environment_none():
with pytest.raises(AssertionError):
Environment.set_environment(None)
def test_set_and_get_app_name():
Environment.set_app_name("TestApp")
assert Environment.get_app_name() == "TestApp"
def test_get_host_name():
hostname = Environment.get_host_name()
assert isinstance(hostname, str)
assert len(hostname) > 0
def test_get_cwd():
cwd = Environment.get_cwd()
assert isinstance(cwd, str)
assert os.path.isdir(cwd)
def test_set_cwd(tmp_path):
original = Environment.get_cwd()
Environment.set_cwd(str(tmp_path))
assert Environment.get_cwd() == str(tmp_path)
Environment.set_cwd(original)
def test_set_cwd_empty():
with pytest.raises(AssertionError):
Environment.set_cwd("")
def test_set_cwd_none():
with pytest.raises(AssertionError):
Environment.set_cwd(None)
def test_set_and_get_generic():
Environment.set("CPL_TEST_KEY", "hello")
assert Environment.get("CPL_TEST_KEY", str) == "hello"
def test_get_missing_key_returns_default():
result = Environment.get("CPL_NONEXISTENT_KEY_XYZ", str, "default_value")
assert result == "default_value"
def test_get_missing_key_returns_none():
result = Environment.get("CPL_NONEXISTENT_KEY_XYZ2", str)
assert result is None
def test_set_key_empty():
with pytest.raises(AssertionError):
Environment.set("", "value")
def test_set_key_none():
with pytest.raises(AssertionError):
Environment.set(None, "value")
def test_environment_enum_values():
values = [e.value for e in EnvironmentEnum]
assert "production" in values
assert "staging" in values
assert "testing" in values
assert "development" in values