Adapt unittests for different tracebacks on python 3.11
authorChristian Herdtweck <christian.herdtweck@intra2net.com>
Fri, 24 Nov 2023 14:46:46 +0000 (15:46 +0100)
committerChristian Herdtweck <christian.herdtweck@intra2net.com>
Mon, 30 Sep 2024 08:48:12 +0000 (10:48 +0200)
Apparently, error tracebacks now have extra lines with pointers to the
position in the line where the error occurred. Exact version, where this
happens, is unknown.

test/test_log_helpers.py

index 46ad2cd..0423e0a 100644 (file)
@@ -12,6 +12,7 @@ from tempfile import mkstemp
 import os
 from os.path import isfile, basename
 import re
+import sys
 
 from src import log_helpers
 
@@ -46,11 +47,15 @@ class LogHelperTest(unittest.TestCase):
                     enumerate(zip(log_reader, expected_contents.splitlines())):
                 if re.match(r'\d{1,2}:\d{2}:\d{2}:\d{3}\s+', actual_line):
                     cmp_line = actual_line[13:].rstrip()
-                elif re.match(r'\s*File ".+", line \d+, in .+', actual_line):
+                elif re.match(r'\s*File ".+", line \d+, in .+', actual_line) \
+                        or re.match(r'\s+~\^~\s*', actual_line):
                     cmp_line = '  TRACEBACK LINE REPLACED'
                 else:
                     cmp_line = actual_line.rstrip()
-                self.assertEqual(cmp_line, expect_line)
+                self.assertEqual(cmp_line, expect_line,
+                                 msg="Unexpected line in output (line {line_no+1}:\n" \
+                                     "expect: {expect_line}\n" \
+                                     "   got: {cmp_line}")
 
     def test_short_level_format(self):
         """Tests that calls to :py:class:`ShortLevelFormatter` do not fail."""
@@ -104,7 +109,10 @@ class LogHelperTest(unittest.TestCase):
             'err | 1/0 still does not work!   [regular levelname=ERROR]\n' \
             'Traceback (most recent call last):\n' \
             '  TRACEBACK LINE REPLACED\n' \
-            '    impossible_result = 1/0\n' \
+            '    impossible_result = 1/0\n'
+        if sys.version_info[0] > 3 or sys.version_info[0] == 3 and sys.version_info[1] > 7:
+            expected_contents += '  TRACEBACK LINE REPLACED\n'
+        expected_contents += \
             'ZeroDivisionError: division by zero\n' \
             'info| done testing   [regular levelname=INFO]\n'
         self.check_expected_contents(expected_contents)