/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: Robert Collins
  • Date: 2010-05-06 11:08:10 UTC
  • mto: This revision was merged to the branch mainline in revision 5223.
  • Revision ID: robertc@robertcollins.net-20100506110810-h3j07fh5gmw54s25
Cleaner matcher matching revised unlocking protocol.

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
28
27
from cStringIO import StringIO
29
28
 
30
29
from bzrlib import (
31
 
    errors,
32
30
    osutils,
33
31
    tests,
34
32
    )
57
55
    Input lines start with '<'.
58
56
    Output lines start with nothing.
59
57
    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.
64
58
    """
65
59
 
66
60
    commands = []
79
73
    cmd_line = 1
80
74
    lineno = 0
81
75
    input, output, error = None, None, None
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:
 
76
    for line in text.split('\n'):
91
77
        lineno += 1
92
78
        # Keep a copy for error reporting
93
79
        orig = line
94
80
        comment =  line.find('#')
95
81
        if comment >= 0:
96
82
            # Delete comments
97
 
            # NB: this syntax means comments are allowed inside output, which
98
 
            # may be confusing...
99
83
            line = line[0:comment]
100
84
            line = line.rstrip()
101
 
            if line == '':
102
 
                continue
 
85
        if line == '':
 
86
            # Ignore empty lines
 
87
            continue
103
88
        if line.startswith('$'):
104
89
            # Time to output the current command
105
90
            add_command(cmd_cur, input, output, error)
196
181
        self.output_checker = doctest.OutputChecker()
197
182
        self.check_options = doctest.ELLIPSIS
198
183
 
199
 
    def run_script(self, test_case, text, null_output_matches_anything=False):
 
184
    def run_script(self, test_case, text):
200
185
        """Run a shell-like script as a test.
201
186
 
202
187
        :param test_case: A TestCase instance that should provide the fail(),
204
189
            attribute used as a jail root.
205
190
 
206
191
        :param text: A shell-like script (see _script_to_commands for syntax).
207
 
 
208
 
        :param null_output_matches_anything: For commands with no specified
209
 
            output, ignore any output that does happen, including output on
210
 
            standard error.
211
192
        """
212
 
        self.null_output_matches_anything = null_output_matches_anything
213
193
        for cmd, input, output, error in _script_to_commands(text):
214
194
            self.run_command(test_case, cmd, input, output, error)
215
195
 
218
198
        method = getattr(self, mname, None)
219
199
        if method is None:
220
200
            raise SyntaxError('Command not found "%s"' % (cmd[0],),
221
 
                              (None, 1, 1, ' '.join(cmd)))
 
201
                              None, 1, ' '.join(cmd))
222
202
        if input is None:
223
203
            str_input = ''
224
204
        else:
227
207
        retcode, actual_output, actual_error = method(test_case,
228
208
                                                      str_input, args)
229
209
 
230
 
        try:
231
 
            self._check_output(output, actual_output, test_case)
232
 
        except AssertionError, e:
233
 
            raise AssertionError(str(e) + " in stdout of command %s" % cmd)
234
 
        try:
235
 
            self._check_output(error, actual_error, test_case)
236
 
        except AssertionError, e:
237
 
            raise AssertionError(str(e) +
238
 
                " in stderr of running command %s" % cmd)
 
210
        self._check_output(output, actual_output, test_case)
 
211
        self._check_output(error, actual_error, test_case)
239
212
        if retcode and not error and actual_error:
240
213
            test_case.fail('In \n\t%s\nUnexpected error: %s'
241
214
                           % (' '.join(cmd), actual_error))
242
215
        return retcode, actual_output, actual_error
243
216
 
244
217
    def _check_output(self, expected, actual, test_case):
245
 
        if not actual:
246
 
            if expected is None:
247
 
                return
248
 
            elif expected == '...\n':
249
 
                return
250
 
            else:
251
 
                test_case.fail('expected output: %r, but found nothing'
252
 
                            % (expected,))
253
 
 
254
 
        null_output_matches_anything = getattr(
255
 
            self, 'null_output_matches_anything', False)
256
 
        if null_output_matches_anything and expected is None:
 
218
        if expected is None:
 
219
            # Specifying None means: any output is accepted
257
220
            return
258
 
 
259
 
        expected = expected or ''
 
221
        if actual is None:
 
222
            test_case.fail('We expected output: %r, but found None'
 
223
                           % (expected,))
260
224
        matching = self.output_checker.check_output(
261
225
            expected, actual, self.check_options)
262
226
        if not matching:
266
230
            # 'expected' parameter. So we just fallback to our good old
267
231
            # assertEqualDiff since we know there *are* differences and the
268
232
            # output should be decently readable.
269
 
            #
270
 
            # As a special case, we allow output that's missing a final
271
 
            # newline to match an expected string that does have one, so that
272
 
            # we can match a prompt printed on one line, then input given on
273
 
            # the next line.
274
 
            if expected == actual + '\n':
275
 
                pass
276
 
            else:
277
 
                test_case.assertEqualDiff(expected, actual)
 
233
            test_case.assertEqualDiff(expected, actual)
278
234
 
279
235
    def _pre_process_args(self, args):
280
236
        new_args = []
484
440
        super(TestCaseWithMemoryTransportAndScript, self).setUp()
485
441
        self.script_runner = ScriptRunner()
486
442
 
487
 
    def run_script(self, script, null_output_matches_anything=False):
488
 
        return self.script_runner.run_script(self, script, 
489
 
                   null_output_matches_anything=null_output_matches_anything)
 
443
    def run_script(self, script):
 
444
        return self.script_runner.run_script(self, script)
490
445
 
491
446
    def run_command(self, cmd, input, output, error):
492
447
        return self.script_runner.run_command(self, cmd, input, output, error)
514
469
        super(TestCaseWithTransportAndScript, self).setUp()
515
470
        self.script_runner = ScriptRunner()
516
471
 
517
 
    def run_script(self, script, null_output_matches_anything=False):
518
 
        return self.script_runner.run_script(self, script,
519
 
                   null_output_matches_anything=null_output_matches_anything)
 
472
    def run_script(self, script):
 
473
        return self.script_runner.run_script(self, script)
520
474
 
521
475
    def run_command(self, cmd, input, output, error):
522
476
        return self.script_runner.run_command(self, cmd, input, output, error)
523
477
 
524
 
 
525
 
def run_script(test_case, script_string, null_output_matches_anything=False):
526
 
    """Run the given script within a testcase"""
527
 
    return ScriptRunner().run_script(test_case, script_string,
528
 
               null_output_matches_anything=null_output_matches_anything)
529