55
57
Input lines start with '<'.
56
58
Output lines start with nothing.
57
59
Error lines start with '2>'.
61
:return: A sequence of ([args], input, output, errors), where the args are
62
split in to words, and the input, output, and errors are just strings,
63
typically containing newlines.
75
81
input, output, error = None, None, None
76
for line in text.split('\n'):
82
text = textwrap.dedent(text)
83
lines = text.split('\n')
84
# to make use of triple-quoted strings easier, we ignore a blank line
85
# right at the start and right at the end; the rest are meaningful
86
if lines and lines[0] == '':
88
if lines and lines[-1] == '':
78
92
# Keep a copy for error reporting
80
94
comment = line.find('#')
97
# NB: this syntax means comments are allowed inside output, which
83
99
line = line[0:comment]
84
100
line = line.rstrip()
88
103
if line.startswith('$'):
89
104
# Time to output the current command
90
105
add_command(cmd_cur, input, output, error)
181
196
self.output_checker = doctest.OutputChecker()
182
197
self.check_options = doctest.ELLIPSIS
184
def run_script(self, test_case, text):
199
def run_script(self, test_case, text, null_output_matches_anything=False):
185
200
"""Run a shell-like script as a test.
187
202
:param test_case: A TestCase instance that should provide the fail(),
189
204
attribute used as a jail root.
191
206
:param text: A shell-like script (see _script_to_commands for syntax).
208
:param null_output_matches_anything: For commands with no specified
209
output, ignore any output that does happen, including output on
212
self.null_output_matches_anything = null_output_matches_anything
193
213
for cmd, input, output, error in _script_to_commands(text):
194
214
self.run_command(test_case, cmd, input, output, error)
198
218
method = getattr(self, mname, None)
199
219
if method is None:
200
220
raise SyntaxError('Command not found "%s"' % (cmd[0],),
201
None, 1, ' '.join(cmd))
221
(None, 1, 1, ' '.join(cmd)))
202
222
if input is None:
207
227
retcode, actual_output, actual_error = method(test_case,
210
self._check_output(output, actual_output, test_case)
211
self._check_output(error, actual_error, test_case)
231
self._check_output(output, actual_output, test_case)
232
except AssertionError, e:
233
raise AssertionError(str(e) + " in stdout of command %s" % cmd)
235
self._check_output(error, actual_error, test_case)
236
except AssertionError, e:
237
raise AssertionError(str(e) +
238
" in stderr of running command %s" % cmd)
212
239
if retcode and not error and actual_error:
213
240
test_case.fail('In \n\t%s\nUnexpected error: %s'
214
241
% (' '.join(cmd), actual_error))
215
242
return retcode, actual_output, actual_error
217
244
def _check_output(self, expected, actual, test_case):
219
# Specifying None means: any output is accepted
248
elif expected == '...\n':
251
test_case.fail('expected output: %r, but found nothing'
254
null_output_matches_anything = getattr(
255
self, 'null_output_matches_anything', False)
256
if null_output_matches_anything and expected is None:
222
test_case.fail('We expected output: %r, but found None'
259
expected = expected or ''
224
260
matching = self.output_checker.check_output(
225
261
expected, actual, self.check_options)
230
266
# 'expected' parameter. So we just fallback to our good old
231
267
# assertEqualDiff since we know there *are* differences and the
232
268
# output should be decently readable.
233
test_case.assertEqualDiff(expected, actual)
270
# As a special case, we allow output that's missing a final
271
# newline to match an expected string that does have one, so that
272
# we can match a prompt printed on one line, then input given on
274
if expected == actual + '\n':
277
test_case.assertEqualDiff(expected, actual)
235
279
def _pre_process_args(self, args):
440
484
super(TestCaseWithMemoryTransportAndScript, self).setUp()
441
485
self.script_runner = ScriptRunner()
443
def run_script(self, script):
444
return self.script_runner.run_script(self, script)
487
def run_script(self, script, null_output_matches_anything=False):
488
return self.script_runner.run_script(self, script,
489
null_output_matches_anything=null_output_matches_anything)
446
491
def run_command(self, cmd, input, output, error):
447
492
return self.script_runner.run_command(self, cmd, input, output, error)
469
514
super(TestCaseWithTransportAndScript, self).setUp()
470
515
self.script_runner = ScriptRunner()
472
def run_script(self, script):
473
return self.script_runner.run_script(self, script)
517
def run_script(self, script, null_output_matches_anything=False):
518
return self.script_runner.run_script(self, script,
519
null_output_matches_anything=null_output_matches_anything)
475
521
def run_command(self, cmd, input, output, error):
476
522
return self.script_runner.run_command(self, cmd, input, output, error)
525
def run_script(test_case, script_string, null_output_matches_anything=False):
526
"""Run the given script within a testcase"""
527
return ScriptRunner().run_script(test_case, script_string,
528
null_output_matches_anything=null_output_matches_anything)