1
# Copyright (C) 2009, 2010, 2011 Canonical Ltd
1
# Copyright (C) 2009, 2010 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
194
195
self.output_checker = doctest.OutputChecker()
195
196
self.check_options = doctest.ELLIPSIS
197
def run_script(self, test_case, text, null_output_matches_anything=False):
198
def run_script(self, test_case, text):
198
199
"""Run a shell-like script as a test.
200
201
:param test_case: A TestCase instance that should provide the fail(),
202
203
attribute used as a jail root.
204
205
: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
211
207
for cmd, input, output, error in _script_to_commands(text):
212
208
self.run_command(test_case, cmd, input, output, error)
216
212
method = getattr(self, mname, None)
217
213
if method is None:
218
214
raise SyntaxError('Command not found "%s"' % (cmd[0],),
219
(None, 1, 1, ' '.join(cmd)))
215
None, 1, ' '.join(cmd))
220
216
if input is None:
225
221
retcode, actual_output, actual_error = method(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)
224
self._check_output(output, actual_output, test_case)
225
self._check_output(error, actual_error, test_case)
237
226
if retcode and not error and actual_error:
238
227
test_case.fail('In \n\t%s\nUnexpected error: %s'
239
228
% (' '.join(cmd), actual_error))
240
229
return retcode, actual_output, actual_error
242
231
def _check_output(self, expected, actual, test_case):
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:
233
# Specifying None means: any output is accepted
257
expected = expected or ''
236
test_case.fail('We expected output: %r, but found None'
258
238
matching = self.output_checker.check_output(
259
239
expected, actual, self.check_options)
482
462
super(TestCaseWithMemoryTransportAndScript, self).setUp()
483
463
self.script_runner = ScriptRunner()
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)
465
def run_script(self, script):
466
return self.script_runner.run_script(self, script)
489
468
def run_command(self, cmd, input, output, error):
490
469
return self.script_runner.run_command(self, cmd, input, output, error)
512
491
super(TestCaseWithTransportAndScript, self).setUp()
513
492
self.script_runner = ScriptRunner()
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)
494
def run_script(self, script):
495
return self.script_runner.run_script(self, script)
519
497
def run_command(self, cmd, input, output, error):
520
498
return self.script_runner.run_command(self, cmd, input, output, error)
523
def run_script(test_case, script_string, null_output_matches_anything=False):
501
def run_script(test_case, script_string):
524
502
"""Run the given script within a testcase"""
525
return ScriptRunner().run_script(test_case, script_string,
526
null_output_matches_anything=null_output_matches_anything)
503
return ScriptRunner().run_script(test_case, script_string)