195
196
self.output_checker = doctest.OutputChecker()
196
197
self.check_options = doctest.ELLIPSIS
198
def run_script(self, test_case, text):
199
def run_script(self, test_case, text, null_output_matches_anything=False):
199
200
"""Run a shell-like script as a test.
201
202
:param test_case: A TestCase instance that should provide the fail(),
203
204
attribute used as a jail root.
205
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
207
213
for cmd, input, output, error in _script_to_commands(text):
208
214
self.run_command(test_case, cmd, input, output, error)
212
218
method = getattr(self, mname, None)
213
219
if method is None:
214
220
raise SyntaxError('Command not found "%s"' % (cmd[0],),
215
None, 1, ' '.join(cmd))
221
(None, 1, 1, ' '.join(cmd)))
216
222
if input is None:
221
227
retcode, actual_output, actual_error = method(test_case,
224
self._check_output(output, actual_output, test_case)
225
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)
226
239
if retcode and not error and actual_error:
227
240
test_case.fail('In \n\t%s\nUnexpected error: %s'
228
241
% (' '.join(cmd), actual_error))
229
242
return retcode, actual_output, actual_error
231
244
def _check_output(self, expected, actual, test_case):
233
# 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:
236
test_case.fail('We expected output: %r, but found None'
259
expected = expected or ''
238
260
matching = self.output_checker.check_output(
239
261
expected, actual, self.check_options)
462
484
super(TestCaseWithMemoryTransportAndScript, self).setUp()
463
485
self.script_runner = ScriptRunner()
465
def run_script(self, script):
466
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)
468
491
def run_command(self, cmd, input, output, error):
469
492
return self.script_runner.run_command(self, cmd, input, output, error)
491
514
super(TestCaseWithTransportAndScript, self).setUp()
492
515
self.script_runner = ScriptRunner()
494
def run_script(self, script):
495
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)
497
521
def run_command(self, cmd, input, output, error):
498
522
return self.script_runner.run_command(self, cmd, input, output, error)
501
def run_script(test_case, script_string):
525
def run_script(test_case, script_string, null_output_matches_anything=False):
502
526
"""Run the given script within a testcase"""
503
return ScriptRunner().run_script(test_case, script_string)
527
return ScriptRunner().run_script(test_case, script_string,
528
null_output_matches_anything=null_output_matches_anything)