1
# Copyright (C) 2009, 2010 Canonical Ltd
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.
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.
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
17
"""Shell-like test scripts.
19
See developers/testing.html for more explanations.
28
from cStringIO import StringIO
37
"""Split a command line respecting quotes."""
38
scanner = shlex.shlex(s)
39
scanner.quotes = '\'"`'
40
scanner.whitespace_split = True
41
for t in list(scanner):
45
def _script_to_commands(text, file_name=None):
46
"""Turn a script into a list of commands with their associated IOs.
48
Each command appears on a line by itself starting with '$ '. It can be
49
associated with an input that will feed it and an expected output.
51
Comments starts with '#' until the end of line.
52
Empty lines are ignored.
54
Input and output are full lines terminated by a '\n'.
56
Input lines start with '<'.
57
Output lines start with nothing.
58
Error lines start with '2>'.
63
def add_command(cmd, input, output, error):
66
input = ''.join(input)
67
if output is not None:
68
output = ''.join(output)
70
error = ''.join(error)
71
commands.append((cmd, input, output, error))
76
input, output, error = None, None, None
77
text = textwrap.dedent(text)
78
for line in text.split('\n'):
80
# Keep a copy for error reporting
82
comment = line.find('#')
85
line = line[0:comment]
90
if line.startswith('$'):
91
# Time to output the current command
92
add_command(cmd_cur, input, output, error)
94
cmd_cur = list(split(line[1:]))
96
input, output, error = None, None, None
97
elif line.startswith('<'):
100
raise SyntaxError('No command for that input',
101
(file_name, lineno, 1, orig))
103
input.append(line[1:] + '\n')
104
elif line.startswith('2>'):
107
raise SyntaxError('No command for that error',
108
(file_name, lineno, 1, orig))
110
error.append(line[2:] + '\n')
112
# can happen if the first line is not recognized as a command, eg
113
# if the prompt has leading whitespace
116
raise SyntaxError('No command for line %r' % (line,),
117
(file_name, lineno, 1, orig))
119
output.append(line + '\n')
120
# Add the last seen command
121
add_command(cmd_cur, input, output, error)
125
def _scan_redirection_options(args):
126
"""Recognize and process input and output redirections.
128
:param args: The command line arguments
130
:return: A tuple containing:
131
- The file name redirected from or None
132
- The file name redirected to or None
133
- The mode to open the output file or None
134
- The reamining arguments
136
def redirected_file_name(direction, name, args):
141
# We leave the error handling to higher levels, an empty name
148
out_name, out_mode = None, None
151
if arg.startswith('<'):
152
in_name = redirected_file_name('<', arg[1:], args)
153
elif arg.startswith('>>'):
154
out_name = redirected_file_name('>>', arg[2:], args)
156
elif arg.startswith('>',):
157
out_name = redirected_file_name('>', arg[1:], args)
160
remaining.append(arg)
161
return in_name, out_name, out_mode, remaining
164
class ScriptRunner(object):
165
"""Run a shell-like script from a test.
169
from bzrlib.tests import script
173
def test_bug_nnnnn(self):
174
sr = script.ScriptRunner()
175
sr.run_script(self, '''
183
self.output_checker = doctest.OutputChecker()
184
self.check_options = doctest.ELLIPSIS
186
def run_script(self, test_case, text):
187
"""Run a shell-like script as a test.
189
:param test_case: A TestCase instance that should provide the fail(),
190
assertEqualDiff and _run_bzr_core() methods as well as a 'test_dir'
191
attribute used as a jail root.
193
:param text: A shell-like script (see _script_to_commands for syntax).
195
for cmd, input, output, error in _script_to_commands(text):
196
self.run_command(test_case, cmd, input, output, error)
198
def run_command(self, test_case, cmd, input, output, error):
199
mname = 'do_' + cmd[0]
200
method = getattr(self, mname, None)
202
raise SyntaxError('Command not found "%s"' % (cmd[0],),
203
None, 1, ' '.join(cmd))
207
str_input = ''.join(input)
208
args = list(self._pre_process_args(cmd[1:]))
209
retcode, actual_output, actual_error = method(test_case,
212
self._check_output(output, actual_output, test_case)
213
self._check_output(error, actual_error, test_case)
214
if retcode and not error and actual_error:
215
test_case.fail('In \n\t%s\nUnexpected error: %s'
216
% (' '.join(cmd), actual_error))
217
return retcode, actual_output, actual_error
219
def _check_output(self, expected, actual, test_case):
221
# Specifying None means: any output is accepted
224
test_case.fail('We expected output: %r, but found None'
226
matching = self.output_checker.check_output(
227
expected, actual, self.check_options)
229
# Note that we can't use output_checker.output_difference() here
230
# because... the API is broken ('expected' must be a doctest
231
# specific object of which a 'want' attribute will be our
232
# 'expected' parameter. So we just fallback to our good old
233
# assertEqualDiff since we know there *are* differences and the
234
# output should be decently readable.
235
test_case.assertEqualDiff(expected, actual)
237
def _pre_process_args(self, args):
240
# Strip the simple and double quotes since we don't care about
241
# them. We leave the backquotes in place though since they have a
242
# different semantic.
243
if arg[0] in ('"', "'") and arg[0] == arg[-1]:
246
if glob.has_magic(arg):
247
matches = glob.glob(arg)
249
# We care more about order stability than performance
257
def _read_input(self, input, in_name):
258
if in_name is not None:
259
infile = open(in_name, 'rb')
261
# Command redirection takes precedence over provided input
262
input = infile.read()
267
def _write_output(self, output, out_name, out_mode):
268
if out_name is not None:
269
outfile = open(out_name, out_mode)
271
outfile.write(output)
277
def do_bzr(self, test_case, input, args):
278
retcode, out, err = test_case._run_bzr_core(
279
args, retcode=None, encoding=None, stdin=input, working_dir=None)
280
return retcode, out, err
282
def do_cat(self, test_case, input, args):
283
(in_name, out_name, out_mode, args) = _scan_redirection_options(args)
284
if args and in_name is not None:
285
raise SyntaxError('Specify a file OR use redirection')
293
for in_name in input_names:
295
inputs.append(self._read_input(None, in_name))
297
# Some filenames are illegal on Windows and generate EINVAL
298
# rather than just saying the filename doesn't exist
299
if e.errno in (errno.ENOENT, errno.EINVAL):
301
'%s: No such file or directory\n' % (in_name,))
303
# Basically cat copy input to output
304
output = ''.join(inputs)
305
# Handle output redirections
307
output = self._write_output(output, out_name, out_mode)
309
# If out_name cannot be created, we may get 'ENOENT', however if
310
# out_name is something like '', we can get EINVAL
311
if e.errno in (errno.ENOENT, errno.EINVAL):
312
return 1, None, '%s: No such file or directory\n' % (out_name,)
314
return 0, output, None
316
def do_echo(self, test_case, input, args):
317
(in_name, out_name, out_mode, args) = _scan_redirection_options(args)
319
raise SyntaxError('echo doesn\'t read from stdin')
321
input = ' '.join(args)
322
# Always append a \n'
326
# Handle output redirections
328
output = self._write_output(output, out_name, out_mode)
330
if e.errno in (errno.ENOENT, errno.EINVAL):
331
return 1, None, '%s: No such file or directory\n' % (out_name,)
333
return 0, output, None
335
def _get_jail_root(self, test_case):
336
return test_case.test_dir
338
def _ensure_in_jail(self, test_case, path):
339
jail_root = self._get_jail_root(test_case)
340
if not osutils.is_inside(jail_root, osutils.normalizepath(path)):
341
raise ValueError('%s is not inside %s' % (path, jail_root))
343
def do_cd(self, test_case, input, args):
345
raise SyntaxError('Usage: cd [dir]')
348
self._ensure_in_jail(test_case, d)
350
# The test "home" directory is the root of its jail
351
d = self._get_jail_root(test_case)
355
def do_mkdir(self, test_case, input, args):
356
if not args or len(args) != 1:
357
raise SyntaxError('Usage: mkdir dir')
359
self._ensure_in_jail(test_case, d)
363
def do_rm(self, test_case, input, args):
366
def error(msg, path):
367
return "rm: cannot remove '%s': %s\n" % (path, msg)
369
force, recursive = False, False
371
if args and args[0][0] == '-':
372
opts = args.pop(0)[1:]
375
opts = opts.replace('f', '', 1)
378
opts = opts.replace('r', '', 1)
380
raise SyntaxError('Usage: rm [-fr] path+')
382
self._ensure_in_jail(test_case, p)
383
# FIXME: Should we put that in osutils ?
387
# Various OSes raises different exceptions (linux: EISDIR,
388
# win32: EACCES, OSX: EPERM) when invoked on a directory
389
if e.errno in (errno.EISDIR, errno.EPERM, errno.EACCES):
393
err = error('Is a directory', p)
395
elif e.errno == errno.ENOENT:
397
err = error('No such file or directory', p)
405
return retcode, None, err
407
def do_mv(self, test_case, input, args):
409
def error(msg, src, dst):
410
return "mv: cannot move %s to %s: %s\n" % (src, dst, msg)
412
if not args or len(args) != 2:
413
raise SyntaxError("Usage: mv path1 path2")
417
if os.path.isdir(dst):
418
real_dst = os.path.join(dst, os.path.basename(src))
419
os.rename(src, real_dst)
421
if e.errno == errno.ENOENT:
422
err = error('No such file or directory', src, dst)
429
return retcode, None, err
433
class TestCaseWithMemoryTransportAndScript(tests.TestCaseWithMemoryTransport):
434
"""Helper class to experiment shell-like test and memory fs.
436
This not intended to be used outside of experiments in implementing memoy
437
based file systems and evolving bzr so that test can use only memory based
442
super(TestCaseWithMemoryTransportAndScript, self).setUp()
443
self.script_runner = ScriptRunner()
445
def run_script(self, script):
446
return self.script_runner.run_script(self, script)
448
def run_command(self, cmd, input, output, error):
449
return self.script_runner.run_command(self, cmd, input, output, error)
452
class TestCaseWithTransportAndScript(tests.TestCaseWithTransport):
453
"""Helper class to quickly define shell-like tests.
457
from bzrlib.tests import script
460
class TestBug(script.TestCaseWithTransportAndScript):
462
def test_bug_nnnnn(self):
471
super(TestCaseWithTransportAndScript, self).setUp()
472
self.script_runner = ScriptRunner()
474
def run_script(self, script):
475
return self.script_runner.run_script(self, script)
477
def run_command(self, cmd, input, output, error):
478
return self.script_runner.run_command(self, cmd, input, output, error)
481
def run_script(test_case, script_string):
482
"""Run the given script within a testcase"""
483
return ScriptRunner().run_script(test_case, script_string)