Fix encoding issue when building rpm
authorChristian Herdtweck <christian.herdtweck@intra2net.com>
Wed, 24 Oct 2018 15:30:27 +0000 (17:30 +0200)
committerPlamen Dimitrov <pdimitrov@pevogam.com>
Mon, 5 Nov 2018 08:59:00 +0000 (16:59 +0800)
This issue never occured in regular shells because usually LOCALE and
system encoding and similar is set.

test/test_simple_cnf.py

index 60951ea..92ef9e7 100755 (executable)
@@ -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:')