Skip to content

Commit

Permalink
Merge pull request #579 from FAIRmat-NFDI/read-nexus-log-level
Browse files Browse the repository at this point in the history
Do not log `read_nexus` to the console in tests
  • Loading branch information
lukaspie authored Mar 7, 2025
2 parents a08f56b + 2478fe7 commit 09fb12d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
13 changes: 9 additions & 4 deletions src/pynxtools/testing/nexus_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,8 @@ def compare_logs(gen_lines: List[str], ref_lines: List[str]) -> None:

section_ignore_lines = []
section = None

diffs = []
for ind, (gen_l, ref_l) in enumerate(zip(gen_lines, ref_lines)):
if gen_l.startswith(SECTION_SEPARATOR) and ref_l.startswith(
SECTION_SEPARATOR
Expand All @@ -219,10 +221,13 @@ def compare_logs(gen_lines: List[str], ref_lines: List[str]) -> None:
if gen_l != ref_l and not should_skip_line(
gen_l, ref_l, IGNORE_LINES + section_ignore_lines
):
raise AssertionError(
f"Log files are different at line {ind}\n"
f"generated: {gen_l}\nreferenced: {ref_l}"
)
diffs += [
f"Log files are different at line {ind}\ngenerated: {gen_l}\nreferenced: {ref_l}"
]

if diffs:
diff_report = "\n".join(diffs)
raise AssertionError(diff_report)

# Load log paths
ref_log_path = get_log_file(self.ref_nexus_file, "ref_nexus.log", self.tmp_path)
Expand Down
17 changes: 16 additions & 1 deletion tests/nexus/test_nexus.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import logging
import os
import difflib

import lxml.etree as ET
import numpy as np
Expand Down Expand Up @@ -167,6 +168,8 @@ def test_nexus(tmp_path):
example_data = os.path.join(
os.getcwd(), "src", "pynxtools", "data", "201805_WSe2_arpes.nxs"
)

logger.handlers.clear()
logger.setLevel(logging.DEBUG)
handler = logging.FileHandler(os.path.join(tmp_path, "nexus_test.log"), "w")
formatter = logging.Formatter("%(levelname)s - %(message)s")
Expand All @@ -186,7 +189,19 @@ def test_nexus(tmp_path):
encoding="utf-8",
) as reffile:
ref = reffile.readlines()
assert log == ref

if log != ref:
differences = list(
difflib.unified_diff(
ref, log, fromfile="reference", tofile="actual", lineterm=""
)
)
diff_report = "\n".join(differences)
if diff_report:
pytest.fail(f"Log output does not match reference:\n{diff_report}")
pytest.fail(
f"Log output does not match reference even though each individual line matches."
)

# import filecmp
# # didn't work with filecmp library
Expand Down

0 comments on commit 09fb12d

Please sign in to comment.