-
Notifications
You must be signed in to change notification settings - Fork 851
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2702 from materialsproject/test-load-settings
Test `_load_pmg_settings()`
- Loading branch information
Showing
3 changed files
with
245 additions
and
202 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
from pathlib import Path | ||
|
||
from pytest import MonkeyPatch | ||
|
||
from pymatgen.core import _load_pmg_settings | ||
|
||
__author__ = "Janosh Riebesell" | ||
__date__ = "2022-10-21" | ||
__email__ = "janosh@lbl.gov" | ||
|
||
|
||
def test_load_settings(tmp_path: Path, monkeypatch: MonkeyPatch) -> None: | ||
"""Test .pmgrc.yaml file is loaded correctly and env vars take precedence.""" | ||
monkeypatch.setattr("os.environ", {}) # reset outer env vars | ||
|
||
settings_file = tmp_path / ".pmgrc.yaml" | ||
monkeypatch.setattr("pymatgen.core.SETTINGS_FILE", settings_file) | ||
|
||
# should return empty dict if file doesn't exist | ||
assert _load_pmg_settings() == {} | ||
|
||
# should return empty dict if file is empty | ||
settings_file.write_text("") | ||
assert _load_pmg_settings() == {} | ||
|
||
settings_file.write_text("PMG_VASP_PSP_DIR: /path/to/psp") | ||
assert _load_pmg_settings() == {"PMG_VASP_PSP_DIR": "/path/to/psp"} | ||
|
||
settings_file.write_text("PMG_MAPI_KEY: FOOBAR") | ||
assert _load_pmg_settings() == {"PMG_MAPI_KEY": "FOOBAR"} | ||
|
||
# env vars should override .pmgrc.yaml | ||
with monkeypatch.context() as ctx: | ||
ctx.setenv("PMG_MAPI_KEY", "BAZ") | ||
assert _load_pmg_settings() == {"PMG_MAPI_KEY": "BAZ"} | ||
|
||
# should return empty dict if file is invalid | ||
settings_file.write_text("---") | ||
assert _load_pmg_settings() == {} |
Oops, something went wrong.