Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix tests if deprecated ~/.esmvaltool/config-user.yml file is available #2543

Merged
merged 2 commits into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import pytest

from esmvalcore.config import CFG
from esmvalcore.config import CFG, Config


@pytest.fixture
Expand All @@ -22,3 +22,13 @@ def session(tmp_path: Path, cfg_default, monkeypatch):
monkeypatch.setitem(CFG, "rootpath", {"default": {tmp_path: "default"}})
monkeypatch.setitem(CFG, "output_dir", tmp_path / "esmvaltool_output")
return CFG.start_session("recipe_test")


# TODO: remove in v2.14.0
@pytest.fixture(autouse=True)
def ignore_old_config_user(tmp_path, monkeypatch):
"""Ignore potentially existing old config-user.yml file in all tests."""
nonexistent_config_dir = tmp_path / "nonexistent_config_dir"
monkeypatch.setattr(
Config, "_DEFAULT_USER_CONFIG_DIR", nonexistent_config_dir
)
37 changes: 27 additions & 10 deletions tests/unit/config/test_config_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,14 @@ def test_load_from_file(monkeypatch):


# TODO: remove in v2.14.0
def test_load_from_file_filenotfound(monkeypatch):
def test_load_from_file_filenotfound(monkeypatch, tmp_path):
"""Test `Config.load_from_file`."""
config = Config()
assert not config

expected_path = Path.home() / ".esmvaltool" / "not_existent_file.yml"
expected_path = (
tmp_path / "nonexistent_config_dir" / "not_existent_file.yml"
)
msg = f"Config file '{expected_path}' does not exist"
with pytest.raises(FileNotFoundError, match=msg):
config.load_from_file("not_existent_file.yml")
Expand Down Expand Up @@ -110,6 +112,9 @@ def test_config_key_error():

def test_reload(cfg_default, monkeypatch, tmp_path):
"""Test `Config.reload`."""
# TODO: remove in v2.14.0
monkeypatch.delenv("_ESMVALTOOL_USER_CONFIG_FILE_", raising=False)

monkeypatch.setattr(
esmvalcore.config._config_object,
"USER_CONFIG_DIR",
Expand All @@ -124,6 +129,9 @@ def test_reload(cfg_default, monkeypatch, tmp_path):

def test_reload_fail(monkeypatch, tmp_path):
"""Test `Config.reload`."""
# TODO: remove in v2.14.0
monkeypatch.delenv("_ESMVALTOOL_USER_CONFIG_FILE_", raising=False)

config_file = tmp_path / "invalid_config_file.yml"
config_file.write_text("invalid_option: 1")
monkeypatch.setattr(
Expand Down Expand Up @@ -160,26 +168,32 @@ def test_session_config_dir():


TEST_GET_CFG_PATH = [
(None, None, None, "~/.esmvaltool/config-user.yml", False),
(
None,
None,
None,
"{tmp_path}/nonexistent_config_dir/config-user.yml",
False,
),
(
None,
None,
("any_other_module", "--config_file=cli.yml"),
"~/.esmvaltool/config-user.yml",
"{tmp_path}/nonexistent_config_dir/config-user.yml",
False,
),
(
None,
None,
("esmvaltool", "run", "--max_parallel_tasks=4"),
"~/.esmvaltool/config-user.yml",
"{tmp_path}/nonexistent_config_dir/config-user.yml",
True,
),
(
None,
None,
("esmvaltool", "--config_file"),
"~/.esmvaltool/config-user.yml",
"{tmp_path}/nonexistent_config_dir/config-user.yml",
True,
),
(
Expand Down Expand Up @@ -214,7 +228,7 @@ def test_session_config_dir():
None,
None,
("esmvaltool", "run", "--config-file=relative_cli.yml"),
"~/.esmvaltool/relative_cli.yml",
"{tmp_path}/nonexistent_config_dir/relative_cli.yml",
True,
),
(
Expand Down Expand Up @@ -264,7 +278,7 @@ def test_session_config_dir():
"filename.yml",
None,
None,
"~/.esmvaltool/filename.yml",
"{tmp_path}/nonexistent_config_dir/filename.yml",
False,
),
(
Expand All @@ -285,6 +299,7 @@ def test_get_config_user_path(
filename, env, cli_args, output, env_var_set, monkeypatch, tmp_path
):
"""Test `Config._get_config_user_path`."""
output = output.format(tmp_path=tmp_path)
monkeypatch.delenv("_ESMVALTOOL_USER_CONFIG_FILE_", raising=False)

# Create empty test file
Expand Down Expand Up @@ -313,9 +328,11 @@ def test_get_config_user_path(


# TODO: remove in v2.14.0
def test_load_user_config_filenotfound():
def test_load_user_config_filenotfound(tmp_path):
"""Test `Config._load_user_config`."""
expected_path = Path.home() / ".esmvaltool" / "not_existent_file.yml"
expected_path = (
tmp_path / "nonexistent_config_dir" / "not_existent_file.yml"
)
msg = f"Config file '{expected_path}' does not exist"
with pytest.raises(FileNotFoundError, match=msg):
Config._load_user_config("not_existent_file.yml")
Expand Down
14 changes: 8 additions & 6 deletions tests/unit/main/test_esmvaltool.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,11 @@ def test_do_not_clean_preproc_dir(session):
assert session._fixed_file_dir.exists()


@mock.patch("esmvalcore._main.ESMValTool._get_config_info")
@mock.patch("esmvalcore._main.entry_points")
def test_header(mock_entry_points, monkeypatch, tmp_path, caplog):
def test_header(
mock_entry_points, mock_get_config_info, monkeypatch, tmp_path, caplog
):
tmp_path.mkdir(parents=True, exist_ok=True)
monkeypatch.setattr(
esmvalcore.config._config_object, "USER_CONFIG_DIR", tmp_path
Expand All @@ -221,6 +224,9 @@ def test_header(mock_entry_points, monkeypatch, tmp_path, caplog):
mock_entry_points.return_value = [entry_point]
cli_config_dir = tmp_path / "this" / "does" / "not" / "exist"

# TODO: remove in v2.14.0
mock_get_config_info.return_value = "config_dir (SOURCE)"

with caplog.at_level(logging.INFO):
ESMValTool()._log_header(
["path_to_log_file1", "path_to_log_file2"],
Expand All @@ -235,11 +241,7 @@ def test_header(mock_entry_points, monkeypatch, tmp_path, caplog):
assert caplog.messages[4] == "MyEntry: v42.42.42"
assert caplog.messages[5] == "----------------"
assert caplog.messages[6] == (
f"Reading configuration files from:\n"
f"{Path(esmvalcore.__file__).parent}/config/configurations/defaults "
f"(defaults)\n"
f"{tmp_path} (SOURCE)\n"
f"{cli_config_dir} [NOT AN EXISTING DIRECTORY] (command line argument)"
"Reading configuration files from:\nconfig_dir (SOURCE)"
)
assert caplog.messages[7] == (
"Writing program log files to:\n"
Expand Down