6 import pty
, signal
, atexit
7 from subprocess
import Popen
, STDOUT
, PIPE
8 from select
import select
10 IS_PY_3
= sys
.version_info
[0] == 3
12 # TODO: do we need to support '\n' too
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")
25 parser
.add_argument('--pre-eval', default
=None, type=str,
26 help="Mal code to evaluate prior to running the test")
27 parser
.add_argument('--mono', action
='store_true',
28 help="Use workarounds Mono/.Net Console misbehaviors")
30 parser
.add_argument('test_file', type=argparse
.FileType('r'),
31 help="a test file formatted as with mal test data")
32 parser
.add_argument('mal_cmd', nargs
="*",
33 help="Mal implementation command line. Use '--' to "
34 "specify a Mal command line with dashed options.")
37 def __init__(self
, args
, mono
=False):
38 #print "args: %s" % repr(args)
41 # Cleanup child process on exit
42 atexit
.register(self
.cleanup
)
45 self
.p
= Popen(args
, bufsize
=0,
46 stdin
=PIPE
, stdout
=PIPE
, stderr
=STDOUT
,
48 self
.stdin
= self
.p
.stdin
49 self
.stdout
= self
.p
.stdout
51 # provide tty to get 'interactive' readline to work
52 master
, slave
= pty
.openpty()
53 self
.p
= Popen(args
, bufsize
=0,
54 stdin
=slave
, stdout
=slave
, stderr
=STDOUT
,
56 self
.stdin
= os
.fdopen(master
, 'r+b', 0)
57 self
.stdout
= self
.stdin
63 def read_to_prompt(self
, prompts
, timeout
):
64 end_time
= time
.time() + timeout
65 while time
.time() < end_time
:
66 [outs
,_
,_
] = select([self
.stdout
], [], [], 1)
67 if self
.stdout
in outs
:
68 new_data
= self
.stdout
.read(1)
69 #print "new_data: '%s'" % new_data
70 new_data
= new_data
.decode("utf-8") if IS_PY_3
else new_data
72 self
.buf
+= new_data
.replace("\n", "\r\n")
75 for prompt
in prompts
:
76 regexp
= re
.compile(prompt
)
77 match
= regexp
.search(self
.buf
)
80 buf
= self
.buf
[0:end
-len(prompt
)]
81 self
.buf
= self
.buf
[end
:]
82 self
.last_prompt
= prompt
86 def writeline(self
, str):
88 return bytes(s
, "utf-8") if IS_PY_3
else s
90 self
.stdin
.write(_to_bytes(str + "\n"))
93 self
.buf
+= _to_bytes(str + "\r\n")
98 os
.killpg(self
.p
.pid
, signal
.SIGTERM
)
102 args
= parser
.parse_args(sys
.argv
[1:])
103 test_data
= args
.test_file
.read().split('\n')
105 if args
.rundir
: os
.chdir(args
.rundir
)
107 r
= Runner(args
.mal_cmd
, mono
=args
.mono
)
113 form
, output
, ret
= None, "", None
117 if re
.match(r
"^\s*$", line
): # blank line
119 elif line
[0:3] == ";;;": # ignore comment
121 elif line
[0:2] == ";;": # output comment
124 elif line
[0:2] == ";": # unexpected comment
125 print("Test data error at line %d:\n%s" % (test_idx
, line
))
126 return None, None, None, test_idx
127 form
= line
# the line is a form to send
129 # Now find the output and return value
132 if line
[0:3] == ";=>":
133 ret
= line
[3:].replace('\\r', '\r').replace('\\n', '\n')
137 elif line
[0:2] == "; ":
138 output
= output
+ line
[2:] + sep
146 return form
, output
, ret
, test_idx
148 def assert_prompt(timeout
):
149 # Wait for the initial prompt
150 header
= r
.read_to_prompt(['user> ', 'mal-user> '], timeout
=timeout
)
151 if not header
== None:
153 print("Started with:\n%s" % header
)
155 print("Did not get 'user> ' or 'mal-user> ' prompt")
156 print(" Got : %s" % repr(r
.buf
))
160 # Wait for the initial prompt
161 assert_prompt(args
.start_timeout
)
163 # Send the pre-eval code if any
165 sys
.stdout
.write("RUNNING pre-eval: %s" % args
.pre_eval
)
166 p
.write(args
.pre_eval
)
167 assert_prompt(args
.test_timeout
)
172 form
, out
, ret
, line_num
= read_test(test_data
)
175 sys
.stdout
.write("TEST: %s -> [%s,%s]" % (form
, repr(out
), repr(ret
)))
177 expected
= "%s%s%s%s" % (form
, sep
, out
, ret
)
181 res
= r
.read_to_prompt(['\r\nuser> ', '\nuser> ',
182 '\r\nmal-user> ', '\nmal-user> '],
183 timeout
=args
.test_timeout
)
184 #print "%s,%s,%s" % (idx, repr(p.before), repr(p.after))
185 if ret
== "*" or res
== expected
:
188 print(" -> FAIL (line %d):" % line_num
)
189 print(" Expected : %s" % repr(expected
))
190 print(" Got : %s" % repr(res
))
193 print("Got Exception")
197 print("FAILURES: %d" % fail_cnt
)