summaryrefslogtreecommitdiff
path: root/tests/run-tests.py
diff options
context:
space:
mode:
authorAndrew Leech <andrew.leech@planetinnovation.com.au>2025-05-26 19:31:30 +1000
committerDamien George <damien@micropython.org>2025-11-22 00:06:59 +1100
commite067d96c8b71b3fe258583fd6cddb1094cb4efe5 (patch)
treed04978be6c7739c6987d6f40137191682a5b2baf /tests/run-tests.py
parent7ac8fcf752389e2af37b49994478bd9d93f48434 (diff)
tests/run-tests.py: Add general newline normalization function.
Add a general normalize_newlines() function that handles newline variations (\\r\\r\\n, \\r\\n) to \\n while preserving literal \\r characters that are part of test content. This provides a robust solution for cross-platform test compatibility, particularly addressing PTY double-newline issues that can occur with some terminal implementations. The function is applied to all test output before comparison, eliminating platform-specific newline issues. Includes a unit test to verify the normalization behavior. Signed-off-by: Andrew Leech <andrew.leech@planetinnovation.com.au>
Diffstat (limited to 'tests/run-tests.py')
-rwxr-xr-xtests/run-tests.py19
1 files changed, 18 insertions, 1 deletions
diff --git a/tests/run-tests.py b/tests/run-tests.py
index b4654c2e6..fba011fb5 100755
--- a/tests/run-tests.py
+++ b/tests/run-tests.py
@@ -60,6 +60,23 @@ DIFF = os.getenv("MICROPY_DIFF", "diff -u")
# Set PYTHONIOENCODING so that CPython will use utf-8 on systems which set another encoding in the locale
os.environ["PYTHONIOENCODING"] = "utf-8"
+
+def normalize_newlines(data):
+ """Normalize newline variations to \\n.
+
+ Only normalizes actual line endings, not literal \\r characters in strings.
+ Handles \\r\\r\\n and \\r\\n cases to ensure consistent comparison
+ across different platforms and terminals.
+ """
+ if isinstance(data, bytes):
+ # Handle PTY double-newline issue first
+ data = data.replace(b"\r\r\n", b"\n")
+ # Then handle standard Windows line endings
+ data = data.replace(b"\r\n", b"\n")
+ # Don't convert standalone \r as it might be literal content
+ return data
+
+
# Code to allow a target MicroPython to import an .mpy from RAM
# Note: the module is named `__injected_test` but it needs to have `__name__` set to
# `__main__` so that the test sees itself as the main module, eg so unittest works.
@@ -706,7 +723,7 @@ def run_micropython(pyb, args, test_file, test_file_abspath, is_special=False):
)
# canonical form for all ports/platforms is to use \n for end-of-line
- output_mupy = output_mupy.replace(b"\r\n", b"\n")
+ output_mupy = normalize_newlines(output_mupy)
# don't try to convert the output if we should skip this test
if had_crash or output_mupy in (b"SKIP\n", b"SKIP-TOO-LARGE\n", b"CRASH"):