1
# Copyright (C) 2009 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
55
55
Input lines start with '<'.
56
56
Output lines start with nothing.
57
57
Error lines start with '2>'.
59
:return: A sequence of ([args], input, output, errors), where the args are
60
split in to words, and the input, output, and errors are just strings,
61
typically containing newlines.
75
79
input, output, error = None, None, None
76
for line in text.split('\n'):
80
text = textwrap.dedent(text)
81
lines = text.split('\n')
82
# to make use of triple-quoted strings easier, we ignore a blank line
83
# right at the start and right at the end; the rest are meaningful
84
if lines and lines[0] == '':
86
if lines and lines[-1] == '':
78
90
# Keep a copy for error reporting
80
92
comment = line.find('#')
95
# NB: this syntax means comments are allowed inside output, which
83
97
line = line[0:comment]
84
98
line = line.rstrip()
88
101
if line.startswith('$'):
89
102
# Time to output the current command
90
103
add_command(cmd_cur, input, output, error)
181
194
self.output_checker = doctest.OutputChecker()
182
195
self.check_options = doctest.ELLIPSIS
184
def run_script(self, test_case, text):
197
def run_script(self, test_case, text, null_output_matches_anything=False):
185
198
"""Run a shell-like script as a test.
187
200
:param test_case: A TestCase instance that should provide the fail(),
189
202
attribute used as a jail root.
191
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
193
211
for cmd, input, output, error in _script_to_commands(text):
194
212
self.run_command(test_case, cmd, input, output, error)
198
216
method = getattr(self, mname, None)
199
217
if method is None:
200
218
raise SyntaxError('Command not found "%s"' % (cmd[0],),
201
None, 1, ' '.join(cmd))
219
(None, 1, 1, ' '.join(cmd)))
202
220
if input is None:
207
225
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)
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)
212
237
if retcode and not error and actual_error:
213
238
test_case.fail('In \n\t%s\nUnexpected error: %s'
214
239
% (' '.join(cmd), actual_error))
215
240
return retcode, actual_output, actual_error
217
242
def _check_output(self, expected, actual, test_case):
219
# 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:
222
test_case.fail('We expected output: %r, but found None'
257
expected = expected or ''
224
258
matching = self.output_checker.check_output(
225
259
expected, actual, self.check_options)
230
264
# 'expected' parameter. So we just fallback to our good old
231
265
# assertEqualDiff since we know there *are* differences and the
232
266
# output should be decently readable.
233
test_case.assertEqualDiff(expected, actual)
268
# As a special case, we allow output that's missing a final
269
# newline to match an expected string that does have one, so that
270
# we can match a prompt printed on one line, then input given on
272
if expected == actual + '\n':
275
test_case.assertEqualDiff(expected, actual)
235
277
def _pre_process_args(self, args):
440
482
super(TestCaseWithMemoryTransportAndScript, self).setUp()
441
483
self.script_runner = ScriptRunner()
443
def run_script(self, script):
444
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)
446
489
def run_command(self, cmd, input, output, error):
447
490
return self.script_runner.run_command(self, cmd, input, output, error)
469
512
super(TestCaseWithTransportAndScript, self).setUp()
470
513
self.script_runner = ScriptRunner()
472
def run_script(self, script):
473
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)
475
519
def run_command(self, cmd, input, output, error):
476
520
return self.script_runner.run_command(self, cmd, input, output, error)
523
def run_script(test_case, script_string, null_output_matches_anything=False):
524
"""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)