Skip to content

Commit

Permalink
Tests: Ignore config in current test execution directory. Closes #342 (
Browse files Browse the repository at this point in the history
  • Loading branch information
Lasall authored Jan 6, 2025
1 parent 0ac3134 commit ef60f5b
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
19 changes: 18 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,16 @@ def cfg_non_existent(request):

user_dir = user_config_dir(ConfigEOS.APP_NAME)
assert not Path(user_dir).joinpath(ConfigEOS.CONFIG_FILE_NAME).exists()
assert not Path.cwd().joinpath(ConfigEOS.CONFIG_FILE_NAME).exists()


@pytest.fixture(autouse=True)
def user_cwd(config_default_dirs):
with patch(
"pathlib.Path.cwd",
return_value=config_default_dirs[1],
) as user_cwd_patch:
yield user_cwd_patch


@pytest.fixture(autouse=True)
Expand All @@ -99,18 +109,22 @@ def config_eos(
disable_debug_logging,
user_config_dir,
user_data_dir,
user_cwd,
config_default_dirs,
monkeypatch,
) -> ConfigEOS:
"""Fixture to reset EOS config to default values."""
monkeypatch.setenv("data_cache_subpath", str(config_default_dirs[-1] / "data/cache"))
monkeypatch.setenv("data_output_subpath", str(config_default_dirs[-1] / "data/output"))
config_file = config_default_dirs[0] / ConfigEOS.CONFIG_FILE_NAME
config_file_cwd = config_default_dirs[1] / ConfigEOS.CONFIG_FILE_NAME
assert not config_file.exists()
assert not config_file_cwd.exists()
config_eos = get_config()
config_eos.reset_settings()
assert config_file == config_eos.config_file_path
assert config_file.exists()
assert not config_file_cwd.exists()
assert config_default_dirs[-1] / "data" == config_eos.data_folder_path
assert config_default_dirs[-1] / "data/cache" == config_eos.data_cache_path
assert config_default_dirs[-1] / "data/output" == config_eos.data_output_path
Expand All @@ -123,8 +137,11 @@ def config_default_dirs():
with tempfile.TemporaryDirectory() as tmp_user_home_dir:
# Default config directory from platform user config directory
config_default_dir_user = Path(tmp_user_home_dir) / "config"

# Default config directory from current working directory
config_default_dir_cwd = Path.cwd()
config_default_dir_cwd = Path(tmp_user_home_dir) / "cwd"
config_default_dir_cwd.mkdir()

# Default config directory from default config file
config_default_dir_default = Path(__file__).parent.parent.joinpath("src/akkudoktoreos/data")

Expand Down
18 changes: 18 additions & 0 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,24 @@ def test_default_config_path(config_eos, config_default_dirs):
assert config_eos.config_default_file_path.is_file()


def test_config_file_priority(config_default_dirs):
"""Test config file priority."""
from akkudoktoreos.config.config import get_config

config_default_dir_user, config_default_dir_cwd, _, _ = config_default_dirs

config_file = Path(config_default_dir_cwd) / ConfigEOS.CONFIG_FILE_NAME
config_file.write_text("{}")
config_eos = get_config()
assert config_eos.config_file_path == config_file

config_file = Path(config_default_dir_user) / ConfigEOS.CONFIG_FILE_NAME
config_file.parent.mkdir()
config_file.write_text("{}")
config_eos = get_config()
assert config_eos.config_file_path == config_file


@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."""
Expand Down

0 comments on commit ef60f5b

Please sign in to comment.