-
Notifications
You must be signed in to change notification settings - Fork 9
/
conftest.py
89 lines (67 loc) · 2.74 KB
/
conftest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
"""A pytest ``conftest.py`` file."""
from __future__ import annotations
import os
import sys
import types
import tempfile
import importlib
import contextlib
from collections.abc import Generator
from pathlib import Path
import pytest
from scm.plams import config, add_to_class, Cp2kJob
from qmflows import InitRestart, logger
from qmflows._logger import stdout_handler
_ROOT = Path("src") / "qmflows"
_collect_ignore = [
_ROOT / "settings.py",
]
collect_ignore = [str(i) for i in _collect_ignore]
def _del_all_attr(module: types.ModuleType) -> None:
"""Delete all module attributes so they will be reloaded upon reloading the module."""
attr_names = [i for i in vars(module) if not (i.startswith("__") and i.endswith("__"))]
for name in attr_names:
delattr(module, name)
@pytest.fixture(autouse=True, scope="session")
def reload_qmflows() -> Generator[None, None, None]:
"""Reload qmflows in order to re-trigger coverage.
This is necasary as the setup.cfg ``filterwarnings`` option will load qmflows prior
to code coverage being turned on, thus falsely excluding a large portion of qmflows
from the code coverage report.
"""
yield None
module_names = [i for i in sys.modules if i.startswith("qmflows")]
for name in module_names:
module = sys.modules.pop(name)
_del_all_attr(module)
importlib.import_module("qmflows")
@pytest.fixture(autouse=True, scope="session")
def configure_plams_logger() -> "Generator[None, None, None]":
"""Remove the date/time prefix from the PLAMS logging output."""
# Ensure the plams.config dict is populated by firing up plams.init once
with open(os.devnull, "w", encoding="utf8") as f1, tempfile.TemporaryDirectory() as f2:
with contextlib.redirect_stdout(f1), InitRestart(f2):
pass
assert "log" in config
log_backup = config.log.copy()
config.log.time = False
config.log.date = False
yield None
config.log = log_backup
@pytest.fixture(autouse=True, scope="session")
def prepare_logger() -> "Generator[None, None, None]":
"""Remove logging output to the stdout stream while running tests."""
assert stdout_handler in logger.handlers
logger.removeHandler(stdout_handler)
yield None
logger.addHandler(stdout_handler)
@add_to_class(Cp2kJob)
def get_runscript(self) -> str:
"""Run a parallel version of CP2K without mpirun or srun, \
as this can cause issues with some executables.
This method is monkey-patched into the PLAMS ``Cp2kJob`` class.
"""
cp2k_command = os.environ.get("QMFLOWS_CP2K_COMMAND")
if cp2k_command is None:
cp2k_command = self.settings.get("executable", "cp2k.ssmp")
return f"{cp2k_command} -i {self._filename('inp')} -o {self._filename('out')}"