@@ -48,3 +48,34 @@ def test_load_settings(tmp_path: Path, monkeypatch: MonkeyPatch) -> None:
48
48
# should return empty dict if file is invalid
49
49
settings_file .write_text ("---" )
50
50
assert _load_pmg_settings () == {}
51
+
52
+
53
+ def test_env_var_pmg_config_file (tmp_path : Path , monkeypatch : MonkeyPatch ) -> None :
54
+ custom_config_file = tmp_path / "custom_config.yaml"
55
+ custom_config_file .write_text ("PMG_CUSTOM_SETTING: custom_value" )
56
+
57
+ with monkeypatch .context () as ctx :
58
+ ctx .setenv ("PMG_CONFIG_FILE" , str (custom_config_file ))
59
+ settings = _load_pmg_settings ()
60
+ assert "PMG_CUSTOM_SETTING" in settings
61
+ assert settings ["PMG_CUSTOM_SETTING" ] == "custom_value"
62
+
63
+ # Test that PMG_CONFIG_FILE takes precedence over the default location
64
+ settings_file = tmp_path / ".pmgrc.yaml"
65
+ monkeypatch .setattr ("pymatgen.core.SETTINGS_FILE" , settings_file )
66
+ settings_file .write_text ("PMG_DEFAULT_SETTING: default_value" )
67
+ custom_config_file .write_text ("PMG_CUSTOM_SETTING: custom_value" )
68
+
69
+ with monkeypatch .context () as ctx :
70
+ ctx .setenv ("PMG_CONFIG_FILE" , str (custom_config_file ))
71
+ settings = _load_pmg_settings ()
72
+ assert "PMG_CUSTOM_SETTING" in settings
73
+ assert "PMG_DEFAULT_SETTING" not in settings
74
+ assert settings ["PMG_CUSTOM_SETTING" ] == "custom_value"
75
+
76
+ # Test that env vars still take precedence over the values specified in PMG_CONFIG_FILE
77
+ with monkeypatch .context () as ctx :
78
+ ctx .setenv ("PMG_CONFIG_FILE" , str (custom_config_file ))
79
+ ctx .setenv ("PMG_CUSTOM_SETTING" , "env_value" )
80
+ settings = _load_pmg_settings ()
81
+ assert settings ["PMG_CUSTOM_SETTING" ] == "env_value"
0 commit comments