forge-tools/tests/test_configuration.py

63 lines
1.6 KiB
Python
Raw Normal View History

import pytest
import yaml
from frg.configuration import Config, get_configuration
@pytest.fixture(autouse=True)
def mock_env(tmp_path, monkeypatch):
monkeypatch.setenv("HOME", str(tmp_path))
def test_get_configuration_fails_if_noexist():
with pytest.raises(FileNotFoundError):
get_configuration(path="does-not-exist.yml")
def test_get_configuration_default_if_no_path_provided(tmp_path):
config_dir = tmp_path / ".config"
config_file = config_dir / "frg.yaml"
config_dir.mkdir()
mock_config = {"domain_aliases": {"a": "b"}}
config_file.write_text(yaml.dump(mock_config))
config_data = get_configuration()
assert config_data.domain_aliases == mock_config["domain_aliases"]
def test_get_configuration_use_path_provided(tmp_path):
config_dir = tmp_path / ".config"
config_file = config_dir / "frg.yaml"
custom_config_file = tmp_path / "my_config.yaml"
config_dir.mkdir()
mock_config = {"domain_aliases": {"a": "b"}}
mock_config_ignored = {"domain_aliases": {"c": "d"}}
config_file.write_text(yaml.dump(mock_config_ignored))
custom_config_file.write_text(yaml.dump(mock_config))
config_data = get_configuration(path=str(custom_config_file))
assert config_data.domain_aliases == mock_config["domain_aliases"]
def test_get_configuration_parses_config(tmp_path):
config_dir = tmp_path / ".config"
config_file = config_dir / "frg.yaml"
config_dir.mkdir()
mock_config = {"domain_aliases": {"a": "b"}}
expected = Config(**mock_config)
config_file.write_text(yaml.dump(mock_config))
config_data = get_configuration()
assert config_data == expected