/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: 2020-05-06 02:13:25 UTC
  • mfrom: (7490.7.21 work)
  • mto: This revision was merged to the branch mainline in revision 7501.
  • Revision ID: jelmer@jelmer.uk-20200506021325-awbmmqu1zyorz7sj
Merge 3.1 branch.

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