we can now self-host...almost
[jackhill/mal.git] / runtest.py
index 3a8e8c9..c5b1f42 100755 (executable)
@@ -28,8 +28,7 @@ def log(data, end='\n'):
     print(data, end=end)
     sys.stdout.flush()
 
-# TODO: do we need to support '\n' too
-sep = "\r\n"
+sep = "\n"
 rundir = None
 
 parser = argparse.ArgumentParser(
@@ -68,9 +67,11 @@ parser.add_argument('test_file', type=str,
 parser.add_argument('mal_cmd', nargs="*",
         help="Mal implementation command line. Use '--' to "
              "specify a Mal command line with dashed options.")
+parser.add_argument('--crlf', dest='crlf', action='store_true',
+        help="Write \\r\\n instead of \\n to the input")
 
 class Runner():
-    def __init__(self, args, no_pty=False):
+    def __init__(self, args, no_pty=False, line_break="\n"):
         #print "args: %s" % repr(args)
         self.no_pty = no_pty
 
@@ -113,6 +114,8 @@ class Runner():
         self.buf = ""
         self.last_prompt = ""
 
+        self.line_break = line_break
+
     def read_to_prompt(self, prompts, timeout):
         end_time = time.time() + timeout
         while time.time() < end_time:
@@ -123,18 +126,7 @@ class Runner():
                 #print("new_data: '%s'" % new_data)
                 debug(new_data)
                 # Perform newline cleanup
-                if self.no_pty:
-                    self.buf += new_data.replace("\n", "\r\n")
-                else:
-                    self.buf += new_data
-                self.buf = self.buf.replace("\r\r", "\r")
-                # Remove ANSI codes generally
-                #ansi_escape = re.compile(r'\x1B\[[0-?]*[ -/]*[@-~]')
-                # Remove rustyline ANSI CSI codes:
-                #  - [6C - CR + cursor forward
-                #  - [6K - CR + erase in line
-                ansi_escape = re.compile(r'\r\x1B\[[0-9]*[CK]')
-                self.buf = ansi_escape.sub('', self.buf)
+                self.buf += new_data.replace("\r", "")
                 for prompt in prompts:
                     regexp = re.compile(prompt)
                     match = regexp.search(self.buf)
@@ -143,14 +135,14 @@ class Runner():
                         buf = self.buf[0:match.start()]
                         self.buf = self.buf[end:]
                         self.last_prompt = prompt
-                        return buf.replace("^M", "\r")
+                        return buf
         return None
 
     def writeline(self, str):
         def _to_bytes(s):
             return bytes(s, "utf-8") if IS_PY_3 else s
 
-        self.stdin.write(_to_bytes(str.replace('\r', '\x16\r') + "\n"))
+        self.stdin.write(_to_bytes(str.replace('\r', '\x16\r') + self.line_break))
 
     def cleanup(self):
         #print "cleaning up"
@@ -219,10 +211,10 @@ class TestReader:
                     break
             if self.ret != None: break
 
-        if self.out[-2:] == sep and not self.ret:
+        if self.out[-1:] == sep and not self.ret:
             # If there is no return value, output should not end in
             # separator
-            self.out = self.out[0:-2]
+            self.out = self.out[0:-1]
         return self.form
 
 args = parser.parse_args(sys.argv[1:])
@@ -235,7 +227,7 @@ if args.rundir: os.chdir(args.rundir)
 if args.log_file:   log_file   = open(args.log_file, "a")
 if args.debug_file: debug_file = open(args.debug_file, "a")
 
-r = Runner(args.mal_cmd, no_pty=args.no_pty)
+r = Runner(args.mal_cmd, no_pty=args.no_pty, line_break="\r\n" if args.crlf else "\n")
 t = TestReader(args.test_file)
 
 
@@ -246,7 +238,7 @@ def assert_prompt(runner, prompts, timeout):
         if header:
             log("Started with:\n%s" % header)
     else:
-        log("Did not one of following prompt(s): %s" % repr(prompts))
+        log("Did not receive one of following prompt(s): %s" % repr(prompts))
         log("    Got      : %s" % repr(r.buf))
         sys.exit(1)
 
@@ -292,11 +284,8 @@ while t.next():
     # The repeated form is to get around an occasional OS X issue
     # where the form is repeated.
     # https://github.com/kanaka/mal/issues/30
-    expects = ["%s%s%s%s" % (re.escape(t.form), sep,
-                              t.out, re.escape(t.ret)),
-               "%s%s%s%s%s%s" % (re.escape(t.form), sep,
-                                  re.escape(t.form), sep,
-                                  t.out, re.escape(t.ret))]
+    expects = [".*%s%s%s" % (sep, t.out, re.escape(t.ret)),
+               ".*%s.*%s%s%s" % (sep, sep, t.out, re.escape(t.ret))]
 
     r.writeline(t.form)
     try: