Commit | Line | Data |
---|---|---|
31690700 JM |
1 | #!/usr/bin/env python |
2 | ||
3 | import os, sys, re | |
7907cd90 | 4 | import argparse, time |
31690700 | 5 | |
612bfe4a | 6 | import pty, signal, atexit |
7907cd90 JM |
7 | from subprocess import Popen, STDOUT, PIPE |
8 | from select import select | |
31690700 | 9 | |
8e4628da | 10 | IS_PY_3 = sys.version_info[0] == 3 |
11 | ||
31690700 JM |
12 | # TODO: do we need to support '\n' too |
13 | sep = "\r\n" | |
7907cd90 | 14 | #sep = "\n" |
31690700 JM |
15 | rundir = None |
16 | ||
17 | parser = argparse.ArgumentParser( | |
18 | description="Run a test file against a Mal implementation") | |
19 | parser.add_argument('--rundir', | |
20 | help="change to the directory before running tests") | |
21 | parser.add_argument('--start-timeout', default=10, type=int, | |
22 | help="default timeout for initial prompt") | |
23 | parser.add_argument('--test-timeout', default=20, type=int, | |
24 | help="default timeout for each individual test action") | |
cc021efe JM |
25 | parser.add_argument('--pre-eval', default=None, type=str, |
26 | help="Mal code to evaluate prior to running the test") | |
ab01be18 JM |
27 | parser.add_argument('--no-pty', action='store_true', |
28 | help="Use direct pipes instead of pseudo-tty") | |
9a383535 | 29 | parser.add_argument('--mono', action='store_true', |
ab01be18 | 30 | help="Use workarounds Mono/.Net Console misbehaviors, implies --no-pty") |
31690700 JM |
31 | |
32 | parser.add_argument('test_file', type=argparse.FileType('r'), | |
33 | help="a test file formatted as with mal test data") | |
34 | parser.add_argument('mal_cmd', nargs="*", | |
35 | help="Mal implementation command line. Use '--' to " | |
36 | "specify a Mal command line with dashed options.") | |
37 | ||
7907cd90 | 38 | class Runner(): |
ab01be18 | 39 | def __init__(self, args, no_pty=False, mono=False): |
612bfe4a | 40 | #print "args: %s" % repr(args) |
ab01be18 JM |
41 | if mono: no_pty = True |
42 | self.no_pty = no_pty | |
9a383535 | 43 | self.mono = mono |
612bfe4a JM |
44 | |
45 | # Cleanup child process on exit | |
46 | atexit.register(self.cleanup) | |
47 | ||
ab01be18 | 48 | if no_pty: |
612bfe4a JM |
49 | self.p = Popen(args, bufsize=0, |
50 | stdin=PIPE, stdout=PIPE, stderr=STDOUT, | |
51 | preexec_fn=os.setsid) | |
7907cd90 JM |
52 | self.stdin = self.p.stdin |
53 | self.stdout = self.p.stdout | |
54 | else: | |
55 | # provide tty to get 'interactive' readline to work | |
56 | master, slave = pty.openpty() | |
612bfe4a JM |
57 | self.p = Popen(args, bufsize=0, |
58 | stdin=slave, stdout=slave, stderr=STDOUT, | |
59 | preexec_fn=os.setsid) | |
7907cd90 JM |
60 | self.stdin = os.fdopen(master, 'r+b', 0) |
61 | self.stdout = self.stdin | |
62 | ||
63 | #print "started" | |
64 | self.buf = "" | |
65 | self.last_prompt = "" | |
66 | ||
67 | def read_to_prompt(self, prompts, timeout): | |
68 | end_time = time.time() + timeout | |
69 | while time.time() < end_time: | |
96deb6a9 JM |
70 | [outs,_,_] = select([self.stdout], [], [], 1) |
71 | if self.stdout in outs: | |
72 | new_data = self.stdout.read(1) | |
7907cd90 | 73 | #print "new_data: '%s'" % new_data |
8e4628da | 74 | new_data = new_data.decode("utf-8") if IS_PY_3 else new_data |
ab01be18 | 75 | if self.no_pty: |
9a383535 JM |
76 | self.buf += new_data.replace("\n", "\r\n") |
77 | else: | |
78 | self.buf += new_data | |
7907cd90 JM |
79 | for prompt in prompts: |
80 | regexp = re.compile(prompt) | |
81 | match = regexp.search(self.buf) | |
82 | if match: | |
83 | end = match.end() | |
84 | buf = self.buf[0:end-len(prompt)] | |
85 | self.buf = self.buf[end:] | |
86 | self.last_prompt = prompt | |
87 | return buf | |
88 | return None | |
89 | ||
96deb6a9 | 90 | def writeline(self, str): |
8e4628da | 91 | def _to_bytes(s): |
92 | return bytes(s, "utf-8") if IS_PY_3 else s | |
93 | ||
94 | self.stdin.write(_to_bytes(str + "\n")) | |
9a383535 | 95 | if self.mono: |
96deb6a9 | 96 | # Simulate echo |
8e4628da | 97 | self.buf += _to_bytes(str + "\r\n") |
7907cd90 | 98 | |
f6c83b2b | 99 | def cleanup(self): |
612bfe4a | 100 | #print "cleaning up" |
f6c83b2b | 101 | if self.p: |
612bfe4a | 102 | os.killpg(self.p.pid, signal.SIGTERM) |
f6c83b2b JM |
103 | self.p = None |
104 | ||
105 | ||
31690700 JM |
106 | args = parser.parse_args(sys.argv[1:]) |
107 | test_data = args.test_file.read().split('\n') | |
108 | ||
109 | if args.rundir: os.chdir(args.rundir) | |
110 | ||
ab01be18 | 111 | r = Runner(args.mal_cmd, no_pty=args.no_pty, mono=args.mono) |
53beaa0a | 112 | |
31690700 JM |
113 | |
114 | test_idx = 0 | |
115 | def read_test(data): | |
116 | global test_idx | |
117 | form, output, ret = None, "", None | |
118 | while data: | |
119 | test_idx += 1 | |
120 | line = data.pop(0) | |
121 | if re.match(r"^\s*$", line): # blank line | |
122 | continue | |
123 | elif line[0:3] == ";;;": # ignore comment | |
124 | continue | |
125 | elif line[0:2] == ";;": # output comment | |
8e4628da | 126 | print(line[3:]) |
31690700 JM |
127 | continue |
128 | elif line[0:2] == ";": # unexpected comment | |
8e4628da | 129 | print("Test data error at line %d:\n%s" % (test_idx, line)) |
31690700 JM |
130 | return None, None, None, test_idx |
131 | form = line # the line is a form to send | |
132 | ||
133 | # Now find the output and return value | |
134 | while data: | |
135 | line = data[0] | |
136 | if line[0:3] == ";=>": | |
137 | ret = line[3:].replace('\\r', '\r').replace('\\n', '\n') | |
138 | test_idx += 1 | |
139 | data.pop(0) | |
140 | break | |
141 | elif line[0:2] == "; ": | |
142 | output = output + line[2:] + sep | |
143 | test_idx += 1 | |
144 | data.pop(0) | |
145 | else: | |
146 | ret = "*" | |
147 | break | |
148 | if ret: break | |
149 | ||
150 | return form, output, ret, test_idx | |
151 | ||
cc021efe JM |
152 | def assert_prompt(timeout): |
153 | # Wait for the initial prompt | |
7907cd90 JM |
154 | header = r.read_to_prompt(['user> ', 'mal-user> '], timeout=timeout) |
155 | if not header == None: | |
156 | if header: | |
8e4628da | 157 | print("Started with:\n%s" % header) |
7907cd90 | 158 | else: |
8e4628da | 159 | print("Did not get 'user> ' or 'mal-user> ' prompt") |
160 | print(" Got : %s" % repr(r.buf)) | |
cc021efe JM |
161 | sys.exit(1) |
162 | ||
31690700 JM |
163 | |
164 | # Wait for the initial prompt | |
cc021efe JM |
165 | assert_prompt(args.start_timeout) |
166 | ||
167 | # Send the pre-eval code if any | |
168 | if args.pre_eval: | |
169 | sys.stdout.write("RUNNING pre-eval: %s" % args.pre_eval) | |
7907cd90 | 170 | p.write(args.pre_eval) |
cc021efe | 171 | assert_prompt(args.test_timeout) |
31690700 JM |
172 | |
173 | fail_cnt = 0 | |
174 | ||
175 | while test_data: | |
176 | form, out, ret, line_num = read_test(test_data) | |
177 | if form == None: | |
178 | break | |
179 | sys.stdout.write("TEST: %s -> [%s,%s]" % (form, repr(out), repr(ret))) | |
180 | sys.stdout.flush() | |
181 | expected = "%s%s%s%s" % (form, sep, out, ret) | |
182 | ||
96deb6a9 | 183 | r.writeline(form) |
31690700 | 184 | try: |
7907cd90 JM |
185 | res = r.read_to_prompt(['\r\nuser> ', '\nuser> ', |
186 | '\r\nmal-user> ', '\nmal-user> '], | |
187 | timeout=args.test_timeout) | |
31690700 | 188 | #print "%s,%s,%s" % (idx, repr(p.before), repr(p.after)) |
7907cd90 | 189 | if ret == "*" or res == expected: |
8e4628da | 190 | print(" -> SUCCESS") |
31690700 | 191 | else: |
8e4628da | 192 | print(" -> FAIL (line %d):" % line_num) |
193 | print(" Expected : %s" % repr(expected)) | |
194 | print(" Got : %s" % repr(res)) | |
31690700 | 195 | fail_cnt += 1 |
7907cd90 | 196 | except: |
8e4628da | 197 | print("Got Exception") |
31690700 JM |
198 | sys.exit(1) |
199 | ||
200 | if fail_cnt > 0: | |
8e4628da | 201 | print("FAILURES: %d" % fail_cnt) |
31690700 JM |
202 | sys.exit(2) |
203 | sys.exit(0) |