From b1433d954781e8b83f62ad59dc2a980cb0912bfa Mon Sep 17 00:00:00 2001 From: "T.Tian" Date: Wed, 17 Jan 2024 20:32:01 +0800 Subject: [PATCH] use monkeypatch in pytest when setting env --- tests/test_api_version.py | 4 ++-- tests/test_sparc_bundle.py | 18 +++++++++--------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/tests/test_api_version.py b/tests/test_api_version.py index eeb3f72..a0a6f7d 100644 --- a/tests/test_api_version.py +++ b/tests/test_api_version.py @@ -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 @@ -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 diff --git a/tests/test_sparc_bundle.py b/tests/test_sparc_bundle.py index 548ea60..b5976b1 100644 --- a/tests/test_sparc_bundle.py +++ b/tests/test_sparc_bundle.py @@ -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() @@ -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() @@ -151,7 +151,7 @@ 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 @@ -159,8 +159,8 @@ def test_write_ion_inpt_real(): 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: