Skip to content

Commit 9fc61b0

Browse files
authored
do not propagate test log to CLI (#695)
1 parent d923b76 commit 9fc61b0

File tree

1 file changed

+59
-16
lines changed

1 file changed

+59
-16
lines changed

src/pynxtools/testing/nexus_conversion.py

Lines changed: 59 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ def get_log_file(nxs_file, log_file, tmp_path):
4343
logger = logging.getLogger("pynxtools")
4444
logger.handlers.clear()
4545
logger.setLevel(logging.DEBUG)
46+
logger.propagate = False
4647
log_file = os.path.join(tmp_path, log_file)
4748
handler = logging.FileHandler(log_file, "w")
4849
formatter = logging.Formatter("%(levelname)s - %(message)s")
@@ -195,10 +196,24 @@ def check_reproducibility_of_nexus(self, **kwargs):
195196

196197
SECTION_SEPARATOR = "DEBUG - ===== "
197198

198-
def should_skip_line(gen_l: str, ref_l: str, ignore_lines: list[str]) -> bool:
199-
"""Check if both lines start with any ignored prefix."""
199+
def should_skip_line(*lines: str, ignore_lines: list[str]) -> bool:
200+
"""
201+
Check if all given lines start with any ignored prefix.
202+
203+
Parameters
204+
----------
205+
*lines : str
206+
One or more lines to check.
207+
ignore_lines : list[str]
208+
List of prefixes to ignore.
209+
210+
Returns
211+
-------
212+
bool
213+
True if all lines start with any of the ignored prefixes.
214+
"""
200215
return any(
201-
gen_l.startswith(ignore) and ref_l.startswith(ignore)
216+
all(line.startswith(ignore) for line in lines)
202217
for ignore in ignore_lines
203218
)
204219

@@ -214,35 +229,63 @@ def load_logs(
214229

215230
def compare_logs(gen_lines: list[str], ref_lines: list[str]) -> None:
216231
"""Compare log lines, ignoring specific differences."""
232+
233+
def extra_lines(
234+
lines1: list[str], lines2: list[str]
235+
) -> list[Optional[str]]:
236+
"""Return lines in lines1 but not in lines2, with line numbers and ignoring specified lines."""
237+
diffs: list[Optional[str]] = []
238+
section_ignore_lines = []
239+
section = None
240+
for ind, line in enumerate(lines1):
241+
if line.startswith(SECTION_SEPARATOR):
242+
section = line.rsplit(SECTION_SEPARATOR)[-1].strip()
243+
section_ignore_lines = IGNORE_SECTIONS.get(section, [])
244+
if line not in lines2 and not should_skip_line(
245+
line, ignore_lines=IGNORE_LINES + section_ignore_lines
246+
):
247+
diffs.append(f"{line.strip()} (line: {ind})")
248+
return diffs
249+
250+
# Case 1: line counts differ
217251
if len(gen_lines) != len(ref_lines):
218-
raise AssertionError(
252+
diffs_gen = extra_lines(gen_lines, ref_lines)
253+
diffs_ref = extra_lines(ref_lines, gen_lines)
254+
255+
error_msg = (
219256
f"Log files are different: mismatched line counts. "
220257
f"Generated file has {len(gen_lines)} lines, "
221258
f"while reference file has {len(ref_lines)} lines."
222259
)
223-
260+
if diffs_gen:
261+
error_msg += "\n\nExtra lines in generated:\n" + "\n".join(
262+
diffs_gen
263+
)
264+
if diffs_ref:
265+
error_msg += "\n\nExtra lines in reference:\n" + "\n".join(
266+
diffs_ref
267+
)
268+
269+
raise AssertionError(error_msg)
270+
271+
# Case 2: same line counts, check for diffs
272+
diffs = []
224273
section_ignore_lines = []
225274
section = None
226-
227-
diffs = []
228275
for ind, (gen_l, ref_l) in enumerate(zip(gen_lines, ref_lines)):
229276
if gen_l.startswith(SECTION_SEPARATOR) and ref_l.startswith(
230277
SECTION_SEPARATOR
231278
):
232279
section = gen_l.rsplit(SECTION_SEPARATOR)[-1].strip()
233280
section_ignore_lines = IGNORE_SECTIONS.get(section, [])
234-
235-
# Compare lines if not in ignore list
236281
if gen_l != ref_l and not should_skip_line(
237-
gen_l, ref_l, IGNORE_LINES + section_ignore_lines
282+
gen_l, ref_l, ignore_lines=IGNORE_LINES + section_ignore_lines
238283
):
239-
diffs += [
240-
f"Log files are different at line {ind}\ngenerated: {gen_l}\nreferenced: {ref_l}"
241-
]
242-
284+
diffs.append(
285+
f"Log files are different at line {ind}:\n generated: {gen_l}\n reference: {ref_l}"
286+
)
243287
if diffs:
244-
diff_report = "\n".join(diffs)
245-
raise AssertionError(diff_report)
288+
raise AssertionError("\n".join(diffs))
246289

247290
# Load log paths
248291
ref_log_path = get_log_file(self.ref_nexus_file, "ref_nexus.log", self.tmp_path)

0 commit comments

Comments
 (0)