~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/script.py

Start some shell-like capability to write tests.

* bzrlib/tests/test_script.py: 
Associated tests.

* bzrlib/tests/script.py: 
Start implementing shell-like test scripts.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2009 Canonical Ltd
 
2
#
 
3
# This program is free software; you can redistribute it and/or modify
 
4
# it under the terms of the GNU General Public License as published by
 
5
# the Free Software Foundation; either version 2 of the License, or
 
6
# (at your option) any later version.
 
7
#
 
8
# This program is distributed in the hope that it will be useful,
 
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
# GNU General Public License for more details.
 
12
#
 
13
# You should have received a copy of the GNU General Public License
 
14
# along with this program; if not, write to the Free Software
 
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
16
 
 
17
import shlex
 
18
 
 
19
 
 
20
def _script_to_commands(script, file_name=None):
 
21
    """Turn a script into a list of commands with their associated IOs.
 
22
 
 
23
    Each command appears on a line by itself. It can be associated with an
 
24
    input that will feed it and an expected output.
 
25
    Comments starts with '#' until the end of line.
 
26
    Empty lines are ignored.
 
27
    Input and output are full lines terminated by a '\n'.
 
28
    Input lines start with '<'.
 
29
    Output lines start with '>' 
 
30
    """
 
31
    commands = []
 
32
    cmd_cur = None
 
33
    cmd_line = 1
 
34
    lineno = 0
 
35
    input = None
 
36
    output = None
 
37
    for line in script.split('\n'):
 
38
        lineno += 1
 
39
        # Keep a copy for error reporting
 
40
        orig = line
 
41
        comment =  line.find('#')
 
42
        if comment >= 0:
 
43
            # Delete comments
 
44
            line = line[0:comment]
 
45
            line = line.rstrip()
 
46
        if line == '':
 
47
            # Ignore empty lines
 
48
            continue
 
49
        if line.startswith('<'):
 
50
            if input is None:
 
51
                if cmd_cur is None:
 
52
                    raise SyntaxError('No command for that input',
 
53
                                      (file_name, lineno, 1, orig))
 
54
                input = []
 
55
            input.append(line[1:] + '\n')
 
56
            continue
 
57
        elif line.startswith('>'):
 
58
            if output is None:
 
59
                if cmd_cur is None:
 
60
                    raise SyntaxError('No command for that output',
 
61
                                      (file_name, lineno, 1, orig))
 
62
                output = []
 
63
            output.append(line[1:] + '\n')
 
64
            continue
 
65
        else:
 
66
            # Time to output the current command
 
67
            if cmd_cur is not None:
 
68
                commands.append((cmd_cur, input, output))
 
69
            # And start a new one
 
70
            cmd_cur = shlex.split(line)
 
71
            cmd_line = lineno
 
72
            input = None
 
73
            output = None
 
74
    # Add the last seen command
 
75
    if cmd_cur is not None:
 
76
        commands.append((cmd_cur, input, output))
 
77
    return commands
 
78
 
 
79
 
 
80