Skip to content

Commit

Permalink
Fix lint issues SCMSUITE-8674 SO--
Browse files Browse the repository at this point in the history
  • Loading branch information
dormrod committed Sep 20, 2024
1 parent c74e1c0 commit 891b12b
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 30 deletions.
5 changes: 2 additions & 3 deletions interfaces/molecule/packmol.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,9 +282,8 @@ def run(self):
structure.molecule.write(structure_fname)
input_file.write(structure.get_input_block(structure_fname, tolerance=self.tolerance))

my_input = open(input_fname, "r")
saferun(self.executable, stdin=my_input, stdout=subprocess.DEVNULL)
my_input.close()
with open(input_fname) as my_input:
saferun(self.executable, stdin=my_input, stdout=subprocess.DEVNULL)

if not os.path.exists(output_fname):
raise PackMolError("Packmol failed. It may work if you try a lower density.")
Expand Down
2 changes: 0 additions & 2 deletions unit_tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
from unittest.mock import patch, MagicMock
from pathlib import Path

from scm.plams.mol.molecule import Molecule


@pytest.fixture(autouse=True)
def config():
Expand Down
2 changes: 0 additions & 2 deletions unit_tests/test_identify.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import pytest
import numpy as np

from scm.plams.tools.periodic_table import PT
from scm.plams.mol.molecule import Molecule
from scm.plams.mol.identify import reorder, get_graph, find_permutation

PT.set_connectors("Mg", 4)

Expand Down
49 changes: 26 additions & 23 deletions unit_tests/test_molecule_interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,33 +262,36 @@ def five_water(self):

def test_packmol(self, five_water, plams_mols):
# Packmol requires calling AMS, so mock out the call to the executable and return an expected xyz
with get_mock_open_function(lambda f: f.endswith("output.xyz"), five_water), patch(
"os.path.exists", return_value=True
), patch("scm.plams.interfaces.molecule.packmol.saferun") as mock_saferun:
input_mol = plams_mols["water"]

# Packmol has a lot of internal function calls, so even returning successfully is pretty good
# But also sanity-check the temporary input files passed to AMS
def read_input_file(*args, **kwargs):
# Intercept the tmp input files and check they are as expected
content = kwargs["stdin"].read()
assert int(re.search(r"number (\d+)", content).group(1)) == 5

structure_file = re.search(r"structure\s+([/\w\d-]+)", content).group(1)
with open(f"{structure_file}.xyz") as f:
s = f.read()
assert (
s
== """3
with get_mock_open_function(lambda f: f.endswith("output.xyz"), five_water):
with patch("os.path.exists", return_value=True):
with patch("scm.plams.interfaces.molecule.packmol.saferun") as mock_saferun:
input_mol = plams_mols["water"]

# Packmol has a lot of internal function calls, so even returning successfully is pretty good
# But also sanity-check the temporary input files passed to AMS
def read_input_file(*args, **kwargs):
# Intercept the tmp input files and check they are as expected
input_file = kwargs["stdin"]
content = input_file.read()
assert int(re.search(r"number (\d+)", content).group(1)) == 5

print(content)

structure_file_name = re.search(r"structure\s+([\\:/\w\d-]+)", content).group(1)
with open(f"{structure_file_name}.xyz") as structure_file:
s = structure_file.read()
assert (
s
== """3
O 0.06692105 0.06692105 0.00000000
H 1.01204160 -0.07896265 0.00000000
H -0.07896265 1.01204160 0.00000000
"""
)
)

mock_saferun.side_effect = read_input_file
mols = packmol(input_mol, n_atoms=15, density=1.0, executable="mock_exe")
mock_saferun.side_effect = read_input_file
mols = packmol(input_mol, n_atoms=15, density=1.0, executable="mock_exe")

# And assert on the number of returned molecules
assert len(mols) == 15
# And assert on the number of returned molecules
assert len(mols) == 15

0 comments on commit 891b12b

Please sign in to comment.