/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/tests/script.py

  • Committer: Vincent Ladeuil
  • Date: 2010-10-08 12:47:21 UTC
  • mto: This revision was merged to the branch mainline in revision 5501.
  • Revision ID: v.ladeuil+lp@free.fr-20101008124721-xf6nf40j0h5zu242
MoreĀ docs.

Show diffs side-by-side

added added

removed removed

Lines of Context:
24
24
import glob
25
25
import os
26
26
import shlex
 
27
import textwrap
27
28
from cStringIO import StringIO
28
29
 
29
30
from bzrlib import (
 
31
    commands,
 
32
    errors,
30
33
    osutils,
31
34
    tests,
32
35
    )
55
58
    Input lines start with '<'.
56
59
    Output lines start with nothing.
57
60
    Error lines start with '2>'.
 
61
 
 
62
    :return: A sequence of ([args], input, output, errors), where the args are
 
63
        split in to words, and the input, output, and errors are just strings,
 
64
        typically containing newlines.
58
65
    """
59
66
 
60
67
    commands = []
73
80
    cmd_line = 1
74
81
    lineno = 0
75
82
    input, output, error = None, None, None
76
 
    for line in text.split('\n'):
 
83
    text = textwrap.dedent(text)
 
84
    lines = text.split('\n')
 
85
    # to make use of triple-quoted strings easier, we ignore a blank line
 
86
    # right at the start and right at the end; the rest are meaningful
 
87
    if lines and lines[0] == '':
 
88
        del lines[0]
 
89
    if lines and lines[-1] == '':
 
90
        del lines[-1]
 
91
    for line in lines:
77
92
        lineno += 1
78
93
        # Keep a copy for error reporting
79
94
        orig = line
80
95
        comment =  line.find('#')
81
96
        if comment >= 0:
82
97
            # Delete comments
 
98
            # NB: this syntax means comments are allowed inside output, which
 
99
            # may be confusing...
83
100
            line = line[0:comment]
84
101
            line = line.rstrip()
85
 
        if line == '':
86
 
            # Ignore empty lines
87
 
            continue
 
102
            if line == '':
 
103
                continue
88
104
        if line.startswith('$'):
89
105
            # Time to output the current command
90
106
            add_command(cmd_cur, input, output, error)
230
246
            # 'expected' parameter. So we just fallback to our good old
231
247
            # assertEqualDiff since we know there *are* differences and the
232
248
            # output should be decently readable.
233
 
            test_case.assertEqualDiff(expected, actual)
 
249
            #
 
250
            # As a special case, we allow output that's missing a final
 
251
            # newline to match an expected string that does have one, so that
 
252
            # we can match a prompt printed on one line, then input given on
 
253
            # the next line.
 
254
            if expected == actual + '\n':
 
255
                pass
 
256
            else:
 
257
                test_case.assertEqualDiff(expected, actual)
234
258
 
235
259
    def _pre_process_args(self, args):
236
260
        new_args = []
475
499
    def run_command(self, cmd, input, output, error):
476
500
        return self.script_runner.run_command(self, cmd, input, output, error)
477
501
 
 
502
 
 
503
def run_script(test_case, script_string):
 
504
    """Run the given script within a testcase"""
 
505
    return ScriptRunner().run_script(test_case, script_string)
 
506
 
 
507
 
 
508
class cmd_test_script(commands.Command):
 
509
    """Run a shell-like test from a file."""
 
510
 
 
511
    hidden = True
 
512
    takes_args = ['infile']
 
513
 
 
514
    @commands.display_command
 
515
    def run(self, infile):
 
516
 
 
517
        f = open(infile)
 
518
        try:
 
519
            script = f.read()
 
520
        finally:
 
521
            f.close()
 
522
 
 
523
        class Test(TestCaseWithTransportAndScript):
 
524
 
 
525
            script = None # Set before running
 
526
 
 
527
            def test_it(self):
 
528
                self.run_script(script)
 
529
 
 
530
        runner = tests.TextTestRunner(stream=self.outf)
 
531
        test = Test('test_it')
 
532
        test.path = os.path.realpath(infile)
 
533
        res = runner.run(test)
 
534
        return len(res.errors) + len(res.failures)