/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

Better redirection handling.

* bzrlib/tests/test_script.py:
(TestRedirections): Explicit tests for redirections including the
cases used by Maritza Mendez in the #425510 and $426410 reports.

* bzrlib/tests/script.py:
(_scan_redirection_options.redirected_file_name): Factor out
redirection file name handling.
(_scan_redirection_options): Handle the case where the redirection
is specified with 2 arguments instead of one.
(ScriptRunner.run_command): Tweak error message.
(ScriptRunner.do_cat, ScriptRunner.do_echo): Catch redirection
errors.

Show diffs side-by-side

added added

removed removed

Lines of Context:
210
210
        - The mode to open the output file or None
211
211
        - The reamining arguments
212
212
    """
 
213
    def redirected_file_name(direction, name, args):
 
214
        if name == '':
 
215
            try:
 
216
                name = args.pop(0)
 
217
            except IndexError:
 
218
                # We leave the error handling to higher levels, an empty name
 
219
                # can't be legal.
 
220
                name = ''
 
221
        return name
 
222
 
213
223
    remaining = []
214
224
    in_name = None
215
225
    out_name, out_mode = None, None
216
 
    for arg in  args:
 
226
    while args:
 
227
        arg = args.pop(0)
217
228
        if arg.startswith('<'):
218
 
            in_name = arg[1:]
 
229
            in_name = redirected_file_name('<', arg[1:], args)
219
230
        elif arg.startswith('>>'):
220
 
            out_name = arg[2:]
 
231
            out_name = redirected_file_name('>>', arg[2:], args)
221
232
            out_mode = 'ab+'
222
 
        elif arg.startswith('>'):
223
 
            out_name = arg[1:]
 
233
        elif arg.startswith('>',):
 
234
            out_name = redirected_file_name('>', arg[1:], args)
224
235
            out_mode = 'wb+'
225
236
        else:
226
237
            remaining.append(arg)
270
281
        self._check_output(output, actual_output)
271
282
        self._check_output(error, actual_error)
272
283
        if retcode and not error and actual_error:
273
 
            self.test_case.fail('Unexpected error: %s' % actual_error)
 
284
            self.test_case.fail('In \n\t%s\nUnexpected error: %s'
 
285
                                % (' '.join(cmd), actual_error))
274
286
        return retcode, actual_output, actual_error
275
287
 
276
288
    def _read_input(self, input, in_name):
306
318
            if in_name is not None:
307
319
                raise SyntaxError('Specify a file OR use redirection')
308
320
            in_name = args[0]
309
 
        input = self._read_input(input, in_name)
 
321
        try:
 
322
            input = self._read_input(input, in_name)
 
323
        except IOError, e:
 
324
            if e.errno == errno.ENOENT:
 
325
                return 1, None, '%s: No such file or directory\n' % (in_name,)
310
326
        # Basically cat copy input to output
311
327
        output = input
312
328
        # Handle output redirections
313
 
        output = self._write_output(output, out_name, out_mode)
 
329
        try:
 
330
            output = self._write_output(output, out_name, out_mode)
 
331
        except IOError, e:
 
332
            if e.errno == errno.ENOENT:
 
333
                return 1, None, '%s: No such file or directory\n' % (out_name,)
314
334
        return 0, output, None
315
335
 
316
336
    def do_echo(self, input, args):
319
339
                raise SyntaxError('Specify parameters OR use redirection')
320
340
        if args:
321
341
            input = ''.join(args)
322
 
        input = self._read_input(input, in_name)
 
342
        try:
 
343
            input = self._read_input(input, in_name)
 
344
        except IOError, e:
 
345
            if e.errno == errno.ENOENT:
 
346
                return 1, None, '%s: No such file or directory\n' % (in_name,)
323
347
        # Always append a \n'
324
348
        input += '\n'
325
349
        # Process output
326
350
        output = input
327
351
        # Handle output redirections
328
 
        output = self._write_output(output, out_name, out_mode)
 
352
        try:
 
353
            output = self._write_output(output, out_name, out_mode)
 
354
        except IOError, e:
 
355
            if e.errno == errno.ENOENT:
 
356
                return 1, None, '%s: No such file or directory\n' % (out_name,)
329
357
        return 0, output, None
330
358
 
331
359
    def _ensure_in_jail(self, path):