~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/script.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2010-09-01 06:45:57 UTC
  • mfrom: (5247.2.41 more-ignored-exceptions)
  • Revision ID: pqm@pqm.ubuntu.com-20100901064557-qsxmjmp195ozbluf
(vila) Catch EPIPE when shutting down test servers. (Vincent Ladeuil)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2009 Canonical Ltd
 
1
# Copyright (C) 2009, 2010 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
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 (
73
74
    cmd_line = 1
74
75
    lineno = 0
75
76
    input, output, error = None, None, None
 
77
    text = textwrap.dedent(text)
76
78
    for line in text.split('\n'):
77
79
        lineno += 1
78
80
        # Keep a copy for error reporting
107
109
                error = []
108
110
            error.append(line[2:] + '\n')
109
111
        else:
 
112
            # can happen if the first line is not recognized as a command, eg
 
113
            # if the prompt has leading whitespace
110
114
            if output is None:
111
115
                if cmd_cur is None:
112
 
                    raise SyntaxError('No command for that output',
 
116
                    raise SyntaxError('No command for line %r' % (line,),
113
117
                                      (file_name, lineno, 1, orig))
114
118
                output = []
115
119
            output.append(line + '\n')
217
221
            # Specifying None means: any output is accepted
218
222
            return
219
223
        if actual is None:
220
 
            test_case.fail('Unexpected: %s' % actual)
 
224
            test_case.fail('We expected output: %r, but found None'
 
225
                           % (expected,))
221
226
        matching = self.output_checker.check_output(
222
227
            expected, actual, self.check_options)
223
228
        if not matching:
289
294
            try:
290
295
                inputs.append(self._read_input(None, in_name))
291
296
            except IOError, e:
292
 
                if e.errno == errno.ENOENT:
 
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):
293
300
                    return (1, None,
294
301
                            '%s: No such file or directory\n' % (in_name,))
 
302
                raise
295
303
        # Basically cat copy input to output
296
304
        output = ''.join(inputs)
297
305
        # Handle output redirections
298
306
        try:
299
307
            output = self._write_output(output, out_name, out_mode)
300
308
        except IOError, e:
301
 
            if e.errno == errno.ENOENT:
 
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):
302
312
                return 1, None, '%s: No such file or directory\n' % (out_name,)
 
313
            raise
303
314
        return 0, output, None
304
315
 
305
316
    def do_echo(self, test_case, input, args):
306
317
        (in_name, out_name, out_mode, args) = _scan_redirection_options(args)
307
 
        if input and args:
308
 
                raise SyntaxError('Specify parameters OR use redirection')
 
318
        if input or in_name:
 
319
            raise SyntaxError('echo doesn\'t read from stdin')
309
320
        if args:
310
321
            input = ' '.join(args)
311
 
        try:
312
 
            input = self._read_input(input, in_name)
313
 
        except IOError, e:
314
 
            if e.errno == errno.ENOENT:
315
 
                return 1, None, '%s: No such file or directory\n' % (in_name,)
316
322
        # Always append a \n'
317
323
        input += '\n'
318
324
        # Process output
321
327
        try:
322
328
            output = self._write_output(output, out_name, out_mode)
323
329
        except IOError, e:
324
 
            if e.errno == errno.ENOENT:
 
330
            if e.errno in (errno.ENOENT, errno.EINVAL):
325
331
                return 1, None, '%s: No such file or directory\n' % (out_name,)
 
332
            raise
326
333
        return 0, output, None
327
334
 
328
335
    def _get_jail_root(self, test_case):
397
404
            retcode = 0
398
405
        return retcode, None, err
399
406
 
 
407
    def do_mv(self, test_case, input, args):
 
408
        err = None
 
409
        def error(msg, src, dst):
 
410
            return "mv: cannot move %s to %s: %s\n" % (src, dst, msg)
 
411
 
 
412
        if not args or len(args) != 2:
 
413
            raise SyntaxError("Usage: mv path1 path2")
 
414
        src, dst = args
 
415
        try:
 
416
            real_dst = dst
 
417
            if os.path.isdir(dst):
 
418
                real_dst = os.path.join(dst, os.path.basename(src))
 
419
            os.rename(src, real_dst)
 
420
        except OSError, e:
 
421
            if e.errno == errno.ENOENT:
 
422
                err = error('No such file or directory', src, dst)
 
423
            else:
 
424
                raise
 
425
        if err:
 
426
            retcode = 1
 
427
        else:
 
428
            retcode = 0
 
429
        return retcode, None, err
 
430
 
 
431
 
400
432
 
401
433
class TestCaseWithMemoryTransportAndScript(tests.TestCaseWithMemoryTransport):
402
434
    """Helper class to experiment shell-like test and memory fs.
445
477
    def run_command(self, cmd, input, output, error):
446
478
        return self.script_runner.run_command(self, cmd, input, output, error)
447
479
 
 
480
 
 
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)