/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

(mbp) status shows missing newly-added files (Rory Yorke)

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
    errors,
30
32
    osutils,
31
33
    tests,
32
34
    )
55
57
    Input lines start with '<'.
56
58
    Output lines start with nothing.
57
59
    Error lines start with '2>'.
 
60
 
 
61
    :return: A sequence of ([args], input, output, errors), where the args are
 
62
        split in to words, and the input, output, and errors are just strings,
 
63
        typically containing newlines.
58
64
    """
59
65
 
60
66
    commands = []
73
79
    cmd_line = 1
74
80
    lineno = 0
75
81
    input, output, error = None, None, None
76
 
    for line in text.split('\n'):
 
82
    text = textwrap.dedent(text)
 
83
    lines = text.split('\n')
 
84
    # to make use of triple-quoted strings easier, we ignore a blank line
 
85
    # right at the start and right at the end; the rest are meaningful
 
86
    if lines and lines[0] == '':
 
87
        del lines[0]
 
88
    if lines and lines[-1] == '':
 
89
        del lines[-1]
 
90
    for line in lines:
77
91
        lineno += 1
78
92
        # Keep a copy for error reporting
79
93
        orig = line
80
94
        comment =  line.find('#')
81
95
        if comment >= 0:
82
96
            # Delete comments
 
97
            # NB: this syntax means comments are allowed inside output, which
 
98
            # may be confusing...
83
99
            line = line[0:comment]
84
100
            line = line.rstrip()
85
 
        if line == '':
86
 
            # Ignore empty lines
87
 
            continue
 
101
            if line == '':
 
102
                continue
88
103
        if line.startswith('$'):
89
104
            # Time to output the current command
90
105
            add_command(cmd_cur, input, output, error)
198
213
        method = getattr(self, mname, None)
199
214
        if method is None:
200
215
            raise SyntaxError('Command not found "%s"' % (cmd[0],),
201
 
                              None, 1, ' '.join(cmd))
 
216
                              (None, 1, 1, ' '.join(cmd)))
202
217
        if input is None:
203
218
            str_input = ''
204
219
        else:
207
222
        retcode, actual_output, actual_error = method(test_case,
208
223
                                                      str_input, args)
209
224
 
210
 
        self._check_output(output, actual_output, test_case)
211
 
        self._check_output(error, actual_error, test_case)
 
225
        try:
 
226
            self._check_output(output, actual_output, test_case)
 
227
        except AssertionError, e:
 
228
            raise AssertionError(str(e) + " in stdout of command %s" % cmd)
 
229
        try:
 
230
            self._check_output(error, actual_error, test_case)
 
231
        except AssertionError, e:
 
232
            raise AssertionError(str(e) +
 
233
                " in stderr of running command %s" % cmd)
212
234
        if retcode and not error and actual_error:
213
235
            test_case.fail('In \n\t%s\nUnexpected error: %s'
214
236
                           % (' '.join(cmd), actual_error))
215
237
        return retcode, actual_output, actual_error
216
238
 
217
239
    def _check_output(self, expected, actual, test_case):
218
 
        if expected is None:
219
 
            # Specifying None means: any output is accepted
220
 
            return
221
 
        if actual is None:
222
 
            test_case.fail('We expected output: %r, but found None'
223
 
                           % (expected,))
 
240
        if not actual:
 
241
            if expected is None:
 
242
                return
 
243
            elif expected == '...\n':
 
244
                return
 
245
            else:
 
246
                test_case.fail('expected output: %r, but found nothing'
 
247
                            % (expected,))
 
248
        expected = expected or ''
224
249
        matching = self.output_checker.check_output(
225
250
            expected, actual, self.check_options)
226
251
        if not matching:
230
255
            # 'expected' parameter. So we just fallback to our good old
231
256
            # assertEqualDiff since we know there *are* differences and the
232
257
            # output should be decently readable.
233
 
            test_case.assertEqualDiff(expected, actual)
 
258
            #
 
259
            # As a special case, we allow output that's missing a final
 
260
            # newline to match an expected string that does have one, so that
 
261
            # we can match a prompt printed on one line, then input given on
 
262
            # the next line.
 
263
            if expected == actual + '\n':
 
264
                pass
 
265
            else:
 
266
                test_case.assertEqualDiff(expected, actual)
234
267
 
235
268
    def _pre_process_args(self, args):
236
269
        new_args = []
475
508
    def run_command(self, cmd, input, output, error):
476
509
        return self.script_runner.run_command(self, cmd, input, output, error)
477
510
 
 
511
 
 
512
def run_script(test_case, script_string):
 
513
    """Run the given script within a testcase"""
 
514
    return ScriptRunner().run_script(test_case, script_string)
 
515