git » chasquid » commit 1b28734

test: Make "chamuyero" work in utf8 regardless of the environment

author Alberto Bertogli
2018-03-25 23:44:44 UTC
committer Alberto Bertogli
2018-03-26 00:58:47 UTC
parent b0011f5a5152b8017dce59c48362086bc8b6f6b6

test: Make "chamuyero" work in utf8 regardless of the environment

Our chamuyero tests involve reading and writing utf8. This usually
works, but is dependent on the environment: on LC_ALL=POSIX environment,
for example, Python enforces ascii as the default encoding, and the
tests break.

So this patch makes chamuyero explicitly set utf8 encodings in stdout
and all the sockets.

test/util/chamuyero +11 -7

diff --git a/test/util/chamuyero b/test/util/chamuyero
index 4cdb042..40929ff 100755
--- a/test/util/chamuyero
+++ b/test/util/chamuyero
@@ -21,9 +21,13 @@ import time
 
 # Command-line flags.
 ap = argparse.ArgumentParser()
-ap.add_argument("script", type=argparse.FileType('r', encoding='UTF-8'))
+ap.add_argument("script", type=argparse.FileType('r', encoding='utf8'))
 args = ap.parse_args()
 
+# Make sure stdout is open in utf8 mode, as we will print our input, which is
+# utf8, and want it to work regardless of the environment.
+sys.stdout = open(sys.stdout.fileno(), mode='w', encoding='utf8', buffering=1)
+
 
 class Process (object):
     def __init__(self, cmd, **kwargs):
@@ -60,8 +64,8 @@ class Sock (object):
 
     def _accept(self):
         conn, _ = self.sock.accept()
-        self.connr = conn.makefile(mode="r")
-        self.connw = conn.makefile(mode="w")
+        self.connr = conn.makefile(mode="r", encoding="utf8")
+        self.connw = conn.makefile(mode="w", encoding="utf8")
         self.has_conn.set()
 
     def write(self, s):
@@ -93,8 +97,8 @@ class TCPSock (Sock):
 
     def connect(self):
         self.sock = socket.create_connection(self.addr)
-        self.connr = self.sock.makefile(mode="r")
-        self.connw = self.sock.makefile(mode="w")
+        self.connr = self.sock.makefile(mode="r", encoding="utf8")
+        self.connw = self.sock.makefile(mode="w", encoding="utf8")
         self.has_conn.set()
 
 
@@ -106,8 +110,8 @@ class TLSSock (Sock):
         self.sock = ssl.wrap_socket(plain_sock)
 
     def connect(self):
-        self.connr = self.sock.makefile(mode="r")
-        self.connw = self.sock.makefile(mode="w")
+        self.connr = self.sock.makefile(mode="r", encoding="utf8")
+        self.connw = self.sock.makefile(mode="w", encoding="utf8")
         self.has_conn.set()