7 from subprocess
import Popen
, STDOUT
, PIPE
8 from select
import select
10 # Pseudo-TTY and terminal manipulation
11 import pty
, array
, fcntl
, termios
13 IS_PY_3
= sys
.version_info
[0] == 3
15 # TODO: do we need to support '\n' too
20 parser
= argparse
.ArgumentParser(
21 description
="Run a test file against a Mal implementation")
22 parser
.add_argument('--rundir',
23 help="change to the directory before running tests")
24 parser
.add_argument('--start-timeout', default
=10, type=int,
25 help="default timeout for initial prompt")
26 parser
.add_argument('--test-timeout', default
=20, type=int,
27 help="default timeout for each individual test action")
28 parser
.add_argument('--pre-eval', default
=None, type=str,
29 help="Mal code to evaluate prior to running the test")
30 parser
.add_argument('--no-pty', action
='store_true',
31 help="Use direct pipes instead of pseudo-tty")
33 parser
.add_argument('test_file', type=argparse
.FileType('r'),
34 help="a test file formatted as with mal test data")
35 parser
.add_argument('mal_cmd', nargs
="*",
36 help="Mal implementation command line. Use '--' to "
37 "specify a Mal command line with dashed options.")
40 def __init__(self
, args
, no_pty
=False):
41 #print "args: %s" % repr(args)
44 # Cleanup child process on exit
45 atexit
.register(self
.cleanup
)
50 env
['INPUTRC'] = '/dev/null'
51 env
['PERL_RL'] = 'false'
53 self
.p
= Popen(args
, bufsize
=0,
54 stdin
=PIPE
, stdout
=PIPE
, stderr
=STDOUT
,
57 self
.stdin
= self
.p
.stdin
58 self
.stdout
= self
.p
.stdout
60 # provide tty to get 'interactive' readline to work
61 master
, slave
= pty
.openpty()
63 # Set terminal size large so that readline will not send
64 # ANSI/VT escape codes when the lines are long.
65 buf
= array
.array('h', [100, 200, 0, 0])
66 fcntl
.ioctl(master
, termios
.TIOCSWINSZ
, buf
, True)
68 self
.p
= Popen(args
, bufsize
=0,
69 stdin
=slave
, stdout
=slave
, stderr
=STDOUT
,
72 # Now close slave so that we will get an exception from
73 # read when the child exits early
74 # http://stackoverflow.com/questions/11165521
76 self
.stdin
= os
.fdopen(master
, 'r+b', 0)
77 self
.stdout
= self
.stdin
83 def read_to_prompt(self
, prompts
, timeout
):
84 end_time
= time
.time() + timeout
85 while time
.time() < end_time
:
86 [outs
,_
,_
] = select([self
.stdout
], [], [], 1)
87 if self
.stdout
in outs
:
88 new_data
= self
.stdout
.read(1)
89 new_data
= new_data
.decode("utf-8") if IS_PY_3
else new_data
90 #print "new_data: '%s'" % new_data
92 self
.buf
+= new_data
.replace("\n", "\r\n")
95 for prompt
in prompts
:
96 regexp
= re
.compile(prompt
)
97 match
= regexp
.search(self
.buf
)
100 buf
= self
.buf
[0:end
-len(prompt
)]
101 self
.buf
= self
.buf
[end
:]
102 self
.last_prompt
= prompt
106 def writeline(self
, str):
108 return bytes(s
, "utf-8") if IS_PY_3
else s
110 self
.stdin
.write(_to_bytes(str + "\n"))
116 os
.killpg(self
.p
.pid
, signal
.SIGTERM
)
122 args
= parser
.parse_args(sys
.argv
[1:])
123 test_data
= args
.test_file
.read().split('\n')
125 if args
.rundir
: os
.chdir(args
.rundir
)
127 r
= Runner(args
.mal_cmd
, no_pty
=args
.no_pty
)
133 form
, output
, ret
= None, "", None
137 if re
.match(r
"^\s*$", line
): # blank line
139 elif line
[0:3] == ";;;": # ignore comment
141 elif line
[0:2] == ";;": # output comment
144 elif line
[0:2] == ";": # unexpected comment
145 print("Test data error at line %d:\n%s" % (test_idx
, line
))
146 return None, None, None, test_idx
147 form
= line
# the line is a form to send
149 # Now find the output and return value
152 if line
[0:3] == ";=>":
153 ret
= line
[3:].replace('\\r', '\r').replace('\\n', '\n')
157 elif line
[0:2] == "; ":
158 output
= output
+ line
[2:] + sep
166 return form
, output
, ret
, test_idx
168 def assert_prompt(timeout
):
169 # Wait for the initial prompt
170 header
= r
.read_to_prompt(['user> ', 'mal-user> '], timeout
=timeout
)
171 if not header
== None:
173 print("Started with:\n%s" % header
)
175 print("Did not get 'user> ' or 'mal-user> ' prompt")
176 print(" Got : %s" % repr(r
.buf
))
180 # Wait for the initial prompt
181 assert_prompt(args
.start_timeout
)
183 # Send the pre-eval code if any
185 sys
.stdout
.write("RUNNING pre-eval: %s" % args
.pre_eval
)
186 p
.write(args
.pre_eval
)
187 assert_prompt(args
.test_timeout
)
192 form
, out
, ret
, line_num
= read_test(test_data
)
195 sys
.stdout
.write("TEST: %s -> [%s,%s]" % (form
, repr(out
), repr(ret
)))
198 # The repeated form is to get around an occasional OS X issue
199 # where the form is repeated.
200 # https://github.com/kanaka/mal/issues/30
201 expected
= ["%s%s%s%s" % (form
, sep
, out
, ret
),
202 "%s%s%s%s%s%s" % (form
, sep
, form
, sep
, out
, ret
)]
206 res
= r
.read_to_prompt(['\r\nuser> ', '\nuser> ',
207 '\r\nmal-user> ', '\nmal-user> '],
208 timeout
=args
.test_timeout
)
209 #print "%s,%s,%s" % (idx, repr(p.before), repr(p.after))
210 if ret
== "*" or res
in expected
:
213 print(" -> FAIL (line %d):" % line_num
)
214 print(" Expected : %s" % repr(expected
))
215 print(" Got : %s" % repr(res
))
218 _
, exc
, _
= sys
.exc_info()
219 print("\nException: %s" % repr(exc
))
220 print("Output before exception:\n%s" % r
.buf
)
224 print("FAILURES: %d" % fail_cnt
)