Skip to content

Commit

Permalink
Move tests to their own root-level directory (#305)
Browse files Browse the repository at this point in the history
* move custodian/**/tests to tests/** and test_files to tests/files

* fix crazy "..", "..", "..", "tests/files" stuff

* more crazy path clean up

* just use f-strings for path construction, no need for os.path.join

* fix needlessly specific .github/workflows/pytest.yml causing test discovery to fail
  • Loading branch information
janosh authored Nov 15, 2023
1 parent 53b031c commit ebbd822
Show file tree
Hide file tree
Showing 830 changed files with 131 additions and 134 deletions.
2 changes: 1 addition & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# declare HTML, rST and test files as vendored/docs to exclude them when calculating repo languages on GitHub
**/test_files/**/* linguist-vendored
**/tests/files/**/* linguist-vendored
docs/**/* linguist-documentation
12 changes: 7 additions & 5 deletions .github/workflows/pytest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,22 @@ jobs:
steps:
- uses: actions/checkout@v3

- uses: conda-incubator/setup-miniconda@v2
- uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}

- name: Install dependencies
run: |
python -m pip install --upgrade pip
python -m pip install --upgrade pymatgen
python -m pip install -e '.[dev]'
pip install --upgrade pip
pip install --upgrade pymatgen
pip install -e '.[dev]'
- name: pytest
env:
PMG_MAPI_KEY: ${{ secrets.PMG_MAPI_KEY }}
MPLBACKEND: "Agg"
run: pytest --cov=custodian custodian --color=yes
run: pytest --cov=. --color=yes

- name: Upload coverage reports to Codecov
uses: codecov/codecov-action@v3
env:
Expand Down
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
exclude: ^(docs|test_files)
exclude: ^(docs|tests/files)

ci:
autoupdate_schedule: monthly
Expand Down
16 changes: 14 additions & 2 deletions custodian/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,19 @@
framework written in Python.
"""

__author__ = "Shyue Ping Ong, William Davidson Richards, Stephen Dacek, Xiaohui Qu, Matthew Horton, Samuel M. Blau"
__version__ = "2023.10.9"

import os

from .custodian import Custodian

__author__ = (
"Shyue Ping Ong, William Davidson Richards, Stephen Dacek, Xiaohui Qu, Matthew Horton, "
"Samuel M. Blau, Janosh Riebesell"
)
__version__ = "2023.10.9"


PKG_DIR = os.path.dirname(__file__)
ROOT = os.path.dirname(PKG_DIR)
TEST_DIR = f"{ROOT}/tests"
TEST_FILES = f"{TEST_DIR}/files"
3 changes: 0 additions & 3 deletions custodian/tests/__init__.py

This file was deleted.

6 changes: 0 additions & 6 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,6 @@

from pymatgen.core import __author__

# sys.path.insert(0, os.path.abspath("."))
# sys.path.insert(0, os.path.dirname(".."))
# sys.path.insert(0, os.path.dirname("../pymatgen"))
# sys.path.insert(0, os.path.dirname("../.."))


project = "pymatgen"
copyright = "2022, Materials Virtual Lab"
author = __author__
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ Package = "https://pypi.org/project/custodian"

[tool.setuptools.packages.find]
include = ["custodian*"]
exclude = ["*.tests", "*.tests.*", "test_files"]
exclude = ["*.tests", "*.tests.*"]

[tool.ruff]
target-version = "py39"
Expand Down
1 change: 1 addition & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""Tests for main Custodian class."""
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
import time
import unittest
import warnings
from pathlib import Path

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 @@ -22,6 +22,8 @@
)
from custodian.cp2k.interpreter import Cp2kModder

TEST_FILES_DIR = f"{TEST_FILES}/cp2k"


def clean_dir(d):
for file in glob.glob(os.path.join(d, "error.*.tar.gz")):
Expand All @@ -34,23 +36,21 @@ class HandlerTests(unittest.TestCase):
def setUp(self):
warnings.filterwarnings("ignore")

self.TEST_FILES_DIR = os.path.join(Path(__file__).parent.absolute(), "../../../test_files/cp2k")

clean_dir(self.TEST_FILES_DIR)
clean_dir(TEST_FILES_DIR)

time.sleep(1) # for frozenhandler

shutil.copy(os.path.join(self.TEST_FILES_DIR, "cp2k.inp.orig"), os.path.join(self.TEST_FILES_DIR, "cp2k.inp"))
shutil.copy(f"{TEST_FILES_DIR}/cp2k.inp.orig", f"{TEST_FILES_DIR}/cp2k.inp")

self.input_file = os.path.join(self.TEST_FILES_DIR, "cp2k.inp")
self.input_file = f"{TEST_FILES_DIR}/cp2k.inp"

self.output_file_preconditioner = os.path.join(self.TEST_FILES_DIR, "cp2k.out.precondstuck")
self.output_file_cholesky = os.path.join(self.TEST_FILES_DIR, "cp2k.out.cholesky")
self.output_file_imprecise = os.path.join(self.TEST_FILES_DIR, "cp2k.out.imprecise")
self.output_file_unconverged = os.path.join(self.TEST_FILES_DIR, "cp2k.out.unconverged")
self.output_file_stderr = os.path.join(self.TEST_FILES_DIR, "std_err.txt")
self.output_file_hybrid = os.path.join(self.TEST_FILES_DIR, "cp2k.out.hybrid")
self.output_file_conv = os.path.join(self.TEST_FILES_DIR, "cp2k.out.conv")
self.output_file_preconditioner = f"{TEST_FILES_DIR}/cp2k.out.precondstuck"
self.output_file_cholesky = f"{TEST_FILES_DIR}/cp2k.out.cholesky"
self.output_file_imprecise = f"{TEST_FILES_DIR}/cp2k.out.imprecise"
self.output_file_unconverged = f"{TEST_FILES_DIR}/cp2k.out.unconverged"
self.output_file_stderr = f"{TEST_FILES_DIR}/std_err.txt"
self.output_file_hybrid = f"{TEST_FILES_DIR}/cp2k.out.hybrid"
self.output_file_conv = f"{TEST_FILES_DIR}/cp2k.out.conv"

self.modder = Cp2kModder(filename=self.input_file)

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

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

MODULE_DIR = Path(__file__).resolve().parent
TEST_FILES_DIR = f"{TEST_FILES}/cp2k"

cwd = os.getcwd()

Expand All @@ -26,15 +27,13 @@ class HandlerTests(unittest.TestCase):
def setUp(self):
warnings.filterwarnings("ignore")

self.TEST_FILES_DIR = os.path.join(Path(__file__).parent.absolute(), "../../../test_files/cp2k")
clean_dir(TEST_FILES_DIR)

clean_dir(self.TEST_FILES_DIR)

self.input_file = os.path.join(self.TEST_FILES_DIR, "cp2k.inp")
self.input_file_hybrid = os.path.join(self.TEST_FILES_DIR, "cp2k.inp.hybrid")
self.output_file = os.path.join(self.TEST_FILES_DIR, "cp2k.out.test")
self.std_err = os.path.join(self.TEST_FILES_DIR, "std_err.tmp")
self.logfile = os.path.join(self.TEST_FILES_DIR, "custodian.json")
self.input_file = f"{TEST_FILES_DIR}/cp2k.inp"
self.input_file_hybrid = f"{TEST_FILES_DIR}/cp2k.inp.hybrid"
self.output_file = f"{TEST_FILES_DIR}/cp2k.out.test"
self.std_err = f"{TEST_FILES_DIR}/std_err.tmp"
self.logfile = f"{TEST_FILES_DIR}/custodian.json"

if os.path.isfile(Custodian.LOG_FILE):
os.remove("custodian.json")
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
__author__ = "Chen Zheng"
__copyright__ = "Copyright 2012, The Materials Project"
__version__ = "0.1"
__maintainer__ = "Chen Zheng"
__email__ = "chz022@ucsd.edu"
__date__ = "Oct 18, 2017"

import glob
import os
import shutil
import unittest

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

test_dir = os.path.join(os.path.dirname(__file__), "..", "..", "..", "test_files")
__author__ = "Chen Zheng"
__copyright__ = "Copyright 2012, The Materials Project"
__version__ = "0.1"
__maintainer__ = "Chen Zheng"
__email__ = "chz022@ucsd.edu"
__date__ = "Oct 18, 2017"


def clean_dir():
Expand All @@ -22,8 +21,8 @@ def clean_dir():

class UnconvergedErrorHandlerTest(unittest.TestCase):
def setUp(self):
os.chdir(test_dir)
subdir = os.path.join(test_dir, "feff_unconverge")
os.chdir(TEST_FILES)
subdir = f"{TEST_FILES}/feff_unconverged"
os.chdir(subdir)
shutil.copy("ATOMS", "ATOMS.orig")
shutil.copy("PARAMETERS", "PARAMETERS.orig")
Expand Down
7 changes: 4 additions & 3 deletions custodian/feff/tests/test_jobs.py → tests/feff/test_jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@
from monty.tempfile import ScratchDir
from pymatgen.io.feff.inputs import Atoms, Tags

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

test_dir = os.path.join(os.path.dirname(__file__), "..", "..", "..", "test_files", "feff_unconverge")
TEST_DIR = f"{TEST_FILES}/feff_unconverged"


class FeffJobTest(unittest.TestCase):
Expand All @@ -25,7 +26,7 @@ def test_to_from_dict(self):
assert f2.feff_cmd == "hello"

def test_setup(self):
with cd(test_dir), ScratchDir(".", copy_from_current_on_enter=True):
with cd(TEST_DIR), ScratchDir(".", copy_from_current_on_enter=True):
f = FeffJob("hello", backup=True)
f.setup()

Expand All @@ -38,7 +39,7 @@ def test_setup(self):
assert atom == atom_origin

def test_postprocess(self):
with cd(test_dir), ScratchDir(".", copy_from_current_on_enter=True):
with cd(TEST_DIR), ScratchDir(".", copy_from_current_on_enter=True):
f = FeffJob("hello", backup=True, gzipped=True)
f.postprocess()
assert os.path.exists("feff_out.1.tar.gz")
Expand Down
Loading

0 comments on commit ebbd822

Please sign in to comment.