~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/script.py

  • Committer: Jared Bunting
  • Date: 2010-10-21 22:27:43 UTC
  • mto: This revision was merged to the branch mainline in revision 5514.
  • Revision ID: jared.bunting@peachjean.com-20101021222743-tn9n0cgzg3z8cb25
Changed _win32_local_path_from_url to not allow "file:///C:" form.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2009, 2010, 2011 Canonical Ltd
 
1
# Copyright (C) 2009, 2010 Canonical Ltd
2
2
#
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
25
25
import os
26
26
import shlex
27
27
import textwrap
 
28
from cStringIO import StringIO
28
29
 
29
30
from bzrlib import (
 
31
    commands,
 
32
    errors,
30
33
    osutils,
31
34
    tests,
32
35
    )
194
197
        self.output_checker = doctest.OutputChecker()
195
198
        self.check_options = doctest.ELLIPSIS
196
199
 
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.
199
202
 
200
203
        :param test_case: A TestCase instance that should provide the fail(),
202
205
            attribute used as a jail root.
203
206
 
204
207
        :param text: A shell-like script (see _script_to_commands for syntax).
205
 
 
206
 
        :param null_output_matches_anything: For commands with no specified
207
 
            output, ignore any output that does happen, including output on
208
 
            standard error.
209
208
        """
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)
213
211
 
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:
221
219
            str_input = ''
222
220
        else:
248
246
            else:
249
247
                test_case.fail('expected output: %r, but found nothing'
250
248
                            % (expected,))
251
 
 
252
 
        null_output_matches_anything = getattr(
253
 
            self, 'null_output_matches_anything', False)
254
 
        if null_output_matches_anything and expected is None:
255
 
            return
256
 
 
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()
484
476
 
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)
488
479
 
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()
514
505
 
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)
518
508
 
519
509
    def run_command(self, cmd, input, output, error):
520
510
        return self.script_runner.run_command(self, cmd, input, output, error)
521
511
 
522
512
 
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)
527
 
 
 
515
    return ScriptRunner().run_script(test_case, script_string)
 
516
 
 
517
 
 
518
class cmd_test_script(commands.Command):
 
519
    """Run a shell-like test from a file."""
 
520
 
 
521
    hidden = True
 
522
    takes_args = ['infile']
 
523
 
 
524
    @commands.display_command
 
525
    def run(self, infile):
 
526
 
 
527
        f = open(infile)
 
528
        try:
 
529
            script = f.read()
 
530
        finally:
 
531
            f.close()
 
532
 
 
533
        class Test(TestCaseWithTransportAndScript):
 
534
 
 
535
            script = None # Set before running
 
536
 
 
537
            def test_it(self):
 
538
                self.run_script(script)
 
539
 
 
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)