Test: Be more paranoid about our log output.
authorGerald Combs <gerald@wireshark.org>
Mon, 21 May 2018 16:02:26 +0000 (09:02 -0700)
committerGerald Combs <gerald@wireshark.org>
Mon, 21 May 2018 18:03:42 +0000 (18:03 +0000)
Run our CTest tests with PYTHONIOENCODING=UTF-8.

If someone runs our tests manually and their output encoding isn't
UTF-8, print replacement characters instead of failing with an error.

Open our log files with "errors='backslashreplace'" in case their
contents aren't UTF-8.

Change-Id: Ifa4d12c2b5e272cf3903f3e0c6102e4d961562f1
Reviewed-on: https://code.wireshark.org/review/27686
Petri-Dish: Gerald Combs <gerald@wireshark.org>
Tested-by: Petri Dish Buildbot
Reviewed-by: Gerald Combs <gerald@wireshark.org>
CMakeLists.txt
test/subprocesstest.py
test/test.py

index 5d1e2100541c768b17e8b4de15430cb060bb40df..a69c1e08b619d889a4a0b40193eb60edf29b49fb 100644 (file)
@@ -2941,7 +2941,8 @@ separate_arguments(TEST_EXTRA_ARGS)
 foreach(_suite_name ${_test_suite_list})
        add_test(
                NAME ${_suite_name}
-               COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_SOURCE_DIR}/test/test.py
+               COMMAND ${CMAKE_COMMAND} -E env PYTHONIOENCODING=UTF-8
+                       ${PYTHON_EXECUTABLE} ${CMAKE_SOURCE_DIR}/test/test.py
                        --verbose
                        --program-path ${_test_suite_program_path}
                        ${TEST_EXTRA_ARGS}
index 61bab5f970da5cd707c9913f0bce4a06ca9f9ca8..8fae5da038f64d62dfc4cde63a65caf4a5e67931 100644 (file)
@@ -182,7 +182,7 @@ class SubprocessTestCase(unittest.TestCase):
                 # Leave some evidence behind.
                 self.cleanup_files = []
                 print('\nProcess output for {}:'.format(self.id()))
-                with io.open(self.log_fname, 'r', encoding='UTF-8') as log_fd:
+                with io.open(self.log_fname, 'r', encoding='UTF-8', errors='backslashreplace') as log_fd:
                     for line in log_fd:
                         sys.stdout.write(line)
         for filename in self.cleanup_files:
index 91bddaa5f8fe8722e8ca9af922902a6f3f7b47d4..cc6c134e30ffbcac63f99911bfff1b70007e9ca5 100755 (executable)
@@ -112,6 +112,18 @@ def main():
         parser.print_usage()
         sys.exit(1)
 
+    #
+    if sys.stdout.encoding != 'UTF-8':
+        import codecs
+        import locale
+        sys.stderr.write('Warning: Output encoding is {0} and not UTF-8.\n'.format(sys.stdout.encoding))
+        if sys.version_info[0] >= 3:
+            sys.stdout = codecs.getwriter(locale.getpreferredencoding())(sys.stdout.buffer, 'backslashreplace')
+            sys.stderr = codecs.getwriter(locale.getpreferredencoding())(sys.stderr.buffer, 'backslashreplace')
+        else:
+            sys.stdout = codecs.getwriter(locale.getpreferredencoding())(sys.stdout, 'backslashreplace')
+            sys.stderr = codecs.getwriter(locale.getpreferredencoding())(sys.stderr, 'backslashreplace')
+
     run_suite = unittest.defaultTestLoader.loadTestsFromNames(run_ids)
     runner = unittest.TextTestRunner(verbosity=args.verbose)
     test_result = runner.run(run_suite)