From: Christian Herdtweck Date: Wed, 24 Oct 2018 15:30:27 +0000 (+0200) Subject: Fix encoding issue when building rpm X-Git-Tag: v1.3~2 X-Git-Url: http://developer.intra2net.com/git/?a=commitdiff_plain;h=0ba9936cb9e456c7920c91a916d7239da482958c;p=pyi2ncommon Fix encoding issue when building rpm This issue never occured in regular shells because usually LOCALE and system encoding and similar is set. --- diff --git a/test/test_simple_cnf.py b/test/test_simple_cnf.py index 60951ea..92ef9e7 100755 --- a/test/test_simple_cnf.py +++ b/test/test_simple_cnf.py @@ -25,6 +25,7 @@ import unittest from traceback import print_exc from tempfile import mkstemp import os +import sys from src.simple_cnf import SimpleCnf @@ -92,6 +93,27 @@ TEST_SET = """ 61 TESTVAR,0: "test" """ +#: encoding for text files, set in :py:func:`setUpModule` +ENCODING = None + + +def setUpModule(): + """Called once before all tests; determines encoding + + This test might run in very limited build environments without locale, so + auto-selection of temp-file text encoding may return 'ASCII'. + Therefore, do the encoding "manually". + """ + global ENCODING + ENCODING = sys.getfilesystemencoding() + if ENCODING.lower() in ('ascii', 'ansi_x3.4-1968'): + ENCODING = 'utf8' + print('Using fallback encoding {} for temp file creation' + .format(ENCODING)) + else: + print('Using filesystem encoding {} for temp file creation' + .format(ENCODING)) + class SimpleCnfTest(unittest.TestCase): """ The one and only test case in this module `-->` see module doc """ @@ -111,10 +133,10 @@ class SimpleCnfTest(unittest.TestCase): def setUpClass(cls): """ before running tests: write conf var string to temp file """ try: - sys_file_descriptor, cls.import_file = mkstemp(text=True) + sys_file_descriptor, cls.import_file = mkstemp() os.close(sys_file_descriptor) - with open(cls.import_file, 'wt') as file_handle: - file_handle.write(TEST_SET) + with open(cls.import_file, 'wb') as file_handle: + file_handle.write(TEST_SET.encode(ENCODING)) print('created temp file {0}'.format(cls.import_file)) except Exception: print('exception creating temp file:')