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