Skip to content

Commit

Permalink
Add test for multiline log
Browse files Browse the repository at this point in the history
  • Loading branch information
ElliottKasoar committed Mar 28, 2024
1 parent 357452f commit d193d6f
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
3 changes: 3 additions & 0 deletions janus_core/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ def format(self, record: logging.LogRecord) -> str:
str
Formatted log message.
"""
# Replace "" with '' to prevent invalid wrapping
record.msg = record.msg.replace('"', "'")

# Convert new lines into yaml list
if len(record.msg.split("\n")) > 1:
msg = record.msg
Expand Down
32 changes: 32 additions & 0 deletions tests/test_md.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from ase.io import read
import numpy as np
import pytest
import yaml

from janus_core.md import NPH, NPT, NVE, NVT, NVT_NH
from janus_core.mlip_calculators import choose_calculator
Expand Down Expand Up @@ -525,3 +526,34 @@ def test_atoms_struct(tmp_path):
assert " | Epot/N [eV]" in lines[0]
# Includes step 0
assert len(lines) == 6


def test_multiline_log(tmp_path):
"""Test multiline log messages are written correctly."""
file_prefix = tmp_path / "nvt-T300"
log_path = tmp_path / "test.log"

single_point = SinglePoint(
struct_path=DATA_PATH / "NaCl.cif",
architecture="mace",
calc_kwargs={"model": MODEL_PATH},
log_kwargs={"filename": log_path, "force": True},
)

nvt = NVT(
struct=single_point.struct,
steps=2,
file_prefix=file_prefix,
rescale_every=5,
rescale_velocities=True,
log_kwargs={"filename": log_path, "filemode": "a"},
)
nvt.run()

assert log_path.exists()
with open(log_path, encoding="utf8") as log:
log_dicts = yaml.safe_load(log)

assert log_dicts[2]["level"] == "WARNING"
assert len(log_dicts[2]["message"]) == 2
assert "UserWarning: Velocities" in log_dicts[2]["message"][0]

0 comments on commit d193d6f

Please sign in to comment.