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
56
55
Input lines start with '<'.
57
56
Output lines start with nothing.
58
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.
76
79
input, output, error = None, None, None
77
80
text = textwrap.dedent(text)
78
for line in text.split('\n'):
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] == '':
80
90
# Keep a copy for error reporting
82
92
comment = line.find('#')
95
# NB: this syntax means comments are allowed inside output, which
85
97
line = line[0:comment]
86
98
line = line.rstrip()
90
101
if line.startswith('$'):
91
102
# Time to output the current command
92
103
add_command(cmd_cur, input, output, error)
183
194
self.output_checker = doctest.OutputChecker()
184
195
self.check_options = doctest.ELLIPSIS
186
def run_script(self, test_case, text):
197
def run_script(self, test_case, text, null_output_matches_anything=False):
187
198
"""Run a shell-like script as a test.
189
200
:param test_case: A TestCase instance that should provide the fail(),
191
202
attribute used as a jail root.
193
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
195
211
for cmd, input, output, error in _script_to_commands(text):
196
212
self.run_command(test_case, cmd, input, output, error)
200
216
method = getattr(self, mname, None)
201
217
if method is None:
202
218
raise SyntaxError('Command not found "%s"' % (cmd[0],),
203
None, 1, ' '.join(cmd))
219
(None, 1, 1, ' '.join(cmd)))
204
220
if input is None:
209
225
retcode, actual_output, actual_error = method(test_case,
212
self._check_output(output, actual_output, test_case)
213
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)
214
237
if retcode and not error and actual_error:
215
238
test_case.fail('In \n\t%s\nUnexpected error: %s'
216
239
% (' '.join(cmd), actual_error))
217
240
return retcode, actual_output, actual_error
219
242
def _check_output(self, expected, actual, test_case):
221
# 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:
224
test_case.fail('We expected output: %r, but found None'
257
expected = expected or ''
226
258
matching = self.output_checker.check_output(
227
259
expected, actual, self.check_options)
232
264
# 'expected' parameter. So we just fallback to our good old
233
265
# assertEqualDiff since we know there *are* differences and the
234
266
# output should be decently readable.
235
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)
237
277
def _pre_process_args(self, args):
442
482
super(TestCaseWithMemoryTransportAndScript, self).setUp()
443
483
self.script_runner = ScriptRunner()
445
def run_script(self, script):
446
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)
448
489
def run_command(self, cmd, input, output, error):
449
490
return self.script_runner.run_command(self, cmd, input, output, error)
471
512
super(TestCaseWithTransportAndScript, self).setUp()
472
513
self.script_runner = ScriptRunner()
474
def run_script(self, script):
475
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)
477
519
def run_command(self, cmd, input, output, error):
478
520
return self.script_runner.run_command(self, cmd, input, output, error)
481
def run_script(test_case, script_string):
523
def run_script(test_case, script_string, null_output_matches_anything=False):
482
524
"""Run the given script within a testcase"""
483
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)