diff options
Diffstat (limited to 'runtest.py')
| -rwxr-xr-x | runtest.py | 54 |
1 files changed, 33 insertions, 21 deletions
@@ -3,7 +3,7 @@ import os, sys, re import argparse, time -import pty +import pty, signal, atexit from subprocess import Popen, STDOUT, PIPE from select import select @@ -22,8 +22,8 @@ parser.add_argument('--test-timeout', default=20, type=int, help="default timeout for each individual test action") parser.add_argument('--pre-eval', default=None, type=str, help="Mal code to evaluate prior to running the test") -parser.add_argument('--redirect', action='store_true', - help="Run implementation in bash and redirect output to /dev/null") +parser.add_argument('--mono', action='store_true', + help="Use workarounds Mono/.Net Console misbehaviors") parser.add_argument('test_file', type=argparse.FileType('r'), help="a test file formatted as with mal test data") @@ -32,17 +32,25 @@ parser.add_argument('mal_cmd', nargs="*", "specify a Mal command line with dashed options.") class Runner(): - def __init__(self, args, redirect=False): - print "args: %s" % repr(args) - if redirect: - print "using redirect" - self.p = Popen(args, bufsize=0, stdin=PIPE, stdout=PIPE, stderr=STDOUT) + def __init__(self, args, mono=False): + #print "args: %s" % repr(args) + self.mono = mono + + # Cleanup child process on exit + atexit.register(self.cleanup) + + if mono: + self.p = Popen(args, bufsize=0, + stdin=PIPE, stdout=PIPE, stderr=STDOUT, + preexec_fn=os.setsid) self.stdin = self.p.stdin self.stdout = self.p.stdout else: # provide tty to get 'interactive' readline to work master, slave = pty.openpty() - self.p = Popen(args, bufsize=0, stdin=slave, stdout=slave, stderr=STDOUT) + self.p = Popen(args, bufsize=0, + stdin=slave, stdout=slave, stderr=STDOUT, + preexec_fn=os.setsid) self.stdin = os.fdopen(master, 'r+b', 0) self.stdout = self.stdin @@ -53,11 +61,14 @@ class Runner(): def read_to_prompt(self, prompts, timeout): end_time = time.time() + timeout while time.time() < end_time: - [outs,_,_] = select([self.stdin], [], [], 1) - if self.stdin in outs: - new_data = self.stdin.read(1) + [outs,_,_] = select([self.stdout], [], [], 1) + if self.stdout in outs: + new_data = self.stdout.read(1) #print "new_data: '%s'" % new_data - self.buf += new_data + if self.mono: + self.buf += new_data.replace("\n", "\r\n") + else: + self.buf += new_data for prompt in prompts: regexp = re.compile(prompt) match = regexp.search(self.buf) @@ -69,12 +80,16 @@ class Runner(): return buf return None - def write(self, str): - self.stdout.write(str) + def writeline(self, str): + self.stdin.write(str + "\n") + if self.mono: + # Simulate echo + self.buf += str + "\r\n" def cleanup(self): + #print "cleaning up" if self.p: - self.p.terminate() + os.killpg(self.p.pid, signal.SIGTERM) self.p = None @@ -83,7 +98,7 @@ test_data = args.test_file.read().split('\n') if args.rundir: os.chdir(args.rundir) -r = Runner(args.mal_cmd, redirect=args.redirect) +r = Runner(args.mal_cmd, mono=args.mono) test_idx = 0 @@ -133,7 +148,6 @@ def assert_prompt(timeout): else: print "Did not get 'user> ' or 'mal-user> ' prompt" print " Got : %s" % repr(r.buf) - r.cleanup() sys.exit(1) @@ -156,7 +170,7 @@ while test_data: sys.stdout.flush() expected = "%s%s%s%s" % (form, sep, out, ret) - r.write(form + "\n") + r.writeline(form) try: res = r.read_to_prompt(['\r\nuser> ', '\nuser> ', '\r\nmal-user> ', '\nmal-user> '], @@ -171,9 +185,7 @@ while test_data: fail_cnt += 1 except: print "Got Exception" - r.cleanup() sys.exit(1) -r.cleanup() if fail_cnt > 0: print "FAILURES: %d" % fail_cnt |
