~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/script.py

  • Committer: Martin Pool
  • Date: 2010-10-15 10:19:25 UTC
  • mto: This revision was merged to the branch mainline in revision 5503.
  • Revision ID: mbp@sourcefrog.net-20101015101925-qr2m0i5sv6ad0bs2
Superstitious argument quoting in makefile

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