Step 0 of Make-a-Lisp for Erlang
[jackhill/mal.git] / runtest.py
index 4b57ae9..436b769 100755 (executable)
@@ -2,11 +2,14 @@
 
 import os, sys, re
 import argparse, time
+import signal, atexit
 
-import pty, signal, atexit
 from subprocess import Popen, STDOUT, PIPE
 from select import select
 
+# Pseudo-TTY and terminal manipulation
+import pty, array, fcntl, termios
+
 IS_PY_3 = sys.version_info[0] == 3
 
 # TODO: do we need to support '\n' too
@@ -26,8 +29,6 @@ parser.add_argument('--pre-eval', default=None, type=str,
         help="Mal code to evaluate prior to running the test")
 parser.add_argument('--no-pty', action='store_true',
         help="Use direct pipes instead of pseudo-tty")
-parser.add_argument('--mono', action='store_true',
-        help="Use workarounds Mono/.Net Console misbehaviors, implies --no-pty")
 
 parser.add_argument('test_file', type=argparse.FileType('r'),
         help="a test file formatted as with mal test data")
@@ -36,27 +37,42 @@ parser.add_argument('mal_cmd', nargs="*",
              "specify a Mal command line with dashed options.")
 
 class Runner():
-    def __init__(self, args, no_pty=False, mono=False):
+    def __init__(self, args, no_pty=False):
         #print "args: %s" % repr(args)
-        if mono: no_pty = True
         self.no_pty = no_pty
-        self.mono = mono
 
         # Cleanup child process on exit
         atexit.register(self.cleanup)
 
+        self.p = None
+        env = os.environ
+        env['TERM'] = 'dumb'
+        env['INPUTRC'] = '/dev/null'
+        env['PERL_RL'] = 'false'
         if no_pty:
             self.p = Popen(args, bufsize=0,
                            stdin=PIPE, stdout=PIPE, stderr=STDOUT,
-                           preexec_fn=os.setsid)
+                           preexec_fn=os.setsid,
+                           env=env)
             self.stdin = self.p.stdin
             self.stdout = self.p.stdout
         else:
             # provide tty to get 'interactive' readline to work
             master, slave = pty.openpty()
+
+            # Set terminal size large so that readline will not send
+            # ANSI/VT escape codes when the lines are long.
+            buf = array.array('h', [100, 200, 0, 0])
+            fcntl.ioctl(master, termios.TIOCSWINSZ, buf, True)
+
             self.p = Popen(args, bufsize=0,
                            stdin=slave, stdout=slave, stderr=STDOUT,
-                           preexec_fn=os.setsid)
+                           preexec_fn=os.setsid,
+                           env=env)
+            # Now close slave so that we will get an exception from
+            # read when the child exits early
+            # http://stackoverflow.com/questions/11165521
+            os.close(slave)
             self.stdin = os.fdopen(master, 'r+b', 0)
             self.stdout = self.stdin
 
@@ -70,8 +86,8 @@ class Runner():
             [outs,_,_] = select([self.stdout], [], [], 1)
             if self.stdout in outs:
                 new_data = self.stdout.read(1)
-                #print "new_data: '%s'" % new_data
                 new_data = new_data.decode("utf-8") if IS_PY_3 else new_data
+                #print "new_data: '%s'" % new_data
                 if self.no_pty:
                     self.buf += new_data.replace("\n", "\r\n")
                 else:
@@ -92,14 +108,14 @@ class Runner():
             return bytes(s, "utf-8") if IS_PY_3 else s
 
         self.stdin.write(_to_bytes(str + "\n"))
-        if self.mono:
-            # Simulate echo
-            self.buf += _to_bytes(str + "\r\n")
 
     def cleanup(self):
         #print "cleaning up"
         if self.p:
-            os.killpg(self.p.pid, signal.SIGTERM)
+            try:
+                os.killpg(self.p.pid, signal.SIGTERM)
+            except OSError:
+                pass
             self.p = None
 
 
@@ -108,7 +124,7 @@ test_data = args.test_file.read().split('\n')
 
 if args.rundir: os.chdir(args.rundir)
 
-r = Runner(args.mal_cmd, no_pty=args.no_pty, mono=args.mono)
+r = Runner(args.mal_cmd, no_pty=args.no_pty)
 
 
 test_idx = 0
@@ -178,7 +194,12 @@ while test_data:
         break
     sys.stdout.write("TEST: %s -> [%s,%s]" % (form, repr(out), repr(ret)))
     sys.stdout.flush()
-    expected = "%s%s%s%s" % (form, sep, out, ret)
+
+    # The repeated form is to get around an occasional OS X issue
+    # where the form is repeated.
+    # https://github.com/kanaka/mal/issues/30
+    expected = ["%s%s%s%s" % (form, sep, out, ret),
+                "%s%s%s%s%s%s" % (form, sep, form, sep, out, ret)]
 
     r.writeline(form)
     try:
@@ -186,7 +207,7 @@ while test_data:
                                 '\r\nmal-user> ', '\nmal-user> '],
                                 timeout=args.test_timeout)
         #print "%s,%s,%s" % (idx, repr(p.before), repr(p.after))
-        if ret == "*" or res == expected:
+        if ret == "*" or res in expected:
             print(" -> SUCCESS")
         else:
             print(" -> FAIL (line %d):" % line_num)
@@ -194,7 +215,9 @@ while test_data:
             print("    Got      : %s" % repr(res))
             fail_cnt += 1
     except:
-        print("Got Exception")
+        _, exc, _ = sys.exc_info()
+        print("\nException: %s" % repr(exc))
+        print("Output before exception:\n%s" % r.buf)
         sys.exit(1)
 
 if fail_cnt > 0: