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