/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 breezy/tests/script.py

  • Committer: Jelmer Vernooij
  • Date: 2018-05-06 11:48:54 UTC
  • mto: This revision was merged to the branch mainline in revision 6960.
  • Revision ID: jelmer@jelmer.uk-20180506114854-h4qd9ojaqy8wxjsd
Move .mailmap to root.

Show diffs side-by-side

added added

removed removed

Lines of Context:
22
22
import doctest
23
23
import errno
24
24
import glob
25
 
import logging
26
25
import os
27
26
import shlex
28
 
import sys
29
27
import textwrap
30
28
 
31
29
from .. import (
32
30
    osutils,
33
31
    tests,
34
 
    trace,
35
32
    )
36
 
from ..tests import ui_testing
37
33
 
38
34
 
39
35
def split(s):
93
89
        lineno += 1
94
90
        # Keep a copy for error reporting
95
91
        orig = line
96
 
        comment = line.find('#')
 
92
        comment =  line.find('#')
97
93
        if comment >= 0:
98
94
            # Delete comments
99
95
            # NB: this syntax means comments are allowed inside output, which
142
138
 
143
139
    :param args: The command line arguments
144
140
 
145
 
    :return: A tuple containing:
 
141
    :return: A tuple containing: 
146
142
        - The file name redirected from or None
147
143
        - The file name redirected to or None
148
144
        - The mode to open the output file or None
167
163
            in_name = redirected_file_name('<', arg[1:], args)
168
164
        elif arg.startswith('>>'):
169
165
            out_name = redirected_file_name('>>', arg[2:], args)
170
 
            out_mode = 'a+'
 
166
            out_mode = 'ab+'
171
167
        elif arg.startswith('>',):
172
168
            out_name = redirected_file_name('>', arg[1:], args)
173
 
            out_mode = 'w+'
 
169
            out_mode = 'wb+'
174
170
        else:
175
171
            remaining.append(arg)
176
172
    return in_name, out_name, out_mode, remaining
178
174
 
179
175
class ScriptRunner(object):
180
176
    """Run a shell-like script from a test.
181
 
 
 
177
    
182
178
    Can be used as:
183
179
 
184
180
    from breezy.tests import script
236
232
        try:
237
233
            self._check_output(error, actual_error, test_case)
238
234
        except AssertionError as e:
239
 
            raise AssertionError(str(e)
240
 
                                 + " in stderr of running command %s" % cmd)
 
235
            raise AssertionError(str(e) +
 
236
                " in stderr of running command %s" % cmd)
241
237
        if retcode and not error and actual_error:
242
238
            test_case.fail('In \n\t%s\nUnexpected error: %s'
243
239
                           % (' '.join(cmd), actual_error))
251
247
                return
252
248
            else:
253
249
                test_case.fail('expected output: %r, but found nothing'
254
 
                               % (expected,))
 
250
                            % (expected,))
255
251
 
256
252
        null_output_matches_anything = getattr(
257
253
            self, 'null_output_matches_anything', False)
284
280
            # Strip the simple and double quotes since we don't care about
285
281
            # them.  We leave the backquotes in place though since they have a
286
282
            # different semantic.
287
 
            if arg[0] in ('"', "'") and arg[0] == arg[-1]:
 
283
            if arg[0] in  ('"', "'") and arg[0] == arg[-1]:
288
284
                yield arg[1:-1]
289
285
            else:
290
286
                if glob.has_magic(arg):
300
296
 
301
297
    def _read_input(self, input, in_name):
302
298
        if in_name is not None:
303
 
            infile = open(in_name, 'r')
 
299
            infile = open(in_name, 'rb')
304
300
            try:
305
301
                # Command redirection takes precedence over provided input
306
302
                input = infile.read()
319
315
        return output
320
316
 
321
317
    def do_brz(self, test_case, input, args):
322
 
        encoding = osutils.get_user_encoding()
323
 
        if sys.version_info[0] == 2:
324
 
            stdout = ui_testing.BytesIOWithEncoding()
325
 
            stderr = ui_testing.BytesIOWithEncoding()
326
 
            stdout.encoding = stderr.encoding = encoding
327
 
 
328
 
            # FIXME: don't call into logging here
329
 
            handler = trace.EncodedStreamHandler(
330
 
                stderr, errors="replace")
331
 
        else:
332
 
            stdout = ui_testing.StringIOWithEncoding()
333
 
            stderr = ui_testing.StringIOWithEncoding()
334
 
            stdout.encoding = stderr.encoding = encoding
335
 
            handler = logging.StreamHandler(stderr)
336
 
        handler.setLevel(logging.INFO)
337
 
 
338
 
        logger = logging.getLogger('')
339
 
        logger.addHandler(handler)
340
 
        try:
341
 
            retcode = test_case._run_bzr_core(
342
 
                args, encoding=encoding, stdin=input, stdout=stdout,
343
 
                stderr=stderr, working_dir=None)
344
 
        finally:
345
 
            logger.removeHandler(handler)
346
 
 
347
 
        return retcode, stdout.getvalue(), stderr.getvalue()
 
318
        retcode, out, err = test_case._run_bzr_core(
 
319
            args, retcode=None, encoding=None, stdin=input, working_dir=None)
 
320
        return retcode, out, err
348
321
 
349
322
    def do_cat(self, test_case, input, args):
350
323
        (in_name, out_name, out_mode, args) = _scan_redirection_options(args)
431
404
        err = None
432
405
 
433
406
        def error(msg, path):
434
 
            return "rm: cannot remove '%s': %s\n" % (path, msg)
 
407
            return  "rm: cannot remove '%s': %s\n" % (path, msg)
435
408
 
436
409
        force, recursive = False, False
437
410
        opts = None
461
434
                        break
462
435
                elif e.errno == errno.ENOENT:
463
436
                    if not force:
464
 
                        err = error('No such file or directory', p)
 
437
                        err =  error('No such file or directory', p)
465
438
                        break
466
439
                else:
467
440
                    raise
473
446
 
474
447
    def do_mv(self, test_case, input, args):
475
448
        err = None
476
 
 
477
449
        def error(msg, src, dst):
478
450
            return "mv: cannot move %s to %s: %s\n" % (src, dst, msg)
479
451
 
497
469
        return retcode, None, err
498
470
 
499
471
 
 
472
 
500
473
class TestCaseWithMemoryTransportAndScript(tests.TestCaseWithMemoryTransport):
501
474
    """Helper class to experiment shell-like test and memory fs.
502
475
 
514
487
        self.overrideEnv('INSIDE_EMACS', '1')
515
488
 
516
489
    def run_script(self, script, null_output_matches_anything=False):
517
 
        return self.script_runner.run_script(self, script,
518
 
                                             null_output_matches_anything=null_output_matches_anything)
 
490
        return self.script_runner.run_script(self, script, 
 
491
                   null_output_matches_anything=null_output_matches_anything)
519
492
 
520
493
    def run_command(self, cmd, input, output, error):
521
494
        return self.script_runner.run_command(self, cmd, input, output, error)
549
522
 
550
523
    def run_script(self, script, null_output_matches_anything=False):
551
524
        return self.script_runner.run_script(self, script,
552
 
                                             null_output_matches_anything=null_output_matches_anything)
 
525
                   null_output_matches_anything=null_output_matches_anything)
553
526
 
554
527
    def run_command(self, cmd, input, output, error):
555
528
        return self.script_runner.run_command(self, cmd, input, output, error)
558
531
def run_script(test_case, script_string, null_output_matches_anything=False):
559
532
    """Run the given script within a testcase"""
560
533
    return ScriptRunner().run_script(test_case, script_string,
561
 
                                     null_output_matches_anything=null_output_matches_anything)
 
534
               null_output_matches_anything=null_output_matches_anything)
 
535