Skip to content

Commit

Permalink
clean up ref log comparison
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaspie committed Oct 31, 2024
1 parent c7d58a8 commit 3a9d9e3
Showing 1 changed file with 47 additions and 40 deletions.
87 changes: 47 additions & 40 deletions src/pynxtools/testing/nexus_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,47 +148,54 @@ def check_reproducibility_of_nexus(self):
"ATTRS (//@file_update_time)": ["DEBUG - value:"],
"ATTRS (//@h5py_version)": ["DEBUG - value:"],
}
SECTION_SEPARATOR = "DEBUG - ===== "

section = None
section_ignore_lines = []
section_separator = "DEBUG - ===== "

ref_log = get_log_file(self.ref_nexus_file, "ref_nexus.log", self.tmp_path)
gen_log = get_log_file(self.created_nexus, "gen_nexus.log", self.tmp_path)
with open(gen_log, "r", encoding="utf-8") as gen, open(
ref_log, "r", encoding="utf-8"
) as ref:
gen_lines = gen.readlines()
ref_lines = ref.readlines()
if len(gen_lines) != len(ref_lines):
assert False, (
f"Log files are different: mismatched line counts. "
f"Generated file has {len(gen_lines)} lines, "
f"while reference file has {len(ref_lines)} lines."
def should_skip_line(gen_l: str, ref_l: str, ignore_lines: list[str]) -> bool:
"""Check if both lines start with any ignored prefix."""
return any(
gen_l.startswith(ignore) and ref_l.startswith(ignore)
for ignore in ignore_lines
)
for ind, (gen_l, ref_l) in enumerate(zip(gen_lines, ref_lines)):
skip_it = False
if gen_l.startswith(section_separator) and ref_l.startswith(
section_separator
):
section = gen_l.rsplit(section_separator)[-1].strip()
section_ignore_lines = SECTION_IGNORE.get(section, [])
if gen_l != ref_l:
# skip ignored lines (mainly version conflicts)
for ignore_line in IGNORE_LINES:
if gen_l.startswith(ignore_line) and ref_l.startswith(ignore_line):
skip_it = True
break
if not skip_it:
# skip ignored lines for this section
for ignore_line in section_ignore_lines:
if gen_l.startswith(ignore_line) and ref_l.startswith(
ignore_line
):
skip_it = True
break
if not skip_it:

def load_logs(
gen_log_path: str, ref_log_path: str
) -> tuple[list[str], list[str]]:
"""Load log files and return their contents as lists of lines."""
with open(gen_log_path, "r", encoding="utf-8") as gen, open(
ref_log_path, "r", encoding="utf-8"
) as ref:
return gen.readlines(), ref.readlines()

def compare_logs(gen_lines: list[str], ref_lines: list[str]) -> None:
"""Compare log lines, ignoring specific differences."""
if len(gen_lines) != len(ref_lines):
assert False, (
f"Log files are different: mismatched line counts. "
f"Generated file has {len(gen_lines)} lines, "
f"while reference file has {len(ref_lines)} lines."
)

section_ignore_lines = []
section = None
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
):
section = gen_l.rsplit(SECTION_SEPARATOR)[-1].strip()
section_ignore_lines = SECTION_IGNORE.get(section, [])

# Compare lines if not in ignore list
if gen_l != ref_l and not should_skip_line(
gen_l, ref_l, IGNORE_LINES + section_ignore_lines
):
assert False, (
f"Log files are different at line {ind}"
f" generated: {gen_l} \n referenced : {ref_l}"
f"Log files are different at line {ind}\n"
f"generated: {gen_l}\nreferenced: {ref_l}"
)

ref_log_path = get_log_file(self.ref_nexus_file, "ref_nexus.log", self.tmp_path)
gen_log_path = get_log_file(self.created_nexus, "gen_nexus.log", self.tmp_path)
gen_lines, ref_lines = load_logs(gen_log_path, ref_log_path)

# Compare logs
compare_logs(gen_lines, ref_lines)

0 comments on commit 3a9d9e3

Please sign in to comment.