2024-12-15 14:40:03 +01:00
|
|
|
import tempfile
|
2024-11-11 21:38:13 +01:00
|
|
|
from pathlib import Path
|
2024-12-30 13:41:39 +01:00
|
|
|
from unittest.mock import patch
|
2024-11-11 21:38:13 +01:00
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
2024-12-30 13:41:39 +01:00
|
|
|
from akkudoktoreos.config.config import ConfigEOS
|
2024-12-15 14:40:03 +01:00
|
|
|
from akkudoktoreos.utils.logutil import get_logger
|
2024-11-11 21:38:13 +01:00
|
|
|
|
2024-12-15 14:40:03 +01:00
|
|
|
logger = get_logger(__name__)
|
2024-11-11 21:38:13 +01:00
|
|
|
|
|
|
|
|
2024-12-30 13:41:39 +01:00
|
|
|
# overwrite config_mixin fixture from conftest
|
|
|
|
@pytest.fixture(autouse=True)
|
|
|
|
def config_mixin():
|
|
|
|
pass
|
2024-11-11 21:38:13 +01:00
|
|
|
|
2024-12-15 14:40:03 +01:00
|
|
|
|
2024-12-30 13:41:39 +01:00
|
|
|
def test_fixture_new_config_file(config_default_dirs):
|
2024-12-15 14:40:03 +01:00
|
|
|
"""Assure fixture stash_config_file is working."""
|
2024-12-30 13:41:39 +01:00
|
|
|
config_default_dir_user, config_default_dir_cwd, _, _ = config_default_dirs
|
2024-12-15 14:40:03 +01:00
|
|
|
|
2024-12-30 13:41:39 +01:00
|
|
|
config_file_path_user = config_default_dir_user.joinpath(ConfigEOS.CONFIG_FILE_NAME)
|
|
|
|
config_file_path_cwd = config_default_dir_cwd.joinpath(ConfigEOS.CONFIG_FILE_NAME)
|
2024-12-15 14:40:03 +01:00
|
|
|
|
|
|
|
assert not config_file_path_user.exists()
|
|
|
|
assert not config_file_path_cwd.exists()
|
2024-11-11 21:38:13 +01:00
|
|
|
|
|
|
|
|
2024-12-30 13:41:39 +01:00
|
|
|
def test_config_constants(config_eos):
|
2024-12-15 14:40:03 +01:00
|
|
|
"""Test config constants are the way expected by the tests."""
|
|
|
|
assert config_eos.APP_NAME == "net.akkudoktor.eos"
|
|
|
|
assert config_eos.APP_AUTHOR == "akkudoktor"
|
|
|
|
assert config_eos.EOS_DIR == "EOS_DIR"
|
|
|
|
assert config_eos.ENCODING == "UTF-8"
|
|
|
|
assert config_eos.CONFIG_FILE_NAME == "EOS.config.json"
|
2024-11-11 21:38:13 +01:00
|
|
|
|
|
|
|
|
2024-12-30 13:41:39 +01:00
|
|
|
def test_computed_paths(config_eos):
|
2024-12-15 14:40:03 +01:00
|
|
|
"""Test computed paths for output and cache."""
|
|
|
|
config_eos.merge_settings_from_dict(
|
|
|
|
{
|
|
|
|
"data_folder_path": "/base/data",
|
|
|
|
"data_output_subpath": "output",
|
|
|
|
"data_cache_subpath": "cache",
|
2024-11-11 21:38:13 +01:00
|
|
|
}
|
2024-12-15 14:40:03 +01:00
|
|
|
)
|
|
|
|
assert config_eos.data_output_path == Path("/base/data/output")
|
|
|
|
assert config_eos.data_cache_path == Path("/base/data/cache")
|
2024-12-30 13:41:39 +01:00
|
|
|
# reset settings so the config_eos fixture can verify the default paths
|
|
|
|
config_eos.reset_settings()
|
2024-11-11 21:38:13 +01:00
|
|
|
|
|
|
|
|
2024-12-30 13:41:39 +01:00
|
|
|
def test_singleton_behavior(config_eos, config_default_dirs):
|
2024-12-15 14:40:03 +01:00
|
|
|
"""Test that ConfigEOS behaves as a singleton."""
|
2024-12-30 13:41:39 +01:00
|
|
|
initial_cfg_file = config_eos.config_file_path
|
|
|
|
with patch(
|
|
|
|
"akkudoktoreos.config.config.user_config_dir", return_value=str(config_default_dirs[0])
|
|
|
|
):
|
|
|
|
instance1 = ConfigEOS()
|
|
|
|
instance2 = ConfigEOS()
|
|
|
|
assert instance1 is config_eos
|
2024-12-15 14:40:03 +01:00
|
|
|
assert instance1 is instance2
|
2024-12-30 13:41:39 +01:00
|
|
|
assert instance1.config_file_path == initial_cfg_file
|
2024-11-11 21:38:13 +01:00
|
|
|
|
|
|
|
|
2024-12-30 13:41:39 +01:00
|
|
|
def test_default_config_path(config_eos, config_default_dirs):
|
2024-12-15 14:40:03 +01:00
|
|
|
"""Test that the default config file path is computed correctly."""
|
2024-12-30 13:41:39 +01:00
|
|
|
_, _, config_default_dir_default, _ = config_default_dirs
|
2024-11-11 21:38:13 +01:00
|
|
|
|
2024-12-15 14:40:03 +01:00
|
|
|
expected_path = config_default_dir_default.joinpath("default.config.json")
|
|
|
|
assert config_eos.config_default_file_path == expected_path
|
|
|
|
assert config_eos.config_default_file_path.is_file()
|
2024-11-11 21:38:13 +01:00
|
|
|
|
|
|
|
|
2024-12-30 13:41:39 +01:00
|
|
|
@patch("akkudoktoreos.config.config.user_config_dir")
|
|
|
|
def test_get_config_file_path(user_config_dir_patch, config_eos, config_default_dirs, monkeypatch):
|
|
|
|
"""Test that _get_config_file_path identifies the correct config file."""
|
|
|
|
config_default_dir_user, _, _, _ = config_default_dirs
|
|
|
|
user_config_dir_patch.return_value = str(config_default_dir_user)
|
|
|
|
|
|
|
|
def cfg_file(dir: Path) -> Path:
|
|
|
|
return dir.joinpath(ConfigEOS.CONFIG_FILE_NAME)
|
|
|
|
|
|
|
|
# Config newly created from fixture with fresh user config directory
|
|
|
|
assert config_eos._get_config_file_path() == (cfg_file(config_default_dir_user), True)
|
|
|
|
cfg_file(config_default_dir_user).unlink()
|
|
|
|
|
|
|
|
with tempfile.TemporaryDirectory() as temp_dir:
|
|
|
|
temp_dir_path = Path(temp_dir)
|
|
|
|
monkeypatch.setenv("EOS_DIR", str(temp_dir_path))
|
|
|
|
assert config_eos._get_config_file_path() == (cfg_file(temp_dir_path), False)
|
2024-11-11 21:38:13 +01:00
|
|
|
|
2024-12-30 13:41:39 +01:00
|
|
|
monkeypatch.setenv("EOS_CONFIG_DIR", "config")
|
|
|
|
assert config_eos._get_config_file_path() == (
|
|
|
|
cfg_file(temp_dir_path / "config"),
|
|
|
|
False,
|
|
|
|
)
|
2024-12-15 14:40:03 +01:00
|
|
|
|
2024-12-30 13:41:39 +01:00
|
|
|
monkeypatch.setenv("EOS_CONFIG_DIR", str(temp_dir_path / "config2"))
|
|
|
|
assert config_eos._get_config_file_path() == (
|
|
|
|
cfg_file(temp_dir_path / "config2"),
|
|
|
|
False,
|
|
|
|
)
|
2024-12-15 14:40:03 +01:00
|
|
|
|
2024-12-30 13:41:39 +01:00
|
|
|
monkeypatch.delenv("EOS_DIR")
|
|
|
|
monkeypatch.setenv("EOS_CONFIG_DIR", "config3")
|
|
|
|
assert config_eos._get_config_file_path() == (cfg_file(config_default_dir_user), False)
|
2024-12-15 14:40:03 +01:00
|
|
|
|
2024-12-30 13:41:39 +01:00
|
|
|
monkeypatch.setenv("EOS_CONFIG_DIR", str(temp_dir_path / "config3"))
|
|
|
|
assert config_eos._get_config_file_path() == (
|
|
|
|
cfg_file(temp_dir_path / "config3"),
|
|
|
|
False,
|
|
|
|
)
|
2024-12-15 14:40:03 +01:00
|
|
|
|
|
|
|
|
2024-12-30 13:41:39 +01:00
|
|
|
def test_config_copy(config_eos, monkeypatch):
|
2024-12-15 14:40:03 +01:00
|
|
|
"""Test if the config is copied to the provided path."""
|
2024-12-30 13:41:39 +01:00
|
|
|
with tempfile.TemporaryDirectory() as temp_dir:
|
|
|
|
temp_folder_path = Path(temp_dir)
|
|
|
|
temp_config_file_path = temp_folder_path.joinpath(config_eos.CONFIG_FILE_NAME).resolve()
|
|
|
|
monkeypatch.setenv(config_eos.EOS_DIR, str(temp_folder_path))
|
|
|
|
assert not temp_config_file_path.exists()
|
|
|
|
with patch("akkudoktoreos.config.config.user_config_dir", return_value=temp_dir):
|
|
|
|
assert config_eos._get_config_file_path() == (temp_config_file_path, False)
|
|
|
|
config_eos.from_config_file()
|
|
|
|
assert temp_config_file_path.exists()
|