/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-15 12:35:00 UTC
  • mto: This revision was merged to the branch mainline in revision 5502.
  • Revision ID: v.ladeuil+lp@free.fr-20101015123500-iyqj7e0r62ie6qfy
Unbreak pqm by commenting out the bogus use of doctest +SKIP not supported by python2.4

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)
207
223
        retcode, actual_output, actual_error = method(test_case,
208
224
                                                      str_input, args)
209
225
 
210
 
        self._check_output(output, actual_output, test_case)
211
 
        self._check_output(error, actual_error, test_case)
 
226
        try:
 
227
            self._check_output(output, actual_output, test_case)
 
228
        except AssertionError, e:
 
229
            raise AssertionError(str(e) + " in stdout of command %s" % cmd)
 
230
        try:
 
231
            self._check_output(error, actual_error, test_case)
 
232
        except AssertionError, e:
 
233
            raise AssertionError(str(e) +
 
234
                " in stderr of running command %s" % cmd)
212
235
        if retcode and not error and actual_error:
213
236
            test_case.fail('In \n\t%s\nUnexpected error: %s'
214
237
                           % (' '.join(cmd), actual_error))
215
238
        return retcode, actual_output, actual_error
216
239
 
217
240
    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,))
 
241
        if not actual:
 
242
            if expected is None:
 
243
                return
 
244
            elif expected == '...\n':
 
245
                return
 
246
            else:
 
247
                test_case.fail('expected output: %r, but found nothing'
 
248
                            % (expected,))
 
249
        expected = expected or ''
224
250
        matching = self.output_checker.check_output(
225
251
            expected, actual, self.check_options)
226
252
        if not matching:
230
256
            # 'expected' parameter. So we just fallback to our good old
231
257
            # assertEqualDiff since we know there *are* differences and the
232
258
            # output should be decently readable.
233
 
            test_case.assertEqualDiff(expected, actual)
 
259
            #
 
260
            # As a special case, we allow output that's missing a final
 
261
            # newline to match an expected string that does have one, so that
 
262
            # we can match a prompt printed on one line, then input given on
 
263
            # the next line.
 
264
            if expected == actual + '\n':
 
265
                pass
 
266
            else:
 
267
                test_case.assertEqualDiff(expected, actual)
234
268
 
235
269
    def _pre_process_args(self, args):
236
270
        new_args = []
475
509
    def run_command(self, cmd, input, output, error):
476
510
        return self.script_runner.run_command(self, cmd, input, output, error)
477
511
 
 
512
 
 
513
def run_script(test_case, script_string):
 
514
    """Run the given script within a testcase"""
 
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)