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()
484
# FIXME: See shelf_ui.Shelver._char_based. This allow using shelve in
485
# scripts while providing a line-based input (better solution in
486
# progress). -- vila 2011-09-28
487
self.overrideEnv('INSIDE_EMACS', '1')
465
def run_script(self, script):
466
return self.script_runner.run_script(self, script)
489
def run_script(self, script, null_output_matches_anything=False):
490
return self.script_runner.run_script(self, script,
491
null_output_matches_anything=null_output_matches_anything)
468
493
def run_command(self, cmd, input, output, error):
469
494
return self.script_runner.run_command(self, cmd, input, output, error)
491
516
super(TestCaseWithTransportAndScript, self).setUp()
492
517
self.script_runner = ScriptRunner()
518
# FIXME: See shelf_ui.Shelver._char_based. This allow using shelve in
519
# scripts while providing a line-based input (better solution in
520
# progress). -- vila 2011-09-28
521
self.overrideEnv('INSIDE_EMACS', '1')
494
def run_script(self, script):
495
return self.script_runner.run_script(self, script)
523
def run_script(self, script, null_output_matches_anything=False):
524
return self.script_runner.run_script(self, script,
525
null_output_matches_anything=null_output_matches_anything)
497
527
def run_command(self, cmd, input, output, error):
498
528
return self.script_runner.run_command(self, cmd, input, output, error)
501
def run_script(test_case, script_string):
531
def run_script(test_case, script_string, null_output_matches_anything=False):
502
532
"""Run the given script within a testcase"""
503
return ScriptRunner().run_script(test_case, script_string)
533
return ScriptRunner().run_script(test_case, script_string,
534
null_output_matches_anything=null_output_matches_anything)