From 5c16854cd909c170d47b3293d3f3c04245f9908a Mon Sep 17 00:00:00 2001 From: Janosh Riebesell Date: Sat, 23 Sep 2023 10:55:25 -0700 Subject: [PATCH] Fix `TestPotcar.test_write` polluting git repo on failure (#3347) * improve StructureError msg on species/coord length mismatch * fix TestPotcar.test_write polluting git repo on failure --- pymatgen/core/structure.py | 2 +- pymatgen/io/vasp/inputs.py | 2 +- tests/core/test_structure.py | 2 +- tests/io/vasp/test_inputs.py | 20 ++++++++------------ 4 files changed, 11 insertions(+), 15 deletions(-) diff --git a/pymatgen/core/structure.py b/pymatgen/core/structure.py index c4ce3441a7e..38514dc3f09 100644 --- a/pymatgen/core/structure.py +++ b/pymatgen/core/structure.py @@ -888,7 +888,7 @@ def __init__( lost when converting to other formats. """ if len(species) != len(coords): - raise StructureError("atomic species and fractional coordinates must have same length") + raise StructureError(f"{len(species)=} != {len(coords)=}") self._lattice = lattice if isinstance(lattice, Lattice) else Lattice(lattice) diff --git a/pymatgen/io/vasp/inputs.py b/pymatgen/io/vasp/inputs.py index 5ca47b60304..bb27545e5c2 100644 --- a/pymatgen/io/vasp/inputs.py +++ b/pymatgen/io/vasp/inputs.py @@ -1804,7 +1804,7 @@ def __init__(self, data, symbol=None): has_sh256, hash_check_passed = self.verify_potcar() if not has_sh256 and not hash_check_passed: warnings.warn( - f"POTCAR data with symbol { self.symbol} does not match any VASP " + f"POTCAR data with symbol {self.symbol} does not match any VASP " "POTCAR known to pymatgen. There is a possibility your " "POTCAR is corrupted or that the pymatgen database is incomplete.", UnknownPotcarWarning, diff --git a/tests/core/test_structure.py b/tests/core/test_structure.py index 70ccc118b71..58f63727491 100644 --- a/tests/core/test_structure.py +++ b/tests/core/test_structure.py @@ -1396,7 +1396,7 @@ def test_set_item(self): assert struct.formula == "Si1.25 C0.125" def test_init_error(self): - with pytest.raises(StructureError, match="atomic species and fractional coordinates must have same length"): + with pytest.raises(StructureError, match=r"len\(species\)=1 != len\(coords\)=2"): Structure(Lattice.cubic(3), ["Si"], [[0, 0, 0], [0.5, 0.5, 0.5]]) def test_from_sites(self): diff --git a/tests/io/vasp/test_inputs.py b/tests/io/vasp/test_inputs.py index 861e2f3d73a..fa1f17f31a4 100644 --- a/tests/io/vasp/test_inputs.py +++ b/tests/io/vasp/test_inputs.py @@ -1096,7 +1096,7 @@ def test_repr(self): ) -class TestPotcar(unittest.TestCase): +class TestPotcar(PymatgenTest): def setUp(self): if "PMG_VASP_PSP_DIR" not in SETTINGS: SETTINGS["PMG_VASP_PSP_DIR"] = str(TEST_FILES_DIR) @@ -1125,24 +1125,20 @@ def test_as_from_dict(self): assert potcar.symbols == ["Fe", "P", "O"] def test_write(self): - tempfname = Path("POTCAR.testing") - self.potcar.write_file(tempfname) - p = Potcar.from_file(tempfname) + tmp_file = f"{self.tmp_path}/POTCAR.testing" + self.potcar.write_file(tmp_file) + p = Potcar.from_file(tmp_file) assert p.symbols == self.potcar.symbols - # check line by line - with open(self.filepath) as f_ref, open(tempfname) as f_new: + with open(self.filepath) as f_ref, open(tmp_file) as f_new: ref_potcar = f_ref.readlines() new_potcar = f_new.readlines() - if len(ref_potcar) != len(new_potcar): - raise AssertionError("POTCAR file lengths are not equal") + assert len(ref_potcar) == len(new_potcar), f"wrong POTCAR line count: {len(ref_potcar)} != {len(new_potcar)}" + # check line by line for line1, line2 in zip(ref_potcar, new_potcar): - if line1.strip() != line2.strip(): - raise AssertionError("POTCAR contents are not") - - tempfname.unlink() + assert line1.strip() == line2.strip(), f"wrong POTCAR line: {line1} != {line2}" def test_set_symbol(self): assert self.potcar.symbols == ["Fe", "P", "O"]