Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/changes/1712.bugfix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix warning messages in simtel metadata reader to avoid the log-file testing routines to fail.
2 changes: 1 addition & 1 deletion src/simtools/simtel/simtel_io_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def safe_decode(byte_str, encoding, errors="ignore"):
return {k.decode(encoding, errors="ignore"): v.decode(encoding) for k, v in meta.items()}
except UnicodeDecodeError as e:
_logger.warning(
f"Failed to decode metadata with encoding {encoding}: {e}. "
f"Unable to decode metadata with encoding {encoding}: {e}. "
"Falling back to 'utf-8' with errors='ignore'."
)
return {safe_decode(k, encoding): safe_decode(v, encoding) for k, v in meta.items()}
Expand Down
24 changes: 13 additions & 11 deletions src/simtools/testing/log_inspector.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
re.compile(r"segmentation fault", re.IGNORECASE),
]

IGNORE_PATTERNS = [re.compile(r"Falling back to 'utf-8' with errors='ignore'", re.IGNORECASE)]


def inspect(log_text):
"""
Expand All @@ -34,17 +36,17 @@ def inspect(log_text):
True if no errors or warnings are found, False otherwise.
"""
log_text = log_text if isinstance(log_text, list) else [log_text]
issues = []
for txt in log_text:
for lineno, line in enumerate(txt.splitlines(), 1):
# Skip lines containing "INFO::"
if "INFO::" in line:
continue
for pattern in ERROR_PATTERNS:
if pattern.search(line):
issues.append((lineno, line))
break

issues = [
(lineno, line)
for txt in log_text
for lineno, line in enumerate(txt.splitlines(), 1)
if "INFO::" not in line
and any(p.search(line) for p in ERROR_PATTERNS)
and not any(p.search(line) for p in IGNORE_PATTERNS)
]

for lineno, line in issues:
_logger.error(f"Error or warning found in log at line {lineno}: {line.strip()}")
return len(issues) == 0

return not issues
2 changes: 1 addition & 1 deletion tests/unit_tests/simtel/test_simtel_io_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def test_decode_with_unicode_error(caplog):
assert "key2" in result
assert result["key1"] == "value1"
assert result["key2"] == " invalid utf8"
assert "Failed to decode metadata with encoding utf-8" in caplog.text
assert "Unable to decode metadata with encoding utf-8" in caplog.text


def test_read_sim_telarray_metadata(sim_telarray_file_gamma):
Expand Down
10 changes: 10 additions & 0 deletions tests/unit_tests/testing/test_log_inspector.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,13 @@ def test_inspect_single_string_input(mock_logger):
assert result is False
assert len(mock_logger.records) == 1
assert "Error or warning found in log at line 2" in mock_logger.text


def test_inspect_ignore_patterns(mock_logger):
log_text = (
"WARNING::simtel_io_metadata(l80)::_decode_dictionary::Unable to decode metadata "
"with encoding utf8: 'utf-8' codec can't decode byte 0x80 in position 128: invalid "
"start byte. Falling back to 'utf-8' with errors='ignore'."
)
result = inspect(log_text)
assert result is True