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
197
self.output_checker = doctest.OutputChecker()
195
198
self.check_options = doctest.ELLIPSIS
197
def run_script(self, test_case, text, null_output_matches_anything=False):
200
def run_script(self, test_case, text):
198
201
"""Run a shell-like script as a test.
200
203
:param test_case: A TestCase instance that should provide the fail(),
202
205
attribute used as a jail root.
204
207
: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
209
for cmd, input, output, error in _script_to_commands(text):
212
210
self.run_command(test_case, cmd, input, output, error)
216
214
method = getattr(self, mname, None)
217
215
if method is None:
218
216
raise SyntaxError('Command not found "%s"' % (cmd[0],),
219
(None, 1, 1, ' '.join(cmd)))
217
None, 1, ' '.join(cmd))
220
218
if input is None:
249
247
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:
257
249
expected = expected or ''
258
250
matching = self.output_checker.check_output(
259
251
expected, actual, self.check_options)
482
474
super(TestCaseWithMemoryTransportAndScript, self).setUp()
483
475
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)
477
def run_script(self, script):
478
return self.script_runner.run_script(self, script)
489
480
def run_command(self, cmd, input, output, error):
490
481
return self.script_runner.run_command(self, cmd, input, output, error)
512
503
super(TestCaseWithTransportAndScript, self).setUp()
513
504
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)
506
def run_script(self, script):
507
return self.script_runner.run_script(self, script)
519
509
def run_command(self, cmd, input, output, error):
520
510
return self.script_runner.run_command(self, cmd, input, output, error)
523
def run_script(test_case, script_string, null_output_matches_anything=False):
513
def run_script(test_case, script_string):
524
514
"""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)
515
return ScriptRunner().run_script(test_case, script_string)
518
class cmd_test_script(commands.Command):
519
"""Run a shell-like test from a file."""
522
takes_args = ['infile']
524
@commands.display_command
525
def run(self, infile):
533
class Test(TestCaseWithTransportAndScript):
535
script = None # Set before running
538
self.run_script(script)
540
runner = tests.TextTestRunner(stream=self.outf)
541
test = Test('test_it')
542
test.path = os.path.realpath(infile)
543
res = runner.run(test)
544
return len(res.errors) + len(res.failures)