From b44a7817b8dc3a7898ee6445d2c7494644b10797 Mon Sep 17 00:00:00 2001 From: Christian Stenger Date: Wed, 19 Jul 2023 09:26:37 +0200 Subject: [PATCH] SquishTests: Handle codec problems Reading files depends on the system encoding and will return a bytes object instead of a str even for text files on some Windows setups. Handle this by explcitly converting to str if needed. Handle possible decoding issues by falling back to UTF8 encoding when running into decoding error while decoding on Windows. Change-Id: I8c1f24ff052710e4b1927399d54e321088e3b171 Reviewed-by: Marcus Tillmanns --- tests/system/shared/utils.py | 8 ++++++-- tests/system/suite_editors/tst_edit_externally/test.py | 3 +++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/tests/system/shared/utils.py b/tests/system/shared/utils.py index 2a17e0333c2..8639c6e2651 100644 --- a/tests/system/shared/utils.py +++ b/tests/system/shared/utils.py @@ -611,8 +611,12 @@ def stringify(obj): if isinstance(obj, stringTypes): return obj if isinstance(obj, bytes): - tmp = obj.decode('cp1252') if platform.system() in ('Microsoft','Windows') else obj.decode() - return tmp + if not platform.system() in ('Microsoft', 'Windows'): + return obj.decode() + try: + return obj.decode('cp1252') + except UnicodeDecodeError: + return obj.decode('utf-8') class GitClone: diff --git a/tests/system/suite_editors/tst_edit_externally/test.py b/tests/system/suite_editors/tst_edit_externally/test.py index dea2f5d8ec6..e04ab19670a 100644 --- a/tests/system/suite_editors/tst_edit_externally/test.py +++ b/tests/system/suite_editors/tst_edit_externally/test.py @@ -39,7 +39,10 @@ def main(): test.fatal("Could not get the editor for '%s'" % currentFile, "Skipping this file for now.") continue + contentBefore = readFile(currentFile) + if not currentFile.endswith(".bin"): + contentBefore = stringify(contentBefore) if i % 2 == 0: # modify current file and store content for next modification formerContent = contentBefore