Skip to content

Commit

Permalink
move TEST_DIR + TEST_FILES from custodian/__init__.py to tests/confte…
Browse files Browse the repository at this point in the history
…st.py

fixes TEST_DIR location hardcoded to installation dir
  • Loading branch information
janosh committed Jan 22, 2024
1 parent dbd3f2d commit 6970f8c
Show file tree
Hide file tree
Showing 29 changed files with 84 additions and 96 deletions.
2 changes: 0 additions & 2 deletions custodian/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,3 @@

PKG_DIR = os.path.dirname(__file__)
ROOT = os.path.dirname(PKG_DIR)
TEST_DIR = f"{ROOT}/tests"
TEST_FILES = f"{TEST_DIR}/files"
12 changes: 6 additions & 6 deletions custodian/cp2k/jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,17 +127,17 @@ def run(self):
# and custodian should only run the job itself
def postprocess(self):
"""Postprocessing includes renaming and gzipping where necessary."""
fs = os.listdir(".")
files = os.listdir(".")
if os.path.exists(self.output_file) and self.suffix != "":
os.mkdir(f"run{self.suffix}")
for f in fs:
if "json" in f:
for file in files:
if "json" in file:
continue
if not os.path.isdir(f):
if not os.path.isdir(file):
if self.final:
shutil.move(f, f"run{self.suffix}/{f}")
shutil.move(file, f"run{self.suffix}/{file}")
else:
shutil.copy(f, f"run{self.suffix}/{f}")
shutil.copy(file, f"run{self.suffix}/{file}")

# Remove continuation so if a subsequent job is run in
# the same directory, will not restart this job.
Expand Down
4 changes: 2 additions & 2 deletions custodian/custodian.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,8 +235,8 @@ def safe_extract(tar, path=".", members=None, *, numeric_owner=False):

@staticmethod
def _delete_checkpoints(cwd):
for f in glob(os.path.join(cwd, "custodian.chk.*.tar.gz")):
os.remove(f)
for file in glob(os.path.join(cwd, "custodian.chk.*.tar.gz")):
os.remove(file)

@staticmethod
def _save_checkpoint(cwd, index):
Expand Down
19 changes: 7 additions & 12 deletions custodian/feff/jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,22 +59,17 @@ def __init__(
self.gzipped = gzipped
self.gzipped_prefix = gzipped_prefix

def setup(self):
"""
Performs initial setup for FeffJob, do backing up.
Returns:
"""
def setup(self) -> None:
"""Performs initial setup for FeffJob, do backing up."""
decompress_dir(".")

if self.backup:
for f in FEFF_INPUT_FILES:
shutil.copy(f, f"{f}.orig")
for file in FEFF_INPUT_FILES:
shutil.copy(file, f"{file}.orig")

for f in FEFF_BACKUP_FILES:
if os.path.isfile(f):
shutil.copy(f, f"{f}.orig")
for file in FEFF_BACKUP_FILES:
if os.path.isfile(file):
shutil.copy(file, f"{file}.orig")

def run(self):
"""
Expand Down
4 changes: 2 additions & 2 deletions custodian/lobster/jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ def __init__(
def setup(self):
"""Will backup lobster input files."""
if self.backup:
for f in LOBSTERINPUT_FILES:
shutil.copy(f, f"{f}.orig")
for file in LOBSTERINPUT_FILES:
shutil.copy(file, f"{file}.orig")

def run(self):
"""Runs the job."""
Expand Down
4 changes: 2 additions & 2 deletions custodian/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ def backup(filenames, prefix="error"):
logging.info(f"Backing up run to {filename}.")
with tarfile.open(filename, "w:gz") as tar:
for fname in filenames:
for f in glob(fname):
tar.add(f)
for file in glob(fname):
tar.add(file)


def get_execution_host_info():
Expand Down
4 changes: 2 additions & 2 deletions custodian/vasp/interpreter.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,5 @@ def apply_actions(self, actions):
self.modify(a["action"], a["file"])
else:
raise ValueError(f"Unrecognized format: {a}")
for f in modified:
self.vi[f].write_file(f)
for file in modified:
self.vi[file].write_file(file)
4 changes: 1 addition & 3 deletions custodian/vasp/io.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
"""
Helper functions for dealing with vasp files.
"""
"""Helper functions for dealing with vasp files."""

from pymatgen.io.vasp.outputs import Outcar, Vasprun

Expand Down
36 changes: 18 additions & 18 deletions custodian/vasp/jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,11 +174,11 @@ def setup(self):
decompress_dir(".")

if self.backup:
for f in VASP_INPUT_FILES:
for file in VASP_INPUT_FILES:
try:
shutil.copy(f, f"{f}.orig")
shutil.copy(file, f"{file}.orig")
except FileNotFoundError: # handle the situation when there is no KPOINTS file
if f == "KPOINTS":
if file == "KPOINTS":
pass

if self.auto_npar:
Expand Down Expand Up @@ -257,12 +257,12 @@ def postprocess(self):
Postprocessing includes renaming and gzipping where necessary.
Also copies the magmom to the incar if necessary.
"""
for f in [*VASP_OUTPUT_FILES, self.output_file]:
if os.path.exists(f):
for file in [*VASP_OUTPUT_FILES, self.output_file]:
if os.path.exists(file):
if self.final and self.suffix != "":
shutil.move(f, f"{f}{self.suffix}")
shutil.move(file, f"{file}{self.suffix}")
elif self.suffix != "":
shutil.copy(f, f"{f}{self.suffix}")
shutil.copy(file, f"{file}{self.suffix}")

if self.copy_magmom and not self.final:
try:
Expand Down Expand Up @@ -793,8 +793,8 @@ def setup(self):

if self.backup:
# Back up KPOINTS, INCAR, POTCAR
for f in VASP_NEB_INPUT_FILES:
shutil.copy(f, f"{f}.orig")
for file in VASP_NEB_INPUT_FILES:
shutil.copy(file, f"{file}.orig")
# Back up POSCARs
for path in neb_dirs:
poscar = os.path.join(path, "POSCAR")
Expand Down Expand Up @@ -870,21 +870,21 @@ def postprocess(self):
"""Postprocessing includes renaming and gzipping where necessary."""
# Add suffix to all sub_dir/{items}
for path in self.neb_dirs:
for f in VASP_NEB_OUTPUT_SUB_FILES:
f = os.path.join(path, f)
if os.path.exists(f):
for file in VASP_NEB_OUTPUT_SUB_FILES:
file = os.path.join(path, file)
if os.path.exists(file):
if self.final and self.suffix != "":
shutil.move(f, f"{f}{self.suffix}")
shutil.move(file, f"{file}{self.suffix}")
elif self.suffix != "":
shutil.copy(f, f"{f}{self.suffix}")
shutil.copy(file, f"{file}{self.suffix}")

# Add suffix to all output files
for f in [*VASP_NEB_OUTPUT_FILES, self.output_file]:
if os.path.exists(f):
for file in [*VASP_NEB_OUTPUT_FILES, self.output_file]:
if os.path.exists(file):
if self.final and self.suffix != "":
shutil.move(f, f"{f}{self.suffix}")
shutil.move(file, f"{file}{self.suffix}")
elif self.suffix != "":
shutil.copy(f, f"{f}{self.suffix}")
shutil.copy(file, f"{file}{self.suffix}")


class GenerateVaspInputJob(Job):
Expand Down
4 changes: 1 addition & 3 deletions tests/ansible/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
"""
Tests for ansible package.
"""
"""Tests for ansible package."""
4 changes: 4 additions & 0 deletions tests/vasp/conftest.py → tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
"""This module mocks functions needed for pytest."""

import multiprocessing
import os

import pytest

TEST_DIR = os.path.dirname(__file__)
TEST_FILES = f"{TEST_DIR}/files"


@pytest.fixture(autouse=True)
def _patch_get_potential_energy(monkeypatch):
Expand Down
2 changes: 1 addition & 1 deletion tests/cp2k/test_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
from pymatgen.io.cp2k.inputs import Keyword, KeywordList
from pymatgen.io.cp2k.sets import StaticSet

from custodian import TEST_FILES
from custodian.cp2k.handlers import (
AbortHandler,
FrozenJobErrorHandler,
Expand All @@ -21,6 +20,7 @@
get_conv,
)
from custodian.cp2k.interpreter import Cp2kModder
from tests.conftest import TEST_FILES

TEST_FILES_DIR = f"{TEST_FILES}/cp2k"

Expand Down
11 changes: 6 additions & 5 deletions tests/cp2k/test_jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
from glob import glob
from pathlib import Path

from custodian import TEST_FILES, Custodian
from custodian import Custodian
from custodian.cp2k.jobs import Cp2kJob
from tests.conftest import TEST_FILES

MODULE_DIR = Path(__file__).resolve().parent
TEST_FILES_DIR = f"{TEST_FILES}/cp2k"
Expand All @@ -17,10 +18,10 @@


def clean_dir(dir):
for f in glob(os.path.join(dir, "error.*.tar.gz")):
os.remove(f)
for f in glob(os.path.join(dir, "custodian.chk.*.tar.gz")):
os.remove(f)
for file in glob(os.path.join(dir, "error.*.tar.gz")):
os.remove(file)
for file in glob(os.path.join(dir, "custodian.chk.*.tar.gz")):
os.remove(file)


class HandlerTests(unittest.TestCase):
Expand Down
6 changes: 3 additions & 3 deletions tests/feff/test_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import unittest
from glob import glob

from custodian import TEST_FILES
from custodian.feff.handlers import UnconvergedErrorHandler
from tests.conftest import TEST_FILES

__author__ = "Chen Zheng"
__copyright__ = "Copyright 2012, The Materials Project"
Expand All @@ -15,8 +15,8 @@


def clean_dir():
for f in glob("error.*.tar.gz"):
os.remove(f)
for file in glob("error.*.tar.gz"):
os.remove(file)


class UnconvergedErrorHandlerTest(unittest.TestCase):
Expand Down
2 changes: 1 addition & 1 deletion tests/feff/test_jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
from monty.tempfile import ScratchDir
from pymatgen.io.feff.inputs import Atoms, Tags

from custodian import TEST_FILES
from custodian.feff.jobs import FeffJob
from tests.conftest import TEST_FILES

__author__ = "Chen Zheng"
__copyright__ = "Copyright 2012, The Materials Project"
Expand Down
2 changes: 1 addition & 1 deletion tests/lobster/test_handlers.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import os

from custodian import TEST_FILES
from custodian.lobster.handlers import ChargeSpillingValidator, EnoughBandsValidator, LobsterFilesValidator
from tests.conftest import TEST_FILES

test_files_lobster = f"{TEST_FILES}/lobster/lobsterouts"

Expand Down
2 changes: 1 addition & 1 deletion tests/lobster/test_jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
from monty.os import cd
from monty.tempfile import ScratchDir

from custodian import TEST_FILES
from custodian.lobster.jobs import LobsterJob
from tests.conftest import TEST_FILES

test_files_lobster2 = f"{TEST_FILES}/lobster/lobsterins"
test_files_lobster3 = f"{TEST_FILES}/lobster/vasp_lobster_output"
Expand Down
4 changes: 1 addition & 3 deletions tests/nwchem/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
"""
Tests for nwchem package.
"""
"""Tests for nwchem package."""
6 changes: 3 additions & 3 deletions tests/nwchem/test_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
import shutil
from glob import glob

from custodian import TEST_FILES
from custodian.nwchem.handlers import NwchemErrorHandler
from tests.conftest import TEST_FILES

__author__ = "shyuepingong"
__version__ = "0.1"
Expand All @@ -25,5 +25,5 @@ def test_check_correct():
handler.check()
handler.correct()
shutil.move("Li1_1.nw.orig", "Li1_1.nw")
for f in glob("error.*.tar.gz"):
os.remove(f)
for file in glob("error.*.tar.gz"):
os.remove(file)
4 changes: 1 addition & 3 deletions tests/qchem/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
"""
Tests for QChem Custodian
"""
"""Tests for QChem Custodian"""

import os
import unittest
Expand Down
2 changes: 1 addition & 1 deletion tests/qchem/test_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

from pymatgen.io.qchem.inputs import QCInput

from custodian import TEST_FILES
from custodian.qchem.handlers import QChemErrorHandler
from tests.conftest import TEST_FILES

try:
from openbabel import openbabel as ob
Expand Down
2 changes: 1 addition & 1 deletion tests/qchem/test_job_handler_interaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@

from pymatgen.io.qchem.inputs import QCInput

from custodian import TEST_FILES
from custodian.qchem.handlers import QChemErrorHandler
from custodian.qchem.jobs import QCJob
from tests.conftest import TEST_FILES

try:
from openbabel import openbabel as ob
Expand Down
2 changes: 1 addition & 1 deletion tests/qchem/test_jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
import pytest
from pymatgen.io.qchem.inputs import QCInput

from custodian import TEST_FILES
from custodian.qchem.jobs import QCJob
from tests.conftest import TEST_FILES

try:
from openbabel import openbabel as ob
Expand Down
4 changes: 2 additions & 2 deletions tests/test_custodian.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,8 +310,8 @@ def test_from_spec(self):
assert len(c.validators) == 1

def tearDown(self):
for f in glob("custodian.*.tar.gz"):
os.remove(f)
for file in glob("custodian.*.tar.gz"):
os.remove(file)
try:
os.remove("custodian.json")
except OSError:
Expand Down
4 changes: 1 addition & 3 deletions tests/vasp/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
"""
Tests for vasp package.
"""
"""Tests for vasp package."""
2 changes: 1 addition & 1 deletion tests/vasp/test_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
from pymatgen.io.vasp.inputs import Incar, Kpoints, Structure, VaspInput
from pymatgen.util.testing import PymatgenTest

from custodian import TEST_FILES
from custodian.utils import tracked_lru_cache
from custodian.vasp.handlers import (
AliasingErrorHandler,
Expand All @@ -27,6 +26,7 @@
VaspErrorHandler,
WalltimeHandler,
)
from tests.conftest import TEST_FILES

__author__ = "Shyue Ping Ong, Stephen Dacek, Janosh Riebesell"
__copyright__ = "Copyright 2012, The Materials Project"
Expand Down
2 changes: 1 addition & 1 deletion tests/vasp/test_io.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import pytest

from custodian import TEST_FILES
from custodian.utils import tracked_lru_cache
from custodian.vasp.io import load_outcar, load_vasprun
from tests.conftest import TEST_FILES


@pytest.fixture(autouse=True)
Expand Down
Loading

0 comments on commit 6970f8c

Please sign in to comment.