1
# Copyright (C) 2009, 2010 Canonical Ltd
1
# Copyright (C) 2009, 2010, 2011 Canonical Ltd
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
195
194
self.output_checker = doctest.OutputChecker()
196
195
self.check_options = doctest.ELLIPSIS
198
def run_script(self, test_case, text):
197
def run_script(self, test_case, text, null_output_matches_anything=False):
199
198
"""Run a shell-like script as a test.
201
200
:param test_case: A TestCase instance that should provide the fail(),
203
202
attribute used as a jail root.
205
204
:param text: A shell-like script (see _script_to_commands for syntax).
206
:param null_output_matches_anything: For commands with no specified
207
output, ignore any output that does happen, including output on
210
self.null_output_matches_anything = null_output_matches_anything
207
211
for cmd, input, output, error in _script_to_commands(text):
208
212
self.run_command(test_case, cmd, input, output, error)
212
216
method = getattr(self, mname, None)
213
217
if method is None:
214
218
raise SyntaxError('Command not found "%s"' % (cmd[0],),
215
None, 1, ' '.join(cmd))
219
(None, 1, 1, ' '.join(cmd)))
216
220
if input is None:
221
225
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)
229
self._check_output(output, actual_output, test_case)
230
except AssertionError, e:
231
raise AssertionError(str(e) + " in stdout of command %s" % cmd)
233
self._check_output(error, actual_error, test_case)
234
except AssertionError, e:
235
raise AssertionError(str(e) +
236
" in stderr of running command %s" % cmd)
226
237
if retcode and not error and actual_error:
227
238
test_case.fail('In \n\t%s\nUnexpected error: %s'
228
239
% (' '.join(cmd), actual_error))
229
240
return retcode, actual_output, actual_error
231
242
def _check_output(self, expected, actual, test_case):
233
# Specifying None means: any output is accepted
246
elif expected == '...\n':
249
test_case.fail('expected output: %r, but found nothing'
252
null_output_matches_anything = getattr(
253
self, 'null_output_matches_anything', False)
254
if null_output_matches_anything and expected is None:
236
test_case.fail('We expected output: %r, but found None'
257
expected = expected or ''
238
258
matching = self.output_checker.check_output(
239
259
expected, actual, self.check_options)
462
482
super(TestCaseWithMemoryTransportAndScript, self).setUp()
463
483
self.script_runner = ScriptRunner()
465
def run_script(self, script):
466
return self.script_runner.run_script(self, script)
485
def run_script(self, script, null_output_matches_anything=False):
486
return self.script_runner.run_script(self, script,
487
null_output_matches_anything=null_output_matches_anything)
468
489
def run_command(self, cmd, input, output, error):
469
490
return self.script_runner.run_command(self, cmd, input, output, error)
491
512
super(TestCaseWithTransportAndScript, self).setUp()
492
513
self.script_runner = ScriptRunner()
494
def run_script(self, script):
495
return self.script_runner.run_script(self, script)
515
def run_script(self, script, null_output_matches_anything=False):
516
return self.script_runner.run_script(self, script,
517
null_output_matches_anything=null_output_matches_anything)
497
519
def run_command(self, cmd, input, output, error):
498
520
return self.script_runner.run_command(self, cmd, input, output, error)
501
def run_script(test_case, script_string):
523
def run_script(test_case, script_string, null_output_matches_anything=False):
502
524
"""Run the given script within a testcase"""
503
return ScriptRunner().run_script(test_case, script_string)
525
return ScriptRunner().run_script(test_case, script_string,
526
null_output_matches_anything=null_output_matches_anything)