Skip to content

Commit

Permalink
use monkeypatch in pytest when setting env
Browse files Browse the repository at this point in the history
  • Loading branch information
alchem0x2A committed Jan 17, 2024
1 parent 512b866 commit b1433d9
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
4 changes: 2 additions & 2 deletions tests/test_api_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
curdir = Path(__file__).parent


def test_sparc_api():
def test_sparc_api(monkeypatch):
from sparc.api import SparcAPI
from sparc.utils import locate_api

Expand All @@ -19,7 +19,7 @@ def test_sparc_api():
older_ver = locate_api(doc_path=curdir / "sparc-latex-doc-202302").sparc_version
assert older_ver is None
# Specify SPARC_DOC_PATH
os.environ["SPARC_DOC_PATH"] = (
monkeypatch.setenv("SPARC_DOC_PATH",
(curdir / "sparc-latex-doc-202302").resolve().as_posix()
)
older_version = locate_api().sparc_version
Expand Down
18 changes: 9 additions & 9 deletions tests/test_sparc_bundle.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,20 @@ def test_bundle_psp(monkeypatch):

from sparc.io import SparcBundle

os.environ.pop("SPARC_PP_PATH", None)
os.environ.pop("SPARC_PSP_PATH", None)
monkeypatch.delenv("SPARC_PP_PATH", raising=False)
monkeypatch.delenv("SPARC_PSP_PATH", raising=False)
sb = SparcBundle(directory=test_output_dir / "Cu_FCC.sparc")

with pytest.warns(UserWarning, match="re-download"):
sb = SparcBundle(directory=test_output_dir / "Cu_FCC.sparc", mode="w")
assert sb.psp_dir is None

os.environ["SPARC_PP_PATH"] = test_psp_dir.as_posix()
monkeypatch.setenv("SPARC_PP_PATH", test_psp_dir.as_posix())
sb = SparcBundle(directory=test_output_dir / "Cu_FCC.sparc")
assert sb.psp_dir.resolve() == test_psp_dir.resolve()

# SPARC_PSP_PATH has higher priority
os.environ["SPARC_PSP_PATH"] = test_psp_dir.parent.as_posix()
monkeypatch.setenv("SPARC_PSP_PATH", test_psp_dir.parent.as_posix())
sb = SparcBundle(directory=test_output_dir / "Cu_FCC.sparc")
assert sb.psp_dir.resolve() == test_psp_dir.parent.resolve()

Expand All @@ -54,8 +54,8 @@ def _fake_psp_check(directory):
from sparc.common import psp_dir as default_psp_dir
from sparc.io import SparcBundle

os.environ.pop("SPARC_PP_PATH", None)
os.environ.pop("SPARC_PSP_PATH", None)
monkeypatch.delenv("SPARC_PP_PATH", raising=False)
monkeypatch.delenv("SPARC_PSP_PATH", raising=False)
sb = SparcBundle(directory=test_output_dir / "Cu_FCC.sparc")
assert Path(sb.psp_dir).resolve() == Path(default_psp_dir).resolve()

Expand Down Expand Up @@ -151,16 +151,16 @@ def test_write_ion_inpt(fs):
# Copy psp should have the psps available


def test_write_ion_inpt_real():
def test_write_ion_inpt_real(monkeypatch):
"""Same example as in test_parse_atoms but try writing inpt and atoms"""
from ase.build import bulk
from ase.units import Angstrom, Bohr

from sparc.io import SparcBundle

# Even without SPARC_PP_PATH, the psp files should exist
os.environ.pop("SPARC_PP_PATH", None)
os.environ.pop("SPARC_PSP_PATH", None)
monkeypatch.delenv("SPARC_PP_PATH", raising=False)
monkeypatch.delenv("SPARC_PSP_PATH", raising=False)

atoms = bulk("Cu") * [4, 4, 4]
with tempfile.TemporaryDirectory() as tmpdir:
Expand Down

0 comments on commit b1433d9

Please sign in to comment.