import os from pathlib import Path import pytest from dynaconf import Validator from pyinfra.config.loader import load_settings, local_pyinfra_root_path, normalize_to_settings_files from pyinfra.config.validators import webserver_validators @pytest.fixture def test_validators(): return [ Validator("test.value.int", must_exist=True, is_type_of=int), Validator("test.value.str", must_exist=True, is_type_of=str), ] class TestConfig: def test_config_validation(self): os.environ["WEBSERVER__HOST"] = "localhost" os.environ["WEBSERVER__PORT"] = "8080" validators = webserver_validators test_settings = load_settings(root_path=local_pyinfra_root_path, validators=validators) assert test_settings.webserver.host == "localhost" def test_env_into_correct_type_conversion(self, test_validators): os.environ["TEST__VALUE__INT"] = "1" os.environ["TEST__VALUE__STR"] = "test" test_settings = load_settings(root_path=local_pyinfra_root_path, validators=test_validators) assert test_settings.test.value.int == 1 assert test_settings.test.value.str == "test" @pytest.mark.parametrize( "settings_path,expected_file_paths", [ (None, []), ("config", [f"{local_pyinfra_root_path}/config/settings.toml"]), ("config/settings.toml", [f"{local_pyinfra_root_path}/config/settings.toml"]), (f"{local_pyinfra_root_path}/config", [f"{local_pyinfra_root_path}/config/settings.toml"]), ], ) def test_normalize_settings_files(self, settings_path, expected_file_paths): files = normalize_to_settings_files(settings_path, local_pyinfra_root_path) print(files) assert len(files) == len(expected_file_paths) for path, expected in zip(files, expected_file_paths): assert path == Path(expected).absolute()