/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: Breezy landing bot
  • Author(s): Colin Watson
  • Date: 2020-11-16 21:47:08 UTC
  • mfrom: (7521.1.1 remove-lp-workaround)
  • Revision ID: breezy.the.bot@gmail.com-20201116214708-jos209mgxi41oy15
Remove breezy.git workaround for bazaar.launchpad.net.

Merged from https://code.launchpad.net/~cjwatson/brz/remove-lp-workaround/+merge/393710

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
 
from bzrlib import (
 
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
 
    from bzrlib.tests import script
 
184
    from breezy.tests import script
181
185
 
182
186
    ...
183
187
 
184
188
        def test_bug_nnnnn(self):
185
189
            sr = script.ScriptRunner()
186
190
            sr.run_script(self, '''
187
 
            $ bzr init
188
 
            $ bzr do-this
 
191
            $ brz init
 
192
            $ brz do-this
189
193
            # Boom, error
190
194
            ''')
191
195
    """
227
231
 
228
232
        try:
229
233
            self._check_output(output, actual_output, test_case)
230
 
        except AssertionError, e:
 
234
        except AssertionError as e:
231
235
            raise AssertionError(str(e) + " in stdout of command %s" % cmd)
232
236
        try:
233
237
            self._check_output(error, actual_error, test_case)
234
 
        except AssertionError, e:
235
 
            raise AssertionError(str(e) +
236
 
                " in stderr of running command %s" % cmd)
 
238
        except AssertionError as e:
 
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()
314
318
            output = None
315
319
        return output
316
320
 
317
 
    def do_bzr(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
 
321
    def do_brz(self, test_case, input, args):
 
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)
333
351
        for in_name in input_names:
334
352
            try:
335
353
                inputs.append(self._read_input(None, in_name))
336
 
            except IOError, e:
 
354
            except IOError as e:
337
355
                # Some filenames are illegal on Windows and generate EINVAL
338
356
                # rather than just saying the filename doesn't exist
339
357
                if e.errno in (errno.ENOENT, errno.EINVAL):
345
363
        # Handle output redirections
346
364
        try:
347
365
            output = self._write_output(output, out_name, out_mode)
348
 
        except IOError, e:
 
366
        except IOError as e:
349
367
            # If out_name cannot be created, we may get 'ENOENT', however if
350
368
            # out_name is something like '', we can get EINVAL
351
369
            if e.errno in (errno.ENOENT, errno.EINVAL):
366
384
        # Handle output redirections
367
385
        try:
368
386
            output = self._write_output(output, out_name, out_mode)
369
 
        except IOError, e:
 
387
        except IOError as e:
370
388
            if e.errno in (errno.ENOENT, errno.EINVAL):
371
389
                return 1, None, '%s: No such file or directory\n' % (out_name,)
372
390
            raise
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
423
441
            # FIXME: Should we put that in osutils ?
424
442
            try:
425
443
                os.remove(p)
426
 
            except OSError, e:
 
444
            except OSError as e:
427
445
                # Various OSes raises different exceptions (linux: EISDIR,
428
446
                #   win32: EACCES, OSX: EPERM) when invoked on a directory
429
447
                if e.errno in (errno.EISDIR, errno.EPERM, errno.EACCES):
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
 
457
476
            if os.path.isdir(dst):
458
477
                real_dst = os.path.join(dst, os.path.basename(src))
459
478
            os.rename(src, real_dst)
460
 
        except OSError, e:
 
479
        except OSError as e:
461
480
            if e.errno == errno.ENOENT:
462
481
                err = error('No such file or directory', src, dst)
463
482
            else:
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)
499
517
 
500
518
    Can be used as:
501
519
 
502
 
    from bzrlib.tests import script
 
520
    from breezy.tests import script
503
521
 
504
522
 
505
523
    class TestBug(script.TestCaseWithTransportAndScript):
506
524
 
507
525
        def test_bug_nnnnn(self):
508
526
            self.run_script('''
509
 
            $ bzr init
510
 
            $ bzr do-this
 
527
            $ brz init
 
528
            $ brz do-this
511
529
            # Boom, error
512
530
            ''')
513
531
    """
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)