~ubuntu-branches/ubuntu/lucid/bzr/lucid-proposed

« back to all changes in this revision

Viewing changes to bzrlib/tests/__init__.py

Tags: upstream-0.7
ImportĀ upstreamĀ versionĀ 0.7

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2005 by Canonical Ltd
 
2
 
 
3
# This program is free software; you can redistribute it and/or modify
 
4
# it under the terms of the GNU General Public License as published by
 
5
# the Free Software Foundation; either version 2 of the License, or
 
6
# (at your option) any later version.
 
7
 
 
8
# This program is distributed in the hope that it will be useful,
 
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
# GNU General Public License for more details.
 
12
 
 
13
# You should have received a copy of the GNU General Public License
 
14
# along with this program; if not, write to the Free Software
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
16
 
 
17
 
 
18
# TODO: Perhaps there should be an API to find out if bzr running under the
 
19
# test suite -- some plugins might want to avoid making intrusive changes if
 
20
# this is the case.  However, we want behaviour under to test to diverge as
 
21
# little as possible, so this should be used rarely if it's added at all.
 
22
# (Suggestion from j-a-meinel, 2005-11-24)
 
23
 
 
24
from cStringIO import StringIO
 
25
import difflib
 
26
import errno
 
27
import logging
 
28
import os
 
29
import re
 
30
import shutil
 
31
import sys
 
32
import tempfile
 
33
import unittest
 
34
import time
 
35
import codecs
 
36
 
 
37
import bzrlib.branch
 
38
import bzrlib.commands
 
39
from bzrlib.errors import BzrError
 
40
import bzrlib.inventory
 
41
import bzrlib.merge3
 
42
import bzrlib.osutils
 
43
import bzrlib.osutils as osutils
 
44
import bzrlib.plugin
 
45
import bzrlib.store
 
46
import bzrlib.trace
 
47
from bzrlib.trace import mutter
 
48
from bzrlib.tests.TestUtil import TestLoader, TestSuite
 
49
from bzrlib.tests.treeshape import build_tree_contents
 
50
 
 
51
MODULES_TO_TEST = []
 
52
MODULES_TO_DOCTEST = [
 
53
                      bzrlib.branch,
 
54
                      bzrlib.commands,
 
55
                      bzrlib.errors,
 
56
                      bzrlib.inventory,
 
57
                      bzrlib.merge3,
 
58
                      bzrlib.osutils,
 
59
                      bzrlib.store,
 
60
                      ]
 
61
def packages_to_test():
 
62
    import bzrlib.tests.blackbox
 
63
    return [
 
64
            bzrlib.tests.blackbox
 
65
            ]
 
66
 
 
67
 
 
68
class EarlyStoppingTestResultAdapter(object):
 
69
    """An adapter for TestResult to stop at the first first failure or error"""
 
70
 
 
71
    def __init__(self, result):
 
72
        self._result = result
 
73
 
 
74
    def addError(self, test, err):
 
75
        self._result.addError(test, err)
 
76
        self._result.stop()
 
77
 
 
78
    def addFailure(self, test, err):
 
79
        self._result.addFailure(test, err)
 
80
        self._result.stop()
 
81
 
 
82
    def __getattr__(self, name):
 
83
        return getattr(self._result, name)
 
84
 
 
85
    def __setattr__(self, name, value):
 
86
        if name == '_result':
 
87
            object.__setattr__(self, name, value)
 
88
        return setattr(self._result, name, value)
 
89
 
 
90
 
 
91
class _MyResult(unittest._TextTestResult):
 
92
    """Custom TestResult.
 
93
 
 
94
    Shows output in a different format, including displaying runtime for tests.
 
95
    """
 
96
 
 
97
    def _elapsedTime(self):
 
98
        return "%5dms" % (1000 * (time.time() - self._start_time))
 
99
 
 
100
    def startTest(self, test):
 
101
        unittest.TestResult.startTest(self, test)
 
102
        # In a short description, the important words are in
 
103
        # the beginning, but in an id, the important words are
 
104
        # at the end
 
105
        SHOW_DESCRIPTIONS = False
 
106
        if self.showAll:
 
107
            width = osutils.terminal_width()
 
108
            name_width = width - 15
 
109
            what = None
 
110
            if SHOW_DESCRIPTIONS:
 
111
                what = test.shortDescription()
 
112
                if what:
 
113
                    if len(what) > name_width:
 
114
                        what = what[:name_width-3] + '...'
 
115
            if what is None:
 
116
                what = test.id()
 
117
                if what.startswith('bzrlib.tests.'):
 
118
                    what = what[13:]
 
119
                if len(what) > name_width:
 
120
                    what = '...' + what[3-name_width:]
 
121
            what = what.ljust(name_width)
 
122
            self.stream.write(what)
 
123
        self.stream.flush()
 
124
        self._start_time = time.time()
 
125
 
 
126
    def addError(self, test, err):
 
127
        if isinstance(err[1], TestSkipped):
 
128
            return self.addSkipped(test, err)    
 
129
        unittest.TestResult.addError(self, test, err)
 
130
        if self.showAll:
 
131
            self.stream.writeln("ERROR %s" % self._elapsedTime())
 
132
        elif self.dots:
 
133
            self.stream.write('E')
 
134
        self.stream.flush()
 
135
 
 
136
    def addFailure(self, test, err):
 
137
        unittest.TestResult.addFailure(self, test, err)
 
138
        if self.showAll:
 
139
            self.stream.writeln(" FAIL %s" % self._elapsedTime())
 
140
        elif self.dots:
 
141
            self.stream.write('F')
 
142
        self.stream.flush()
 
143
 
 
144
    def addSuccess(self, test):
 
145
        if self.showAll:
 
146
            self.stream.writeln('   OK %s' % self._elapsedTime())
 
147
        elif self.dots:
 
148
            self.stream.write('~')
 
149
        self.stream.flush()
 
150
        unittest.TestResult.addSuccess(self, test)
 
151
 
 
152
    def addSkipped(self, test, skip_excinfo):
 
153
        if self.showAll:
 
154
            print >>self.stream, ' SKIP %s' % self._elapsedTime()
 
155
            print >>self.stream, '     %s' % skip_excinfo[1]
 
156
        elif self.dots:
 
157
            self.stream.write('S')
 
158
        self.stream.flush()
 
159
        # seems best to treat this as success from point-of-view of unittest
 
160
        # -- it actually does nothing so it barely matters :)
 
161
        unittest.TestResult.addSuccess(self, test)
 
162
 
 
163
    def printErrorList(self, flavour, errors):
 
164
        for test, err in errors:
 
165
            self.stream.writeln(self.separator1)
 
166
            self.stream.writeln("%s: %s" % (flavour,self.getDescription(test)))
 
167
            if hasattr(test, '_get_log'):
 
168
                print >>self.stream
 
169
                print >>self.stream, \
 
170
                        ('vvvv[log from %s]' % test).ljust(78,'-')
 
171
                print >>self.stream, test._get_log()
 
172
                print >>self.stream, \
 
173
                        ('^^^^[log from %s]' % test).ljust(78,'-')
 
174
            self.stream.writeln(self.separator2)
 
175
            self.stream.writeln("%s" % err)
 
176
 
 
177
 
 
178
class TextTestRunner(unittest.TextTestRunner):
 
179
    stop_on_failure = False
 
180
 
 
181
    def _makeResult(self):
 
182
        result = _MyResult(self.stream, self.descriptions, self.verbosity)
 
183
        if self.stop_on_failure:
 
184
            result = EarlyStoppingTestResultAdapter(result)
 
185
        return result
 
186
 
 
187
 
 
188
def iter_suite_tests(suite):
 
189
    """Return all tests in a suite, recursing through nested suites"""
 
190
    for item in suite._tests:
 
191
        if isinstance(item, unittest.TestCase):
 
192
            yield item
 
193
        elif isinstance(item, unittest.TestSuite):
 
194
            for r in iter_suite_tests(item):
 
195
                yield r
 
196
        else:
 
197
            raise Exception('unknown object %r inside test suite %r'
 
198
                            % (item, suite))
 
199
 
 
200
 
 
201
class TestSkipped(Exception):
 
202
    """Indicates that a test was intentionally skipped, rather than failing."""
 
203
    # XXX: Not used yet
 
204
 
 
205
 
 
206
class CommandFailed(Exception):
 
207
    pass
 
208
 
 
209
class TestCase(unittest.TestCase):
 
210
    """Base class for bzr unit tests.
 
211
    
 
212
    Tests that need access to disk resources should subclass 
 
213
    TestCaseInTempDir not TestCase.
 
214
 
 
215
    Error and debug log messages are redirected from their usual
 
216
    location into a temporary file, the contents of which can be
 
217
    retrieved by _get_log().  We use a real OS file, not an in-memory object,
 
218
    so that it can also capture file IO.  When the test completes this file
 
219
    is read into memory and removed from disk.
 
220
       
 
221
    There are also convenience functions to invoke bzr's command-line
 
222
    routine, and to build and check bzr trees.
 
223
   
 
224
    In addition to the usual method of overriding tearDown(), this class also
 
225
    allows subclasses to register functions into the _cleanups list, which is
 
226
    run in order as the object is torn down.  It's less likely this will be
 
227
    accidentally overlooked.
 
228
    """
 
229
 
 
230
    BZRPATH = 'bzr'
 
231
    _log_file_name = None
 
232
    _log_contents = ''
 
233
 
 
234
    def setUp(self):
 
235
        unittest.TestCase.setUp(self)
 
236
        self._cleanups = []
 
237
        self._cleanEnvironment()
 
238
        bzrlib.trace.disable_default_logging()
 
239
        self._startLogFile()
 
240
 
 
241
    def _ndiff_strings(self, a, b):
 
242
        """Return ndiff between two strings containing lines.
 
243
        
 
244
        A trailing newline is added if missing to make the strings
 
245
        print properly."""
 
246
        if b and b[-1] != '\n':
 
247
            b += '\n'
 
248
        if a and a[-1] != '\n':
 
249
            a += '\n'
 
250
        difflines = difflib.ndiff(a.splitlines(True),
 
251
                                  b.splitlines(True),
 
252
                                  linejunk=lambda x: False,
 
253
                                  charjunk=lambda x: False)
 
254
        return ''.join(difflines)
 
255
 
 
256
    def assertEqualDiff(self, a, b):
 
257
        """Assert two texts are equal, if not raise an exception.
 
258
        
 
259
        This is intended for use with multi-line strings where it can 
 
260
        be hard to find the differences by eye.
 
261
        """
 
262
        # TODO: perhaps override assertEquals to call this for strings?
 
263
        if a == b:
 
264
            return
 
265
        raise AssertionError("texts not equal:\n" + 
 
266
                             self._ndiff_strings(a, b))      
 
267
        
 
268
    def assertStartsWith(self, s, prefix):
 
269
        if not s.startswith(prefix):
 
270
            raise AssertionError('string %r does not start with %r' % (s, prefix))
 
271
 
 
272
    def assertEndsWith(self, s, suffix):
 
273
        if not s.endswith(prefix):
 
274
            raise AssertionError('string %r does not end with %r' % (s, suffix))
 
275
 
 
276
    def assertContainsRe(self, haystack, needle_re):
 
277
        """Assert that a contains something matching a regular expression."""
 
278
        if not re.search(needle_re, haystack):
 
279
            raise AssertionError('pattern "%s" not found in "%s"'
 
280
                    % (needle_re, haystack))
 
281
 
 
282
    def AssertSubset(self, sublist, superlist):
 
283
        """Assert that every entry in sublist is present in superlist."""
 
284
        missing = []
 
285
        for entry in sublist:
 
286
            if entry not in superlist:
 
287
                missing.append(entry)
 
288
        if len(missing) > 0:
 
289
            raise AssertionError("value(s) %r not present in container %r" % 
 
290
                                 (missing, superlist))
 
291
 
 
292
    def _startLogFile(self):
 
293
        """Send bzr and test log messages to a temporary file.
 
294
 
 
295
        The file is removed as the test is torn down.
 
296
        """
 
297
        fileno, name = tempfile.mkstemp(suffix='.log', prefix='testbzr')
 
298
        encoder, decoder, stream_reader, stream_writer = codecs.lookup('UTF-8')
 
299
        self._log_file = stream_writer(os.fdopen(fileno, 'w+'))
 
300
        bzrlib.trace.enable_test_log(self._log_file)
 
301
        self._log_file_name = name
 
302
        self.addCleanup(self._finishLogFile)
 
303
 
 
304
    def _finishLogFile(self):
 
305
        """Finished with the log file.
 
306
 
 
307
        Read contents into memory, close, and delete.
 
308
        """
 
309
        bzrlib.trace.disable_test_log()
 
310
        self._log_file.seek(0)
 
311
        self._log_contents = self._log_file.read()
 
312
        self._log_file.close()
 
313
        os.remove(self._log_file_name)
 
314
        self._log_file = self._log_file_name = None
 
315
 
 
316
    def addCleanup(self, callable):
 
317
        """Arrange to run a callable when this case is torn down.
 
318
 
 
319
        Callables are run in the reverse of the order they are registered, 
 
320
        ie last-in first-out.
 
321
        """
 
322
        if callable in self._cleanups:
 
323
            raise ValueError("cleanup function %r already registered on %s" 
 
324
                    % (callable, self))
 
325
        self._cleanups.append(callable)
 
326
 
 
327
    def _cleanEnvironment(self):
 
328
        new_env = {
 
329
            'HOME': os.getcwd(),
 
330
            'APPDATA': os.getcwd(),
 
331
            'BZREMAIL': None,
 
332
            'EMAIL': None,
 
333
        }
 
334
        self.__old_env = {}
 
335
        self.addCleanup(self._restoreEnvironment)
 
336
        for name, value in new_env.iteritems():
 
337
            self._captureVar(name, value)
 
338
 
 
339
 
 
340
    def _captureVar(self, name, newvalue):
 
341
        """Set an environment variable, preparing it to be reset when finished."""
 
342
        self.__old_env[name] = os.environ.get(name, None)
 
343
        if newvalue is None:
 
344
            if name in os.environ:
 
345
                del os.environ[name]
 
346
        else:
 
347
            os.environ[name] = newvalue
 
348
 
 
349
    @staticmethod
 
350
    def _restoreVar(name, value):
 
351
        if value is None:
 
352
            if name in os.environ:
 
353
                del os.environ[name]
 
354
        else:
 
355
            os.environ[name] = value
 
356
 
 
357
    def _restoreEnvironment(self):
 
358
        for name, value in self.__old_env.iteritems():
 
359
            self._restoreVar(name, value)
 
360
 
 
361
    def tearDown(self):
 
362
        self._runCleanups()
 
363
        unittest.TestCase.tearDown(self)
 
364
 
 
365
    def _runCleanups(self):
 
366
        """Run registered cleanup functions. 
 
367
 
 
368
        This should only be called from TestCase.tearDown.
 
369
        """
 
370
        for cleanup_fn in reversed(self._cleanups):
 
371
            cleanup_fn()
 
372
 
 
373
    def log(self, *args):
 
374
        mutter(*args)
 
375
 
 
376
    def _get_log(self):
 
377
        """Return as a string the log for this test"""
 
378
        if self._log_file_name:
 
379
            return open(self._log_file_name).read()
 
380
        else:
 
381
            return self._log_contents
 
382
        # TODO: Delete the log after it's been read in
 
383
 
 
384
    def capture(self, cmd, retcode=0):
 
385
        """Shortcut that splits cmd into words, runs, and returns stdout"""
 
386
        return self.run_bzr_captured(cmd.split(), retcode=retcode)[0]
 
387
 
 
388
    def run_bzr_captured(self, argv, retcode=0):
 
389
        """Invoke bzr and return (stdout, stderr).
 
390
 
 
391
        Useful for code that wants to check the contents of the
 
392
        output, the way error messages are presented, etc.
 
393
 
 
394
        This should be the main method for tests that want to exercise the
 
395
        overall behavior of the bzr application (rather than a unit test
 
396
        or a functional test of the library.)
 
397
 
 
398
        Much of the old code runs bzr by forking a new copy of Python, but
 
399
        that is slower, harder to debug, and generally not necessary.
 
400
 
 
401
        This runs bzr through the interface that catches and reports
 
402
        errors, and with logging set to something approximating the
 
403
        default, so that error reporting can be checked.
 
404
 
 
405
        argv -- arguments to invoke bzr
 
406
        retcode -- expected return code, or None for don't-care.
 
407
        """
 
408
        stdout = StringIO()
 
409
        stderr = StringIO()
 
410
        self.log('run bzr: %s', ' '.join(argv))
 
411
        # FIXME: don't call into logging here
 
412
        handler = logging.StreamHandler(stderr)
 
413
        handler.setFormatter(bzrlib.trace.QuietFormatter())
 
414
        handler.setLevel(logging.INFO)
 
415
        logger = logging.getLogger('')
 
416
        logger.addHandler(handler)
 
417
        try:
 
418
            result = self.apply_redirected(None, stdout, stderr,
 
419
                                           bzrlib.commands.run_bzr_catch_errors,
 
420
                                           argv)
 
421
        finally:
 
422
            logger.removeHandler(handler)
 
423
        out = stdout.getvalue()
 
424
        err = stderr.getvalue()
 
425
        if out:
 
426
            self.log('output:\n%s', out)
 
427
        if err:
 
428
            self.log('errors:\n%s', err)
 
429
        if retcode is not None:
 
430
            self.assertEquals(result, retcode)
 
431
        return out, err
 
432
 
 
433
    def run_bzr(self, *args, **kwargs):
 
434
        """Invoke bzr, as if it were run from the command line.
 
435
 
 
436
        This should be the main method for tests that want to exercise the
 
437
        overall behavior of the bzr application (rather than a unit test
 
438
        or a functional test of the library.)
 
439
 
 
440
        This sends the stdout/stderr results into the test's log,
 
441
        where it may be useful for debugging.  See also run_captured.
 
442
        """
 
443
        retcode = kwargs.pop('retcode', 0)
 
444
        return self.run_bzr_captured(args, retcode)
 
445
 
 
446
    def check_inventory_shape(self, inv, shape):
 
447
        """Compare an inventory to a list of expected names.
 
448
 
 
449
        Fail if they are not precisely equal.
 
450
        """
 
451
        extras = []
 
452
        shape = list(shape)             # copy
 
453
        for path, ie in inv.entries():
 
454
            name = path.replace('\\', '/')
 
455
            if ie.kind == 'dir':
 
456
                name = name + '/'
 
457
            if name in shape:
 
458
                shape.remove(name)
 
459
            else:
 
460
                extras.append(name)
 
461
        if shape:
 
462
            self.fail("expected paths not found in inventory: %r" % shape)
 
463
        if extras:
 
464
            self.fail("unexpected paths found in inventory: %r" % extras)
 
465
 
 
466
    def apply_redirected(self, stdin=None, stdout=None, stderr=None,
 
467
                         a_callable=None, *args, **kwargs):
 
468
        """Call callable with redirected std io pipes.
 
469
 
 
470
        Returns the return code."""
 
471
        if not callable(a_callable):
 
472
            raise ValueError("a_callable must be callable.")
 
473
        if stdin is None:
 
474
            stdin = StringIO("")
 
475
        if stdout is None:
 
476
            if hasattr(self, "_log_file"):
 
477
                stdout = self._log_file
 
478
            else:
 
479
                stdout = StringIO()
 
480
        if stderr is None:
 
481
            if hasattr(self, "_log_file"):
 
482
                stderr = self._log_file
 
483
            else:
 
484
                stderr = StringIO()
 
485
        real_stdin = sys.stdin
 
486
        real_stdout = sys.stdout
 
487
        real_stderr = sys.stderr
 
488
        try:
 
489
            sys.stdout = stdout
 
490
            sys.stderr = stderr
 
491
            sys.stdin = stdin
 
492
            return a_callable(*args, **kwargs)
 
493
        finally:
 
494
            sys.stdout = real_stdout
 
495
            sys.stderr = real_stderr
 
496
            sys.stdin = real_stdin
 
497
 
 
498
 
 
499
BzrTestBase = TestCase
 
500
 
 
501
     
 
502
class TestCaseInTempDir(TestCase):
 
503
    """Derived class that runs a test within a temporary directory.
 
504
 
 
505
    This is useful for tests that need to create a branch, etc.
 
506
 
 
507
    The directory is created in a slightly complex way: for each
 
508
    Python invocation, a new temporary top-level directory is created.
 
509
    All test cases create their own directory within that.  If the
 
510
    tests complete successfully, the directory is removed.
 
511
 
 
512
    InTempDir is an old alias for FunctionalTestCase.
 
513
    """
 
514
 
 
515
    TEST_ROOT = None
 
516
    _TEST_NAME = 'test'
 
517
    OVERRIDE_PYTHON = 'python'
 
518
 
 
519
    def check_file_contents(self, filename, expect):
 
520
        self.log("check contents of file %s" % filename)
 
521
        contents = file(filename, 'r').read()
 
522
        if contents != expect:
 
523
            self.log("expected: %r" % expect)
 
524
            self.log("actually: %r" % contents)
 
525
            self.fail("contents of %s not as expected" % filename)
 
526
 
 
527
    def _make_test_root(self):
 
528
        if TestCaseInTempDir.TEST_ROOT is not None:
 
529
            return
 
530
        i = 0
 
531
        while True:
 
532
            root = u'test%04d.tmp' % i
 
533
            try:
 
534
                os.mkdir(root)
 
535
            except OSError, e:
 
536
                if e.errno == errno.EEXIST:
 
537
                    i += 1
 
538
                    continue
 
539
                else:
 
540
                    raise
 
541
            # successfully created
 
542
            TestCaseInTempDir.TEST_ROOT = osutils.abspath(root)
 
543
            break
 
544
        # make a fake bzr directory there to prevent any tests propagating
 
545
        # up onto the source directory's real branch
 
546
        os.mkdir(osutils.pathjoin(TestCaseInTempDir.TEST_ROOT, '.bzr'))
 
547
 
 
548
    def setUp(self):
 
549
        super(TestCaseInTempDir, self).setUp()
 
550
        self._make_test_root()
 
551
        _currentdir = os.getcwdu()
 
552
        short_id = self.id().replace('bzrlib.tests.', '') \
 
553
                   .replace('__main__.', '')
 
554
        self.test_dir = osutils.pathjoin(self.TEST_ROOT, short_id)
 
555
        os.mkdir(self.test_dir)
 
556
        os.chdir(self.test_dir)
 
557
        os.environ['HOME'] = self.test_dir
 
558
        os.environ['APPDATA'] = self.test_dir
 
559
        def _leaveDirectory():
 
560
            os.chdir(_currentdir)
 
561
        self.addCleanup(_leaveDirectory)
 
562
        
 
563
    def build_tree(self, shape, line_endings='native'):
 
564
        """Build a test tree according to a pattern.
 
565
 
 
566
        shape is a sequence of file specifications.  If the final
 
567
        character is '/', a directory is created.
 
568
 
 
569
        This doesn't add anything to a branch.
 
570
        :param line_endings: Either 'binary' or 'native'
 
571
                             in binary mode, exact contents are written
 
572
                             in native mode, the line endings match the
 
573
                             default platform endings.
 
574
        """
 
575
        # XXX: It's OK to just create them using forward slashes on windows?
 
576
        for name in shape:
 
577
            self.assert_(isinstance(name, basestring))
 
578
            if name[-1] == '/':
 
579
                os.mkdir(name[:-1])
 
580
            else:
 
581
                if line_endings == 'binary':
 
582
                    f = file(name, 'wb')
 
583
                elif line_endings == 'native':
 
584
                    f = file(name, 'wt')
 
585
                else:
 
586
                    raise BzrError('Invalid line ending request %r' % (line_endings,))
 
587
                print >>f, "contents of", name
 
588
                f.close()
 
589
 
 
590
    def build_tree_contents(self, shape):
 
591
        build_tree_contents(shape)
 
592
 
 
593
    def failUnlessExists(self, path):
 
594
        """Fail unless path, which may be abs or relative, exists."""
 
595
        self.failUnless(osutils.lexists(path))
 
596
 
 
597
    def failIfExists(self, path):
 
598
        """Fail if path, which may be abs or relative, exists."""
 
599
        self.failIf(osutils.lexists(path))
 
600
        
 
601
    def assertFileEqual(self, content, path):
 
602
        """Fail if path does not contain 'content'."""
 
603
        self.failUnless(osutils.lexists(path))
 
604
        self.assertEqualDiff(content, open(path, 'r').read())
 
605
 
 
606
 
 
607
def filter_suite_by_re(suite, pattern):
 
608
    result = TestSuite()
 
609
    filter_re = re.compile(pattern)
 
610
    for test in iter_suite_tests(suite):
 
611
        if filter_re.search(test.id()):
 
612
            result.addTest(test)
 
613
    return result
 
614
 
 
615
 
 
616
def run_suite(suite, name='test', verbose=False, pattern=".*",
 
617
              stop_on_failure=False, keep_output=False):
 
618
    TestCaseInTempDir._TEST_NAME = name
 
619
    if verbose:
 
620
        verbosity = 2
 
621
    else:
 
622
        verbosity = 1
 
623
    runner = TextTestRunner(stream=sys.stdout,
 
624
                            descriptions=0,
 
625
                            verbosity=verbosity)
 
626
    runner.stop_on_failure=stop_on_failure
 
627
    if pattern != '.*':
 
628
        suite = filter_suite_by_re(suite, pattern)
 
629
    result = runner.run(suite)
 
630
    # This is still a little bogus, 
 
631
    # but only a little. Folk not using our testrunner will
 
632
    # have to delete their temp directories themselves.
 
633
    if result.wasSuccessful() or not keep_output:
 
634
        if TestCaseInTempDir.TEST_ROOT is not None:
 
635
            if sys.platform != "win32":
 
636
                shutil.rmtree(TestCaseInTempDir.TEST_ROOT)
 
637
            else:
 
638
                shutil.rmtree(TestCaseInTempDir.TEST_ROOT,
 
639
                              onerror=_win32_delete_readonly)
 
640
    else:
 
641
        print "Failed tests working directories are in '%s'\n" % TestCaseInTempDir.TEST_ROOT
 
642
    return result.wasSuccessful()
 
643
 
 
644
 
 
645
def selftest(verbose=False, pattern=".*", stop_on_failure=True,
 
646
             keep_output=False):
 
647
    """Run the whole test suite under the enhanced runner"""
 
648
    return run_suite(test_suite(), 'testbzr', verbose=verbose, pattern=pattern,
 
649
                     stop_on_failure=stop_on_failure, keep_output=keep_output)
 
650
 
 
651
 
 
652
def test_suite():
 
653
    """Build and return TestSuite for the whole program."""
 
654
    from doctest import DocTestSuite
 
655
 
 
656
    global MODULES_TO_DOCTEST
 
657
 
 
658
    testmod_names = [ \
 
659
                   'bzrlib.tests.test_ancestry',
 
660
                   'bzrlib.tests.test_annotate',
 
661
                   'bzrlib.tests.test_api',
 
662
                   'bzrlib.tests.test_bad_files',
 
663
                   'bzrlib.tests.test_basis_inventory',
 
664
                   'bzrlib.tests.test_branch',
 
665
                   'bzrlib.tests.test_command',
 
666
                   'bzrlib.tests.test_commit',
 
667
                   'bzrlib.tests.test_commit_merge',
 
668
                   'bzrlib.tests.test_config',
 
669
                   'bzrlib.tests.test_conflicts',
 
670
                   'bzrlib.tests.test_diff',
 
671
                   'bzrlib.tests.test_fetch',
 
672
                   'bzrlib.tests.test_gpg',
 
673
                   'bzrlib.tests.test_graph',
 
674
                   'bzrlib.tests.test_hashcache',
 
675
                   'bzrlib.tests.test_http',
 
676
                   'bzrlib.tests.test_identitymap',
 
677
                   'bzrlib.tests.test_inv',
 
678
                   'bzrlib.tests.test_log',
 
679
                   'bzrlib.tests.test_merge',
 
680
                   'bzrlib.tests.test_merge3',
 
681
                   'bzrlib.tests.test_merge_core',
 
682
                   'bzrlib.tests.test_missing',
 
683
                   'bzrlib.tests.test_msgeditor',
 
684
                   'bzrlib.tests.test_nonascii',
 
685
                   'bzrlib.tests.test_options',
 
686
                   'bzrlib.tests.test_osutils',
 
687
                   'bzrlib.tests.test_parent',
 
688
                   'bzrlib.tests.test_permissions',
 
689
                   'bzrlib.tests.test_plugins',
 
690
                   'bzrlib.tests.test_remove',
 
691
                   'bzrlib.tests.test_revision',
 
692
                   'bzrlib.tests.test_revisionnamespaces',
 
693
                   'bzrlib.tests.test_revprops',
 
694
                   'bzrlib.tests.test_reweave',
 
695
                   'bzrlib.tests.test_rio',
 
696
                   'bzrlib.tests.test_sampler',
 
697
                   'bzrlib.tests.test_selftest',
 
698
                   'bzrlib.tests.test_setup',
 
699
                   'bzrlib.tests.test_sftp_transport',
 
700
                   'bzrlib.tests.test_smart_add',
 
701
                   'bzrlib.tests.test_source',
 
702
                   'bzrlib.tests.test_status',
 
703
                   'bzrlib.tests.test_store',
 
704
                   'bzrlib.tests.test_symbol_versioning',
 
705
                   'bzrlib.tests.test_testament',
 
706
                   'bzrlib.tests.test_trace',
 
707
                   'bzrlib.tests.test_transactions',
 
708
                   'bzrlib.tests.test_transport',
 
709
                   'bzrlib.tests.test_tsort',
 
710
                   'bzrlib.tests.test_ui',
 
711
                   'bzrlib.tests.test_uncommit',
 
712
                   'bzrlib.tests.test_upgrade',
 
713
                   'bzrlib.tests.test_weave',
 
714
                   'bzrlib.tests.test_whitebox',
 
715
                   'bzrlib.tests.test_workingtree',
 
716
                   'bzrlib.tests.test_xml',
 
717
                   ]
 
718
 
 
719
    TestCase.BZRPATH = osutils.pathjoin(
 
720
            osutils.realpath(osutils.dirname(bzrlib.__path__[0])), 'bzr')
 
721
    print '%10s: %s' % ('bzr', osutils.realpath(sys.argv[0]))
 
722
    print '%10s: %s' % ('bzrlib', bzrlib.__path__[0])
 
723
    print
 
724
    suite = TestSuite()
 
725
    # python2.4's TestLoader.loadTestsFromNames gives very poor 
 
726
    # errors if it fails to load a named module - no indication of what's
 
727
    # actually wrong, just "no such module".  We should probably override that
 
728
    # class, but for the moment just load them ourselves. (mbp 20051202)
 
729
    loader = TestLoader()
 
730
    for mod_name in testmod_names:
 
731
        mod = _load_module_by_name(mod_name)
 
732
        suite.addTest(loader.loadTestsFromModule(mod))
 
733
    for package in packages_to_test():
 
734
        suite.addTest(package.test_suite())
 
735
    for m in MODULES_TO_TEST:
 
736
        suite.addTest(loader.loadTestsFromModule(m))
 
737
    for m in (MODULES_TO_DOCTEST):
 
738
        suite.addTest(DocTestSuite(m))
 
739
    for name, plugin in bzrlib.plugin.all_plugins().items():
 
740
        if hasattr(plugin, 'test_suite'):
 
741
            suite.addTest(plugin.test_suite())
 
742
    return suite
 
743
 
 
744
 
 
745
def _load_module_by_name(mod_name):
 
746
    parts = mod_name.split('.')
 
747
    module = __import__(mod_name)
 
748
    del parts[0]
 
749
    # for historical reasons python returns the top-level module even though
 
750
    # it loads the submodule; we need to walk down to get the one we want.
 
751
    while parts:
 
752
        module = getattr(module, parts.pop(0))
 
753
    return module
 
754
 
 
755
 
 
756
def _win32_delete_readonly(function, path, excinfo):
 
757
    """Error handler for shutil.rmtree function [for win32]
 
758
    Helps to remove files marked as read-only.
 
759
    """
 
760
    type_, value = excinfo[:2]
 
761
    if function in (os.remove, os.rmdir) \
 
762
        and type_ == OSError \
 
763
        and value.errno == errno.EACCES:
 
764
        bzrlib.osutils.make_writable(path)
 
765
        function(path)
 
766
    else:
 
767
        raise