13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
17
"""Shell-like test scripts.
18
This allows users to write tests in a syntax very close to a shell session,
19
using a restricted and limited set of commands that should be enough to mimic
20
most of the behaviours.
22
A script is a set of commands, each command is composed of:
23
- one mandatory command line,
24
- one optional set of input lines to feed the command,
25
- one optional set of output expected lines,
26
- one optional set of error expected lines.
28
The optional lines starts with a special string (mnemonic: shell redirection):
33
The execution stops as soon as an expected output or an expected error is not
36
When no output is specified, any ouput from the command is accepted
37
and let the execution continue.
39
If an error occurs and no expected error is specified, the execution stops.
41
An error is defined by a returned status different from zero, not by the
42
presence of text on the error stream.
44
The matching is done on a full string comparison basis unless '...' is used, in
45
which case expected output/errors can be lees precise.
49
The following will succeeds only if 'bzr add' outputs 'adding file'.
54
If you want the command to succeed for any output, just use:
58
The following will stop with an error:
62
If you want it to succeed, use:
65
2> bzr: ERROR: unknown command "not-a-command"
67
You can use ellipsis (...) to replace any piece of text you don't want to be
70
bzr branch not-a-branch
71
2>bzr: ERROR: Not a branch...not-a-branch/".
74
This can be used to ignore entire lines too:
86
You can check the content of a file with cat:
91
You can also check the existence of a file with cat, the following will fail if
92
the file doesn't exist:
19
See developpers/testing.html for more explanations.
159
83
# Ignore empty lines
161
if line.startswith('<'):
85
if line.startswith('$'):
86
# Time to output the current command
87
add_command(cmd_cur, input, output, error)
89
cmd_cur = list(split(line[1:]))
91
input, output, error = None, None, None
92
elif line.startswith('<'):
163
94
if cmd_cur is None:
164
95
raise SyntaxError('No command for that input',
165
96
(file_name, lineno, 1, orig))
167
98
input.append(line[1:] + '\n')
169
elif line.startswith('>'):
172
raise SyntaxError('No command for that output',
173
(file_name, lineno, 1, orig))
175
output.append(line[1:] + '\n')
177
99
elif line.startswith('2>'):
178
100
if error is None:
179
101
if cmd_cur is None:
181
103
(file_name, lineno, 1, orig))
183
105
error.append(line[2:] + '\n')
186
# Time to output the current command
187
add_command(cmd_cur, input, output, error)
188
# And start a new one
189
cmd_cur = list(split(line))
191
input, output, error = None, None, None
109
raise SyntaxError('No command for that output',
110
(file_name, lineno, 1, orig))
112
output.append(line + '\n')
192
113
# Add the last seen command
193
114
add_command(cmd_cur, input, output, error)