~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/selftest/__init__.py

  • Committer: John Arbash Meinel
  • Date: 2005-09-19 03:26:14 UTC
  • mto: (1393.2.1)
  • mto: This revision was merged to the branch mainline in revision 1396.
  • Revision ID: john@arbash-meinel.com-20050919032614-79751b0510f24929
Major refactoring of testtransport.
Rewrote test_transport() as a class which can be subclassed which provides
many test_* functions, rather than one large function.
Much easier to debug.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005-2011 Canonical Ltd
2
 
#
 
1
# Copyright (C) 2005 by Canonical Ltd
 
2
 
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
5
5
# the Free Software Foundation; either version 2 of the License, or
6
6
# (at your option) any later version.
7
 
#
 
7
 
8
8
# This program is distributed in the hope that it will be useful,
9
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
11
# GNU General Public License for more details.
12
 
#
 
12
 
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
 
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
 
 
17
 
"""Testing framework extensions"""
18
 
 
19
 
# TODO: Perhaps there should be an API to find out if bzr running under the
20
 
# test suite -- some plugins might want to avoid making intrusive changes if
21
 
# this is the case.  However, we want behaviour under to test to diverge as
22
 
# little as possible, so this should be used rarely if it's added at all.
23
 
# (Suggestion from j-a-meinel, 2005-11-24)
24
 
 
25
 
# NOTE: Some classes in here use camelCaseNaming() rather than
26
 
# underscore_naming().  That's for consistency with unittest; it's not the
27
 
# general style of bzrlib.  Please continue that consistency when adding e.g.
28
 
# new assertFoo() methods.
29
 
 
30
 
import atexit
31
 
import codecs
32
 
import copy
33
 
from cStringIO import StringIO
34
 
import difflib
35
 
import doctest
36
 
import errno
37
 
import itertools
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
16
 
 
17
 
38
18
import logging
 
19
import unittest
 
20
import tempfile
39
21
import os
40
 
import platform
41
 
import pprint
42
 
import random
43
 
import re
44
 
import shlex
45
 
import stat
46
 
import subprocess
47
22
import sys
48
 
import tempfile
49
 
import threading
50
 
import time
51
 
import traceback
52
 
import unittest
53
 
import warnings
54
 
 
55
 
import testtools
56
 
# nb: check this before importing anything else from within it
57
 
_testtools_version = getattr(testtools, '__version__', ())
58
 
if _testtools_version < (0, 9, 5):
59
 
    raise ImportError("need at least testtools 0.9.5: %s is %r"
60
 
        % (testtools.__file__, _testtools_version))
61
 
from testtools import content
62
 
 
63
 
import bzrlib
64
 
from bzrlib import (
65
 
    branchbuilder,
66
 
    bzrdir,
67
 
    chk_map,
68
 
    commands as _mod_commands,
69
 
    config,
70
 
    debug,
71
 
    errors,
72
 
    hooks,
73
 
    lock as _mod_lock,
74
 
    lockdir,
75
 
    memorytree,
76
 
    osutils,
77
 
    plugin as _mod_plugin,
78
 
    pyutils,
79
 
    ui,
80
 
    urlutils,
81
 
    registry,
82
 
    symbol_versioning,
83
 
    trace,
84
 
    transport as _mod_transport,
85
 
    workingtree,
86
 
    )
87
 
try:
88
 
    import bzrlib.lsprof
89
 
except ImportError:
90
 
    # lsprof not available
 
23
import errno
 
24
from warnings import warn
 
25
from cStringIO import StringIO
 
26
 
 
27
import testsweet
 
28
import bzrlib.commands
 
29
 
 
30
import bzrlib.trace
 
31
import bzrlib.fetch
 
32
 
 
33
 
 
34
MODULES_TO_TEST = []
 
35
MODULES_TO_DOCTEST = []
 
36
 
 
37
from logging import debug, warning, error
 
38
 
 
39
class CommandFailed(Exception):
91
40
    pass
92
 
from bzrlib.smart import client, request
93
 
from bzrlib.transport import (
94
 
    memory,
95
 
    pathfilter,
96
 
    )
97
 
from bzrlib.tests import (
98
 
    test_server,
99
 
    TestUtil,
100
 
    treeshape,
101
 
    )
102
 
from bzrlib.ui import NullProgressView
103
 
from bzrlib.ui.text import TextUIFactory
104
 
 
105
 
# Mark this python module as being part of the implementation
106
 
# of unittest: this gives us better tracebacks where the last
107
 
# shown frame is the test code, not our assertXYZ.
108
 
__unittest = 1
109
 
 
110
 
default_transport = test_server.LocalURLServer
111
 
 
112
 
 
113
 
_unitialized_attr = object()
114
 
"""A sentinel needed to act as a default value in a method signature."""
115
 
 
116
 
 
117
 
# Subunit result codes, defined here to prevent a hard dependency on subunit.
118
 
SUBUNIT_SEEK_SET = 0
119
 
SUBUNIT_SEEK_CUR = 1
120
 
 
121
 
# These are intentionally brought into this namespace. That way plugins, etc
122
 
# can just "from bzrlib.tests import TestCase, TestLoader, etc"
123
 
TestSuite = TestUtil.TestSuite
124
 
TestLoader = TestUtil.TestLoader
125
 
 
126
 
# Tests should run in a clean and clearly defined environment. The goal is to
127
 
# keep them isolated from the running environment as mush as possible. The test
128
 
# framework ensures the variables defined below are set (or deleted if the
129
 
# value is None) before a test is run and reset to their original value after
130
 
# the test is run. Generally if some code depends on an environment variable,
131
 
# the tests should start without this variable in the environment. There are a
132
 
# few exceptions but you shouldn't violate this rule lightly.
133
 
isolated_environ = {
134
 
    'BZR_HOME': None,
135
 
    'HOME': None,
136
 
    # bzr now uses the Win32 API and doesn't rely on APPDATA, but the
137
 
    # tests do check our impls match APPDATA
138
 
    'BZR_EDITOR': None, # test_msgeditor manipulates this variable
139
 
    'VISUAL': None,
140
 
    'EDITOR': None,
141
 
    'BZR_EMAIL': None,
142
 
    'BZREMAIL': None, # may still be present in the environment
143
 
    'EMAIL': 'jrandom@example.com', # set EMAIL as bzr does not guess
144
 
    'BZR_PROGRESS_BAR': None,
145
 
    # This should trap leaks to ~/.bzr.log. This occurs when tests use TestCase
146
 
    # as a base class instead of TestCaseInTempDir. Tests inheriting from
147
 
    # TestCase should not use disk resources, BZR_LOG is one.
148
 
    'BZR_LOG': '/you-should-use-TestCaseInTempDir-if-you-need-a-log-file',
149
 
    'BZR_PLUGIN_PATH': None,
150
 
    'BZR_DISABLE_PLUGINS': None,
151
 
    'BZR_PLUGINS_AT': None,
152
 
    'BZR_CONCURRENCY': None,
153
 
    # Make sure that any text ui tests are consistent regardless of
154
 
    # the environment the test case is run in; you may want tests that
155
 
    # test other combinations.  'dumb' is a reasonable guess for tests
156
 
    # going to a pipe or a StringIO.
157
 
    'TERM': 'dumb',
158
 
    'LINES': '25',
159
 
    'COLUMNS': '80',
160
 
    'BZR_COLUMNS': '80',
161
 
    # Disable SSH Agent
162
 
    'SSH_AUTH_SOCK': None,
163
 
    # Proxies
164
 
    'http_proxy': None,
165
 
    'HTTP_PROXY': None,
166
 
    'https_proxy': None,
167
 
    'HTTPS_PROXY': None,
168
 
    'no_proxy': None,
169
 
    'NO_PROXY': None,
170
 
    'all_proxy': None,
171
 
    'ALL_PROXY': None,
172
 
    # Nobody cares about ftp_proxy, FTP_PROXY AFAIK. So far at
173
 
    # least. If you do (care), please update this comment
174
 
    # -- vila 20080401
175
 
    'ftp_proxy': None,
176
 
    'FTP_PROXY': None,
177
 
    'BZR_REMOTE_PATH': None,
178
 
    # Generally speaking, we don't want apport reporting on crashes in
179
 
    # the test envirnoment unless we're specifically testing apport,
180
 
    # so that it doesn't leak into the real system environment.  We
181
 
    # use an env var so it propagates to subprocesses.
182
 
    'APPORT_DISABLE': '1',
183
 
    }
184
 
 
185
 
 
186
 
def override_os_environ(test, env=None):
187
 
    """Modify os.environ keeping a copy.
188
 
    
189
 
    :param test: A test instance
190
 
 
191
 
    :param env: A dict containing variable definitions to be installed
192
 
    """
193
 
    if env is None:
194
 
        env = isolated_environ
195
 
    test._original_os_environ = dict([(var, value)
196
 
                                      for var, value in os.environ.iteritems()])
197
 
    for var, value in env.iteritems():
198
 
        osutils.set_or_unset_env(var, value)
199
 
        if var not in test._original_os_environ:
200
 
            # The var is new, add it with a value of None, so
201
 
            # restore_os_environ will delete it
202
 
            test._original_os_environ[var] = None
203
 
 
204
 
 
205
 
def restore_os_environ(test):
206
 
    """Restore os.environ to its original state.
207
 
 
208
 
    :param test: A test instance previously passed to override_os_environ.
209
 
    """
210
 
    for var, value in test._original_os_environ.iteritems():
211
 
        # Restore the original value (or delete it if the value has been set to
212
 
        # None in override_os_environ).
213
 
        osutils.set_or_unset_env(var, value)
214
 
 
215
 
 
216
 
class ExtendedTestResult(testtools.TextTestResult):
217
 
    """Accepts, reports and accumulates the results of running tests.
218
 
 
219
 
    Compared to the unittest version this class adds support for
220
 
    profiling, benchmarking, stopping as soon as a test fails,  and
221
 
    skipping tests.  There are further-specialized subclasses for
222
 
    different types of display.
223
 
 
224
 
    When a test finishes, in whatever way, it calls one of the addSuccess,
225
 
    addFailure or addError classes.  These in turn may redirect to a more
226
 
    specific case for the special test results supported by our extended
227
 
    tests.
228
 
 
229
 
    Note that just one of these objects is fed the results from many tests.
230
 
    """
231
 
 
232
 
    stop_early = False
233
 
 
234
 
    def __init__(self, stream, descriptions, verbosity,
235
 
                 bench_history=None,
236
 
                 strict=False,
237
 
                 ):
238
 
        """Construct new TestResult.
239
 
 
240
 
        :param bench_history: Optionally, a writable file object to accumulate
241
 
            benchmark results.
242
 
        """
243
 
        testtools.TextTestResult.__init__(self, stream)
244
 
        if bench_history is not None:
245
 
            from bzrlib.version import _get_bzr_source_tree
246
 
            src_tree = _get_bzr_source_tree()
247
 
            if src_tree:
248
 
                try:
249
 
                    revision_id = src_tree.get_parent_ids()[0]
250
 
                except IndexError:
251
 
                    # XXX: if this is a brand new tree, do the same as if there
252
 
                    # is no branch.
253
 
                    revision_id = ''
254
 
            else:
255
 
                # XXX: If there's no branch, what should we do?
256
 
                revision_id = ''
257
 
            bench_history.write("--date %s %s\n" % (time.time(), revision_id))
258
 
        self._bench_history = bench_history
259
 
        self.ui = ui.ui_factory
260
 
        self.num_tests = 0
261
 
        self.error_count = 0
262
 
        self.failure_count = 0
263
 
        self.known_failure_count = 0
264
 
        self.skip_count = 0
265
 
        self.not_applicable_count = 0
266
 
        self.unsupported = {}
267
 
        self.count = 0
268
 
        self._overall_start_time = time.time()
269
 
        self._strict = strict
270
 
        self._first_thread_leaker_id = None
271
 
        self._tests_leaking_threads_count = 0
272
 
        self._traceback_from_test = None
273
 
 
274
 
    def stopTestRun(self):
275
 
        run = self.testsRun
276
 
        actionTaken = "Ran"
277
 
        stopTime = time.time()
278
 
        timeTaken = stopTime - self.startTime
279
 
        # GZ 2010-07-19: Seems testtools has no printErrors method, and though
280
 
        #                the parent class method is similar have to duplicate
281
 
        self._show_list('ERROR', self.errors)
282
 
        self._show_list('FAIL', self.failures)
283
 
        self.stream.write(self.sep2)
284
 
        self.stream.write("%s %d test%s in %.3fs\n\n" % (actionTaken,
285
 
                            run, run != 1 and "s" or "", timeTaken))
286
 
        if not self.wasSuccessful():
287
 
            self.stream.write("FAILED (")
288
 
            failed, errored = map(len, (self.failures, self.errors))
289
 
            if failed:
290
 
                self.stream.write("failures=%d" % failed)
291
 
            if errored:
292
 
                if failed: self.stream.write(", ")
293
 
                self.stream.write("errors=%d" % errored)
294
 
            if self.known_failure_count:
295
 
                if failed or errored: self.stream.write(", ")
296
 
                self.stream.write("known_failure_count=%d" %
297
 
                    self.known_failure_count)
298
 
            self.stream.write(")\n")
299
 
        else:
300
 
            if self.known_failure_count:
301
 
                self.stream.write("OK (known_failures=%d)\n" %
302
 
                    self.known_failure_count)
303
 
            else:
304
 
                self.stream.write("OK\n")
305
 
        if self.skip_count > 0:
306
 
            skipped = self.skip_count
307
 
            self.stream.write('%d test%s skipped\n' %
308
 
                                (skipped, skipped != 1 and "s" or ""))
309
 
        if self.unsupported:
310
 
            for feature, count in sorted(self.unsupported.items()):
311
 
                self.stream.write("Missing feature '%s' skipped %d tests.\n" %
312
 
                    (feature, count))
313
 
        if self._strict:
314
 
            ok = self.wasStrictlySuccessful()
315
 
        else:
316
 
            ok = self.wasSuccessful()
317
 
        if self._first_thread_leaker_id:
318
 
            self.stream.write(
319
 
                '%s is leaking threads among %d leaking tests.\n' % (
320
 
                self._first_thread_leaker_id,
321
 
                self._tests_leaking_threads_count))
322
 
            # We don't report the main thread as an active one.
323
 
            self.stream.write(
324
 
                '%d non-main threads were left active in the end.\n'
325
 
                % (len(self._active_threads) - 1))
326
 
 
327
 
    def getDescription(self, test):
328
 
        return test.id()
329
 
 
330
 
    def _extractBenchmarkTime(self, testCase, details=None):
331
 
        """Add a benchmark time for the current test case."""
332
 
        if details and 'benchtime' in details:
333
 
            return float(''.join(details['benchtime'].iter_bytes()))
334
 
        return getattr(testCase, "_benchtime", None)
335
 
 
336
 
    def _elapsedTestTimeString(self):
337
 
        """Return a time string for the overall time the current test has taken."""
338
 
        return self._formatTime(self._delta_to_float(
339
 
            self._now() - self._start_datetime))
340
 
 
341
 
    def _testTimeString(self, testCase):
342
 
        benchmark_time = self._extractBenchmarkTime(testCase)
343
 
        if benchmark_time is not None:
344
 
            return self._formatTime(benchmark_time) + "*"
345
 
        else:
346
 
            return self._elapsedTestTimeString()
347
 
 
348
 
    def _formatTime(self, seconds):
349
 
        """Format seconds as milliseconds with leading spaces."""
350
 
        # some benchmarks can take thousands of seconds to run, so we need 8
351
 
        # places
352
 
        return "%8dms" % (1000 * seconds)
353
 
 
354
 
    def _shortened_test_description(self, test):
355
 
        what = test.id()
356
 
        what = re.sub(r'^bzrlib\.tests\.', '', what)
357
 
        return what
358
 
 
359
 
    # GZ 2010-10-04: Cloned tests may end up harmlessly calling this method
360
 
    #                multiple times in a row, because the handler is added for
361
 
    #                each test but the container list is shared between cases.
362
 
    #                See lp:498869 lp:625574 and lp:637725 for background.
363
 
    def _record_traceback_from_test(self, exc_info):
364
 
        """Store the traceback from passed exc_info tuple till"""
365
 
        self._traceback_from_test = exc_info[2]
366
 
 
367
 
    def startTest(self, test):
368
 
        super(ExtendedTestResult, self).startTest(test)
369
 
        if self.count == 0:
370
 
            self.startTests()
371
 
        self.count += 1
372
 
        self.report_test_start(test)
373
 
        test.number = self.count
374
 
        self._recordTestStartTime()
375
 
        # Make testtools cases give us the real traceback on failure
376
 
        addOnException = getattr(test, "addOnException", None)
377
 
        if addOnException is not None:
378
 
            addOnException(self._record_traceback_from_test)
379
 
        # Only check for thread leaks on bzrlib derived test cases
380
 
        if isinstance(test, TestCase):
381
 
            test.addCleanup(self._check_leaked_threads, test)
382
 
 
383
 
    def stopTest(self, test):
384
 
        super(ExtendedTestResult, self).stopTest(test)
385
 
        # Manually break cycles, means touching various private things but hey
386
 
        getDetails = getattr(test, "getDetails", None)
387
 
        if getDetails is not None:
388
 
            getDetails().clear()
389
 
        type_equality_funcs = getattr(test, "_type_equality_funcs", None)
390
 
        if type_equality_funcs is not None:
391
 
            type_equality_funcs.clear()
392
 
        self._traceback_from_test = None
393
 
 
394
 
    def startTests(self):
395
 
        self.report_tests_starting()
396
 
        self._active_threads = threading.enumerate()
397
 
 
398
 
    def _check_leaked_threads(self, test):
399
 
        """See if any threads have leaked since last call
400
 
 
401
 
        A sample of live threads is stored in the _active_threads attribute,
402
 
        when this method runs it compares the current live threads and any not
403
 
        in the previous sample are treated as having leaked.
404
 
        """
405
 
        now_active_threads = set(threading.enumerate())
406
 
        threads_leaked = now_active_threads.difference(self._active_threads)
407
 
        if threads_leaked:
408
 
            self._report_thread_leak(test, threads_leaked, now_active_threads)
409
 
            self._tests_leaking_threads_count += 1
410
 
            if self._first_thread_leaker_id is None:
411
 
                self._first_thread_leaker_id = test.id()
412
 
            self._active_threads = now_active_threads
413
 
 
414
 
    def _recordTestStartTime(self):
415
 
        """Record that a test has started."""
416
 
        self._start_datetime = self._now()
417
 
 
418
 
    def addError(self, test, err):
419
 
        """Tell result that test finished with an error.
420
 
 
421
 
        Called from the TestCase run() method when the test
422
 
        fails with an unexpected error.
423
 
        """
424
 
        self._post_mortem(self._traceback_from_test)
425
 
        super(ExtendedTestResult, self).addError(test, err)
426
 
        self.error_count += 1
427
 
        self.report_error(test, err)
428
 
        if self.stop_early:
429
 
            self.stop()
430
 
 
431
 
    def addFailure(self, test, err):
432
 
        """Tell result that test failed.
433
 
 
434
 
        Called from the TestCase run() method when the test
435
 
        fails because e.g. an assert() method failed.
436
 
        """
437
 
        self._post_mortem(self._traceback_from_test)
438
 
        super(ExtendedTestResult, self).addFailure(test, err)
439
 
        self.failure_count += 1
440
 
        self.report_failure(test, err)
441
 
        if self.stop_early:
442
 
            self.stop()
443
 
 
444
 
    def addSuccess(self, test, details=None):
445
 
        """Tell result that test completed successfully.
446
 
 
447
 
        Called from the TestCase run()
448
 
        """
449
 
        if self._bench_history is not None:
450
 
            benchmark_time = self._extractBenchmarkTime(test, details)
451
 
            if benchmark_time is not None:
452
 
                self._bench_history.write("%s %s\n" % (
453
 
                    self._formatTime(benchmark_time),
454
 
                    test.id()))
455
 
        self.report_success(test)
456
 
        super(ExtendedTestResult, self).addSuccess(test)
457
 
        test._log_contents = ''
458
 
 
459
 
    def addExpectedFailure(self, test, err):
460
 
        self.known_failure_count += 1
461
 
        self.report_known_failure(test, err)
462
 
 
463
 
    def addUnexpectedSuccess(self, test, details=None):
464
 
        """Tell result the test unexpectedly passed, counting as a failure
465
 
 
466
 
        When the minimum version of testtools required becomes 0.9.8 this
467
 
        can be updated to use the new handling there.
468
 
        """
469
 
        super(ExtendedTestResult, self).addFailure(test, details=details)
470
 
        self.failure_count += 1
471
 
        self.report_unexpected_success(test,
472
 
            "".join(details["reason"].iter_text()))
473
 
        if self.stop_early:
474
 
            self.stop()
475
 
 
476
 
    def addNotSupported(self, test, feature):
477
 
        """The test will not be run because of a missing feature.
478
 
        """
479
 
        # this can be called in two different ways: it may be that the
480
 
        # test started running, and then raised (through requireFeature)
481
 
        # UnavailableFeature.  Alternatively this method can be called
482
 
        # while probing for features before running the test code proper; in
483
 
        # that case we will see startTest and stopTest, but the test will
484
 
        # never actually run.
485
 
        self.unsupported.setdefault(str(feature), 0)
486
 
        self.unsupported[str(feature)] += 1
487
 
        self.report_unsupported(test, feature)
488
 
 
489
 
    def addSkip(self, test, reason):
490
 
        """A test has not run for 'reason'."""
491
 
        self.skip_count += 1
492
 
        self.report_skip(test, reason)
493
 
 
494
 
    def addNotApplicable(self, test, reason):
495
 
        self.not_applicable_count += 1
496
 
        self.report_not_applicable(test, reason)
497
 
 
498
 
    def _post_mortem(self, tb=None):
499
 
        """Start a PDB post mortem session."""
500
 
        if os.environ.get('BZR_TEST_PDB', None):
501
 
            import pdb
502
 
            pdb.post_mortem(tb)
503
 
 
504
 
    def progress(self, offset, whence):
505
 
        """The test is adjusting the count of tests to run."""
506
 
        if whence == SUBUNIT_SEEK_SET:
507
 
            self.num_tests = offset
508
 
        elif whence == SUBUNIT_SEEK_CUR:
509
 
            self.num_tests += offset
510
 
        else:
511
 
            raise errors.BzrError("Unknown whence %r" % whence)
512
 
 
513
 
    def report_tests_starting(self):
514
 
        """Display information before the test run begins"""
515
 
        if getattr(sys, 'frozen', None) is None:
516
 
            bzr_path = osutils.realpath(sys.argv[0])
517
 
        else:
518
 
            bzr_path = sys.executable
519
 
        self.stream.write(
520
 
            'bzr selftest: %s\n' % (bzr_path,))
521
 
        self.stream.write(
522
 
            '   %s\n' % (
523
 
                    bzrlib.__path__[0],))
524
 
        self.stream.write(
525
 
            '   bzr-%s python-%s %s\n' % (
526
 
                    bzrlib.version_string,
527
 
                    bzrlib._format_version_tuple(sys.version_info),
528
 
                    platform.platform(aliased=1),
529
 
                    ))
530
 
        self.stream.write('\n')
531
 
 
532
 
    def report_test_start(self, test):
533
 
        """Display information on the test just about to be run"""
534
 
 
535
 
    def _report_thread_leak(self, test, leaked_threads, active_threads):
536
 
        """Display information on a test that leaked one or more threads"""
537
 
        # GZ 2010-09-09: A leak summary reported separately from the general
538
 
        #                thread debugging would be nice. Tests under subunit
539
 
        #                need something not using stream, perhaps adding a
540
 
        #                testtools details object would be fitting.
541
 
        if 'threads' in selftest_debug_flags:
542
 
            self.stream.write('%s is leaking, active is now %d\n' %
543
 
                (test.id(), len(active_threads)))
544
 
 
545
 
    def startTestRun(self):
546
 
        self.startTime = time.time()
547
 
 
548
 
    def report_success(self, test):
549
 
        pass
550
 
 
551
 
    def wasStrictlySuccessful(self):
552
 
        if self.unsupported or self.known_failure_count:
553
 
            return False
554
 
        return self.wasSuccessful()
555
 
 
556
 
 
557
 
class TextTestResult(ExtendedTestResult):
558
 
    """Displays progress and results of tests in text form"""
559
 
 
560
 
    def __init__(self, stream, descriptions, verbosity,
561
 
                 bench_history=None,
562
 
                 pb=None,
563
 
                 strict=None,
564
 
                 ):
565
 
        ExtendedTestResult.__init__(self, stream, descriptions, verbosity,
566
 
            bench_history, strict)
567
 
        # We no longer pass them around, but just rely on the UIFactory stack
568
 
        # for state
569
 
        if pb is not None:
570
 
            warnings.warn("Passing pb to TextTestResult is deprecated")
571
 
        self.pb = self.ui.nested_progress_bar()
572
 
        self.pb.show_pct = False
573
 
        self.pb.show_spinner = False
574
 
        self.pb.show_eta = False,
575
 
        self.pb.show_count = False
576
 
        self.pb.show_bar = False
577
 
        self.pb.update_latency = 0
578
 
        self.pb.show_transport_activity = False
579
 
 
580
 
    def stopTestRun(self):
581
 
        # called when the tests that are going to run have run
582
 
        self.pb.clear()
583
 
        self.pb.finished()
584
 
        super(TextTestResult, self).stopTestRun()
585
 
 
586
 
    def report_tests_starting(self):
587
 
        super(TextTestResult, self).report_tests_starting()
588
 
        self.pb.update('[test 0/%d] Starting' % (self.num_tests))
589
 
 
590
 
    def _progress_prefix_text(self):
591
 
        # the longer this text, the less space we have to show the test
592
 
        # name...
593
 
        a = '[%d' % self.count              # total that have been run
594
 
        # tests skipped as known not to be relevant are not important enough
595
 
        # to show here
596
 
        ## if self.skip_count:
597
 
        ##     a += ', %d skip' % self.skip_count
598
 
        ## if self.known_failure_count:
599
 
        ##     a += '+%dX' % self.known_failure_count
600
 
        if self.num_tests:
601
 
            a +='/%d' % self.num_tests
602
 
        a += ' in '
603
 
        runtime = time.time() - self._overall_start_time
604
 
        if runtime >= 60:
605
 
            a += '%dm%ds' % (runtime / 60, runtime % 60)
606
 
        else:
607
 
            a += '%ds' % runtime
608
 
        total_fail_count = self.error_count + self.failure_count
609
 
        if total_fail_count:
610
 
            a += ', %d failed' % total_fail_count
611
 
        # if self.unsupported:
612
 
        #     a += ', %d missing' % len(self.unsupported)
613
 
        a += ']'
614
 
        return a
615
 
 
616
 
    def report_test_start(self, test):
617
 
        self.pb.update(
618
 
                self._progress_prefix_text()
619
 
                + ' '
620
 
                + self._shortened_test_description(test))
621
 
 
622
 
    def _test_description(self, test):
623
 
        return self._shortened_test_description(test)
624
 
 
625
 
    def report_error(self, test, err):
626
 
        self.stream.write('ERROR: %s\n    %s\n' % (
627
 
            self._test_description(test),
628
 
            err[1],
629
 
            ))
630
 
 
631
 
    def report_failure(self, test, err):
632
 
        self.stream.write('FAIL: %s\n    %s\n' % (
633
 
            self._test_description(test),
634
 
            err[1],
635
 
            ))
636
 
 
637
 
    def report_known_failure(self, test, err):
638
 
        pass
639
 
 
640
 
    def report_unexpected_success(self, test, reason):
641
 
        self.stream.write('FAIL: %s\n    %s: %s\n' % (
642
 
            self._test_description(test),
643
 
            "Unexpected success. Should have failed",
644
 
            reason,
645
 
            ))
646
 
 
647
 
    def report_skip(self, test, reason):
648
 
        pass
649
 
 
650
 
    def report_not_applicable(self, test, reason):
651
 
        pass
652
 
 
653
 
    def report_unsupported(self, test, feature):
654
 
        """test cannot be run because feature is missing."""
655
 
 
656
 
 
657
 
class VerboseTestResult(ExtendedTestResult):
658
 
    """Produce long output, with one line per test run plus times"""
659
 
 
660
 
    def _ellipsize_to_right(self, a_string, final_width):
661
 
        """Truncate and pad a string, keeping the right hand side"""
662
 
        if len(a_string) > final_width:
663
 
            result = '...' + a_string[3-final_width:]
664
 
        else:
665
 
            result = a_string
666
 
        return result.ljust(final_width)
667
 
 
668
 
    def report_tests_starting(self):
669
 
        self.stream.write('running %d tests...\n' % self.num_tests)
670
 
        super(VerboseTestResult, self).report_tests_starting()
671
 
 
672
 
    def report_test_start(self, test):
673
 
        name = self._shortened_test_description(test)
674
 
        width = osutils.terminal_width()
675
 
        if width is not None:
676
 
            # width needs space for 6 char status, plus 1 for slash, plus an
677
 
            # 11-char time string, plus a trailing blank
678
 
            # when NUMBERED_DIRS: plus 5 chars on test number, plus 1 char on
679
 
            # space
680
 
            self.stream.write(self._ellipsize_to_right(name, width-18))
681
 
        else:
682
 
            self.stream.write(name)
683
 
        self.stream.flush()
684
 
 
685
 
    def _error_summary(self, err):
686
 
        indent = ' ' * 4
687
 
        return '%s%s' % (indent, err[1])
688
 
 
689
 
    def report_error(self, test, err):
690
 
        self.stream.write('ERROR %s\n%s\n'
691
 
                % (self._testTimeString(test),
692
 
                   self._error_summary(err)))
693
 
 
694
 
    def report_failure(self, test, err):
695
 
        self.stream.write(' FAIL %s\n%s\n'
696
 
                % (self._testTimeString(test),
697
 
                   self._error_summary(err)))
698
 
 
699
 
    def report_known_failure(self, test, err):
700
 
        self.stream.write('XFAIL %s\n%s\n'
701
 
                % (self._testTimeString(test),
702
 
                   self._error_summary(err)))
703
 
 
704
 
    def report_unexpected_success(self, test, reason):
705
 
        self.stream.write(' FAIL %s\n%s: %s\n'
706
 
                % (self._testTimeString(test),
707
 
                   "Unexpected success. Should have failed",
708
 
                   reason))
709
 
 
710
 
    def report_success(self, test):
711
 
        self.stream.write('   OK %s\n' % self._testTimeString(test))
712
 
        for bench_called, stats in getattr(test, '_benchcalls', []):
713
 
            self.stream.write('LSProf output for %s(%s, %s)\n' % bench_called)
714
 
            stats.pprint(file=self.stream)
715
 
        # flush the stream so that we get smooth output. This verbose mode is
716
 
        # used to show the output in PQM.
717
 
        self.stream.flush()
718
 
 
719
 
    def report_skip(self, test, reason):
720
 
        self.stream.write(' SKIP %s\n%s\n'
721
 
                % (self._testTimeString(test), reason))
722
 
 
723
 
    def report_not_applicable(self, test, reason):
724
 
        self.stream.write('  N/A %s\n    %s\n'
725
 
                % (self._testTimeString(test), reason))
726
 
 
727
 
    def report_unsupported(self, test, feature):
728
 
        """test cannot be run because feature is missing."""
729
 
        self.stream.write("NODEP %s\n    The feature '%s' is not available.\n"
730
 
                %(self._testTimeString(test), feature))
731
 
 
732
 
 
733
 
class TextTestRunner(object):
734
 
    stop_on_failure = False
735
 
 
736
 
    def __init__(self,
737
 
                 stream=sys.stderr,
738
 
                 descriptions=0,
739
 
                 verbosity=1,
740
 
                 bench_history=None,
741
 
                 strict=False,
742
 
                 result_decorators=None,
743
 
                 ):
744
 
        """Create a TextTestRunner.
745
 
 
746
 
        :param result_decorators: An optional list of decorators to apply
747
 
            to the result object being used by the runner. Decorators are
748
 
            applied left to right - the first element in the list is the 
749
 
            innermost decorator.
750
 
        """
751
 
        # stream may know claim to know to write unicode strings, but in older
752
 
        # pythons this goes sufficiently wrong that it is a bad idea. (
753
 
        # specifically a built in file with encoding 'UTF-8' will still try
754
 
        # to encode using ascii.
755
 
        new_encoding = osutils.get_terminal_encoding()
756
 
        codec = codecs.lookup(new_encoding)
757
 
        if type(codec) is tuple:
758
 
            # Python 2.4
759
 
            encode = codec[0]
760
 
        else:
761
 
            encode = codec.encode
762
 
        # GZ 2010-09-08: Really we don't want to be writing arbitrary bytes,
763
 
        #                so should swap to the plain codecs.StreamWriter
764
 
        stream = osutils.UnicodeOrBytesToBytesWriter(encode, stream,
765
 
            "backslashreplace")
766
 
        stream.encoding = new_encoding
767
 
        self.stream = stream
768
 
        self.descriptions = descriptions
769
 
        self.verbosity = verbosity
770
 
        self._bench_history = bench_history
771
 
        self._strict = strict
772
 
        self._result_decorators = result_decorators or []
773
 
 
774
 
    def run(self, test):
775
 
        "Run the given test case or test suite."
776
 
        if self.verbosity == 1:
777
 
            result_class = TextTestResult
778
 
        elif self.verbosity >= 2:
779
 
            result_class = VerboseTestResult
780
 
        original_result = result_class(self.stream,
781
 
                              self.descriptions,
782
 
                              self.verbosity,
783
 
                              bench_history=self._bench_history,
784
 
                              strict=self._strict,
785
 
                              )
786
 
        # Signal to result objects that look at stop early policy to stop,
787
 
        original_result.stop_early = self.stop_on_failure
788
 
        result = original_result
789
 
        for decorator in self._result_decorators:
790
 
            result = decorator(result)
791
 
            result.stop_early = self.stop_on_failure
792
 
        result.startTestRun()
793
 
        try:
794
 
            test.run(result)
795
 
        finally:
796
 
            result.stopTestRun()
797
 
        # higher level code uses our extended protocol to determine
798
 
        # what exit code to give.
799
 
        return original_result
800
 
 
801
 
 
802
 
def iter_suite_tests(suite):
803
 
    """Return all tests in a suite, recursing through nested suites"""
804
 
    if isinstance(suite, unittest.TestCase):
805
 
        yield suite
806
 
    elif isinstance(suite, unittest.TestSuite):
807
 
        for item in suite:
808
 
            for r in iter_suite_tests(item):
809
 
                yield r
810
 
    else:
811
 
        raise Exception('unknown type %r for object %r'
812
 
                        % (type(suite), suite))
813
 
 
814
 
 
815
 
TestSkipped = testtools.testcase.TestSkipped
816
 
 
817
 
 
818
 
class TestNotApplicable(TestSkipped):
819
 
    """A test is not applicable to the situation where it was run.
820
 
 
821
 
    This is only normally raised by parameterized tests, if they find that
822
 
    the instance they're constructed upon does not support one aspect
823
 
    of its interface.
824
 
    """
825
 
 
826
 
 
827
 
# traceback._some_str fails to format exceptions that have the default
828
 
# __str__ which does an implicit ascii conversion. However, repr() on those
829
 
# objects works, for all that its not quite what the doctor may have ordered.
830
 
def _clever_some_str(value):
831
 
    try:
832
 
        return str(value)
833
 
    except:
834
 
        try:
835
 
            return repr(value).replace('\\n', '\n')
836
 
        except:
837
 
            return '<unprintable %s object>' % type(value).__name__
838
 
 
839
 
traceback._some_str = _clever_some_str
840
 
 
841
 
 
842
 
# deprecated - use self.knownFailure(), or self.expectFailure.
843
 
KnownFailure = testtools.testcase._ExpectedFailure
844
 
 
845
 
 
846
 
class UnavailableFeature(Exception):
847
 
    """A feature required for this test was not available.
848
 
 
849
 
    This can be considered a specialised form of SkippedTest.
850
 
 
851
 
    The feature should be used to construct the exception.
852
 
    """
853
 
 
854
 
 
855
 
class StringIOWrapper(object):
856
 
    """A wrapper around cStringIO which just adds an encoding attribute.
857
 
 
858
 
    Internally we can check sys.stdout to see what the output encoding
859
 
    should be. However, cStringIO has no encoding attribute that we can
860
 
    set. So we wrap it instead.
861
 
    """
862
 
    encoding='ascii'
863
 
    _cstring = None
864
 
 
865
 
    def __init__(self, s=None):
866
 
        if s is not None:
867
 
            self.__dict__['_cstring'] = StringIO(s)
868
 
        else:
869
 
            self.__dict__['_cstring'] = StringIO()
870
 
 
871
 
    def __getattr__(self, name, getattr=getattr):
872
 
        return getattr(self.__dict__['_cstring'], name)
873
 
 
874
 
    def __setattr__(self, name, val):
875
 
        if name == 'encoding':
876
 
            self.__dict__['encoding'] = val
877
 
        else:
878
 
            return setattr(self._cstring, name, val)
879
 
 
880
 
 
881
 
class TestUIFactory(TextUIFactory):
882
 
    """A UI Factory for testing.
883
 
 
884
 
    Hide the progress bar but emit note()s.
885
 
    Redirect stdin.
886
 
    Allows get_password to be tested without real tty attached.
887
 
 
888
 
    See also CannedInputUIFactory which lets you provide programmatic input in
889
 
    a structured way.
890
 
    """
891
 
    # TODO: Capture progress events at the model level and allow them to be
892
 
    # observed by tests that care.
893
 
    #
894
 
    # XXX: Should probably unify more with CannedInputUIFactory or a
895
 
    # particular configuration of TextUIFactory, or otherwise have a clearer
896
 
    # idea of how they're supposed to be different.
897
 
    # See https://bugs.launchpad.net/bzr/+bug/408213
898
 
 
899
 
    def __init__(self, stdout=None, stderr=None, stdin=None):
900
 
        if stdin is not None:
901
 
            # We use a StringIOWrapper to be able to test various
902
 
            # encodings, but the user is still responsible to
903
 
            # encode the string and to set the encoding attribute
904
 
            # of StringIOWrapper.
905
 
            stdin = StringIOWrapper(stdin)
906
 
        super(TestUIFactory, self).__init__(stdin, stdout, stderr)
907
 
 
908
 
    def get_non_echoed_password(self):
909
 
        """Get password from stdin without trying to handle the echo mode"""
910
 
        password = self.stdin.readline()
911
 
        if not password:
912
 
            raise EOFError
913
 
        if password[-1] == '\n':
914
 
            password = password[:-1]
915
 
        return password
916
 
 
917
 
    def make_progress_view(self):
918
 
        return NullProgressView()
919
 
 
920
 
 
921
 
def isolated_doctest_setUp(test):
922
 
    override_os_environ(test)
923
 
 
924
 
 
925
 
def isolated_doctest_tearDown(test):
926
 
    restore_os_environ(test)
927
 
 
928
 
 
929
 
def IsolatedDocTestSuite(*args, **kwargs):
930
 
    """Overrides doctest.DocTestSuite to handle isolation.
931
 
 
932
 
    The method is really a factory and users are expected to use it as such.
933
 
    """
934
 
 
935
 
    kwargs['setUp'] = isolated_doctest_setUp
936
 
    kwargs['tearDown'] = isolated_doctest_tearDown
937
 
    return doctest.DocTestSuite(*args, **kwargs)
938
 
 
939
 
 
940
 
class TestCase(testtools.TestCase):
 
41
 
 
42
class TestCase(unittest.TestCase):
941
43
    """Base class for bzr unit tests.
942
 
 
943
 
    Tests that need access to disk resources should subclass
 
44
    
 
45
    Tests that need access to disk resources should subclass 
944
46
    TestCaseInTempDir not TestCase.
945
47
 
946
48
    Error and debug log messages are redirected from their usual
947
49
    location into a temporary file, the contents of which can be
948
 
    retrieved by _get_log().  We use a real OS file, not an in-memory object,
949
 
    so that it can also capture file IO.  When the test completes this file
950
 
    is read into memory and removed from disk.
951
 
 
 
50
    retrieved by _get_log().
 
51
       
952
52
    There are also convenience functions to invoke bzr's command-line
953
 
    routine, and to build and check bzr trees.
954
 
 
955
 
    In addition to the usual method of overriding tearDown(), this class also
956
 
    allows subclasses to register cleanup functions via addCleanup, which are
957
 
    run in order as the object is torn down.  It's less likely this will be
958
 
    accidentally overlooked.
959
 
    """
960
 
 
961
 
    _log_file = None
962
 
    # record lsprof data when performing benchmark calls.
963
 
    _gather_lsprof_in_benchmarks = False
964
 
 
965
 
    def __init__(self, methodName='testMethod'):
966
 
        super(TestCase, self).__init__(methodName)
967
 
        self._directory_isolation = True
968
 
        self.exception_handlers.insert(0,
969
 
            (UnavailableFeature, self._do_unsupported_or_skip))
970
 
        self.exception_handlers.insert(0,
971
 
            (TestNotApplicable, self._do_not_applicable))
 
53
    routine, and to build and check bzr trees."""
 
54
 
 
55
    BZRPATH = 'bzr'
972
56
 
973
57
    def setUp(self):
974
 
        super(TestCase, self).setUp()
975
 
        for feature in getattr(self, '_test_needs_features', []):
976
 
            self.requireFeature(feature)
977
 
        self._cleanEnvironment()
978
 
        self._silenceUI()
979
 
        self._startLogFile()
980
 
        self._benchcalls = []
981
 
        self._benchtime = None
982
 
        self._clear_hooks()
983
 
        self._track_transports()
984
 
        self._track_locks()
985
 
        self._clear_debug_flags()
986
 
        # Isolate global verbosity level, to make sure it's reproducible
987
 
        # between tests.  We should get rid of this altogether: bug 656694. --
988
 
        # mbp 20101008
989
 
        self.overrideAttr(bzrlib.trace, '_verbosity_level', 0)
990
 
        # Isolate config option expansion until its default value for bzrlib is
991
 
        # settled on or a the FIXME associated with _get_expand_default_value
992
 
        # is addressed -- vila 20110219
993
 
        self.overrideAttr(config, '_expand_default_value', None)
994
 
        self._log_files = set()
995
 
        # Each key in the ``_counters`` dict holds a value for a different
996
 
        # counter. When the test ends, addDetail() should be used to output the
997
 
        # counter values. This happens in install_counter_hook().
998
 
        self._counters = {}
999
 
        if 'config_stats' in selftest_debug_flags:
1000
 
            self._install_config_stats_hooks()
1001
 
 
1002
 
    def debug(self):
1003
 
        # debug a frame up.
1004
 
        import pdb
1005
 
        pdb.Pdb().set_trace(sys._getframe().f_back)
1006
 
 
1007
 
    def discardDetail(self, name):
1008
 
        """Extend the addDetail, getDetails api so we can remove a detail.
1009
 
 
1010
 
        eg. bzr always adds the 'log' detail at startup, but we don't want to
1011
 
        include it for skipped, xfail, etc tests.
1012
 
 
1013
 
        It is safe to call this for a detail that doesn't exist, in case this
1014
 
        gets called multiple times.
1015
 
        """
1016
 
        # We cheat. details is stored in __details which means we shouldn't
1017
 
        # touch it. but getDetails() returns the dict directly, so we can
1018
 
        # mutate it.
1019
 
        details = self.getDetails()
1020
 
        if name in details:
1021
 
            del details[name]
1022
 
 
1023
 
    def install_counter_hook(self, hooks, name, counter_name=None):
1024
 
        """Install a counting hook.
1025
 
 
1026
 
        Any hook can be counted as long as it doesn't need to return a value.
1027
 
 
1028
 
        :param hooks: Where the hook should be installed.
1029
 
 
1030
 
        :param name: The hook name that will be counted.
1031
 
 
1032
 
        :param counter_name: The counter identifier in ``_counters``, defaults
1033
 
            to ``name``.
1034
 
        """
1035
 
        _counters = self._counters # Avoid closing over self
1036
 
        if counter_name is None:
1037
 
            counter_name = name
1038
 
        if _counters.has_key(counter_name):
1039
 
            raise AssertionError('%s is already used as a counter name'
1040
 
                                  % (counter_name,))
1041
 
        _counters[counter_name] = 0
1042
 
        self.addDetail(counter_name, content.Content(content.UTF8_TEXT,
1043
 
            lambda: ['%d' % (_counters[counter_name],)]))
1044
 
        def increment_counter(*args, **kwargs):
1045
 
            _counters[counter_name] += 1
1046
 
        label = 'count %s calls' % (counter_name,)
1047
 
        hooks.install_named_hook(name, increment_counter, label)
1048
 
        self.addCleanup(hooks.uninstall_named_hook, name, label)
1049
 
 
1050
 
    def _install_config_stats_hooks(self):
1051
 
        """Install config hooks to count hook calls.
1052
 
 
1053
 
        """
1054
 
        for hook_name in ('get', 'set', 'remove', 'load', 'save'):
1055
 
            self.install_counter_hook(config.ConfigHooks, hook_name,
1056
 
                                       'config.%s' % (hook_name,))
1057
 
 
1058
 
        # The OldConfigHooks are private and need special handling to protect
1059
 
        # against recursive tests (tests that run other tests), so we just do
1060
 
        # manually what registering them into _builtin_known_hooks will provide
1061
 
        # us.
1062
 
        self.overrideAttr(config, 'OldConfigHooks', config._OldConfigHooks())
1063
 
        for hook_name in ('get', 'set', 'remove', 'load', 'save'):
1064
 
            self.install_counter_hook(config.OldConfigHooks, hook_name,
1065
 
                                      'old_config.%s' % (hook_name,))
1066
 
 
1067
 
    def _clear_debug_flags(self):
1068
 
        """Prevent externally set debug flags affecting tests.
1069
 
 
1070
 
        Tests that want to use debug flags can just set them in the
1071
 
        debug_flags set during setup/teardown.
1072
 
        """
1073
 
        # Start with a copy of the current debug flags we can safely modify.
1074
 
        self.overrideAttr(debug, 'debug_flags', set(debug.debug_flags))
1075
 
        if 'allow_debug' not in selftest_debug_flags:
1076
 
            debug.debug_flags.clear()
1077
 
        if 'disable_lock_checks' not in selftest_debug_flags:
1078
 
            debug.debug_flags.add('strict_locks')
1079
 
 
1080
 
    def _clear_hooks(self):
1081
 
        # prevent hooks affecting tests
1082
 
        known_hooks = hooks.known_hooks
1083
 
        self._preserved_hooks = {}
1084
 
        for key, (parent, name) in known_hooks.iter_parent_objects():
1085
 
            current_hooks = getattr(parent, name)
1086
 
            self._preserved_hooks[parent] = (name, current_hooks)
1087
 
        self._preserved_lazy_hooks = hooks._lazy_hooks
1088
 
        hooks._lazy_hooks = {}
1089
 
        self.addCleanup(self._restoreHooks)
1090
 
        for key, (parent, name) in known_hooks.iter_parent_objects():
1091
 
            factory = known_hooks.get(key)
1092
 
            setattr(parent, name, factory())
1093
 
        # this hook should always be installed
1094
 
        request._install_hook()
1095
 
 
1096
 
    def disable_directory_isolation(self):
1097
 
        """Turn off directory isolation checks."""
1098
 
        self._directory_isolation = False
1099
 
 
1100
 
    def enable_directory_isolation(self):
1101
 
        """Enable directory isolation checks."""
1102
 
        self._directory_isolation = True
1103
 
 
1104
 
    def _silenceUI(self):
1105
 
        """Turn off UI for duration of test"""
1106
 
        # by default the UI is off; tests can turn it on if they want it.
1107
 
        self.overrideAttr(ui, 'ui_factory', ui.SilentUIFactory())
1108
 
 
1109
 
    def _check_locks(self):
1110
 
        """Check that all lock take/release actions have been paired."""
1111
 
        # We always check for mismatched locks. If a mismatch is found, we
1112
 
        # fail unless -Edisable_lock_checks is supplied to selftest, in which
1113
 
        # case we just print a warning.
1114
 
        # unhook:
1115
 
        acquired_locks = [lock for action, lock in self._lock_actions
1116
 
                          if action == 'acquired']
1117
 
        released_locks = [lock for action, lock in self._lock_actions
1118
 
                          if action == 'released']
1119
 
        broken_locks = [lock for action, lock in self._lock_actions
1120
 
                        if action == 'broken']
1121
 
        # trivially, given the tests for lock acquistion and release, if we
1122
 
        # have as many in each list, it should be ok. Some lock tests also
1123
 
        # break some locks on purpose and should be taken into account by
1124
 
        # considering that breaking a lock is just a dirty way of releasing it.
1125
 
        if len(acquired_locks) != (len(released_locks) + len(broken_locks)):
1126
 
            message = (
1127
 
                'Different number of acquired and '
1128
 
                'released or broken locks.\n'
1129
 
                'acquired=%s\n'
1130
 
                'released=%s\n'
1131
 
                'broken=%s\n' %
1132
 
                (acquired_locks, released_locks, broken_locks))
1133
 
            if not self._lock_check_thorough:
1134
 
                # Rather than fail, just warn
1135
 
                print "Broken test %s: %s" % (self, message)
1136
 
                return
1137
 
            self.fail(message)
1138
 
 
1139
 
    def _track_locks(self):
1140
 
        """Track lock activity during tests."""
1141
 
        self._lock_actions = []
1142
 
        if 'disable_lock_checks' in selftest_debug_flags:
1143
 
            self._lock_check_thorough = False
1144
 
        else:
1145
 
            self._lock_check_thorough = True
1146
 
 
1147
 
        self.addCleanup(self._check_locks)
1148
 
        _mod_lock.Lock.hooks.install_named_hook('lock_acquired',
1149
 
                                                self._lock_acquired, None)
1150
 
        _mod_lock.Lock.hooks.install_named_hook('lock_released',
1151
 
                                                self._lock_released, None)
1152
 
        _mod_lock.Lock.hooks.install_named_hook('lock_broken',
1153
 
                                                self._lock_broken, None)
1154
 
 
1155
 
    def _lock_acquired(self, result):
1156
 
        self._lock_actions.append(('acquired', result))
1157
 
 
1158
 
    def _lock_released(self, result):
1159
 
        self._lock_actions.append(('released', result))
1160
 
 
1161
 
    def _lock_broken(self, result):
1162
 
        self._lock_actions.append(('broken', result))
1163
 
 
1164
 
    def permit_dir(self, name):
1165
 
        """Permit a directory to be used by this test. See permit_url."""
1166
 
        name_transport = _mod_transport.get_transport(name)
1167
 
        self.permit_url(name)
1168
 
        self.permit_url(name_transport.base)
1169
 
 
1170
 
    def permit_url(self, url):
1171
 
        """Declare that url is an ok url to use in this test.
1172
 
        
1173
 
        Do this for memory transports, temporary test directory etc.
1174
 
        
1175
 
        Do not do this for the current working directory, /tmp, or any other
1176
 
        preexisting non isolated url.
1177
 
        """
1178
 
        if not url.endswith('/'):
1179
 
            url += '/'
1180
 
        self._bzr_selftest_roots.append(url)
1181
 
 
1182
 
    def permit_source_tree_branch_repo(self):
1183
 
        """Permit the source tree bzr is running from to be opened.
1184
 
 
1185
 
        Some code such as bzrlib.version attempts to read from the bzr branch
1186
 
        that bzr is executing from (if any). This method permits that directory
1187
 
        to be used in the test suite.
1188
 
        """
1189
 
        path = self.get_source_path()
1190
 
        self.record_directory_isolation()
1191
 
        try:
1192
 
            try:
1193
 
                workingtree.WorkingTree.open(path)
1194
 
            except (errors.NotBranchError, errors.NoWorkingTree):
1195
 
                raise TestSkipped('Needs a working tree of bzr sources')
1196
 
        finally:
1197
 
            self.enable_directory_isolation()
1198
 
 
1199
 
    def _preopen_isolate_transport(self, transport):
1200
 
        """Check that all transport openings are done in the test work area."""
1201
 
        while isinstance(transport, pathfilter.PathFilteringTransport):
1202
 
            # Unwrap pathfiltered transports
1203
 
            transport = transport.server.backing_transport.clone(
1204
 
                transport._filter('.'))
1205
 
        url = transport.base
1206
 
        # ReadonlySmartTCPServer_for_testing decorates the backing transport
1207
 
        # urls it is given by prepending readonly+. This is appropriate as the
1208
 
        # client shouldn't know that the server is readonly (or not readonly).
1209
 
        # We could register all servers twice, with readonly+ prepending, but
1210
 
        # that makes for a long list; this is about the same but easier to
1211
 
        # read.
1212
 
        if url.startswith('readonly+'):
1213
 
            url = url[len('readonly+'):]
1214
 
        self._preopen_isolate_url(url)
1215
 
 
1216
 
    def _preopen_isolate_url(self, url):
1217
 
        if not self._directory_isolation:
1218
 
            return
1219
 
        if self._directory_isolation == 'record':
1220
 
            self._bzr_selftest_roots.append(url)
1221
 
            return
1222
 
        # This prevents all transports, including e.g. sftp ones backed on disk
1223
 
        # from working unless they are explicitly granted permission. We then
1224
 
        # depend on the code that sets up test transports to check that they are
1225
 
        # appropriately isolated and enable their use by calling
1226
 
        # self.permit_transport()
1227
 
        if not osutils.is_inside_any(self._bzr_selftest_roots, url):
1228
 
            raise errors.BzrError("Attempt to escape test isolation: %r %r"
1229
 
                % (url, self._bzr_selftest_roots))
1230
 
 
1231
 
    def record_directory_isolation(self):
1232
 
        """Gather accessed directories to permit later access.
1233
 
        
1234
 
        This is used for tests that access the branch bzr is running from.
1235
 
        """
1236
 
        self._directory_isolation = "record"
1237
 
 
1238
 
    def start_server(self, transport_server, backing_server=None):
1239
 
        """Start transport_server for this test.
1240
 
 
1241
 
        This starts the server, registers a cleanup for it and permits the
1242
 
        server's urls to be used.
1243
 
        """
1244
 
        if backing_server is None:
1245
 
            transport_server.start_server()
1246
 
        else:
1247
 
            transport_server.start_server(backing_server)
1248
 
        self.addCleanup(transport_server.stop_server)
1249
 
        # Obtain a real transport because if the server supplies a password, it
1250
 
        # will be hidden from the base on the client side.
1251
 
        t = _mod_transport.get_transport(transport_server.get_url())
1252
 
        # Some transport servers effectively chroot the backing transport;
1253
 
        # others like SFTPServer don't - users of the transport can walk up the
1254
 
        # transport to read the entire backing transport. This wouldn't matter
1255
 
        # except that the workdir tests are given - and that they expect the
1256
 
        # server's url to point at - is one directory under the safety net. So
1257
 
        # Branch operations into the transport will attempt to walk up one
1258
 
        # directory. Chrooting all servers would avoid this but also mean that
1259
 
        # we wouldn't be testing directly against non-root urls. Alternatively
1260
 
        # getting the test framework to start the server with a backing server
1261
 
        # at the actual safety net directory would work too, but this then
1262
 
        # means that the self.get_url/self.get_transport methods would need
1263
 
        # to transform all their results. On balance its cleaner to handle it
1264
 
        # here, and permit a higher url when we have one of these transports.
1265
 
        if t.base.endswith('/work/'):
1266
 
            # we have safety net/test root/work
1267
 
            t = t.clone('../..')
1268
 
        elif isinstance(transport_server,
1269
 
                        test_server.SmartTCPServer_for_testing):
1270
 
            # The smart server adds a path similar to work, which is traversed
1271
 
            # up from by the client. But the server is chrooted - the actual
1272
 
            # backing transport is not escaped from, and VFS requests to the
1273
 
            # root will error (because they try to escape the chroot).
1274
 
            t2 = t.clone('..')
1275
 
            while t2.base != t.base:
1276
 
                t = t2
1277
 
                t2 = t.clone('..')
1278
 
        self.permit_url(t.base)
1279
 
 
1280
 
    def _track_transports(self):
1281
 
        """Install checks for transport usage."""
1282
 
        # TestCase has no safe place it can write to.
1283
 
        self._bzr_selftest_roots = []
1284
 
        # Currently the easiest way to be sure that nothing is going on is to
1285
 
        # hook into bzr dir opening. This leaves a small window of error for
1286
 
        # transport tests, but they are well known, and we can improve on this
1287
 
        # step.
1288
 
        bzrdir.BzrDir.hooks.install_named_hook("pre_open",
1289
 
            self._preopen_isolate_transport, "Check bzr directories are safe.")
1290
 
 
1291
 
    def _ndiff_strings(self, a, b):
1292
 
        """Return ndiff between two strings containing lines.
1293
 
 
1294
 
        A trailing newline is added if missing to make the strings
1295
 
        print properly."""
1296
 
        if b and b[-1] != '\n':
1297
 
            b += '\n'
1298
 
        if a and a[-1] != '\n':
1299
 
            a += '\n'
1300
 
        difflines = difflib.ndiff(a.splitlines(True),
1301
 
                                  b.splitlines(True),
1302
 
                                  linejunk=lambda x: False,
1303
 
                                  charjunk=lambda x: False)
1304
 
        return ''.join(difflines)
1305
 
 
1306
 
    def assertEqual(self, a, b, message=''):
1307
 
        try:
1308
 
            if a == b:
1309
 
                return
1310
 
        except UnicodeError, e:
1311
 
            # If we can't compare without getting a UnicodeError, then
1312
 
            # obviously they are different
1313
 
            trace.mutter('UnicodeError: %s', e)
1314
 
        if message:
1315
 
            message += '\n'
1316
 
        raise AssertionError("%snot equal:\na = %s\nb = %s\n"
1317
 
            % (message,
1318
 
               pprint.pformat(a), pprint.pformat(b)))
1319
 
 
1320
 
    assertEquals = assertEqual
1321
 
 
1322
 
    def assertEqualDiff(self, a, b, message=None):
1323
 
        """Assert two texts are equal, if not raise an exception.
1324
 
 
1325
 
        This is intended for use with multi-line strings where it can
1326
 
        be hard to find the differences by eye.
1327
 
        """
1328
 
        # TODO: perhaps override assertEquals to call this for strings?
1329
 
        if a == b:
1330
 
            return
1331
 
        if message is None:
1332
 
            message = "texts not equal:\n"
1333
 
        if a + '\n' == b:
1334
 
            message = 'first string is missing a final newline.\n'
1335
 
        if a == b + '\n':
1336
 
            message = 'second string is missing a final newline.\n'
1337
 
        raise AssertionError(message +
1338
 
                             self._ndiff_strings(a, b))
1339
 
 
1340
 
    def assertEqualMode(self, mode, mode_test):
1341
 
        self.assertEqual(mode, mode_test,
1342
 
                         'mode mismatch %o != %o' % (mode, mode_test))
1343
 
 
1344
 
    def assertEqualStat(self, expected, actual):
1345
 
        """assert that expected and actual are the same stat result.
1346
 
 
1347
 
        :param expected: A stat result.
1348
 
        :param actual: A stat result.
1349
 
        :raises AssertionError: If the expected and actual stat values differ
1350
 
            other than by atime.
1351
 
        """
1352
 
        self.assertEqual(expected.st_size, actual.st_size,
1353
 
                         'st_size did not match')
1354
 
        self.assertEqual(expected.st_mtime, actual.st_mtime,
1355
 
                         'st_mtime did not match')
1356
 
        self.assertEqual(expected.st_ctime, actual.st_ctime,
1357
 
                         'st_ctime did not match')
1358
 
        if sys.platform == 'win32':
1359
 
            # On Win32 both 'dev' and 'ino' cannot be trusted. In python2.4 it
1360
 
            # is 'dev' that varies, in python 2.5 (6?) it is st_ino that is
1361
 
            # odd. We just force it to always be 0 to avoid any problems.
1362
 
            self.assertEqual(0, expected.st_dev)
1363
 
            self.assertEqual(0, actual.st_dev)
1364
 
            self.assertEqual(0, expected.st_ino)
1365
 
            self.assertEqual(0, actual.st_ino)
1366
 
        else:
1367
 
            self.assertEqual(expected.st_dev, actual.st_dev,
1368
 
                             'st_dev did not match')
1369
 
            self.assertEqual(expected.st_ino, actual.st_ino,
1370
 
                             'st_ino did not match')
1371
 
        self.assertEqual(expected.st_mode, actual.st_mode,
1372
 
                         'st_mode did not match')
1373
 
 
1374
 
    def assertLength(self, length, obj_with_len):
1375
 
        """Assert that obj_with_len is of length length."""
1376
 
        if len(obj_with_len) != length:
1377
 
            self.fail("Incorrect length: wanted %d, got %d for %r" % (
1378
 
                length, len(obj_with_len), obj_with_len))
1379
 
 
1380
 
    def assertLogsError(self, exception_class, func, *args, **kwargs):
1381
 
        """Assert that `func(*args, **kwargs)` quietly logs a specific error.
1382
 
        """
1383
 
        captured = []
1384
 
        orig_log_exception_quietly = trace.log_exception_quietly
1385
 
        try:
1386
 
            def capture():
1387
 
                orig_log_exception_quietly()
1388
 
                captured.append(sys.exc_info()[1])
1389
 
            trace.log_exception_quietly = capture
1390
 
            func(*args, **kwargs)
1391
 
        finally:
1392
 
            trace.log_exception_quietly = orig_log_exception_quietly
1393
 
        self.assertLength(1, captured)
1394
 
        err = captured[0]
1395
 
        self.assertIsInstance(err, exception_class)
1396
 
        return err
1397
 
 
1398
 
    def assertPositive(self, val):
1399
 
        """Assert that val is greater than 0."""
1400
 
        self.assertTrue(val > 0, 'expected a positive value, but got %s' % val)
1401
 
 
1402
 
    def assertNegative(self, val):
1403
 
        """Assert that val is less than 0."""
1404
 
        self.assertTrue(val < 0, 'expected a negative value, but got %s' % val)
1405
 
 
1406
 
    def assertStartsWith(self, s, prefix):
1407
 
        if not s.startswith(prefix):
1408
 
            raise AssertionError('string %r does not start with %r' % (s, prefix))
1409
 
 
1410
 
    def assertEndsWith(self, s, suffix):
1411
 
        """Asserts that s ends with suffix."""
1412
 
        if not s.endswith(suffix):
1413
 
            raise AssertionError('string %r does not end with %r' % (s, suffix))
1414
 
 
1415
 
    def assertContainsRe(self, haystack, needle_re, flags=0):
1416
 
        """Assert that a contains something matching a regular expression."""
1417
 
        if not re.search(needle_re, haystack, flags):
1418
 
            if '\n' in haystack or len(haystack) > 60:
1419
 
                # a long string, format it in a more readable way
1420
 
                raise AssertionError(
1421
 
                        'pattern "%s" not found in\n"""\\\n%s"""\n'
1422
 
                        % (needle_re, haystack))
1423
 
            else:
1424
 
                raise AssertionError('pattern "%s" not found in "%s"'
1425
 
                        % (needle_re, haystack))
1426
 
 
1427
 
    def assertNotContainsRe(self, haystack, needle_re, flags=0):
1428
 
        """Assert that a does not match a regular expression"""
1429
 
        if re.search(needle_re, haystack, flags):
1430
 
            raise AssertionError('pattern "%s" found in "%s"'
1431
 
                    % (needle_re, haystack))
1432
 
 
1433
 
    def assertContainsString(self, haystack, needle):
1434
 
        if haystack.find(needle) == -1:
1435
 
            self.fail("string %r not found in '''%s'''" % (needle, haystack))
1436
 
 
1437
 
    def assertNotContainsString(self, haystack, needle):
1438
 
        if haystack.find(needle) != -1:
1439
 
            self.fail("string %r found in '''%s'''" % (needle, haystack))
1440
 
 
1441
 
    def assertSubset(self, sublist, superlist):
1442
 
        """Assert that every entry in sublist is present in superlist."""
1443
 
        missing = set(sublist) - set(superlist)
1444
 
        if len(missing) > 0:
1445
 
            raise AssertionError("value(s) %r not present in container %r" %
1446
 
                                 (missing, superlist))
1447
 
 
1448
 
    def assertListRaises(self, excClass, func, *args, **kwargs):
1449
 
        """Fail unless excClass is raised when the iterator from func is used.
1450
 
 
1451
 
        Many functions can return generators this makes sure
1452
 
        to wrap them in a list() call to make sure the whole generator
1453
 
        is run, and that the proper exception is raised.
1454
 
        """
1455
 
        try:
1456
 
            list(func(*args, **kwargs))
1457
 
        except excClass, e:
1458
 
            return e
1459
 
        else:
1460
 
            if getattr(excClass,'__name__', None) is not None:
1461
 
                excName = excClass.__name__
1462
 
            else:
1463
 
                excName = str(excClass)
1464
 
            raise self.failureException, "%s not raised" % excName
1465
 
 
1466
 
    def assertRaises(self, excClass, callableObj, *args, **kwargs):
1467
 
        """Assert that a callable raises a particular exception.
1468
 
 
1469
 
        :param excClass: As for the except statement, this may be either an
1470
 
            exception class, or a tuple of classes.
1471
 
        :param callableObj: A callable, will be passed ``*args`` and
1472
 
            ``**kwargs``.
1473
 
 
1474
 
        Returns the exception so that you can examine it.
1475
 
        """
1476
 
        try:
1477
 
            callableObj(*args, **kwargs)
1478
 
        except excClass, e:
1479
 
            return e
1480
 
        else:
1481
 
            if getattr(excClass,'__name__', None) is not None:
1482
 
                excName = excClass.__name__
1483
 
            else:
1484
 
                # probably a tuple
1485
 
                excName = str(excClass)
1486
 
            raise self.failureException, "%s not raised" % excName
1487
 
 
1488
 
    def assertIs(self, left, right, message=None):
1489
 
        if not (left is right):
1490
 
            if message is not None:
1491
 
                raise AssertionError(message)
1492
 
            else:
1493
 
                raise AssertionError("%r is not %r." % (left, right))
1494
 
 
1495
 
    def assertIsNot(self, left, right, message=None):
1496
 
        if (left is right):
1497
 
            if message is not None:
1498
 
                raise AssertionError(message)
1499
 
            else:
1500
 
                raise AssertionError("%r is %r." % (left, right))
1501
 
 
1502
 
    def assertTransportMode(self, transport, path, mode):
1503
 
        """Fail if a path does not have mode "mode".
1504
 
 
1505
 
        If modes are not supported on this transport, the assertion is ignored.
1506
 
        """
1507
 
        if not transport._can_roundtrip_unix_modebits():
1508
 
            return
1509
 
        path_stat = transport.stat(path)
1510
 
        actual_mode = stat.S_IMODE(path_stat.st_mode)
1511
 
        self.assertEqual(mode, actual_mode,
1512
 
                         'mode of %r incorrect (%s != %s)'
1513
 
                         % (path, oct(mode), oct(actual_mode)))
1514
 
 
1515
 
    def assertIsSameRealPath(self, path1, path2):
1516
 
        """Fail if path1 and path2 points to different files"""
1517
 
        self.assertEqual(osutils.realpath(path1),
1518
 
                         osutils.realpath(path2),
1519
 
                         "apparent paths:\na = %s\nb = %s\n," % (path1, path2))
1520
 
 
1521
 
    def assertIsInstance(self, obj, kls, msg=None):
1522
 
        """Fail if obj is not an instance of kls
1523
 
        
1524
 
        :param msg: Supplementary message to show if the assertion fails.
1525
 
        """
1526
 
        if not isinstance(obj, kls):
1527
 
            m = "%r is an instance of %s rather than %s" % (
1528
 
                obj, obj.__class__, kls)
1529
 
            if msg:
1530
 
                m += ": " + msg
1531
 
            self.fail(m)
1532
 
 
1533
 
    def assertFileEqual(self, content, path):
1534
 
        """Fail if path does not contain 'content'."""
1535
 
        self.assertPathExists(path)
1536
 
        f = file(path, 'rb')
1537
 
        try:
1538
 
            s = f.read()
1539
 
        finally:
1540
 
            f.close()
1541
 
        self.assertEqualDiff(content, s)
1542
 
 
1543
 
    def assertDocstring(self, expected_docstring, obj):
1544
 
        """Fail if obj does not have expected_docstring"""
1545
 
        if __doc__ is None:
1546
 
            # With -OO the docstring should be None instead
1547
 
            self.assertIs(obj.__doc__, None)
1548
 
        else:
1549
 
            self.assertEqual(expected_docstring, obj.__doc__)
1550
 
 
1551
 
    @symbol_versioning.deprecated_method(symbol_versioning.deprecated_in((2, 4)))
1552
 
    def failUnlessExists(self, path):
1553
 
        return self.assertPathExists(path)
1554
 
 
1555
 
    def assertPathExists(self, path):
1556
 
        """Fail unless path or paths, which may be abs or relative, exist."""
1557
 
        if not isinstance(path, basestring):
1558
 
            for p in path:
1559
 
                self.assertPathExists(p)
1560
 
        else:
1561
 
            self.assertTrue(osutils.lexists(path),
1562
 
                path + " does not exist")
1563
 
 
1564
 
    @symbol_versioning.deprecated_method(symbol_versioning.deprecated_in((2, 4)))
1565
 
    def failIfExists(self, path):
1566
 
        return self.assertPathDoesNotExist(path)
1567
 
 
1568
 
    def assertPathDoesNotExist(self, path):
1569
 
        """Fail if path or paths, which may be abs or relative, exist."""
1570
 
        if not isinstance(path, basestring):
1571
 
            for p in path:
1572
 
                self.assertPathDoesNotExist(p)
1573
 
        else:
1574
 
            self.assertFalse(osutils.lexists(path),
1575
 
                path + " exists")
1576
 
 
1577
 
    def _capture_deprecation_warnings(self, a_callable, *args, **kwargs):
1578
 
        """A helper for callDeprecated and applyDeprecated.
1579
 
 
1580
 
        :param a_callable: A callable to call.
1581
 
        :param args: The positional arguments for the callable
1582
 
        :param kwargs: The keyword arguments for the callable
1583
 
        :return: A tuple (warnings, result). result is the result of calling
1584
 
            a_callable(``*args``, ``**kwargs``).
1585
 
        """
1586
 
        local_warnings = []
1587
 
        def capture_warnings(msg, cls=None, stacklevel=None):
1588
 
            # we've hooked into a deprecation specific callpath,
1589
 
            # only deprecations should getting sent via it.
1590
 
            self.assertEqual(cls, DeprecationWarning)
1591
 
            local_warnings.append(msg)
1592
 
        original_warning_method = symbol_versioning.warn
1593
 
        symbol_versioning.set_warning_method(capture_warnings)
1594
 
        try:
1595
 
            result = a_callable(*args, **kwargs)
1596
 
        finally:
1597
 
            symbol_versioning.set_warning_method(original_warning_method)
1598
 
        return (local_warnings, result)
1599
 
 
1600
 
    def applyDeprecated(self, deprecation_format, a_callable, *args, **kwargs):
1601
 
        """Call a deprecated callable without warning the user.
1602
 
 
1603
 
        Note that this only captures warnings raised by symbol_versioning.warn,
1604
 
        not other callers that go direct to the warning module.
1605
 
 
1606
 
        To test that a deprecated method raises an error, do something like
1607
 
        this (remember that both assertRaises and applyDeprecated delays *args
1608
 
        and **kwargs passing)::
1609
 
 
1610
 
            self.assertRaises(errors.ReservedId,
1611
 
                self.applyDeprecated,
1612
 
                deprecated_in((1, 5, 0)),
1613
 
                br.append_revision,
1614
 
                'current:')
1615
 
 
1616
 
        :param deprecation_format: The deprecation format that the callable
1617
 
            should have been deprecated with. This is the same type as the
1618
 
            parameter to deprecated_method/deprecated_function. If the
1619
 
            callable is not deprecated with this format, an assertion error
1620
 
            will be raised.
1621
 
        :param a_callable: A callable to call. This may be a bound method or
1622
 
            a regular function. It will be called with ``*args`` and
1623
 
            ``**kwargs``.
1624
 
        :param args: The positional arguments for the callable
1625
 
        :param kwargs: The keyword arguments for the callable
1626
 
        :return: The result of a_callable(``*args``, ``**kwargs``)
1627
 
        """
1628
 
        call_warnings, result = self._capture_deprecation_warnings(a_callable,
1629
 
            *args, **kwargs)
1630
 
        expected_first_warning = symbol_versioning.deprecation_string(
1631
 
            a_callable, deprecation_format)
1632
 
        if len(call_warnings) == 0:
1633
 
            self.fail("No deprecation warning generated by call to %s" %
1634
 
                a_callable)
1635
 
        self.assertEqual(expected_first_warning, call_warnings[0])
1636
 
        return result
1637
 
 
1638
 
    def callCatchWarnings(self, fn, *args, **kw):
1639
 
        """Call a callable that raises python warnings.
1640
 
 
1641
 
        The caller's responsible for examining the returned warnings.
1642
 
 
1643
 
        If the callable raises an exception, the exception is not
1644
 
        caught and propagates up to the caller.  In that case, the list
1645
 
        of warnings is not available.
1646
 
 
1647
 
        :returns: ([warning_object, ...], fn_result)
1648
 
        """
1649
 
        # XXX: This is not perfect, because it completely overrides the
1650
 
        # warnings filters, and some code may depend on suppressing particular
1651
 
        # warnings.  It's the easiest way to insulate ourselves from -Werror,
1652
 
        # though.  -- Andrew, 20071062
1653
 
        wlist = []
1654
 
        def _catcher(message, category, filename, lineno, file=None, line=None):
1655
 
            # despite the name, 'message' is normally(?) a Warning subclass
1656
 
            # instance
1657
 
            wlist.append(message)
1658
 
        saved_showwarning = warnings.showwarning
1659
 
        saved_filters = warnings.filters
1660
 
        try:
1661
 
            warnings.showwarning = _catcher
1662
 
            warnings.filters = []
1663
 
            result = fn(*args, **kw)
1664
 
        finally:
1665
 
            warnings.showwarning = saved_showwarning
1666
 
            warnings.filters = saved_filters
1667
 
        return wlist, result
1668
 
 
1669
 
    def callDeprecated(self, expected, callable, *args, **kwargs):
1670
 
        """Assert that a callable is deprecated in a particular way.
1671
 
 
1672
 
        This is a very precise test for unusual requirements. The
1673
 
        applyDeprecated helper function is probably more suited for most tests
1674
 
        as it allows you to simply specify the deprecation format being used
1675
 
        and will ensure that that is issued for the function being called.
1676
 
 
1677
 
        Note that this only captures warnings raised by symbol_versioning.warn,
1678
 
        not other callers that go direct to the warning module.  To catch
1679
 
        general warnings, use callCatchWarnings.
1680
 
 
1681
 
        :param expected: a list of the deprecation warnings expected, in order
1682
 
        :param callable: The callable to call
1683
 
        :param args: The positional arguments for the callable
1684
 
        :param kwargs: The keyword arguments for the callable
1685
 
        """
1686
 
        call_warnings, result = self._capture_deprecation_warnings(callable,
1687
 
            *args, **kwargs)
1688
 
        self.assertEqual(expected, call_warnings)
1689
 
        return result
1690
 
 
1691
 
    def _startLogFile(self):
1692
 
        """Send bzr and test log messages to a temporary file.
1693
 
 
1694
 
        The file is removed as the test is torn down.
1695
 
        """
1696
 
        pseudo_log_file = StringIO()
1697
 
        def _get_log_contents_for_weird_testtools_api():
1698
 
            return [pseudo_log_file.getvalue().decode(
1699
 
                "utf-8", "replace").encode("utf-8")]
1700
 
        self.addDetail("log", content.Content(content.ContentType("text",
1701
 
            "plain", {"charset": "utf8"}),
1702
 
            _get_log_contents_for_weird_testtools_api))
1703
 
        self._log_file = pseudo_log_file
1704
 
        self._log_memento = trace.push_log_file(self._log_file)
1705
 
        self.addCleanup(self._finishLogFile)
1706
 
 
1707
 
    def _finishLogFile(self):
1708
 
        """Finished with the log file.
1709
 
 
1710
 
        Close the file and delete it.
1711
 
        """
1712
 
        if trace._trace_file:
1713
 
            # flush the log file, to get all content
1714
 
            trace._trace_file.flush()
1715
 
        trace.pop_log_file(self._log_memento)
1716
 
 
1717
 
    def thisFailsStrictLockCheck(self):
1718
 
        """It is known that this test would fail with -Dstrict_locks.
1719
 
 
1720
 
        By default, all tests are run with strict lock checking unless
1721
 
        -Edisable_lock_checks is supplied. However there are some tests which
1722
 
        we know fail strict locks at this point that have not been fixed.
1723
 
        They should call this function to disable the strict checking.
1724
 
 
1725
 
        This should be used sparingly, it is much better to fix the locking
1726
 
        issues rather than papering over the problem by calling this function.
1727
 
        """
1728
 
        debug.debug_flags.discard('strict_locks')
1729
 
 
1730
 
    def overrideAttr(self, obj, attr_name, new=_unitialized_attr):
1731
 
        """Overrides an object attribute restoring it after the test.
1732
 
 
1733
 
        :param obj: The object that will be mutated.
1734
 
 
1735
 
        :param attr_name: The attribute name we want to preserve/override in
1736
 
            the object.
1737
 
 
1738
 
        :param new: The optional value we want to set the attribute to.
1739
 
 
1740
 
        :returns: The actual attr value.
1741
 
        """
1742
 
        value = getattr(obj, attr_name)
1743
 
        # The actual value is captured by the call below
1744
 
        self.addCleanup(setattr, obj, attr_name, value)
1745
 
        if new is not _unitialized_attr:
1746
 
            setattr(obj, attr_name, new)
1747
 
        return value
1748
 
 
1749
 
    def overrideEnv(self, name, new):
1750
 
        """Set an environment variable, and reset it after the test.
1751
 
 
1752
 
        :param name: The environment variable name.
1753
 
 
1754
 
        :param new: The value to set the variable to. If None, the 
1755
 
            variable is deleted from the environment.
1756
 
 
1757
 
        :returns: The actual variable value.
1758
 
        """
1759
 
        value = osutils.set_or_unset_env(name, new)
1760
 
        self.addCleanup(osutils.set_or_unset_env, name, value)
1761
 
        return value
1762
 
 
1763
 
    def _cleanEnvironment(self):
1764
 
        for name, value in isolated_environ.iteritems():
1765
 
            self.overrideEnv(name, value)
1766
 
 
1767
 
    def _restoreHooks(self):
1768
 
        for klass, (name, hooks) in self._preserved_hooks.items():
1769
 
            setattr(klass, name, hooks)
1770
 
        self._preserved_hooks.clear()
1771
 
        bzrlib.hooks._lazy_hooks = self._preserved_lazy_hooks
1772
 
        self._preserved_lazy_hooks.clear()
1773
 
 
1774
 
    def knownFailure(self, reason):
1775
 
        """This test has failed for some known reason."""
1776
 
        raise KnownFailure(reason)
1777
 
 
1778
 
    def _suppress_log(self):
1779
 
        """Remove the log info from details."""
1780
 
        self.discardDetail('log')
1781
 
 
1782
 
    def _do_skip(self, result, reason):
1783
 
        self._suppress_log()
1784
 
        addSkip = getattr(result, 'addSkip', None)
1785
 
        if not callable(addSkip):
1786
 
            result.addSuccess(result)
1787
 
        else:
1788
 
            addSkip(self, reason)
1789
 
 
1790
 
    @staticmethod
1791
 
    def _do_known_failure(self, result, e):
1792
 
        self._suppress_log()
1793
 
        err = sys.exc_info()
1794
 
        addExpectedFailure = getattr(result, 'addExpectedFailure', None)
1795
 
        if addExpectedFailure is not None:
1796
 
            addExpectedFailure(self, err)
1797
 
        else:
1798
 
            result.addSuccess(self)
1799
 
 
1800
 
    @staticmethod
1801
 
    def _do_not_applicable(self, result, e):
1802
 
        if not e.args:
1803
 
            reason = 'No reason given'
1804
 
        else:
1805
 
            reason = e.args[0]
1806
 
        self._suppress_log ()
1807
 
        addNotApplicable = getattr(result, 'addNotApplicable', None)
1808
 
        if addNotApplicable is not None:
1809
 
            result.addNotApplicable(self, reason)
1810
 
        else:
1811
 
            self._do_skip(result, reason)
1812
 
 
1813
 
    @staticmethod
1814
 
    def _report_skip(self, result, err):
1815
 
        """Override the default _report_skip.
1816
 
 
1817
 
        We want to strip the 'log' detail. If we waint until _do_skip, it has
1818
 
        already been formatted into the 'reason' string, and we can't pull it
1819
 
        out again.
1820
 
        """
1821
 
        self._suppress_log()
1822
 
        super(TestCase, self)._report_skip(self, result, err)
1823
 
 
1824
 
    @staticmethod
1825
 
    def _report_expected_failure(self, result, err):
1826
 
        """Strip the log.
1827
 
 
1828
 
        See _report_skip for motivation.
1829
 
        """
1830
 
        self._suppress_log()
1831
 
        super(TestCase, self)._report_expected_failure(self, result, err)
1832
 
 
1833
 
    @staticmethod
1834
 
    def _do_unsupported_or_skip(self, result, e):
1835
 
        reason = e.args[0]
1836
 
        self._suppress_log()
1837
 
        addNotSupported = getattr(result, 'addNotSupported', None)
1838
 
        if addNotSupported is not None:
1839
 
            result.addNotSupported(self, reason)
1840
 
        else:
1841
 
            self._do_skip(result, reason)
1842
 
 
1843
 
    def time(self, callable, *args, **kwargs):
1844
 
        """Run callable and accrue the time it takes to the benchmark time.
1845
 
 
1846
 
        If lsprofiling is enabled (i.e. by --lsprof-time to bzr selftest) then
1847
 
        this will cause lsprofile statistics to be gathered and stored in
1848
 
        self._benchcalls.
1849
 
        """
1850
 
        if self._benchtime is None:
1851
 
            self.addDetail('benchtime', content.Content(content.ContentType(
1852
 
                "text", "plain"), lambda:[str(self._benchtime)]))
1853
 
            self._benchtime = 0
1854
 
        start = time.time()
1855
 
        try:
1856
 
            if not self._gather_lsprof_in_benchmarks:
1857
 
                return callable(*args, **kwargs)
1858
 
            else:
1859
 
                # record this benchmark
1860
 
                ret, stats = bzrlib.lsprof.profile(callable, *args, **kwargs)
1861
 
                stats.sort()
1862
 
                self._benchcalls.append(((callable, args, kwargs), stats))
1863
 
                return ret
1864
 
        finally:
1865
 
            self._benchtime += time.time() - start
 
58
        # this replaces the default testsweet.TestCase; we don't want logging changed
 
59
        unittest.TestCase.setUp(self)
 
60
        bzrlib.trace.disable_default_logging()
 
61
        self._enable_file_logging()
 
62
 
 
63
 
 
64
    def _enable_file_logging(self):
 
65
        fileno, name = tempfile.mkstemp(suffix='.log', prefix='testbzr')
 
66
 
 
67
        self._log_file = os.fdopen(fileno, 'w+')
 
68
 
 
69
        hdlr = logging.StreamHandler(self._log_file)
 
70
        hdlr.setLevel(logging.DEBUG)
 
71
        hdlr.setFormatter(logging.Formatter('%(levelname)4.4s  %(message)s'))
 
72
        logging.getLogger('').addHandler(hdlr)
 
73
        logging.getLogger('').setLevel(logging.DEBUG)
 
74
        self._log_hdlr = hdlr
 
75
        debug('opened log file %s', name)
 
76
        
 
77
        self._log_file_name = name
 
78
 
 
79
    def tearDown(self):
 
80
        logging.getLogger('').removeHandler(self._log_hdlr)
 
81
        bzrlib.trace.enable_default_logging()
 
82
        logging.debug('%s teardown', self.id())
 
83
        self._log_file.close()
 
84
        unittest.TestCase.tearDown(self)
1866
85
 
1867
86
    def log(self, *args):
1868
 
        trace.mutter(*args)
1869
 
 
1870
 
    def get_log(self):
1871
 
        """Get a unicode string containing the log from bzrlib.trace.
1872
 
 
1873
 
        Undecodable characters are replaced.
1874
 
        """
1875
 
        return u"".join(self.getDetails()['log'].iter_text())
1876
 
 
1877
 
    def requireFeature(self, feature):
1878
 
        """This test requires a specific feature is available.
1879
 
 
1880
 
        :raises UnavailableFeature: When feature is not available.
1881
 
        """
1882
 
        if not feature.available():
1883
 
            raise UnavailableFeature(feature)
1884
 
 
1885
 
    def _run_bzr_autosplit(self, args, retcode, encoding, stdin,
1886
 
            working_dir):
1887
 
        """Run bazaar command line, splitting up a string command line."""
1888
 
        if isinstance(args, basestring):
1889
 
            # shlex don't understand unicode strings,
1890
 
            # so args should be plain string (bialix 20070906)
1891
 
            args = list(shlex.split(str(args)))
1892
 
        return self._run_bzr_core(args, retcode=retcode,
1893
 
                encoding=encoding, stdin=stdin, working_dir=working_dir,
1894
 
                )
1895
 
 
1896
 
    def _run_bzr_core(self, args, retcode, encoding, stdin,
1897
 
            working_dir):
1898
 
        # Clear chk_map page cache, because the contents are likely to mask
1899
 
        # locking errors.
1900
 
        chk_map.clear_cache()
1901
 
        if encoding is None:
1902
 
            encoding = osutils.get_user_encoding()
1903
 
        stdout = StringIOWrapper()
1904
 
        stderr = StringIOWrapper()
1905
 
        stdout.encoding = encoding
1906
 
        stderr.encoding = encoding
1907
 
 
1908
 
        self.log('run bzr: %r', args)
1909
 
        # FIXME: don't call into logging here
 
87
        logging.debug(*args)
 
88
 
 
89
    def _get_log(self):
 
90
        """Return as a string the log for this test"""
 
91
        return open(self._log_file_name).read()
 
92
 
 
93
 
 
94
    def capture(self, cmd):
 
95
        """Shortcut that splits cmd into words, runs, and returns stdout"""
 
96
        return self.run_bzr_captured(cmd.split())[0]
 
97
 
 
98
 
 
99
    def run_bzr_captured(self, argv, retcode=0):
 
100
        """Invoke bzr and return (result, stdout, stderr).
 
101
 
 
102
        Useful for code that wants to check the contents of the
 
103
        output, the way error messages are presented, etc.
 
104
 
 
105
        This should be the main method for tests that want to exercise the
 
106
        overall behavior of the bzr application (rather than a unit test
 
107
        or a functional test of the library.)
 
108
 
 
109
        Much of the old code runs bzr by forking a new copy of Python, but
 
110
        that is slower, harder to debug, and generally not necessary.
 
111
 
 
112
        This runs bzr through the interface that catches and reports
 
113
        errors, and with logging set to something approximating the
 
114
        default, so that error reporting can be checked.
 
115
 
 
116
        argv -- arguments to invoke bzr
 
117
        retcode -- expected return code, or None for don't-care.
 
118
        """
 
119
        stdout = StringIO()
 
120
        stderr = StringIO()
 
121
        self.log('run bzr: %s', ' '.join(argv))
1910
122
        handler = logging.StreamHandler(stderr)
 
123
        handler.setFormatter(bzrlib.trace.QuietFormatter())
1911
124
        handler.setLevel(logging.INFO)
1912
125
        logger = logging.getLogger('')
1913
126
        logger.addHandler(handler)
1914
 
        old_ui_factory = ui.ui_factory
1915
 
        ui.ui_factory = TestUIFactory(stdin=stdin, stdout=stdout, stderr=stderr)
1916
 
 
1917
 
        cwd = None
1918
 
        if working_dir is not None:
1919
 
            cwd = osutils.getcwd()
1920
 
            os.chdir(working_dir)
1921
 
 
1922
127
        try:
1923
 
            try:
1924
 
                result = self.apply_redirected(
1925
 
                    ui.ui_factory.stdin,
1926
 
                    stdout, stderr,
1927
 
                    _mod_commands.run_bzr_catch_user_errors,
1928
 
                    args)
1929
 
            except KeyboardInterrupt:
1930
 
                # Reraise KeyboardInterrupt with contents of redirected stdout
1931
 
                # and stderr as arguments, for tests which are interested in
1932
 
                # stdout and stderr and are expecting the exception.
1933
 
                out = stdout.getvalue()
1934
 
                err = stderr.getvalue()
1935
 
                if out:
1936
 
                    self.log('output:\n%r', out)
1937
 
                if err:
1938
 
                    self.log('errors:\n%r', err)
1939
 
                raise KeyboardInterrupt(out, err)
 
128
            result = self.apply_redirected(None, stdout, stderr,
 
129
                                           bzrlib.commands.run_bzr_catch_errors,
 
130
                                           argv)
1940
131
        finally:
1941
132
            logger.removeHandler(handler)
1942
 
            ui.ui_factory = old_ui_factory
1943
 
            if cwd is not None:
1944
 
                os.chdir(cwd)
1945
 
 
1946
133
        out = stdout.getvalue()
1947
134
        err = stderr.getvalue()
1948
135
        if out:
1949
 
            self.log('output:\n%r', out)
 
136
            self.log('output:\n%s', out)
1950
137
        if err:
1951
 
            self.log('errors:\n%r', err)
 
138
            self.log('errors:\n%s', err)
1952
139
        if retcode is not None:
1953
 
            self.assertEquals(retcode, result,
1954
 
                              message='Unexpected return code')
1955
 
        return result, out, err
1956
 
 
1957
 
    def run_bzr(self, args, retcode=0, encoding=None, stdin=None,
1958
 
                working_dir=None, error_regexes=[], output_encoding=None):
 
140
            self.assertEquals(result, retcode)
 
141
        return out, err
 
142
 
 
143
 
 
144
    def run_bzr(self, *args, **kwargs):
1959
145
        """Invoke bzr, as if it were run from the command line.
1960
146
 
1961
 
        The argument list should not include the bzr program name - the
1962
 
        first argument is normally the bzr command.  Arguments may be
1963
 
        passed in three ways:
1964
 
 
1965
 
        1- A list of strings, eg ["commit", "a"].  This is recommended
1966
 
        when the command contains whitespace or metacharacters, or
1967
 
        is built up at run time.
1968
 
 
1969
 
        2- A single string, eg "add a".  This is the most convenient
1970
 
        for hardcoded commands.
1971
 
 
1972
 
        This runs bzr through the interface that catches and reports
1973
 
        errors, and with logging set to something approximating the
1974
 
        default, so that error reporting can be checked.
1975
 
 
1976
147
        This should be the main method for tests that want to exercise the
1977
148
        overall behavior of the bzr application (rather than a unit test
1978
149
        or a functional test of the library.)
1979
150
 
1980
151
        This sends the stdout/stderr results into the test's log,
1981
152
        where it may be useful for debugging.  See also run_captured.
1982
 
 
1983
 
        :keyword stdin: A string to be used as stdin for the command.
1984
 
        :keyword retcode: The status code the command should return;
1985
 
            default 0.
1986
 
        :keyword working_dir: The directory to run the command in
1987
 
        :keyword error_regexes: A list of expected error messages.  If
1988
 
            specified they must be seen in the error output of the command.
1989
 
        """
1990
 
        retcode, out, err = self._run_bzr_autosplit(
1991
 
            args=args,
1992
 
            retcode=retcode,
1993
 
            encoding=encoding,
1994
 
            stdin=stdin,
1995
 
            working_dir=working_dir,
1996
 
            )
1997
 
        self.assertIsInstance(error_regexes, (list, tuple))
1998
 
        for regex in error_regexes:
1999
 
            self.assertContainsRe(err, regex)
2000
 
        return out, err
2001
 
 
2002
 
    def run_bzr_error(self, error_regexes, *args, **kwargs):
2003
 
        """Run bzr, and check that stderr contains the supplied regexes
2004
 
 
2005
 
        :param error_regexes: Sequence of regular expressions which
2006
 
            must each be found in the error output. The relative ordering
2007
 
            is not enforced.
2008
 
        :param args: command-line arguments for bzr
2009
 
        :param kwargs: Keyword arguments which are interpreted by run_bzr
2010
 
            This function changes the default value of retcode to be 3,
2011
 
            since in most cases this is run when you expect bzr to fail.
2012
 
 
2013
 
        :return: (out, err) The actual output of running the command (in case
2014
 
            you want to do more inspection)
2015
 
 
2016
 
        Examples of use::
2017
 
 
2018
 
            # Make sure that commit is failing because there is nothing to do
2019
 
            self.run_bzr_error(['no changes to commit'],
2020
 
                               ['commit', '-m', 'my commit comment'])
2021
 
            # Make sure --strict is handling an unknown file, rather than
2022
 
            # giving us the 'nothing to do' error
2023
 
            self.build_tree(['unknown'])
2024
 
            self.run_bzr_error(['Commit refused because there are unknown files'],
2025
 
                               ['commit', --strict', '-m', 'my commit comment'])
2026
 
        """
2027
 
        kwargs.setdefault('retcode', 3)
2028
 
        kwargs['error_regexes'] = error_regexes
2029
 
        out, err = self.run_bzr(*args, **kwargs)
2030
 
        return out, err
2031
 
 
2032
 
    def run_bzr_subprocess(self, *args, **kwargs):
2033
 
        """Run bzr in a subprocess for testing.
2034
 
 
2035
 
        This starts a new Python interpreter and runs bzr in there.
2036
 
        This should only be used for tests that have a justifiable need for
2037
 
        this isolation: e.g. they are testing startup time, or signal
2038
 
        handling, or early startup code, etc.  Subprocess code can't be
2039
 
        profiled or debugged so easily.
2040
 
 
2041
 
        :keyword retcode: The status code that is expected.  Defaults to 0.  If
2042
 
            None is supplied, the status code is not checked.
2043
 
        :keyword env_changes: A dictionary which lists changes to environment
2044
 
            variables. A value of None will unset the env variable.
2045
 
            The values must be strings. The change will only occur in the
2046
 
            child, so you don't need to fix the environment after running.
2047
 
        :keyword universal_newlines: Convert CRLF => LF
2048
 
        :keyword allow_plugins: By default the subprocess is run with
2049
 
            --no-plugins to ensure test reproducibility. Also, it is possible
2050
 
            for system-wide plugins to create unexpected output on stderr,
2051
 
            which can cause unnecessary test failures.
2052
 
        """
2053
 
        env_changes = kwargs.get('env_changes', {})
2054
 
        working_dir = kwargs.get('working_dir', None)
2055
 
        allow_plugins = kwargs.get('allow_plugins', False)
2056
 
        if len(args) == 1:
2057
 
            if isinstance(args[0], list):
2058
 
                args = args[0]
2059
 
            elif isinstance(args[0], basestring):
2060
 
                args = list(shlex.split(args[0]))
2061
 
        else:
2062
 
            raise ValueError("passing varargs to run_bzr_subprocess")
2063
 
        process = self.start_bzr_subprocess(args, env_changes=env_changes,
2064
 
                                            working_dir=working_dir,
2065
 
                                            allow_plugins=allow_plugins)
2066
 
        # We distinguish between retcode=None and retcode not passed.
2067
 
        supplied_retcode = kwargs.get('retcode', 0)
2068
 
        return self.finish_bzr_subprocess(process, retcode=supplied_retcode,
2069
 
            universal_newlines=kwargs.get('universal_newlines', False),
2070
 
            process_args=args)
2071
 
 
2072
 
    def start_bzr_subprocess(self, process_args, env_changes=None,
2073
 
                             skip_if_plan_to_signal=False,
2074
 
                             working_dir=None,
2075
 
                             allow_plugins=False, stderr=subprocess.PIPE):
2076
 
        """Start bzr in a subprocess for testing.
2077
 
 
2078
 
        This starts a new Python interpreter and runs bzr in there.
2079
 
        This should only be used for tests that have a justifiable need for
2080
 
        this isolation: e.g. they are testing startup time, or signal
2081
 
        handling, or early startup code, etc.  Subprocess code can't be
2082
 
        profiled or debugged so easily.
2083
 
 
2084
 
        :param process_args: a list of arguments to pass to the bzr executable,
2085
 
            for example ``['--version']``.
2086
 
        :param env_changes: A dictionary which lists changes to environment
2087
 
            variables. A value of None will unset the env variable.
2088
 
            The values must be strings. The change will only occur in the
2089
 
            child, so you don't need to fix the environment after running.
2090
 
        :param skip_if_plan_to_signal: raise TestSkipped when true and system
2091
 
            doesn't support signalling subprocesses.
2092
 
        :param allow_plugins: If False (default) pass --no-plugins to bzr.
2093
 
        :param stderr: file to use for the subprocess's stderr.  Valid values
2094
 
            are those valid for the stderr argument of `subprocess.Popen`.
2095
 
            Default value is ``subprocess.PIPE``.
2096
 
 
2097
 
        :returns: Popen object for the started process.
2098
 
        """
2099
 
        if skip_if_plan_to_signal:
2100
 
            if os.name != "posix":
2101
 
                raise TestSkipped("Sending signals not supported")
2102
 
 
2103
 
        if env_changes is None:
2104
 
            env_changes = {}
2105
 
        old_env = {}
2106
 
 
2107
 
        def cleanup_environment():
2108
 
            for env_var, value in env_changes.iteritems():
2109
 
                old_env[env_var] = osutils.set_or_unset_env(env_var, value)
2110
 
 
2111
 
        def restore_environment():
2112
 
            for env_var, value in old_env.iteritems():
2113
 
                osutils.set_or_unset_env(env_var, value)
2114
 
 
2115
 
        bzr_path = self.get_bzr_path()
2116
 
 
2117
 
        cwd = None
2118
 
        if working_dir is not None:
2119
 
            cwd = osutils.getcwd()
2120
 
            os.chdir(working_dir)
2121
 
 
2122
 
        try:
2123
 
            # win32 subprocess doesn't support preexec_fn
2124
 
            # so we will avoid using it on all platforms, just to
2125
 
            # make sure the code path is used, and we don't break on win32
2126
 
            cleanup_environment()
2127
 
            # Include the subprocess's log file in the test details, in case
2128
 
            # the test fails due to an error in the subprocess.
2129
 
            self._add_subprocess_log(trace._get_bzr_log_filename())
2130
 
            command = [sys.executable]
2131
 
            # frozen executables don't need the path to bzr
2132
 
            if getattr(sys, "frozen", None) is None:
2133
 
                command.append(bzr_path)
2134
 
            if not allow_plugins:
2135
 
                command.append('--no-plugins')
2136
 
            command.extend(process_args)
2137
 
            process = self._popen(command, stdin=subprocess.PIPE,
2138
 
                                  stdout=subprocess.PIPE,
2139
 
                                  stderr=stderr)
2140
 
        finally:
2141
 
            restore_environment()
2142
 
            if cwd is not None:
2143
 
                os.chdir(cwd)
2144
 
 
2145
 
        return process
2146
 
 
2147
 
    def _add_subprocess_log(self, log_file_path):
2148
 
        if len(self._log_files) == 0:
2149
 
            # Register an addCleanup func.  We do this on the first call to
2150
 
            # _add_subprocess_log rather than in TestCase.setUp so that this
2151
 
            # addCleanup is registered after any cleanups for tempdirs that
2152
 
            # subclasses might create, which will probably remove the log file
2153
 
            # we want to read.
2154
 
            self.addCleanup(self._subprocess_log_cleanup)
2155
 
        # self._log_files is a set, so if a log file is reused we won't grab it
2156
 
        # twice.
2157
 
        self._log_files.add(log_file_path)
2158
 
 
2159
 
    def _subprocess_log_cleanup(self):
2160
 
        for count, log_file_path in enumerate(self._log_files):
2161
 
            # We use buffer_now=True to avoid holding the file open beyond
2162
 
            # the life of this function, which might interfere with e.g.
2163
 
            # cleaning tempdirs on Windows.
2164
 
            # XXX: Testtools 0.9.5 doesn't have the content_from_file helper
2165
 
            #detail_content = content.content_from_file(
2166
 
            #    log_file_path, buffer_now=True)
2167
 
            with open(log_file_path, 'rb') as log_file:
2168
 
                log_file_bytes = log_file.read()
2169
 
            detail_content = content.Content(content.ContentType("text",
2170
 
                "plain", {"charset": "utf8"}), lambda: [log_file_bytes])
2171
 
            self.addDetail("start_bzr_subprocess-log-%d" % (count,),
2172
 
                detail_content)
2173
 
 
2174
 
    def _popen(self, *args, **kwargs):
2175
 
        """Place a call to Popen.
2176
 
 
2177
 
        Allows tests to override this method to intercept the calls made to
2178
 
        Popen for introspection.
2179
 
        """
2180
 
        return subprocess.Popen(*args, **kwargs)
2181
 
 
2182
 
    def get_source_path(self):
2183
 
        """Return the path of the directory containing bzrlib."""
2184
 
        return os.path.dirname(os.path.dirname(bzrlib.__file__))
2185
 
 
2186
 
    def get_bzr_path(self):
2187
 
        """Return the path of the 'bzr' executable for this test suite."""
2188
 
        bzr_path = os.path.join(self.get_source_path(), "bzr")
2189
 
        if not os.path.isfile(bzr_path):
2190
 
            # We are probably installed. Assume sys.argv is the right file
2191
 
            bzr_path = sys.argv[0]
2192
 
        return bzr_path
2193
 
 
2194
 
    def finish_bzr_subprocess(self, process, retcode=0, send_signal=None,
2195
 
                              universal_newlines=False, process_args=None):
2196
 
        """Finish the execution of process.
2197
 
 
2198
 
        :param process: the Popen object returned from start_bzr_subprocess.
2199
 
        :param retcode: The status code that is expected.  Defaults to 0.  If
2200
 
            None is supplied, the status code is not checked.
2201
 
        :param send_signal: an optional signal to send to the process.
2202
 
        :param universal_newlines: Convert CRLF => LF
2203
 
        :returns: (stdout, stderr)
2204
 
        """
2205
 
        if send_signal is not None:
2206
 
            os.kill(process.pid, send_signal)
2207
 
        out, err = process.communicate()
2208
 
 
2209
 
        if universal_newlines:
2210
 
            out = out.replace('\r\n', '\n')
2211
 
            err = err.replace('\r\n', '\n')
2212
 
 
2213
 
        if retcode is not None and retcode != process.returncode:
2214
 
            if process_args is None:
2215
 
                process_args = "(unknown args)"
2216
 
            trace.mutter('Output of bzr %s:\n%s', process_args, out)
2217
 
            trace.mutter('Error for bzr %s:\n%s', process_args, err)
2218
 
            self.fail('Command bzr %s failed with retcode %s != %s'
2219
 
                      % (process_args, retcode, process.returncode))
2220
 
        return [out, err]
2221
 
 
2222
 
    def check_tree_shape(self, tree, shape):
2223
 
        """Compare a tree to a list of expected names.
 
153
        """
 
154
        retcode = kwargs.pop('retcode', 0)
 
155
        return self.run_bzr_captured(args, retcode)
 
156
 
 
157
 
 
158
    def check_inventory_shape(self, inv, shape):
 
159
        """
 
160
        Compare an inventory to a list of expected names.
2224
161
 
2225
162
        Fail if they are not precisely equal.
2226
163
        """
2227
164
        extras = []
2228
165
        shape = list(shape)             # copy
2229
 
        for path, ie in tree.iter_entries_by_dir():
 
166
        for path, ie in inv.entries():
2230
167
            name = path.replace('\\', '/')
2231
 
            if ie.kind == 'directory':
 
168
            if ie.kind == 'dir':
2232
169
                name = name + '/'
2233
 
            if name == "/":
2234
 
                pass # ignore root entry
2235
 
            elif name in shape:
 
170
            if name in shape:
2236
171
                shape.remove(name)
2237
172
            else:
2238
173
                extras.append(name)
2251
186
        if stdin is None:
2252
187
            stdin = StringIO("")
2253
188
        if stdout is None:
2254
 
            if getattr(self, "_log_file", None) is not None:
 
189
            if hasattr(self, "_log_file"):
2255
190
                stdout = self._log_file
2256
191
            else:
2257
192
                stdout = StringIO()
2258
193
        if stderr is None:
2259
 
            if getattr(self, "_log_file", None is not None):
 
194
            if hasattr(self, "_log_file"):
2260
195
                stderr = self._log_file
2261
196
            else:
2262
197
                stderr = StringIO()
2273
208
            sys.stderr = real_stderr
2274
209
            sys.stdin = real_stdin
2275
210
 
2276
 
    def reduceLockdirTimeout(self):
2277
 
        """Reduce the default lock timeout for the duration of the test, so that
2278
 
        if LockContention occurs during a test, it does so quickly.
2279
 
 
2280
 
        Tests that expect to provoke LockContention errors should call this.
2281
 
        """
2282
 
        self.overrideAttr(lockdir, '_DEFAULT_TIMEOUT_SECONDS', 0)
2283
 
 
2284
 
    def make_utf8_encoded_stringio(self, encoding_type=None):
2285
 
        """Return a StringIOWrapper instance, that will encode Unicode
2286
 
        input to UTF-8.
2287
 
        """
2288
 
        if encoding_type is None:
2289
 
            encoding_type = 'strict'
2290
 
        sio = StringIO()
2291
 
        output_encoding = 'utf-8'
2292
 
        sio = codecs.getwriter(output_encoding)(sio, errors=encoding_type)
2293
 
        sio.encoding = output_encoding
2294
 
        return sio
2295
 
 
2296
 
    def disable_verb(self, verb):
2297
 
        """Disable a smart server verb for one test."""
2298
 
        from bzrlib.smart import request
2299
 
        request_handlers = request.request_handlers
2300
 
        orig_method = request_handlers.get(verb)
2301
 
        request_handlers.remove(verb)
2302
 
        self.addCleanup(request_handlers.register, verb, orig_method)
2303
 
 
2304
 
 
2305
 
class CapturedCall(object):
2306
 
    """A helper for capturing smart server calls for easy debug analysis."""
2307
 
 
2308
 
    def __init__(self, params, prefix_length):
2309
 
        """Capture the call with params and skip prefix_length stack frames."""
2310
 
        self.call = params
2311
 
        import traceback
2312
 
        # The last 5 frames are the __init__, the hook frame, and 3 smart
2313
 
        # client frames. Beyond this we could get more clever, but this is good
2314
 
        # enough for now.
2315
 
        stack = traceback.extract_stack()[prefix_length:-5]
2316
 
        self.stack = ''.join(traceback.format_list(stack))
2317
 
 
2318
 
    def __str__(self):
2319
 
        return self.call.method
2320
 
 
2321
 
    def __repr__(self):
2322
 
        return self.call.method
2323
 
 
2324
 
    def stack(self):
2325
 
        return self.stack
2326
 
 
2327
 
 
2328
 
class TestCaseWithMemoryTransport(TestCase):
2329
 
    """Common test class for tests that do not need disk resources.
2330
 
 
2331
 
    Tests that need disk resources should derive from TestCaseInTempDir
2332
 
    orTestCaseWithTransport.
2333
 
 
2334
 
    TestCaseWithMemoryTransport sets the TEST_ROOT variable for all bzr tests.
2335
 
 
2336
 
    For TestCaseWithMemoryTransport the ``test_home_dir`` is set to the name of
2337
 
    a directory which does not exist. This serves to help ensure test isolation
2338
 
    is preserved. ``test_dir`` is set to the TEST_ROOT, as is cwd, because they
2339
 
    must exist. However, TestCaseWithMemoryTransport does not offer local file
2340
 
    defaults for the transport in tests, nor does it obey the command line
2341
 
    override, so tests that accidentally write to the common directory should
2342
 
    be rare.
2343
 
 
2344
 
    :cvar TEST_ROOT: Directory containing all temporary directories, plus a
2345
 
        ``.bzr`` directory that stops us ascending higher into the filesystem.
2346
 
    """
2347
 
 
2348
 
    TEST_ROOT = None
2349
 
    _TEST_NAME = 'test'
2350
 
 
2351
 
    def __init__(self, methodName='runTest'):
2352
 
        # allow test parameterization after test construction and before test
2353
 
        # execution. Variables that the parameterizer sets need to be
2354
 
        # ones that are not set by setUp, or setUp will trash them.
2355
 
        super(TestCaseWithMemoryTransport, self).__init__(methodName)
2356
 
        self.vfs_transport_factory = default_transport
2357
 
        self.transport_server = None
2358
 
        self.transport_readonly_server = None
2359
 
        self.__vfs_server = None
2360
 
 
2361
 
    def get_transport(self, relpath=None):
2362
 
        """Return a writeable transport.
2363
 
 
2364
 
        This transport is for the test scratch space relative to
2365
 
        "self._test_root"
2366
 
 
2367
 
        :param relpath: a path relative to the base url.
2368
 
        """
2369
 
        t = _mod_transport.get_transport(self.get_url(relpath))
2370
 
        self.assertFalse(t.is_readonly())
2371
 
        return t
2372
 
 
2373
 
    def get_readonly_transport(self, relpath=None):
2374
 
        """Return a readonly transport for the test scratch space
2375
 
 
2376
 
        This can be used to test that operations which should only need
2377
 
        readonly access in fact do not try to write.
2378
 
 
2379
 
        :param relpath: a path relative to the base url.
2380
 
        """
2381
 
        t = _mod_transport.get_transport(self.get_readonly_url(relpath))
2382
 
        self.assertTrue(t.is_readonly())
2383
 
        return t
2384
 
 
2385
 
    def create_transport_readonly_server(self):
2386
 
        """Create a transport server from class defined at init.
2387
 
 
2388
 
        This is mostly a hook for daughter classes.
2389
 
        """
2390
 
        return self.transport_readonly_server()
2391
 
 
2392
 
    def get_readonly_server(self):
2393
 
        """Get the server instance for the readonly transport
2394
 
 
2395
 
        This is useful for some tests with specific servers to do diagnostics.
2396
 
        """
2397
 
        if self.__readonly_server is None:
2398
 
            if self.transport_readonly_server is None:
2399
 
                # readonly decorator requested
2400
 
                self.__readonly_server = test_server.ReadonlyServer()
2401
 
            else:
2402
 
                # explicit readonly transport.
2403
 
                self.__readonly_server = self.create_transport_readonly_server()
2404
 
            self.start_server(self.__readonly_server,
2405
 
                self.get_vfs_only_server())
2406
 
        return self.__readonly_server
2407
 
 
2408
 
    def get_readonly_url(self, relpath=None):
2409
 
        """Get a URL for the readonly transport.
2410
 
 
2411
 
        This will either be backed by '.' or a decorator to the transport
2412
 
        used by self.get_url()
2413
 
        relpath provides for clients to get a path relative to the base url.
2414
 
        These should only be downwards relative, not upwards.
2415
 
        """
2416
 
        base = self.get_readonly_server().get_url()
2417
 
        return self._adjust_url(base, relpath)
2418
 
 
2419
 
    def get_vfs_only_server(self):
2420
 
        """Get the vfs only read/write server instance.
2421
 
 
2422
 
        This is useful for some tests with specific servers that need
2423
 
        diagnostics.
2424
 
 
2425
 
        For TestCaseWithMemoryTransport this is always a MemoryServer, and there
2426
 
        is no means to override it.
2427
 
        """
2428
 
        if self.__vfs_server is None:
2429
 
            self.__vfs_server = memory.MemoryServer()
2430
 
            self.start_server(self.__vfs_server)
2431
 
        return self.__vfs_server
2432
 
 
2433
 
    def get_server(self):
2434
 
        """Get the read/write server instance.
2435
 
 
2436
 
        This is useful for some tests with specific servers that need
2437
 
        diagnostics.
2438
 
 
2439
 
        This is built from the self.transport_server factory. If that is None,
2440
 
        then the self.get_vfs_server is returned.
2441
 
        """
2442
 
        if self.__server is None:
2443
 
            if (self.transport_server is None or self.transport_server is
2444
 
                self.vfs_transport_factory):
2445
 
                self.__server = self.get_vfs_only_server()
2446
 
            else:
2447
 
                # bring up a decorated means of access to the vfs only server.
2448
 
                self.__server = self.transport_server()
2449
 
                self.start_server(self.__server, self.get_vfs_only_server())
2450
 
        return self.__server
2451
 
 
2452
 
    def _adjust_url(self, base, relpath):
2453
 
        """Get a URL (or maybe a path) for the readwrite transport.
2454
 
 
2455
 
        This will either be backed by '.' or to an equivalent non-file based
2456
 
        facility.
2457
 
        relpath provides for clients to get a path relative to the base url.
2458
 
        These should only be downwards relative, not upwards.
2459
 
        """
2460
 
        if relpath is not None and relpath != '.':
2461
 
            if not base.endswith('/'):
2462
 
                base = base + '/'
2463
 
            # XXX: Really base should be a url; we did after all call
2464
 
            # get_url()!  But sometimes it's just a path (from
2465
 
            # LocalAbspathServer), and it'd be wrong to append urlescaped data
2466
 
            # to a non-escaped local path.
2467
 
            if base.startswith('./') or base.startswith('/'):
2468
 
                base += relpath
2469
 
            else:
2470
 
                base += urlutils.escape(relpath)
2471
 
        return base
2472
 
 
2473
 
    def get_url(self, relpath=None):
2474
 
        """Get a URL (or maybe a path) for the readwrite transport.
2475
 
 
2476
 
        This will either be backed by '.' or to an equivalent non-file based
2477
 
        facility.
2478
 
        relpath provides for clients to get a path relative to the base url.
2479
 
        These should only be downwards relative, not upwards.
2480
 
        """
2481
 
        base = self.get_server().get_url()
2482
 
        return self._adjust_url(base, relpath)
2483
 
 
2484
 
    def get_vfs_only_url(self, relpath=None):
2485
 
        """Get a URL (or maybe a path for the plain old vfs transport.
2486
 
 
2487
 
        This will never be a smart protocol.  It always has all the
2488
 
        capabilities of the local filesystem, but it might actually be a
2489
 
        MemoryTransport or some other similar virtual filesystem.
2490
 
 
2491
 
        This is the backing transport (if any) of the server returned by
2492
 
        get_url and get_readonly_url.
2493
 
 
2494
 
        :param relpath: provides for clients to get a path relative to the base
2495
 
            url.  These should only be downwards relative, not upwards.
2496
 
        :return: A URL
2497
 
        """
2498
 
        base = self.get_vfs_only_server().get_url()
2499
 
        return self._adjust_url(base, relpath)
2500
 
 
2501
 
    def _create_safety_net(self):
2502
 
        """Make a fake bzr directory.
2503
 
 
2504
 
        This prevents any tests propagating up onto the TEST_ROOT directory's
2505
 
        real branch.
2506
 
        """
2507
 
        root = TestCaseWithMemoryTransport.TEST_ROOT
2508
 
        wt = bzrdir.BzrDir.create_standalone_workingtree(root)
2509
 
        # Hack for speed: remember the raw bytes of the dirstate file so that
2510
 
        # we don't need to re-open the wt to check it hasn't changed.
2511
 
        TestCaseWithMemoryTransport._SAFETY_NET_PRISTINE_DIRSTATE = (
2512
 
            wt.control_transport.get_bytes('dirstate'))
2513
 
 
2514
 
    def _check_safety_net(self):
2515
 
        """Check that the safety .bzr directory have not been touched.
2516
 
 
2517
 
        _make_test_root have created a .bzr directory to prevent tests from
2518
 
        propagating. This method ensures than a test did not leaked.
2519
 
        """
2520
 
        root = TestCaseWithMemoryTransport.TEST_ROOT
2521
 
        t = _mod_transport.get_transport(root)
2522
 
        self.permit_url(t.base)
2523
 
        if (t.get_bytes('.bzr/checkout/dirstate') != 
2524
 
                TestCaseWithMemoryTransport._SAFETY_NET_PRISTINE_DIRSTATE):
2525
 
            # The current test have modified the /bzr directory, we need to
2526
 
            # recreate a new one or all the followng tests will fail.
2527
 
            # If you need to inspect its content uncomment the following line
2528
 
            # import pdb; pdb.set_trace()
2529
 
            _rmtree_temp_dir(root + '/.bzr', test_id=self.id())
2530
 
            self._create_safety_net()
2531
 
            raise AssertionError('%s/.bzr should not be modified' % root)
2532
 
 
2533
 
    def _make_test_root(self):
2534
 
        if TestCaseWithMemoryTransport.TEST_ROOT is None:
2535
 
            # Watch out for tricky test dir (on OSX /tmp -> /private/tmp)
2536
 
            root = osutils.realpath(osutils.mkdtemp(prefix='testbzr-',
2537
 
                                                    suffix='.tmp'))
2538
 
            TestCaseWithMemoryTransport.TEST_ROOT = root
2539
 
 
2540
 
            self._create_safety_net()
2541
 
 
2542
 
            # The same directory is used by all tests, and we're not
2543
 
            # specifically told when all tests are finished.  This will do.
2544
 
            atexit.register(_rmtree_temp_dir, root)
2545
 
 
2546
 
        self.permit_dir(TestCaseWithMemoryTransport.TEST_ROOT)
2547
 
        self.addCleanup(self._check_safety_net)
2548
 
 
2549
 
    def makeAndChdirToTestDir(self):
2550
 
        """Create a temporary directories for this one test.
2551
 
 
2552
 
        This must set self.test_home_dir and self.test_dir and chdir to
2553
 
        self.test_dir.
2554
 
 
2555
 
        For TestCaseWithMemoryTransport we chdir to the TEST_ROOT for this test.
2556
 
        """
2557
 
        os.chdir(TestCaseWithMemoryTransport.TEST_ROOT)
2558
 
        self.test_dir = TestCaseWithMemoryTransport.TEST_ROOT
2559
 
        self.test_home_dir = self.test_dir + "/MemoryTransportMissingHomeDir"
2560
 
        self.permit_dir(self.test_dir)
2561
 
 
2562
 
    def make_branch(self, relpath, format=None):
2563
 
        """Create a branch on the transport at relpath."""
2564
 
        repo = self.make_repository(relpath, format=format)
2565
 
        return repo.bzrdir.create_branch()
2566
 
 
2567
 
    def make_bzrdir(self, relpath, format=None):
2568
 
        try:
2569
 
            # might be a relative or absolute path
2570
 
            maybe_a_url = self.get_url(relpath)
2571
 
            segments = maybe_a_url.rsplit('/', 1)
2572
 
            t = _mod_transport.get_transport(maybe_a_url)
2573
 
            if len(segments) > 1 and segments[-1] not in ('', '.'):
2574
 
                t.ensure_base()
2575
 
            if format is None:
2576
 
                format = 'default'
2577
 
            if isinstance(format, basestring):
2578
 
                format = bzrdir.format_registry.make_bzrdir(format)
2579
 
            return format.initialize_on_transport(t)
2580
 
        except errors.UninitializableFormat:
2581
 
            raise TestSkipped("Format %s is not initializable." % format)
2582
 
 
2583
 
    def make_repository(self, relpath, shared=False, format=None):
2584
 
        """Create a repository on our default transport at relpath.
2585
 
 
2586
 
        Note that relpath must be a relative path, not a full url.
2587
 
        """
2588
 
        # FIXME: If you create a remoterepository this returns the underlying
2589
 
        # real format, which is incorrect.  Actually we should make sure that
2590
 
        # RemoteBzrDir returns a RemoteRepository.
2591
 
        # maybe  mbp 20070410
2592
 
        made_control = self.make_bzrdir(relpath, format=format)
2593
 
        return made_control.create_repository(shared=shared)
2594
 
 
2595
 
    def make_smart_server(self, path, backing_server=None):
2596
 
        if backing_server is None:
2597
 
            backing_server = self.get_server()
2598
 
        smart_server = test_server.SmartTCPServer_for_testing()
2599
 
        self.start_server(smart_server, backing_server)
2600
 
        remote_transport = _mod_transport.get_transport(smart_server.get_url()
2601
 
                                                   ).clone(path)
2602
 
        return remote_transport
2603
 
 
2604
 
    def make_branch_and_memory_tree(self, relpath, format=None):
2605
 
        """Create a branch on the default transport and a MemoryTree for it."""
2606
 
        b = self.make_branch(relpath, format=format)
2607
 
        return memorytree.MemoryTree.create_on_branch(b)
2608
 
 
2609
 
    def make_branch_builder(self, relpath, format=None):
2610
 
        branch = self.make_branch(relpath, format=format)
2611
 
        return branchbuilder.BranchBuilder(branch=branch)
2612
 
 
2613
 
    def overrideEnvironmentForTesting(self):
2614
 
        test_home_dir = self.test_home_dir
2615
 
        if isinstance(test_home_dir, unicode):
2616
 
            test_home_dir = test_home_dir.encode(sys.getfilesystemencoding())
2617
 
        self.overrideEnv('HOME', test_home_dir)
2618
 
        self.overrideEnv('BZR_HOME', test_home_dir)
2619
 
 
2620
 
    def setUp(self):
2621
 
        super(TestCaseWithMemoryTransport, self).setUp()
2622
 
        # Ensure that ConnectedTransport doesn't leak sockets
2623
 
        def get_transport_with_cleanup(*args, **kwargs):
2624
 
            t = orig_get_transport(*args, **kwargs)
2625
 
            if isinstance(t, _mod_transport.ConnectedTransport):
2626
 
                self.addCleanup(t.disconnect)
2627
 
            return t
2628
 
 
2629
 
        orig_get_transport = self.overrideAttr(_mod_transport, 'get_transport',
2630
 
                                               get_transport_with_cleanup)
2631
 
        self._make_test_root()
2632
 
        self.addCleanup(os.chdir, os.getcwdu())
2633
 
        self.makeAndChdirToTestDir()
2634
 
        self.overrideEnvironmentForTesting()
2635
 
        self.__readonly_server = None
2636
 
        self.__server = None
2637
 
        self.reduceLockdirTimeout()
2638
 
 
2639
 
    def setup_smart_server_with_call_log(self):
2640
 
        """Sets up a smart server as the transport server with a call log."""
2641
 
        self.transport_server = test_server.SmartTCPServer_for_testing
2642
 
        self.hpss_calls = []
2643
 
        import traceback
2644
 
        # Skip the current stack down to the caller of
2645
 
        # setup_smart_server_with_call_log
2646
 
        prefix_length = len(traceback.extract_stack()) - 2
2647
 
        def capture_hpss_call(params):
2648
 
            self.hpss_calls.append(
2649
 
                CapturedCall(params, prefix_length))
2650
 
        client._SmartClient.hooks.install_named_hook(
2651
 
            'call', capture_hpss_call, None)
2652
 
 
2653
 
    def reset_smart_call_log(self):
2654
 
        self.hpss_calls = []
2655
 
 
2656
 
 
2657
 
class TestCaseInTempDir(TestCaseWithMemoryTransport):
 
211
 
 
212
BzrTestBase = TestCase
 
213
 
 
214
     
 
215
class TestCaseInTempDir(TestCase):
2658
216
    """Derived class that runs a test within a temporary directory.
2659
217
 
2660
218
    This is useful for tests that need to create a branch, etc.
2664
222
    All test cases create their own directory within that.  If the
2665
223
    tests complete successfully, the directory is removed.
2666
224
 
2667
 
    :ivar test_base_dir: The path of the top-level directory for this
2668
 
    test, which contains a home directory and a work directory.
2669
 
 
2670
 
    :ivar test_home_dir: An initially empty directory under test_base_dir
2671
 
    which is used as $HOME for this test.
2672
 
 
2673
 
    :ivar test_dir: A directory under test_base_dir used as the current
2674
 
    directory when the test proper is run.
 
225
    InTempDir is an old alias for FunctionalTestCase.
2675
226
    """
2676
227
 
 
228
    TEST_ROOT = None
 
229
    _TEST_NAME = 'test'
2677
230
    OVERRIDE_PYTHON = 'python'
2678
231
 
2679
 
    def setUp(self):
2680
 
        super(TestCaseInTempDir, self).setUp()
2681
 
        # Remove the protection set in isolated_environ, we have a proper
2682
 
        # access to disk resources now.
2683
 
        self.overrideEnv('BZR_LOG', None)
2684
 
 
2685
232
    def check_file_contents(self, filename, expect):
2686
233
        self.log("check contents of file %s" % filename)
2687
 
        f = file(filename)
2688
 
        try:
2689
 
            contents = f.read()
2690
 
        finally:
2691
 
            f.close()
 
234
        contents = file(filename, 'r').read()
2692
235
        if contents != expect:
2693
236
            self.log("expected: %r" % expect)
2694
237
            self.log("actually: %r" % contents)
2695
 
            self.fail("contents of %s not as expected" % filename)
2696
 
 
2697
 
    def _getTestDirPrefix(self):
2698
 
        # create a directory within the top level test directory
2699
 
        if sys.platform in ('win32', 'cygwin'):
2700
 
            name_prefix = re.sub('[<>*=+",:;_/\\-]', '_', self.id())
2701
 
            # windows is likely to have path-length limits so use a short name
2702
 
            name_prefix = name_prefix[-30:]
2703
 
        else:
2704
 
            name_prefix = re.sub('[/]', '_', self.id())
2705
 
        return name_prefix
2706
 
 
2707
 
    def makeAndChdirToTestDir(self):
2708
 
        """See TestCaseWithMemoryTransport.makeAndChdirToTestDir().
2709
 
 
2710
 
        For TestCaseInTempDir we create a temporary directory based on the test
2711
 
        name and then create two subdirs - test and home under it.
2712
 
        """
2713
 
        name_prefix = osutils.pathjoin(TestCaseWithMemoryTransport.TEST_ROOT,
2714
 
            self._getTestDirPrefix())
2715
 
        name = name_prefix
2716
 
        for i in range(100):
2717
 
            if os.path.exists(name):
2718
 
                name = name_prefix + '_' + str(i)
2719
 
            else:
2720
 
                # now create test and home directories within this dir
2721
 
                self.test_base_dir = name
2722
 
                self.addCleanup(self.deleteTestDir)
2723
 
                os.mkdir(self.test_base_dir)
2724
 
                break
2725
 
        self.permit_dir(self.test_base_dir)
2726
 
        # 'sprouting' and 'init' of a branch both walk up the tree to find
2727
 
        # stacking policy to honour; create a bzr dir with an unshared
2728
 
        # repository (but not a branch - our code would be trying to escape
2729
 
        # then!) to stop them, and permit it to be read.
2730
 
        # control = bzrdir.BzrDir.create(self.test_base_dir)
2731
 
        # control.create_repository()
2732
 
        self.test_home_dir = self.test_base_dir + '/home'
2733
 
        os.mkdir(self.test_home_dir)
2734
 
        self.test_dir = self.test_base_dir + '/work'
 
238
            self.fail("contents of %s not as expected")
 
239
 
 
240
    def _make_test_root(self):
 
241
        if TestCaseInTempDir.TEST_ROOT is not None:
 
242
            return
 
243
        i = 0
 
244
        while True:
 
245
            root = 'test%04d.tmp' % i
 
246
            try:
 
247
                os.mkdir(root)
 
248
            except OSError, e:
 
249
                if e.errno == errno.EEXIST:
 
250
                    i += 1
 
251
                    continue
 
252
                else:
 
253
                    raise
 
254
            # successfully created
 
255
            TestCaseInTempDir.TEST_ROOT = os.path.abspath(root)
 
256
            break
 
257
        # make a fake bzr directory there to prevent any tests propagating
 
258
        # up onto the source directory's real branch
 
259
        os.mkdir(os.path.join(TestCaseInTempDir.TEST_ROOT, '.bzr'))
 
260
 
 
261
    def setUp(self):
 
262
        super(TestCaseInTempDir, self).setUp()
 
263
        self._make_test_root()
 
264
        self._currentdir = os.getcwdu()
 
265
        self.test_dir = os.path.join(self.TEST_ROOT, self.id())
2735
266
        os.mkdir(self.test_dir)
2736
267
        os.chdir(self.test_dir)
2737
 
        # put name of test inside
2738
 
        f = file(self.test_base_dir + '/name', 'w')
2739
 
        try:
2740
 
            f.write(self.id())
2741
 
        finally:
2742
 
            f.close()
2743
 
 
2744
 
    def deleteTestDir(self):
2745
 
        os.chdir(TestCaseWithMemoryTransport.TEST_ROOT)
2746
 
        _rmtree_temp_dir(self.test_base_dir, test_id=self.id())
2747
 
 
2748
 
    def build_tree(self, shape, line_endings='binary', transport=None):
 
268
        
 
269
    def tearDown(self):
 
270
        os.chdir(self._currentdir)
 
271
        super(TestCaseInTempDir, self).tearDown()
 
272
 
 
273
    
 
274
    def build_tree(self, shape):
2749
275
        """Build a test tree according to a pattern.
2750
276
 
2751
277
        shape is a sequence of file specifications.  If the final
2752
278
        character is '/', a directory is created.
2753
279
 
2754
 
        This assumes that all the elements in the tree being built are new.
2755
 
 
2756
280
        This doesn't add anything to a branch.
2757
 
 
2758
 
        :type shape:    list or tuple.
2759
 
        :param line_endings: Either 'binary' or 'native'
2760
 
            in binary mode, exact contents are written in native mode, the
2761
 
            line endings match the default platform endings.
2762
 
        :param transport: A transport to write to, for building trees on VFS's.
2763
 
            If the transport is readonly or None, "." is opened automatically.
2764
 
        :return: None
2765
281
        """
2766
 
        if type(shape) not in (list, tuple):
2767
 
            raise AssertionError("Parameter 'shape' should be "
2768
 
                "a list or a tuple. Got %r instead" % (shape,))
2769
 
        # It's OK to just create them using forward slashes on windows.
2770
 
        if transport is None or transport.is_readonly():
2771
 
            transport = _mod_transport.get_transport(".")
 
282
        # XXX: It's OK to just create them using forward slashes on windows?
2772
283
        for name in shape:
2773
 
            self.assertIsInstance(name, basestring)
 
284
            assert isinstance(name, basestring)
2774
285
            if name[-1] == '/':
2775
 
                transport.mkdir(urlutils.escape(name[:-1]))
2776
 
            else:
2777
 
                if line_endings == 'binary':
2778
 
                    end = '\n'
2779
 
                elif line_endings == 'native':
2780
 
                    end = os.linesep
2781
 
                else:
2782
 
                    raise errors.BzrError(
2783
 
                        'Invalid line ending request %r' % line_endings)
2784
 
                content = "contents of %s%s" % (name.encode('utf-8'), end)
2785
 
                transport.put_bytes_non_atomic(urlutils.escape(name), content)
2786
 
 
2787
 
    build_tree_contents = staticmethod(treeshape.build_tree_contents)
2788
 
 
2789
 
    def assertInWorkingTree(self, path, root_path='.', tree=None):
2790
 
        """Assert whether path or paths are in the WorkingTree"""
2791
 
        if tree is None:
2792
 
            tree = workingtree.WorkingTree.open(root_path)
2793
 
        if not isinstance(path, basestring):
2794
 
            for p in path:
2795
 
                self.assertInWorkingTree(p, tree=tree)
2796
 
        else:
2797
 
            self.assertIsNot(tree.path2id(path), None,
2798
 
                path+' not in working tree.')
2799
 
 
2800
 
    def assertNotInWorkingTree(self, path, root_path='.', tree=None):
2801
 
        """Assert whether path or paths are not in the WorkingTree"""
2802
 
        if tree is None:
2803
 
            tree = workingtree.WorkingTree.open(root_path)
2804
 
        if not isinstance(path, basestring):
2805
 
            for p in path:
2806
 
                self.assertNotInWorkingTree(p,tree=tree)
2807
 
        else:
2808
 
            self.assertIs(tree.path2id(path), None, path+' in working tree.')
2809
 
 
2810
 
 
2811
 
class TestCaseWithTransport(TestCaseInTempDir):
2812
 
    """A test case that provides get_url and get_readonly_url facilities.
2813
 
 
2814
 
    These back onto two transport servers, one for readonly access and one for
2815
 
    read write access.
2816
 
 
2817
 
    If no explicit class is provided for readonly access, a
2818
 
    ReadonlyTransportDecorator is used instead which allows the use of non disk
2819
 
    based read write transports.
2820
 
 
2821
 
    If an explicit class is provided for readonly access, that server and the
2822
 
    readwrite one must both define get_url() as resolving to os.getcwd().
2823
 
    """
2824
 
 
2825
 
    def get_vfs_only_server(self):
2826
 
        """See TestCaseWithMemoryTransport.
2827
 
 
2828
 
        This is useful for some tests with specific servers that need
2829
 
        diagnostics.
2830
 
        """
2831
 
        if self.__vfs_server is None:
2832
 
            self.__vfs_server = self.vfs_transport_factory()
2833
 
            self.start_server(self.__vfs_server)
2834
 
        return self.__vfs_server
2835
 
 
2836
 
    def make_branch_and_tree(self, relpath, format=None):
2837
 
        """Create a branch on the transport and a tree locally.
2838
 
 
2839
 
        If the transport is not a LocalTransport, the Tree can't be created on
2840
 
        the transport.  In that case if the vfs_transport_factory is
2841
 
        LocalURLServer the working tree is created in the local
2842
 
        directory backing the transport, and the returned tree's branch and
2843
 
        repository will also be accessed locally. Otherwise a lightweight
2844
 
        checkout is created and returned.
2845
 
 
2846
 
        We do this because we can't physically create a tree in the local
2847
 
        path, with a branch reference to the transport_factory url, and
2848
 
        a branch + repository in the vfs_transport, unless the vfs_transport
2849
 
        namespace is distinct from the local disk - the two branch objects
2850
 
        would collide. While we could construct a tree with its branch object
2851
 
        pointing at the transport_factory transport in memory, reopening it
2852
 
        would behaving unexpectedly, and has in the past caused testing bugs
2853
 
        when we tried to do it that way.
2854
 
 
2855
 
        :param format: The BzrDirFormat.
2856
 
        :returns: the WorkingTree.
2857
 
        """
2858
 
        # TODO: always use the local disk path for the working tree,
2859
 
        # this obviously requires a format that supports branch references
2860
 
        # so check for that by checking bzrdir.BzrDirFormat.get_default_format()
2861
 
        # RBC 20060208
2862
 
        b = self.make_branch(relpath, format=format)
2863
 
        try:
2864
 
            return b.bzrdir.create_workingtree()
2865
 
        except errors.NotLocalUrl:
2866
 
            # We can only make working trees locally at the moment.  If the
2867
 
            # transport can't support them, then we keep the non-disk-backed
2868
 
            # branch and create a local checkout.
2869
 
            if self.vfs_transport_factory is test_server.LocalURLServer:
2870
 
                # the branch is colocated on disk, we cannot create a checkout.
2871
 
                # hopefully callers will expect this.
2872
 
                local_controldir= bzrdir.BzrDir.open(self.get_vfs_only_url(relpath))
2873
 
                wt = local_controldir.create_workingtree()
2874
 
                if wt.branch._format != b._format:
2875
 
                    wt._branch = b
2876
 
                    # Make sure that assigning to wt._branch fixes wt.branch,
2877
 
                    # in case the implementation details of workingtree objects
2878
 
                    # change.
2879
 
                    self.assertIs(b, wt.branch)
2880
 
                return wt
2881
 
            else:
2882
 
                return b.create_checkout(relpath, lightweight=True)
2883
 
 
2884
 
    def assertIsDirectory(self, relpath, transport):
2885
 
        """Assert that relpath within transport is a directory.
2886
 
 
2887
 
        This may not be possible on all transports; in that case it propagates
2888
 
        a TransportNotPossible.
2889
 
        """
2890
 
        try:
2891
 
            mode = transport.stat(relpath).st_mode
2892
 
        except errors.NoSuchFile:
2893
 
            self.fail("path %s is not a directory; no such file"
2894
 
                      % (relpath))
2895
 
        if not stat.S_ISDIR(mode):
2896
 
            self.fail("path %s is not a directory; has mode %#o"
2897
 
                      % (relpath, mode))
2898
 
 
2899
 
    def assertTreesEqual(self, left, right):
2900
 
        """Check that left and right have the same content and properties."""
2901
 
        # we use a tree delta to check for equality of the content, and we
2902
 
        # manually check for equality of other things such as the parents list.
2903
 
        self.assertEqual(left.get_parent_ids(), right.get_parent_ids())
2904
 
        differences = left.changes_from(right)
2905
 
        self.assertFalse(differences.has_changed(),
2906
 
            "Trees %r and %r are different: %r" % (left, right, differences))
2907
 
 
2908
 
    def setUp(self):
2909
 
        super(TestCaseWithTransport, self).setUp()
2910
 
        self.__vfs_server = None
2911
 
 
2912
 
    def disable_missing_extensions_warning(self):
2913
 
        """Some tests expect a precise stderr content.
2914
 
 
2915
 
        There is no point in forcing them to duplicate the extension related
2916
 
        warning.
2917
 
        """
2918
 
        config.GlobalConfig().set_user_option('ignore_missing_extensions', True)
2919
 
 
2920
 
 
2921
 
class ChrootedTestCase(TestCaseWithTransport):
2922
 
    """A support class that provides readonly urls outside the local namespace.
2923
 
 
2924
 
    This is done by checking if self.transport_server is a MemoryServer. if it
2925
 
    is then we are chrooted already, if it is not then an HttpServer is used
2926
 
    for readonly urls.
2927
 
 
2928
 
    TODO RBC 20060127: make this an option to TestCaseWithTransport so it can
2929
 
                       be used without needed to redo it when a different
2930
 
                       subclass is in use ?
2931
 
    """
2932
 
 
2933
 
    def setUp(self):
2934
 
        from bzrlib.tests import http_server
2935
 
        super(ChrootedTestCase, self).setUp()
2936
 
        if not self.vfs_transport_factory == memory.MemoryServer:
2937
 
            self.transport_readonly_server = http_server.HttpServer
2938
 
 
2939
 
 
2940
 
def condition_id_re(pattern):
2941
 
    """Create a condition filter which performs a re check on a test's id.
2942
 
 
2943
 
    :param pattern: A regular expression string.
2944
 
    :return: A callable that returns True if the re matches.
2945
 
    """
2946
 
    filter_re = re.compile(pattern, 0)
2947
 
    def condition(test):
2948
 
        test_id = test.id()
2949
 
        return filter_re.search(test_id)
2950
 
    return condition
2951
 
 
2952
 
 
2953
 
def condition_isinstance(klass_or_klass_list):
2954
 
    """Create a condition filter which returns isinstance(param, klass).
2955
 
 
2956
 
    :return: A callable which when called with one parameter obj return the
2957
 
        result of isinstance(obj, klass_or_klass_list).
2958
 
    """
2959
 
    def condition(obj):
2960
 
        return isinstance(obj, klass_or_klass_list)
2961
 
    return condition
2962
 
 
2963
 
 
2964
 
def condition_id_in_list(id_list):
2965
 
    """Create a condition filter which verify that test's id in a list.
2966
 
 
2967
 
    :param id_list: A TestIdList object.
2968
 
    :return: A callable that returns True if the test's id appears in the list.
2969
 
    """
2970
 
    def condition(test):
2971
 
        return id_list.includes(test.id())
2972
 
    return condition
2973
 
 
2974
 
 
2975
 
def condition_id_startswith(starts):
2976
 
    """Create a condition filter verifying that test's id starts with a string.
2977
 
 
2978
 
    :param starts: A list of string.
2979
 
    :return: A callable that returns True if the test's id starts with one of
2980
 
        the given strings.
2981
 
    """
2982
 
    def condition(test):
2983
 
        for start in starts:
2984
 
            if test.id().startswith(start):
2985
 
                return True
2986
 
        return False
2987
 
    return condition
2988
 
 
2989
 
 
2990
 
def exclude_tests_by_condition(suite, condition):
2991
 
    """Create a test suite which excludes some tests from suite.
2992
 
 
2993
 
    :param suite: The suite to get tests from.
2994
 
    :param condition: A callable whose result evaluates True when called with a
2995
 
        test case which should be excluded from the result.
2996
 
    :return: A suite which contains the tests found in suite that fail
2997
 
        condition.
2998
 
    """
2999
 
    result = []
3000
 
    for test in iter_suite_tests(suite):
3001
 
        if not condition(test):
3002
 
            result.append(test)
3003
 
    return TestUtil.TestSuite(result)
3004
 
 
3005
 
 
3006
 
def filter_suite_by_condition(suite, condition):
3007
 
    """Create a test suite by filtering another one.
3008
 
 
3009
 
    :param suite: The source suite.
3010
 
    :param condition: A callable whose result evaluates True when called with a
3011
 
        test case which should be included in the result.
3012
 
    :return: A suite which contains the tests found in suite that pass
3013
 
        condition.
3014
 
    """
3015
 
    result = []
3016
 
    for test in iter_suite_tests(suite):
3017
 
        if condition(test):
3018
 
            result.append(test)
3019
 
    return TestUtil.TestSuite(result)
3020
 
 
3021
 
 
3022
 
def filter_suite_by_re(suite, pattern):
3023
 
    """Create a test suite by filtering another one.
3024
 
 
3025
 
    :param suite:           the source suite
3026
 
    :param pattern:         pattern that names must match
3027
 
    :returns: the newly created suite
3028
 
    """
3029
 
    condition = condition_id_re(pattern)
3030
 
    result_suite = filter_suite_by_condition(suite, condition)
3031
 
    return result_suite
3032
 
 
3033
 
 
3034
 
def filter_suite_by_id_list(suite, test_id_list):
3035
 
    """Create a test suite by filtering another one.
3036
 
 
3037
 
    :param suite: The source suite.
3038
 
    :param test_id_list: A list of the test ids to keep as strings.
3039
 
    :returns: the newly created suite
3040
 
    """
3041
 
    condition = condition_id_in_list(test_id_list)
3042
 
    result_suite = filter_suite_by_condition(suite, condition)
3043
 
    return result_suite
3044
 
 
3045
 
 
3046
 
def filter_suite_by_id_startswith(suite, start):
3047
 
    """Create a test suite by filtering another one.
3048
 
 
3049
 
    :param suite: The source suite.
3050
 
    :param start: A list of string the test id must start with one of.
3051
 
    :returns: the newly created suite
3052
 
    """
3053
 
    condition = condition_id_startswith(start)
3054
 
    result_suite = filter_suite_by_condition(suite, condition)
3055
 
    return result_suite
3056
 
 
3057
 
 
3058
 
def exclude_tests_by_re(suite, pattern):
3059
 
    """Create a test suite which excludes some tests from suite.
3060
 
 
3061
 
    :param suite: The suite to get tests from.
3062
 
    :param pattern: A regular expression string. Test ids that match this
3063
 
        pattern will be excluded from the result.
3064
 
    :return: A TestSuite that contains all the tests from suite without the
3065
 
        tests that matched pattern. The order of tests is the same as it was in
3066
 
        suite.
3067
 
    """
3068
 
    return exclude_tests_by_condition(suite, condition_id_re(pattern))
3069
 
 
3070
 
 
3071
 
def preserve_input(something):
3072
 
    """A helper for performing test suite transformation chains.
3073
 
 
3074
 
    :param something: Anything you want to preserve.
3075
 
    :return: Something.
3076
 
    """
3077
 
    return something
3078
 
 
3079
 
 
3080
 
def randomize_suite(suite):
3081
 
    """Return a new TestSuite with suite's tests in random order.
3082
 
 
3083
 
    The tests in the input suite are flattened into a single suite in order to
3084
 
    accomplish this. Any nested TestSuites are removed to provide global
3085
 
    randomness.
3086
 
    """
3087
 
    tests = list(iter_suite_tests(suite))
3088
 
    random.shuffle(tests)
3089
 
    return TestUtil.TestSuite(tests)
3090
 
 
3091
 
 
3092
 
def split_suite_by_condition(suite, condition):
3093
 
    """Split a test suite into two by a condition.
3094
 
 
3095
 
    :param suite: The suite to split.
3096
 
    :param condition: The condition to match on. Tests that match this
3097
 
        condition are returned in the first test suite, ones that do not match
3098
 
        are in the second suite.
3099
 
    :return: A tuple of two test suites, where the first contains tests from
3100
 
        suite matching the condition, and the second contains the remainder
3101
 
        from suite. The order within each output suite is the same as it was in
3102
 
        suite.
3103
 
    """
3104
 
    matched = []
3105
 
    did_not_match = []
3106
 
    for test in iter_suite_tests(suite):
3107
 
        if condition(test):
3108
 
            matched.append(test)
3109
 
        else:
3110
 
            did_not_match.append(test)
3111
 
    return TestUtil.TestSuite(matched), TestUtil.TestSuite(did_not_match)
3112
 
 
3113
 
 
3114
 
def split_suite_by_re(suite, pattern):
3115
 
    """Split a test suite into two by a regular expression.
3116
 
 
3117
 
    :param suite: The suite to split.
3118
 
    :param pattern: A regular expression string. Test ids that match this
3119
 
        pattern will be in the first test suite returned, and the others in the
3120
 
        second test suite returned.
3121
 
    :return: A tuple of two test suites, where the first contains tests from
3122
 
        suite matching pattern, and the second contains the remainder from
3123
 
        suite. The order within each output suite is the same as it was in
3124
 
        suite.
3125
 
    """
3126
 
    return split_suite_by_condition(suite, condition_id_re(pattern))
3127
 
 
3128
 
 
3129
 
def run_suite(suite, name='test', verbose=False, pattern=".*",
3130
 
              stop_on_failure=False,
3131
 
              transport=None, lsprof_timed=None, bench_history=None,
3132
 
              matching_tests_first=None,
3133
 
              list_only=False,
3134
 
              random_seed=None,
3135
 
              exclude_pattern=None,
3136
 
              strict=False,
3137
 
              runner_class=None,
3138
 
              suite_decorators=None,
3139
 
              stream=None,
3140
 
              result_decorators=None,
3141
 
              ):
3142
 
    """Run a test suite for bzr selftest.
3143
 
 
3144
 
    :param runner_class: The class of runner to use. Must support the
3145
 
        constructor arguments passed by run_suite which are more than standard
3146
 
        python uses.
3147
 
    :return: A boolean indicating success.
3148
 
    """
3149
 
    TestCase._gather_lsprof_in_benchmarks = lsprof_timed
3150
 
    if verbose:
3151
 
        verbosity = 2
3152
 
    else:
3153
 
        verbosity = 1
3154
 
    if runner_class is None:
3155
 
        runner_class = TextTestRunner
3156
 
    if stream is None:
3157
 
        stream = sys.stdout
3158
 
    runner = runner_class(stream=stream,
3159
 
                            descriptions=0,
3160
 
                            verbosity=verbosity,
3161
 
                            bench_history=bench_history,
3162
 
                            strict=strict,
3163
 
                            result_decorators=result_decorators,
3164
 
                            )
3165
 
    runner.stop_on_failure=stop_on_failure
3166
 
    # built in decorator factories:
3167
 
    decorators = [
3168
 
        random_order(random_seed, runner),
3169
 
        exclude_tests(exclude_pattern),
3170
 
        ]
3171
 
    if matching_tests_first:
3172
 
        decorators.append(tests_first(pattern))
3173
 
    else:
3174
 
        decorators.append(filter_tests(pattern))
3175
 
    if suite_decorators:
3176
 
        decorators.extend(suite_decorators)
3177
 
    # tell the result object how many tests will be running: (except if
3178
 
    # --parallel=fork is being used. Robert said he will provide a better
3179
 
    # progress design later -- vila 20090817)
3180
 
    if fork_decorator not in decorators:
3181
 
        decorators.append(CountingDecorator)
3182
 
    for decorator in decorators:
3183
 
        suite = decorator(suite)
3184
 
    if list_only:
3185
 
        # Done after test suite decoration to allow randomisation etc
3186
 
        # to take effect, though that is of marginal benefit.
3187
 
        if verbosity >= 2:
3188
 
            stream.write("Listing tests only ...\n")
3189
 
        for t in iter_suite_tests(suite):
3190
 
            stream.write("%s\n" % (t.id()))
3191
 
        return True
3192
 
    result = runner.run(suite)
3193
 
    if strict:
3194
 
        return result.wasStrictlySuccessful()
3195
 
    else:
3196
 
        return result.wasSuccessful()
3197
 
 
3198
 
 
3199
 
# A registry where get() returns a suite decorator.
3200
 
parallel_registry = registry.Registry()
3201
 
 
3202
 
 
3203
 
def fork_decorator(suite):
3204
 
    if getattr(os, "fork", None) is None:
3205
 
        raise errors.BzrCommandError("platform does not support fork,"
3206
 
            " try --parallel=subprocess instead.")
3207
 
    concurrency = osutils.local_concurrency()
3208
 
    if concurrency == 1:
3209
 
        return suite
3210
 
    from testtools import ConcurrentTestSuite
3211
 
    return ConcurrentTestSuite(suite, fork_for_tests)
3212
 
parallel_registry.register('fork', fork_decorator)
3213
 
 
3214
 
 
3215
 
def subprocess_decorator(suite):
3216
 
    concurrency = osutils.local_concurrency()
3217
 
    if concurrency == 1:
3218
 
        return suite
3219
 
    from testtools import ConcurrentTestSuite
3220
 
    return ConcurrentTestSuite(suite, reinvoke_for_tests)
3221
 
parallel_registry.register('subprocess', subprocess_decorator)
3222
 
 
3223
 
 
3224
 
def exclude_tests(exclude_pattern):
3225
 
    """Return a test suite decorator that excludes tests."""
3226
 
    if exclude_pattern is None:
3227
 
        return identity_decorator
3228
 
    def decorator(suite):
3229
 
        return ExcludeDecorator(suite, exclude_pattern)
3230
 
    return decorator
3231
 
 
3232
 
 
3233
 
def filter_tests(pattern):
3234
 
    if pattern == '.*':
3235
 
        return identity_decorator
3236
 
    def decorator(suite):
3237
 
        return FilterTestsDecorator(suite, pattern)
3238
 
    return decorator
3239
 
 
3240
 
 
3241
 
def random_order(random_seed, runner):
3242
 
    """Return a test suite decorator factory for randomising tests order.
3243
 
    
3244
 
    :param random_seed: now, a string which casts to a long, or a long.
3245
 
    :param runner: A test runner with a stream attribute to report on.
3246
 
    """
3247
 
    if random_seed is None:
3248
 
        return identity_decorator
3249
 
    def decorator(suite):
3250
 
        return RandomDecorator(suite, random_seed, runner.stream)
3251
 
    return decorator
3252
 
 
3253
 
 
3254
 
def tests_first(pattern):
3255
 
    if pattern == '.*':
3256
 
        return identity_decorator
3257
 
    def decorator(suite):
3258
 
        return TestFirstDecorator(suite, pattern)
3259
 
    return decorator
3260
 
 
3261
 
 
3262
 
def identity_decorator(suite):
3263
 
    """Return suite."""
3264
 
    return suite
3265
 
 
3266
 
 
3267
 
class TestDecorator(TestUtil.TestSuite):
3268
 
    """A decorator for TestCase/TestSuite objects.
3269
 
    
3270
 
    Usually, subclasses should override __iter__(used when flattening test
3271
 
    suites), which we do to filter, reorder, parallelise and so on, run() and
3272
 
    debug().
3273
 
    """
3274
 
 
3275
 
    def __init__(self, suite):
3276
 
        TestUtil.TestSuite.__init__(self)
3277
 
        self.addTest(suite)
3278
 
 
3279
 
    def countTestCases(self):
3280
 
        cases = 0
3281
 
        for test in self:
3282
 
            cases += test.countTestCases()
3283
 
        return cases
3284
 
 
3285
 
    def debug(self):
3286
 
        for test in self:
3287
 
            test.debug()
3288
 
 
3289
 
    def run(self, result):
3290
 
        # Use iteration on self, not self._tests, to allow subclasses to hook
3291
 
        # into __iter__.
3292
 
        for test in self:
3293
 
            if result.shouldStop:
3294
 
                break
3295
 
            test.run(result)
3296
 
        return result
3297
 
 
3298
 
 
3299
 
class CountingDecorator(TestDecorator):
3300
 
    """A decorator which calls result.progress(self.countTestCases)."""
3301
 
 
3302
 
    def run(self, result):
3303
 
        progress_method = getattr(result, 'progress', None)
3304
 
        if callable(progress_method):
3305
 
            progress_method(self.countTestCases(), SUBUNIT_SEEK_SET)
3306
 
        return super(CountingDecorator, self).run(result)
3307
 
 
3308
 
 
3309
 
class ExcludeDecorator(TestDecorator):
3310
 
    """A decorator which excludes test matching an exclude pattern."""
3311
 
 
3312
 
    def __init__(self, suite, exclude_pattern):
3313
 
        TestDecorator.__init__(self, suite)
3314
 
        self.exclude_pattern = exclude_pattern
3315
 
        self.excluded = False
3316
 
 
3317
 
    def __iter__(self):
3318
 
        if self.excluded:
3319
 
            return iter(self._tests)
3320
 
        self.excluded = True
3321
 
        suite = exclude_tests_by_re(self, self.exclude_pattern)
3322
 
        del self._tests[:]
3323
 
        self.addTests(suite)
3324
 
        return iter(self._tests)
3325
 
 
3326
 
 
3327
 
class FilterTestsDecorator(TestDecorator):
3328
 
    """A decorator which filters tests to those matching a pattern."""
3329
 
 
3330
 
    def __init__(self, suite, pattern):
3331
 
        TestDecorator.__init__(self, suite)
3332
 
        self.pattern = pattern
3333
 
        self.filtered = False
3334
 
 
3335
 
    def __iter__(self):
3336
 
        if self.filtered:
3337
 
            return iter(self._tests)
3338
 
        self.filtered = True
3339
 
        suite = filter_suite_by_re(self, self.pattern)
3340
 
        del self._tests[:]
3341
 
        self.addTests(suite)
3342
 
        return iter(self._tests)
3343
 
 
3344
 
 
3345
 
class RandomDecorator(TestDecorator):
3346
 
    """A decorator which randomises the order of its tests."""
3347
 
 
3348
 
    def __init__(self, suite, random_seed, stream):
3349
 
        TestDecorator.__init__(self, suite)
3350
 
        self.random_seed = random_seed
3351
 
        self.randomised = False
3352
 
        self.stream = stream
3353
 
 
3354
 
    def __iter__(self):
3355
 
        if self.randomised:
3356
 
            return iter(self._tests)
3357
 
        self.randomised = True
3358
 
        self.stream.write("Randomizing test order using seed %s\n\n" %
3359
 
            (self.actual_seed()))
3360
 
        # Initialise the random number generator.
3361
 
        random.seed(self.actual_seed())
3362
 
        suite = randomize_suite(self)
3363
 
        del self._tests[:]
3364
 
        self.addTests(suite)
3365
 
        return iter(self._tests)
3366
 
 
3367
 
    def actual_seed(self):
3368
 
        if self.random_seed == "now":
3369
 
            # We convert the seed to a long to make it reuseable across
3370
 
            # invocations (because the user can reenter it).
3371
 
            self.random_seed = long(time.time())
3372
 
        else:
3373
 
            # Convert the seed to a long if we can
3374
 
            try:
3375
 
                self.random_seed = long(self.random_seed)
3376
 
            except:
3377
 
                pass
3378
 
        return self.random_seed
3379
 
 
3380
 
 
3381
 
class TestFirstDecorator(TestDecorator):
3382
 
    """A decorator which moves named tests to the front."""
3383
 
 
3384
 
    def __init__(self, suite, pattern):
3385
 
        TestDecorator.__init__(self, suite)
3386
 
        self.pattern = pattern
3387
 
        self.filtered = False
3388
 
 
3389
 
    def __iter__(self):
3390
 
        if self.filtered:
3391
 
            return iter(self._tests)
3392
 
        self.filtered = True
3393
 
        suites = split_suite_by_re(self, self.pattern)
3394
 
        del self._tests[:]
3395
 
        self.addTests(suites)
3396
 
        return iter(self._tests)
3397
 
 
3398
 
 
3399
 
def partition_tests(suite, count):
3400
 
    """Partition suite into count lists of tests."""
3401
 
    # This just assigns tests in a round-robin fashion.  On one hand this
3402
 
    # splits up blocks of related tests that might run faster if they shared
3403
 
    # resources, but on the other it avoids assigning blocks of slow tests to
3404
 
    # just one partition.  So the slowest partition shouldn't be much slower
3405
 
    # than the fastest.
3406
 
    partitions = [list() for i in range(count)]
3407
 
    tests = iter_suite_tests(suite)
3408
 
    for partition, test in itertools.izip(itertools.cycle(partitions), tests):
3409
 
        partition.append(test)
3410
 
    return partitions
3411
 
 
3412
 
 
3413
 
def workaround_zealous_crypto_random():
3414
 
    """Crypto.Random want to help us being secure, but we don't care here.
3415
 
 
3416
 
    This workaround some test failure related to the sftp server. Once paramiko
3417
 
    stop using the controversial API in Crypto.Random, we may get rid of it.
3418
 
    """
3419
 
    try:
3420
 
        from Crypto.Random import atfork
3421
 
        atfork()
3422
 
    except ImportError:
3423
 
        pass
3424
 
 
3425
 
 
3426
 
def fork_for_tests(suite):
3427
 
    """Take suite and start up one runner per CPU by forking()
3428
 
 
3429
 
    :return: An iterable of TestCase-like objects which can each have
3430
 
        run(result) called on them to feed tests to result.
3431
 
    """
3432
 
    concurrency = osutils.local_concurrency()
3433
 
    result = []
3434
 
    from subunit import TestProtocolClient, ProtocolTestCase
3435
 
    from subunit.test_results import AutoTimingTestResultDecorator
3436
 
    class TestInOtherProcess(ProtocolTestCase):
3437
 
        # Should be in subunit, I think. RBC.
3438
 
        def __init__(self, stream, pid):
3439
 
            ProtocolTestCase.__init__(self, stream)
3440
 
            self.pid = pid
3441
 
 
3442
 
        def run(self, result):
3443
 
            try:
3444
 
                ProtocolTestCase.run(self, result)
3445
 
            finally:
3446
 
                os.waitpid(self.pid, 0)
3447
 
 
3448
 
    test_blocks = partition_tests(suite, concurrency)
3449
 
    for process_tests in test_blocks:
3450
 
        process_suite = TestUtil.TestSuite()
3451
 
        process_suite.addTests(process_tests)
3452
 
        c2pread, c2pwrite = os.pipe()
3453
 
        pid = os.fork()
3454
 
        if pid == 0:
3455
 
            workaround_zealous_crypto_random()
3456
 
            try:
3457
 
                os.close(c2pread)
3458
 
                # Leave stderr and stdout open so we can see test noise
3459
 
                # Close stdin so that the child goes away if it decides to
3460
 
                # read from stdin (otherwise its a roulette to see what
3461
 
                # child actually gets keystrokes for pdb etc).
3462
 
                sys.stdin.close()
3463
 
                sys.stdin = None
3464
 
                stream = os.fdopen(c2pwrite, 'wb', 1)
3465
 
                subunit_result = AutoTimingTestResultDecorator(
3466
 
                    TestProtocolClient(stream))
3467
 
                process_suite.run(subunit_result)
3468
 
            finally:
3469
 
                os._exit(0)
3470
 
        else:
3471
 
            os.close(c2pwrite)
3472
 
            stream = os.fdopen(c2pread, 'rb', 1)
3473
 
            test = TestInOtherProcess(stream, pid)
3474
 
            result.append(test)
3475
 
    return result
3476
 
 
3477
 
 
3478
 
def reinvoke_for_tests(suite):
3479
 
    """Take suite and start up one runner per CPU using subprocess().
3480
 
 
3481
 
    :return: An iterable of TestCase-like objects which can each have
3482
 
        run(result) called on them to feed tests to result.
3483
 
    """
3484
 
    concurrency = osutils.local_concurrency()
3485
 
    result = []
3486
 
    from subunit import ProtocolTestCase
3487
 
    class TestInSubprocess(ProtocolTestCase):
3488
 
        def __init__(self, process, name):
3489
 
            ProtocolTestCase.__init__(self, process.stdout)
3490
 
            self.process = process
3491
 
            self.process.stdin.close()
3492
 
            self.name = name
3493
 
 
3494
 
        def run(self, result):
3495
 
            try:
3496
 
                ProtocolTestCase.run(self, result)
3497
 
            finally:
3498
 
                self.process.wait()
3499
 
                os.unlink(self.name)
3500
 
            # print "pid %d finished" % finished_process
3501
 
    test_blocks = partition_tests(suite, concurrency)
3502
 
    for process_tests in test_blocks:
3503
 
        # ugly; currently reimplement rather than reuses TestCase methods.
3504
 
        bzr_path = os.path.dirname(os.path.dirname(bzrlib.__file__))+'/bzr'
3505
 
        if not os.path.isfile(bzr_path):
3506
 
            # We are probably installed. Assume sys.argv is the right file
3507
 
            bzr_path = sys.argv[0]
3508
 
        bzr_path = [bzr_path]
3509
 
        if sys.platform == "win32":
3510
 
            # if we're on windows, we can't execute the bzr script directly
3511
 
            bzr_path = [sys.executable] + bzr_path
3512
 
        fd, test_list_file_name = tempfile.mkstemp()
3513
 
        test_list_file = os.fdopen(fd, 'wb', 1)
3514
 
        for test in process_tests:
3515
 
            test_list_file.write(test.id() + '\n')
3516
 
        test_list_file.close()
3517
 
        try:
3518
 
            argv = bzr_path + ['selftest', '--load-list', test_list_file_name,
3519
 
                '--subunit']
3520
 
            if '--no-plugins' in sys.argv:
3521
 
                argv.append('--no-plugins')
3522
 
            # stderr=subprocess.STDOUT would be ideal, but until we prevent
3523
 
            # noise on stderr it can interrupt the subunit protocol.
3524
 
            process = subprocess.Popen(argv, stdin=subprocess.PIPE,
3525
 
                                      stdout=subprocess.PIPE,
3526
 
                                      stderr=subprocess.PIPE,
3527
 
                                      bufsize=1)
3528
 
            test = TestInSubprocess(process, test_list_file_name)
3529
 
            result.append(test)
3530
 
        except:
3531
 
            os.unlink(test_list_file_name)
3532
 
            raise
3533
 
    return result
3534
 
 
3535
 
 
3536
 
class ProfileResult(testtools.ExtendedToOriginalDecorator):
3537
 
    """Generate profiling data for all activity between start and success.
3538
 
    
3539
 
    The profile data is appended to the test's _benchcalls attribute and can
3540
 
    be accessed by the forwarded-to TestResult.
3541
 
 
3542
 
    While it might be cleaner do accumulate this in stopTest, addSuccess is
3543
 
    where our existing output support for lsprof is, and this class aims to
3544
 
    fit in with that: while it could be moved it's not necessary to accomplish
3545
 
    test profiling, nor would it be dramatically cleaner.
3546
 
    """
3547
 
 
3548
 
    def startTest(self, test):
3549
 
        self.profiler = bzrlib.lsprof.BzrProfiler()
3550
 
        # Prevent deadlocks in tests that use lsprof: those tests will
3551
 
        # unavoidably fail.
3552
 
        bzrlib.lsprof.BzrProfiler.profiler_block = 0
3553
 
        self.profiler.start()
3554
 
        testtools.ExtendedToOriginalDecorator.startTest(self, test)
3555
 
 
3556
 
    def addSuccess(self, test):
3557
 
        stats = self.profiler.stop()
3558
 
        try:
3559
 
            calls = test._benchcalls
3560
 
        except AttributeError:
3561
 
            test._benchcalls = []
3562
 
            calls = test._benchcalls
3563
 
        calls.append(((test.id(), "", ""), stats))
3564
 
        testtools.ExtendedToOriginalDecorator.addSuccess(self, test)
3565
 
 
3566
 
    def stopTest(self, test):
3567
 
        testtools.ExtendedToOriginalDecorator.stopTest(self, test)
3568
 
        self.profiler = None
3569
 
 
3570
 
 
3571
 
# Controlled by "bzr selftest -E=..." option
3572
 
# Currently supported:
3573
 
#   -Eallow_debug           Will no longer clear debug.debug_flags() so it
3574
 
#                           preserves any flags supplied at the command line.
3575
 
#   -Edisable_lock_checks   Turns errors in mismatched locks into simple prints
3576
 
#                           rather than failing tests. And no longer raise
3577
 
#                           LockContention when fctnl locks are not being used
3578
 
#                           with proper exclusion rules.
3579
 
#   -Ethreads               Will display thread ident at creation/join time to
3580
 
#                           help track thread leaks
3581
 
 
3582
 
#   -Econfig_stats          Will collect statistics using addDetail
3583
 
selftest_debug_flags = set()
3584
 
 
3585
 
 
3586
 
def selftest(verbose=False, pattern=".*", stop_on_failure=True,
3587
 
             transport=None,
3588
 
             test_suite_factory=None,
3589
 
             lsprof_timed=None,
3590
 
             bench_history=None,
3591
 
             matching_tests_first=None,
3592
 
             list_only=False,
3593
 
             random_seed=None,
3594
 
             exclude_pattern=None,
3595
 
             strict=False,
3596
 
             load_list=None,
3597
 
             debug_flags=None,
3598
 
             starting_with=None,
3599
 
             runner_class=None,
3600
 
             suite_decorators=None,
3601
 
             stream=None,
3602
 
             lsprof_tests=False,
3603
 
             ):
3604
 
    """Run the whole test suite under the enhanced runner"""
3605
 
    # XXX: Very ugly way to do this...
3606
 
    # Disable warning about old formats because we don't want it to disturb
3607
 
    # any blackbox tests.
3608
 
    from bzrlib import repository
3609
 
    repository._deprecation_warning_done = True
3610
 
 
3611
 
    global default_transport
3612
 
    if transport is None:
3613
 
        transport = default_transport
3614
 
    old_transport = default_transport
3615
 
    default_transport = transport
3616
 
    global selftest_debug_flags
3617
 
    old_debug_flags = selftest_debug_flags
3618
 
    if debug_flags is not None:
3619
 
        selftest_debug_flags = set(debug_flags)
3620
 
    try:
3621
 
        if load_list is None:
3622
 
            keep_only = None
3623
 
        else:
3624
 
            keep_only = load_test_id_list(load_list)
3625
 
        if starting_with:
3626
 
            starting_with = [test_prefix_alias_registry.resolve_alias(start)
3627
 
                             for start in starting_with]
3628
 
        if test_suite_factory is None:
3629
 
            # Reduce loading time by loading modules based on the starting_with
3630
 
            # patterns.
3631
 
            suite = test_suite(keep_only, starting_with)
3632
 
        else:
3633
 
            suite = test_suite_factory()
3634
 
        if starting_with:
3635
 
            # But always filter as requested.
3636
 
            suite = filter_suite_by_id_startswith(suite, starting_with)
3637
 
        result_decorators = []
3638
 
        if lsprof_tests:
3639
 
            result_decorators.append(ProfileResult)
3640
 
        return run_suite(suite, 'testbzr', verbose=verbose, pattern=pattern,
3641
 
                     stop_on_failure=stop_on_failure,
3642
 
                     transport=transport,
3643
 
                     lsprof_timed=lsprof_timed,
3644
 
                     bench_history=bench_history,
3645
 
                     matching_tests_first=matching_tests_first,
3646
 
                     list_only=list_only,
3647
 
                     random_seed=random_seed,
3648
 
                     exclude_pattern=exclude_pattern,
3649
 
                     strict=strict,
3650
 
                     runner_class=runner_class,
3651
 
                     suite_decorators=suite_decorators,
3652
 
                     stream=stream,
3653
 
                     result_decorators=result_decorators,
3654
 
                     )
3655
 
    finally:
3656
 
        default_transport = old_transport
3657
 
        selftest_debug_flags = old_debug_flags
3658
 
 
3659
 
 
3660
 
def load_test_id_list(file_name):
3661
 
    """Load a test id list from a text file.
3662
 
 
3663
 
    The format is one test id by line.  No special care is taken to impose
3664
 
    strict rules, these test ids are used to filter the test suite so a test id
3665
 
    that do not match an existing test will do no harm. This allows user to add
3666
 
    comments, leave blank lines, etc.
3667
 
    """
3668
 
    test_list = []
3669
 
    try:
3670
 
        ftest = open(file_name, 'rt')
3671
 
    except IOError, e:
3672
 
        if e.errno != errno.ENOENT:
3673
 
            raise
3674
 
        else:
3675
 
            raise errors.NoSuchFile(file_name)
3676
 
 
3677
 
    for test_name in ftest.readlines():
3678
 
        test_list.append(test_name.strip())
3679
 
    ftest.close()
3680
 
    return test_list
3681
 
 
3682
 
 
3683
 
def suite_matches_id_list(test_suite, id_list):
3684
 
    """Warns about tests not appearing or appearing more than once.
3685
 
 
3686
 
    :param test_suite: A TestSuite object.
3687
 
    :param test_id_list: The list of test ids that should be found in
3688
 
         test_suite.
3689
 
 
3690
 
    :return: (absents, duplicates) absents is a list containing the test found
3691
 
        in id_list but not in test_suite, duplicates is a list containing the
3692
 
        test found multiple times in test_suite.
3693
 
 
3694
 
    When using a prefined test id list, it may occurs that some tests do not
3695
 
    exist anymore or that some tests use the same id. This function warns the
3696
 
    tester about potential problems in his workflow (test lists are volatile)
3697
 
    or in the test suite itself (using the same id for several tests does not
3698
 
    help to localize defects).
3699
 
    """
3700
 
    # Build a dict counting id occurrences
3701
 
    tests = dict()
3702
 
    for test in iter_suite_tests(test_suite):
3703
 
        id = test.id()
3704
 
        tests[id] = tests.get(id, 0) + 1
3705
 
 
3706
 
    not_found = []
3707
 
    duplicates = []
3708
 
    for id in id_list:
3709
 
        occurs = tests.get(id, 0)
3710
 
        if not occurs:
3711
 
            not_found.append(id)
3712
 
        elif occurs > 1:
3713
 
            duplicates.append(id)
3714
 
 
3715
 
    return not_found, duplicates
3716
 
 
3717
 
 
3718
 
class TestIdList(object):
3719
 
    """Test id list to filter a test suite.
3720
 
 
3721
 
    Relying on the assumption that test ids are built as:
3722
 
    <module>[.<class>.<method>][(<param>+)], <module> being in python dotted
3723
 
    notation, this class offers methods to :
3724
 
    - avoid building a test suite for modules not refered to in the test list,
3725
 
    - keep only the tests listed from the module test suite.
3726
 
    """
3727
 
 
3728
 
    def __init__(self, test_id_list):
3729
 
        # When a test suite needs to be filtered against us we compare test ids
3730
 
        # for equality, so a simple dict offers a quick and simple solution.
3731
 
        self.tests = dict().fromkeys(test_id_list, True)
3732
 
 
3733
 
        # While unittest.TestCase have ids like:
3734
 
        # <module>.<class>.<method>[(<param+)],
3735
 
        # doctest.DocTestCase can have ids like:
3736
 
        # <module>
3737
 
        # <module>.<class>
3738
 
        # <module>.<function>
3739
 
        # <module>.<class>.<method>
3740
 
 
3741
 
        # Since we can't predict a test class from its name only, we settle on
3742
 
        # a simple constraint: a test id always begins with its module name.
3743
 
 
3744
 
        modules = {}
3745
 
        for test_id in test_id_list:
3746
 
            parts = test_id.split('.')
3747
 
            mod_name = parts.pop(0)
3748
 
            modules[mod_name] = True
3749
 
            for part in parts:
3750
 
                mod_name += '.' + part
3751
 
                modules[mod_name] = True
3752
 
        self.modules = modules
3753
 
 
3754
 
    def refers_to(self, module_name):
3755
 
        """Is there tests for the module or one of its sub modules."""
3756
 
        return self.modules.has_key(module_name)
3757
 
 
3758
 
    def includes(self, test_id):
3759
 
        return self.tests.has_key(test_id)
3760
 
 
3761
 
 
3762
 
class TestPrefixAliasRegistry(registry.Registry):
3763
 
    """A registry for test prefix aliases.
3764
 
 
3765
 
    This helps implement shorcuts for the --starting-with selftest
3766
 
    option. Overriding existing prefixes is not allowed but not fatal (a
3767
 
    warning will be emitted).
3768
 
    """
3769
 
 
3770
 
    def register(self, key, obj, help=None, info=None,
3771
 
                 override_existing=False):
3772
 
        """See Registry.register.
3773
 
 
3774
 
        Trying to override an existing alias causes a warning to be emitted,
3775
 
        not a fatal execption.
3776
 
        """
3777
 
        try:
3778
 
            super(TestPrefixAliasRegistry, self).register(
3779
 
                key, obj, help=help, info=info, override_existing=False)
3780
 
        except KeyError:
3781
 
            actual = self.get(key)
3782
 
            trace.note(
3783
 
                'Test prefix alias %s is already used for %s, ignoring %s'
3784
 
                % (key, actual, obj))
3785
 
 
3786
 
    def resolve_alias(self, id_start):
3787
 
        """Replace the alias by the prefix in the given string.
3788
 
 
3789
 
        Using an unknown prefix is an error to help catching typos.
3790
 
        """
3791
 
        parts = id_start.split('.')
3792
 
        try:
3793
 
            parts[0] = self.get(parts[0])
3794
 
        except KeyError:
3795
 
            raise errors.BzrCommandError(
3796
 
                '%s is not a known test prefix alias' % parts[0])
3797
 
        return '.'.join(parts)
3798
 
 
3799
 
 
3800
 
test_prefix_alias_registry = TestPrefixAliasRegistry()
3801
 
"""Registry of test prefix aliases."""
3802
 
 
3803
 
 
3804
 
# This alias allows to detect typos ('bzrlin.') by making all valid test ids
3805
 
# appear prefixed ('bzrlib.' is "replaced" by 'bzrlib.').
3806
 
test_prefix_alias_registry.register('bzrlib', 'bzrlib')
3807
 
 
3808
 
# Obvious highest levels prefixes, feel free to add your own via a plugin
3809
 
test_prefix_alias_registry.register('bd', 'bzrlib.doc')
3810
 
test_prefix_alias_registry.register('bu', 'bzrlib.utils')
3811
 
test_prefix_alias_registry.register('bt', 'bzrlib.tests')
3812
 
test_prefix_alias_registry.register('bb', 'bzrlib.tests.blackbox')
3813
 
test_prefix_alias_registry.register('bp', 'bzrlib.plugins')
3814
 
 
3815
 
 
3816
 
def _test_suite_testmod_names():
3817
 
    """Return the standard list of test module names to test."""
3818
 
    return [
3819
 
        'bzrlib.doc',
3820
 
        'bzrlib.tests.blackbox',
3821
 
        'bzrlib.tests.commands',
3822
 
        'bzrlib.tests.doc_generate',
3823
 
        'bzrlib.tests.per_branch',
3824
 
        'bzrlib.tests.per_bzrdir',
3825
 
        'bzrlib.tests.per_controldir',
3826
 
        'bzrlib.tests.per_controldir_colo',
3827
 
        'bzrlib.tests.per_foreign_vcs',
3828
 
        'bzrlib.tests.per_interrepository',
3829
 
        'bzrlib.tests.per_intertree',
3830
 
        'bzrlib.tests.per_inventory',
3831
 
        'bzrlib.tests.per_interbranch',
3832
 
        'bzrlib.tests.per_lock',
3833
 
        'bzrlib.tests.per_merger',
3834
 
        'bzrlib.tests.per_transport',
3835
 
        'bzrlib.tests.per_tree',
3836
 
        'bzrlib.tests.per_pack_repository',
3837
 
        'bzrlib.tests.per_repository',
3838
 
        'bzrlib.tests.per_repository_chk',
3839
 
        'bzrlib.tests.per_repository_reference',
3840
 
        'bzrlib.tests.per_repository_vf',
3841
 
        'bzrlib.tests.per_uifactory',
3842
 
        'bzrlib.tests.per_versionedfile',
3843
 
        'bzrlib.tests.per_workingtree',
3844
 
        'bzrlib.tests.test__annotator',
3845
 
        'bzrlib.tests.test__bencode',
3846
 
        'bzrlib.tests.test__btree_serializer',
3847
 
        'bzrlib.tests.test__chk_map',
3848
 
        'bzrlib.tests.test__dirstate_helpers',
3849
 
        'bzrlib.tests.test__groupcompress',
3850
 
        'bzrlib.tests.test__known_graph',
3851
 
        'bzrlib.tests.test__rio',
3852
 
        'bzrlib.tests.test__simple_set',
3853
 
        'bzrlib.tests.test__static_tuple',
3854
 
        'bzrlib.tests.test__walkdirs_win32',
3855
 
        'bzrlib.tests.test_ancestry',
3856
 
        'bzrlib.tests.test_annotate',
3857
 
        'bzrlib.tests.test_api',
3858
 
        'bzrlib.tests.test_atomicfile',
3859
 
        'bzrlib.tests.test_bad_files',
3860
 
        'bzrlib.tests.test_bisect_multi',
3861
 
        'bzrlib.tests.test_branch',
3862
 
        'bzrlib.tests.test_branchbuilder',
3863
 
        'bzrlib.tests.test_btree_index',
3864
 
        'bzrlib.tests.test_bugtracker',
3865
 
        'bzrlib.tests.test_bundle',
3866
 
        'bzrlib.tests.test_bzrdir',
3867
 
        'bzrlib.tests.test__chunks_to_lines',
3868
 
        'bzrlib.tests.test_cache_utf8',
3869
 
        'bzrlib.tests.test_chk_map',
3870
 
        'bzrlib.tests.test_chk_serializer',
3871
 
        'bzrlib.tests.test_chunk_writer',
3872
 
        'bzrlib.tests.test_clean_tree',
3873
 
        'bzrlib.tests.test_cleanup',
3874
 
        'bzrlib.tests.test_cmdline',
3875
 
        'bzrlib.tests.test_commands',
3876
 
        'bzrlib.tests.test_commit',
3877
 
        'bzrlib.tests.test_commit_merge',
3878
 
        'bzrlib.tests.test_config',
3879
 
        'bzrlib.tests.test_conflicts',
3880
 
        'bzrlib.tests.test_controldir',
3881
 
        'bzrlib.tests.test_counted_lock',
3882
 
        'bzrlib.tests.test_crash',
3883
 
        'bzrlib.tests.test_decorators',
3884
 
        'bzrlib.tests.test_delta',
3885
 
        'bzrlib.tests.test_debug',
3886
 
        'bzrlib.tests.test_diff',
3887
 
        'bzrlib.tests.test_directory_service',
3888
 
        'bzrlib.tests.test_dirstate',
3889
 
        'bzrlib.tests.test_email_message',
3890
 
        'bzrlib.tests.test_eol_filters',
3891
 
        'bzrlib.tests.test_errors',
3892
 
        'bzrlib.tests.test_export',
3893
 
        'bzrlib.tests.test_export_pot',
3894
 
        'bzrlib.tests.test_extract',
3895
 
        'bzrlib.tests.test_fetch',
3896
 
        'bzrlib.tests.test_fixtures',
3897
 
        'bzrlib.tests.test_fifo_cache',
3898
 
        'bzrlib.tests.test_filters',
3899
 
        'bzrlib.tests.test_ftp_transport',
3900
 
        'bzrlib.tests.test_foreign',
3901
 
        'bzrlib.tests.test_generate_docs',
3902
 
        'bzrlib.tests.test_generate_ids',
3903
 
        'bzrlib.tests.test_globbing',
3904
 
        'bzrlib.tests.test_gpg',
3905
 
        'bzrlib.tests.test_graph',
3906
 
        'bzrlib.tests.test_groupcompress',
3907
 
        'bzrlib.tests.test_hashcache',
3908
 
        'bzrlib.tests.test_help',
3909
 
        'bzrlib.tests.test_hooks',
3910
 
        'bzrlib.tests.test_http',
3911
 
        'bzrlib.tests.test_http_response',
3912
 
        'bzrlib.tests.test_https_ca_bundle',
3913
 
        'bzrlib.tests.test_i18n',
3914
 
        'bzrlib.tests.test_identitymap',
3915
 
        'bzrlib.tests.test_ignores',
3916
 
        'bzrlib.tests.test_index',
3917
 
        'bzrlib.tests.test_import_tariff',
3918
 
        'bzrlib.tests.test_info',
3919
 
        'bzrlib.tests.test_inv',
3920
 
        'bzrlib.tests.test_inventory_delta',
3921
 
        'bzrlib.tests.test_knit',
3922
 
        'bzrlib.tests.test_lazy_import',
3923
 
        'bzrlib.tests.test_lazy_regex',
3924
 
        'bzrlib.tests.test_library_state',
3925
 
        'bzrlib.tests.test_lock',
3926
 
        'bzrlib.tests.test_lockable_files',
3927
 
        'bzrlib.tests.test_lockdir',
3928
 
        'bzrlib.tests.test_log',
3929
 
        'bzrlib.tests.test_lru_cache',
3930
 
        'bzrlib.tests.test_lsprof',
3931
 
        'bzrlib.tests.test_mail_client',
3932
 
        'bzrlib.tests.test_matchers',
3933
 
        'bzrlib.tests.test_memorytree',
3934
 
        'bzrlib.tests.test_merge',
3935
 
        'bzrlib.tests.test_merge3',
3936
 
        'bzrlib.tests.test_merge_core',
3937
 
        'bzrlib.tests.test_merge_directive',
3938
 
        'bzrlib.tests.test_mergetools',
3939
 
        'bzrlib.tests.test_missing',
3940
 
        'bzrlib.tests.test_msgeditor',
3941
 
        'bzrlib.tests.test_multiparent',
3942
 
        'bzrlib.tests.test_mutabletree',
3943
 
        'bzrlib.tests.test_nonascii',
3944
 
        'bzrlib.tests.test_options',
3945
 
        'bzrlib.tests.test_osutils',
3946
 
        'bzrlib.tests.test_osutils_encodings',
3947
 
        'bzrlib.tests.test_pack',
3948
 
        'bzrlib.tests.test_patch',
3949
 
        'bzrlib.tests.test_patches',
3950
 
        'bzrlib.tests.test_permissions',
3951
 
        'bzrlib.tests.test_plugins',
3952
 
        'bzrlib.tests.test_progress',
3953
 
        'bzrlib.tests.test_pyutils',
3954
 
        'bzrlib.tests.test_read_bundle',
3955
 
        'bzrlib.tests.test_reconcile',
3956
 
        'bzrlib.tests.test_reconfigure',
3957
 
        'bzrlib.tests.test_registry',
3958
 
        'bzrlib.tests.test_remote',
3959
 
        'bzrlib.tests.test_rename_map',
3960
 
        'bzrlib.tests.test_repository',
3961
 
        'bzrlib.tests.test_revert',
3962
 
        'bzrlib.tests.test_revision',
3963
 
        'bzrlib.tests.test_revisionspec',
3964
 
        'bzrlib.tests.test_revisiontree',
3965
 
        'bzrlib.tests.test_rio',
3966
 
        'bzrlib.tests.test_rules',
3967
 
        'bzrlib.tests.test_sampler',
3968
 
        'bzrlib.tests.test_scenarios',
3969
 
        'bzrlib.tests.test_script',
3970
 
        'bzrlib.tests.test_selftest',
3971
 
        'bzrlib.tests.test_serializer',
3972
 
        'bzrlib.tests.test_setup',
3973
 
        'bzrlib.tests.test_sftp_transport',
3974
 
        'bzrlib.tests.test_shelf',
3975
 
        'bzrlib.tests.test_shelf_ui',
3976
 
        'bzrlib.tests.test_smart',
3977
 
        'bzrlib.tests.test_smart_add',
3978
 
        'bzrlib.tests.test_smart_request',
3979
 
        'bzrlib.tests.test_smart_transport',
3980
 
        'bzrlib.tests.test_smtp_connection',
3981
 
        'bzrlib.tests.test_source',
3982
 
        'bzrlib.tests.test_ssh_transport',
3983
 
        'bzrlib.tests.test_status',
3984
 
        'bzrlib.tests.test_store',
3985
 
        'bzrlib.tests.test_strace',
3986
 
        'bzrlib.tests.test_subsume',
3987
 
        'bzrlib.tests.test_switch',
3988
 
        'bzrlib.tests.test_symbol_versioning',
3989
 
        'bzrlib.tests.test_tag',
3990
 
        'bzrlib.tests.test_test_server',
3991
 
        'bzrlib.tests.test_testament',
3992
 
        'bzrlib.tests.test_textfile',
3993
 
        'bzrlib.tests.test_textmerge',
3994
 
        'bzrlib.tests.test_cethread',
3995
 
        'bzrlib.tests.test_timestamp',
3996
 
        'bzrlib.tests.test_trace',
3997
 
        'bzrlib.tests.test_transactions',
3998
 
        'bzrlib.tests.test_transform',
3999
 
        'bzrlib.tests.test_transport',
4000
 
        'bzrlib.tests.test_transport_log',
4001
 
        'bzrlib.tests.test_tree',
4002
 
        'bzrlib.tests.test_treebuilder',
4003
 
        'bzrlib.tests.test_treeshape',
4004
 
        'bzrlib.tests.test_tsort',
4005
 
        'bzrlib.tests.test_tuned_gzip',
4006
 
        'bzrlib.tests.test_ui',
4007
 
        'bzrlib.tests.test_uncommit',
4008
 
        'bzrlib.tests.test_upgrade',
4009
 
        'bzrlib.tests.test_upgrade_stacked',
4010
 
        'bzrlib.tests.test_urlutils',
4011
 
        'bzrlib.tests.test_utextwrap',
4012
 
        'bzrlib.tests.test_version',
4013
 
        'bzrlib.tests.test_version_info',
4014
 
        'bzrlib.tests.test_versionedfile',
4015
 
        'bzrlib.tests.test_weave',
4016
 
        'bzrlib.tests.test_whitebox',
4017
 
        'bzrlib.tests.test_win32utils',
4018
 
        'bzrlib.tests.test_workingtree',
4019
 
        'bzrlib.tests.test_workingtree_4',
4020
 
        'bzrlib.tests.test_wsgi',
4021
 
        'bzrlib.tests.test_xml',
4022
 
        ]
4023
 
 
4024
 
 
4025
 
def _test_suite_modules_to_doctest():
4026
 
    """Return the list of modules to doctest."""
4027
 
    if __doc__ is None:
4028
 
        # GZ 2009-03-31: No docstrings with -OO so there's nothing to doctest
4029
 
        return []
4030
 
    return [
4031
 
        'bzrlib',
4032
 
        'bzrlib.branchbuilder',
4033
 
        'bzrlib.decorators',
4034
 
        'bzrlib.inventory',
4035
 
        'bzrlib.iterablefile',
4036
 
        'bzrlib.lockdir',
4037
 
        'bzrlib.merge3',
4038
 
        'bzrlib.option',
4039
 
        'bzrlib.pyutils',
4040
 
        'bzrlib.symbol_versioning',
4041
 
        'bzrlib.tests',
4042
 
        'bzrlib.tests.fixtures',
4043
 
        'bzrlib.timestamp',
4044
 
        'bzrlib.transport.http',
4045
 
        'bzrlib.version_info_formats.format_custom',
4046
 
        ]
4047
 
 
4048
 
 
4049
 
def test_suite(keep_only=None, starting_with=None):
4050
 
    """Build and return TestSuite for the whole of bzrlib.
4051
 
 
4052
 
    :param keep_only: A list of test ids limiting the suite returned.
4053
 
 
4054
 
    :param starting_with: An id limiting the suite returned to the tests
4055
 
         starting with it.
4056
 
 
4057
 
    This function can be replaced if you need to change the default test
4058
 
    suite on a global basis, but it is not encouraged.
4059
 
    """
4060
 
 
4061
 
    loader = TestUtil.TestLoader()
4062
 
 
4063
 
    if keep_only is not None:
4064
 
        id_filter = TestIdList(keep_only)
4065
 
    if starting_with:
4066
 
        # We take precedence over keep_only because *at loading time* using
4067
 
        # both options means we will load less tests for the same final result.
4068
 
        def interesting_module(name):
4069
 
            for start in starting_with:
4070
 
                if (
4071
 
                    # Either the module name starts with the specified string
4072
 
                    name.startswith(start)
4073
 
                    # or it may contain tests starting with the specified string
4074
 
                    or start.startswith(name)
4075
 
                    ):
4076
 
                    return True
4077
 
            return False
4078
 
        loader = TestUtil.FilteredByModuleTestLoader(interesting_module)
4079
 
 
4080
 
    elif keep_only is not None:
4081
 
        loader = TestUtil.FilteredByModuleTestLoader(id_filter.refers_to)
4082
 
        def interesting_module(name):
4083
 
            return id_filter.refers_to(name)
4084
 
 
4085
 
    else:
4086
 
        loader = TestUtil.TestLoader()
4087
 
        def interesting_module(name):
4088
 
            # No filtering, all modules are interesting
4089
 
            return True
4090
 
 
4091
 
    suite = loader.suiteClass()
4092
 
 
4093
 
    # modules building their suite with loadTestsFromModuleNames
4094
 
    suite.addTest(loader.loadTestsFromModuleNames(_test_suite_testmod_names()))
4095
 
 
4096
 
    for mod in _test_suite_modules_to_doctest():
4097
 
        if not interesting_module(mod):
4098
 
            # No tests to keep here, move along
4099
 
            continue
4100
 
        try:
4101
 
            # note that this really does mean "report only" -- doctest
4102
 
            # still runs the rest of the examples
4103
 
            doc_suite = IsolatedDocTestSuite(
4104
 
                mod, optionflags=doctest.REPORT_ONLY_FIRST_FAILURE)
4105
 
        except ValueError, e:
4106
 
            print '**failed to get doctest for: %s\n%s' % (mod, e)
4107
 
            raise
4108
 
        if len(doc_suite._tests) == 0:
4109
 
            raise errors.BzrError("no doctests found in %s" % (mod,))
4110
 
        suite.addTest(doc_suite)
4111
 
 
4112
 
    default_encoding = sys.getdefaultencoding()
4113
 
    for name, plugin in _mod_plugin.plugins().items():
4114
 
        if not interesting_module(plugin.module.__name__):
4115
 
            continue
4116
 
        plugin_suite = plugin.test_suite()
4117
 
        # We used to catch ImportError here and turn it into just a warning,
4118
 
        # but really if you don't have --no-plugins this should be a failure.
4119
 
        # mbp 20080213 - see http://bugs.launchpad.net/bugs/189771
4120
 
        if plugin_suite is None:
4121
 
            plugin_suite = plugin.load_plugin_tests(loader)
4122
 
        if plugin_suite is not None:
4123
 
            suite.addTest(plugin_suite)
4124
 
        if default_encoding != sys.getdefaultencoding():
4125
 
            trace.warning(
4126
 
                'Plugin "%s" tried to reset default encoding to: %s', name,
4127
 
                sys.getdefaultencoding())
4128
 
            reload(sys)
4129
 
            sys.setdefaultencoding(default_encoding)
4130
 
 
4131
 
    if keep_only is not None:
4132
 
        # Now that the referred modules have loaded their tests, keep only the
4133
 
        # requested ones.
4134
 
        suite = filter_suite_by_id_list(suite, id_filter)
4135
 
        # Do some sanity checks on the id_list filtering
4136
 
        not_found, duplicates = suite_matches_id_list(suite, keep_only)
4137
 
        if starting_with:
4138
 
            # The tester has used both keep_only and starting_with, so he is
4139
 
            # already aware that some tests are excluded from the list, there
4140
 
            # is no need to tell him which.
4141
 
            pass
4142
 
        else:
4143
 
            # Some tests mentioned in the list are not in the test suite. The
4144
 
            # list may be out of date, report to the tester.
4145
 
            for id in not_found:
4146
 
                trace.warning('"%s" not found in the test suite', id)
4147
 
        for id in duplicates:
4148
 
            trace.warning('"%s" is used as an id by several tests', id)
4149
 
 
4150
 
    return suite
4151
 
 
4152
 
 
4153
 
def multiply_scenarios(*scenarios):
4154
 
    """Multiply two or more iterables of scenarios.
4155
 
 
4156
 
    It is safe to pass scenario generators or iterators.
4157
 
 
4158
 
    :returns: A list of compound scenarios: the cross-product of all 
4159
 
        scenarios, with the names concatenated and the parameters
4160
 
        merged together.
4161
 
    """
4162
 
    return reduce(_multiply_two_scenarios, map(list, scenarios))
4163
 
 
4164
 
 
4165
 
def _multiply_two_scenarios(scenarios_left, scenarios_right):
4166
 
    """Multiply two sets of scenarios.
4167
 
 
4168
 
    :returns: the cartesian product of the two sets of scenarios, that is
4169
 
        a scenario for every possible combination of a left scenario and a
4170
 
        right scenario.
4171
 
    """
4172
 
    return [
4173
 
        ('%s,%s' % (left_name, right_name),
4174
 
         dict(left_dict.items() + right_dict.items()))
4175
 
        for left_name, left_dict in scenarios_left
4176
 
        for right_name, right_dict in scenarios_right]
4177
 
 
4178
 
 
4179
 
def multiply_tests(tests, scenarios, result):
4180
 
    """Multiply tests_list by scenarios into result.
4181
 
 
4182
 
    This is the core workhorse for test parameterisation.
4183
 
 
4184
 
    Typically the load_tests() method for a per-implementation test suite will
4185
 
    call multiply_tests and return the result.
4186
 
 
4187
 
    :param tests: The tests to parameterise.
4188
 
    :param scenarios: The scenarios to apply: pairs of (scenario_name,
4189
 
        scenario_param_dict).
4190
 
    :param result: A TestSuite to add created tests to.
4191
 
 
4192
 
    This returns the passed in result TestSuite with the cross product of all
4193
 
    the tests repeated once for each scenario.  Each test is adapted by adding
4194
 
    the scenario name at the end of its id(), and updating the test object's
4195
 
    __dict__ with the scenario_param_dict.
4196
 
 
4197
 
    >>> import bzrlib.tests.test_sampler
4198
 
    >>> r = multiply_tests(
4199
 
    ...     bzrlib.tests.test_sampler.DemoTest('test_nothing'),
4200
 
    ...     [('one', dict(param=1)),
4201
 
    ...      ('two', dict(param=2))],
4202
 
    ...     TestUtil.TestSuite())
4203
 
    >>> tests = list(iter_suite_tests(r))
4204
 
    >>> len(tests)
4205
 
    2
4206
 
    >>> tests[0].id()
4207
 
    'bzrlib.tests.test_sampler.DemoTest.test_nothing(one)'
4208
 
    >>> tests[0].param
4209
 
    1
4210
 
    >>> tests[1].param
4211
 
    2
4212
 
    """
4213
 
    for test in iter_suite_tests(tests):
4214
 
        apply_scenarios(test, scenarios, result)
4215
 
    return result
4216
 
 
4217
 
 
4218
 
def apply_scenarios(test, scenarios, result):
4219
 
    """Apply the scenarios in scenarios to test and add to result.
4220
 
 
4221
 
    :param test: The test to apply scenarios to.
4222
 
    :param scenarios: An iterable of scenarios to apply to test.
4223
 
    :return: result
4224
 
    :seealso: apply_scenario
4225
 
    """
4226
 
    for scenario in scenarios:
4227
 
        result.addTest(apply_scenario(test, scenario))
4228
 
    return result
4229
 
 
4230
 
 
4231
 
def apply_scenario(test, scenario):
4232
 
    """Copy test and apply scenario to it.
4233
 
 
4234
 
    :param test: A test to adapt.
4235
 
    :param scenario: A tuple describing the scenarion.
4236
 
        The first element of the tuple is the new test id.
4237
 
        The second element is a dict containing attributes to set on the
4238
 
        test.
4239
 
    :return: The adapted test.
4240
 
    """
4241
 
    new_id = "%s(%s)" % (test.id(), scenario[0])
4242
 
    new_test = clone_test(test, new_id)
4243
 
    for name, value in scenario[1].items():
4244
 
        setattr(new_test, name, value)
4245
 
    return new_test
4246
 
 
4247
 
 
4248
 
def clone_test(test, new_id):
4249
 
    """Clone a test giving it a new id.
4250
 
 
4251
 
    :param test: The test to clone.
4252
 
    :param new_id: The id to assign to it.
4253
 
    :return: The new test.
4254
 
    """
4255
 
    new_test = copy.copy(test)
4256
 
    new_test.id = lambda: new_id
4257
 
    # XXX: Workaround <https://bugs.launchpad.net/testtools/+bug/637725>, which
4258
 
    # causes cloned tests to share the 'details' dict.  This makes it hard to
4259
 
    # read the test output for parameterized tests, because tracebacks will be
4260
 
    # associated with irrelevant tests.
4261
 
    try:
4262
 
        details = new_test._TestCase__details
4263
 
    except AttributeError:
4264
 
        # must be a different version of testtools than expected.  Do nothing.
4265
 
        pass
4266
 
    else:
4267
 
        # Reset the '__details' dict.
4268
 
        new_test._TestCase__details = {}
4269
 
    return new_test
4270
 
 
4271
 
 
4272
 
def permute_tests_for_extension(standard_tests, loader, py_module_name,
4273
 
                                ext_module_name):
4274
 
    """Helper for permutating tests against an extension module.
4275
 
 
4276
 
    This is meant to be used inside a modules 'load_tests()' function. It will
4277
 
    create 2 scenarios, and cause all tests in the 'standard_tests' to be run
4278
 
    against both implementations. Setting 'test.module' to the appropriate
4279
 
    module. See bzrlib.tests.test__chk_map.load_tests as an example.
4280
 
 
4281
 
    :param standard_tests: A test suite to permute
4282
 
    :param loader: A TestLoader
4283
 
    :param py_module_name: The python path to a python module that can always
4284
 
        be loaded, and will be considered the 'python' implementation. (eg
4285
 
        'bzrlib._chk_map_py')
4286
 
    :param ext_module_name: The python path to an extension module. If the
4287
 
        module cannot be loaded, a single test will be added, which notes that
4288
 
        the module is not available. If it can be loaded, all standard_tests
4289
 
        will be run against that module.
4290
 
    :return: (suite, feature) suite is a test-suite that has all the permuted
4291
 
        tests. feature is the Feature object that can be used to determine if
4292
 
        the module is available.
4293
 
    """
4294
 
 
4295
 
    py_module = pyutils.get_named_object(py_module_name)
4296
 
    scenarios = [
4297
 
        ('python', {'module': py_module}),
4298
 
    ]
4299
 
    suite = loader.suiteClass()
4300
 
    feature = ModuleAvailableFeature(ext_module_name)
4301
 
    if feature.available():
4302
 
        scenarios.append(('C', {'module': feature.module}))
4303
 
    else:
4304
 
        # the compiled module isn't available, so we add a failing test
4305
 
        class FailWithoutFeature(TestCase):
4306
 
            def test_fail(self):
4307
 
                self.requireFeature(feature)
4308
 
        suite.addTest(loader.loadTestsFromTestCase(FailWithoutFeature))
4309
 
    result = multiply_tests(standard_tests, scenarios, suite)
4310
 
    return result, feature
4311
 
 
4312
 
 
4313
 
def _rmtree_temp_dir(dirname, test_id=None):
4314
 
    # If LANG=C we probably have created some bogus paths
4315
 
    # which rmtree(unicode) will fail to delete
4316
 
    # so make sure we are using rmtree(str) to delete everything
4317
 
    # except on win32, where rmtree(str) will fail
4318
 
    # since it doesn't have the property of byte-stream paths
4319
 
    # (they are either ascii or mbcs)
4320
 
    if sys.platform == 'win32':
4321
 
        # make sure we are using the unicode win32 api
4322
 
        dirname = unicode(dirname)
4323
 
    else:
4324
 
        dirname = dirname.encode(sys.getfilesystemencoding())
4325
 
    try:
4326
 
        osutils.rmtree(dirname)
4327
 
    except OSError, e:
4328
 
        # We don't want to fail here because some useful display will be lost
4329
 
        # otherwise. Polluting the tmp dir is bad, but not giving all the
4330
 
        # possible info to the test runner is even worse.
4331
 
        if test_id != None:
4332
 
            ui.ui_factory.clear_term()
4333
 
            sys.stderr.write('\nWhile running: %s\n' % (test_id,))
4334
 
        # Ugly, but the last thing we want here is fail, so bear with it.
4335
 
        printable_e = str(e).decode(osutils.get_user_encoding(), 'replace'
4336
 
                                    ).encode('ascii', 'replace')
4337
 
        sys.stderr.write('Unable to remove testing dir %s\n%s'
4338
 
                         % (os.path.basename(dirname), printable_e))
4339
 
 
4340
 
 
4341
 
class Feature(object):
4342
 
    """An operating system Feature."""
4343
 
 
4344
 
    def __init__(self):
4345
 
        self._available = None
4346
 
 
4347
 
    def available(self):
4348
 
        """Is the feature available?
4349
 
 
4350
 
        :return: True if the feature is available.
4351
 
        """
4352
 
        if self._available is None:
4353
 
            self._available = self._probe()
4354
 
        return self._available
4355
 
 
4356
 
    def _probe(self):
4357
 
        """Implement this method in concrete features.
4358
 
 
4359
 
        :return: True if the feature is available.
4360
 
        """
4361
 
        raise NotImplementedError
4362
 
 
4363
 
    def __str__(self):
4364
 
        if getattr(self, 'feature_name', None):
4365
 
            return self.feature_name()
4366
 
        return self.__class__.__name__
4367
 
 
4368
 
 
4369
 
class _SymlinkFeature(Feature):
4370
 
 
4371
 
    def _probe(self):
4372
 
        return osutils.has_symlinks()
4373
 
 
4374
 
    def feature_name(self):
4375
 
        return 'symlinks'
4376
 
 
4377
 
SymlinkFeature = _SymlinkFeature()
4378
 
 
4379
 
 
4380
 
class _HardlinkFeature(Feature):
4381
 
 
4382
 
    def _probe(self):
4383
 
        return osutils.has_hardlinks()
4384
 
 
4385
 
    def feature_name(self):
4386
 
        return 'hardlinks'
4387
 
 
4388
 
HardlinkFeature = _HardlinkFeature()
4389
 
 
4390
 
 
4391
 
class _OsFifoFeature(Feature):
4392
 
 
4393
 
    def _probe(self):
4394
 
        return getattr(os, 'mkfifo', None)
4395
 
 
4396
 
    def feature_name(self):
4397
 
        return 'filesystem fifos'
4398
 
 
4399
 
OsFifoFeature = _OsFifoFeature()
4400
 
 
4401
 
 
4402
 
class _UnicodeFilenameFeature(Feature):
4403
 
    """Does the filesystem support Unicode filenames?"""
4404
 
 
4405
 
    def _probe(self):
4406
 
        try:
4407
 
            # Check for character combinations unlikely to be covered by any
4408
 
            # single non-unicode encoding. We use the characters
4409
 
            # - greek small letter alpha (U+03B1) and
4410
 
            # - braille pattern dots-123456 (U+283F).
4411
 
            os.stat(u'\u03b1\u283f')
4412
 
        except UnicodeEncodeError:
4413
 
            return False
4414
 
        except (IOError, OSError):
4415
 
            # The filesystem allows the Unicode filename but the file doesn't
4416
 
            # exist.
4417
 
            return True
4418
 
        else:
4419
 
            # The filesystem allows the Unicode filename and the file exists,
4420
 
            # for some reason.
4421
 
            return True
4422
 
 
4423
 
UnicodeFilenameFeature = _UnicodeFilenameFeature()
4424
 
 
4425
 
 
4426
 
class _CompatabilityThunkFeature(Feature):
4427
 
    """This feature is just a thunk to another feature.
4428
 
 
4429
 
    It issues a deprecation warning if it is accessed, to let you know that you
4430
 
    should really use a different feature.
4431
 
    """
4432
 
 
4433
 
    def __init__(self, dep_version, module, name,
4434
 
                 replacement_name, replacement_module=None):
4435
 
        super(_CompatabilityThunkFeature, self).__init__()
4436
 
        self._module = module
4437
 
        if replacement_module is None:
4438
 
            replacement_module = module
4439
 
        self._replacement_module = replacement_module
4440
 
        self._name = name
4441
 
        self._replacement_name = replacement_name
4442
 
        self._dep_version = dep_version
4443
 
        self._feature = None
4444
 
 
4445
 
    def _ensure(self):
4446
 
        if self._feature is None:
4447
 
            depr_msg = self._dep_version % ('%s.%s'
4448
 
                                            % (self._module, self._name))
4449
 
            use_msg = ' Use %s.%s instead.' % (self._replacement_module,
4450
 
                                               self._replacement_name)
4451
 
            symbol_versioning.warn(depr_msg + use_msg, DeprecationWarning)
4452
 
            # Import the new feature and use it as a replacement for the
4453
 
            # deprecated one.
4454
 
            self._feature = pyutils.get_named_object(
4455
 
                self._replacement_module, self._replacement_name)
4456
 
 
4457
 
    def _probe(self):
4458
 
        self._ensure()
4459
 
        return self._feature._probe()
4460
 
 
4461
 
 
4462
 
class ModuleAvailableFeature(Feature):
4463
 
    """This is a feature than describes a module we want to be available.
4464
 
 
4465
 
    Declare the name of the module in __init__(), and then after probing, the
4466
 
    module will be available as 'self.module'.
4467
 
 
4468
 
    :ivar module: The module if it is available, else None.
4469
 
    """
4470
 
 
4471
 
    def __init__(self, module_name):
4472
 
        super(ModuleAvailableFeature, self).__init__()
4473
 
        self.module_name = module_name
4474
 
 
4475
 
    def _probe(self):
4476
 
        try:
4477
 
            self._module = __import__(self.module_name, {}, {}, [''])
4478
 
            return True
4479
 
        except ImportError:
4480
 
            return False
4481
 
 
4482
 
    @property
4483
 
    def module(self):
4484
 
        if self.available(): # Make sure the probe has been done
4485
 
            return self._module
4486
 
        return None
4487
 
 
4488
 
    def feature_name(self):
4489
 
        return self.module_name
4490
 
 
4491
 
 
4492
 
def probe_unicode_in_user_encoding():
4493
 
    """Try to encode several unicode strings to use in unicode-aware tests.
4494
 
    Return first successfull match.
4495
 
 
4496
 
    :return:  (unicode value, encoded plain string value) or (None, None)
4497
 
    """
4498
 
    possible_vals = [u'm\xb5', u'\xe1', u'\u0410']
4499
 
    for uni_val in possible_vals:
4500
 
        try:
4501
 
            str_val = uni_val.encode(osutils.get_user_encoding())
4502
 
        except UnicodeEncodeError:
4503
 
            # Try a different character
4504
 
            pass
4505
 
        else:
4506
 
            return uni_val, str_val
4507
 
    return None, None
4508
 
 
4509
 
 
4510
 
def probe_bad_non_ascii(encoding):
4511
 
    """Try to find [bad] character with code [128..255]
4512
 
    that cannot be decoded to unicode in some encoding.
4513
 
    Return None if all non-ascii characters is valid
4514
 
    for given encoding.
4515
 
    """
4516
 
    for i in xrange(128, 256):
4517
 
        char = chr(i)
4518
 
        try:
4519
 
            char.decode(encoding)
4520
 
        except UnicodeDecodeError:
4521
 
            return char
4522
 
    return None
4523
 
 
4524
 
 
4525
 
class _HTTPSServerFeature(Feature):
4526
 
    """Some tests want an https Server, check if one is available.
4527
 
 
4528
 
    Right now, the only way this is available is under python2.6 which provides
4529
 
    an ssl module.
4530
 
    """
4531
 
 
4532
 
    def _probe(self):
4533
 
        try:
4534
 
            import ssl
4535
 
            return True
4536
 
        except ImportError:
4537
 
            return False
4538
 
 
4539
 
    def feature_name(self):
4540
 
        return 'HTTPSServer'
4541
 
 
4542
 
 
4543
 
HTTPSServerFeature = _HTTPSServerFeature()
4544
 
 
4545
 
 
4546
 
class _UnicodeFilename(Feature):
4547
 
    """Does the filesystem support Unicode filenames?"""
4548
 
 
4549
 
    def _probe(self):
4550
 
        try:
4551
 
            os.stat(u'\u03b1')
4552
 
        except UnicodeEncodeError:
4553
 
            return False
4554
 
        except (IOError, OSError):
4555
 
            # The filesystem allows the Unicode filename but the file doesn't
4556
 
            # exist.
4557
 
            return True
4558
 
        else:
4559
 
            # The filesystem allows the Unicode filename and the file exists,
4560
 
            # for some reason.
4561
 
            return True
4562
 
 
4563
 
UnicodeFilename = _UnicodeFilename()
4564
 
 
4565
 
 
4566
 
class _ByteStringNamedFilesystem(Feature):
4567
 
    """Is the filesystem based on bytes?"""
4568
 
 
4569
 
    def _probe(self):
4570
 
        if os.name == "posix":
4571
 
            return True
4572
 
        return False
4573
 
 
4574
 
ByteStringNamedFilesystem = _ByteStringNamedFilesystem()
4575
 
 
4576
 
 
4577
 
class _UTF8Filesystem(Feature):
4578
 
    """Is the filesystem UTF-8?"""
4579
 
 
4580
 
    def _probe(self):
4581
 
        if osutils._fs_enc.upper() in ('UTF-8', 'UTF8'):
4582
 
            return True
4583
 
        return False
4584
 
 
4585
 
UTF8Filesystem = _UTF8Filesystem()
4586
 
 
4587
 
 
4588
 
class _BreakinFeature(Feature):
4589
 
    """Does this platform support the breakin feature?"""
4590
 
 
4591
 
    def _probe(self):
4592
 
        from bzrlib import breakin
4593
 
        if breakin.determine_signal() is None:
4594
 
            return False
4595
 
        if sys.platform == 'win32':
4596
 
            # Windows doesn't have os.kill, and we catch the SIGBREAK signal.
4597
 
            # We trigger SIGBREAK via a Console api so we need ctypes to
4598
 
            # access the function
4599
 
            try:
4600
 
                import ctypes
4601
 
            except OSError:
4602
 
                return False
4603
 
        return True
4604
 
 
4605
 
    def feature_name(self):
4606
 
        return "SIGQUIT or SIGBREAK w/ctypes on win32"
4607
 
 
4608
 
 
4609
 
BreakinFeature = _BreakinFeature()
4610
 
 
4611
 
 
4612
 
class _CaseInsCasePresFilenameFeature(Feature):
4613
 
    """Is the file-system case insensitive, but case-preserving?"""
4614
 
 
4615
 
    def _probe(self):
4616
 
        fileno, name = tempfile.mkstemp(prefix='MixedCase')
4617
 
        try:
4618
 
            # first check truly case-preserving for created files, then check
4619
 
            # case insensitive when opening existing files.
4620
 
            name = osutils.normpath(name)
4621
 
            base, rel = osutils.split(name)
4622
 
            found_rel = osutils.canonical_relpath(base, name)
4623
 
            return (found_rel == rel
4624
 
                    and os.path.isfile(name.upper())
4625
 
                    and os.path.isfile(name.lower()))
4626
 
        finally:
4627
 
            os.close(fileno)
4628
 
            os.remove(name)
4629
 
 
4630
 
    def feature_name(self):
4631
 
        return "case-insensitive case-preserving filesystem"
4632
 
 
4633
 
CaseInsCasePresFilenameFeature = _CaseInsCasePresFilenameFeature()
4634
 
 
4635
 
 
4636
 
class _CaseInsensitiveFilesystemFeature(Feature):
4637
 
    """Check if underlying filesystem is case-insensitive but *not* case
4638
 
    preserving.
4639
 
    """
4640
 
    # Note that on Windows, Cygwin, MacOS etc, the file-systems are far
4641
 
    # more likely to be case preserving, so this case is rare.
4642
 
 
4643
 
    def _probe(self):
4644
 
        if CaseInsCasePresFilenameFeature.available():
4645
 
            return False
4646
 
 
4647
 
        if TestCaseWithMemoryTransport.TEST_ROOT is None:
4648
 
            root = osutils.mkdtemp(prefix='testbzr-', suffix='.tmp')
4649
 
            TestCaseWithMemoryTransport.TEST_ROOT = root
4650
 
        else:
4651
 
            root = TestCaseWithMemoryTransport.TEST_ROOT
4652
 
        tdir = osutils.mkdtemp(prefix='case-sensitive-probe-', suffix='',
4653
 
            dir=root)
4654
 
        name_a = osutils.pathjoin(tdir, 'a')
4655
 
        name_A = osutils.pathjoin(tdir, 'A')
4656
 
        os.mkdir(name_a)
4657
 
        result = osutils.isdir(name_A)
4658
 
        _rmtree_temp_dir(tdir)
4659
 
        return result
4660
 
 
4661
 
    def feature_name(self):
4662
 
        return 'case-insensitive filesystem'
4663
 
 
4664
 
CaseInsensitiveFilesystemFeature = _CaseInsensitiveFilesystemFeature()
4665
 
 
4666
 
 
4667
 
class _CaseSensitiveFilesystemFeature(Feature):
4668
 
 
4669
 
    def _probe(self):
4670
 
        if CaseInsCasePresFilenameFeature.available():
4671
 
            return False
4672
 
        elif CaseInsensitiveFilesystemFeature.available():
4673
 
            return False
4674
 
        else:
4675
 
            return True
4676
 
 
4677
 
    def feature_name(self):
4678
 
        return 'case-sensitive filesystem'
4679
 
 
4680
 
# new coding style is for feature instances to be lowercase
4681
 
case_sensitive_filesystem_feature = _CaseSensitiveFilesystemFeature()
4682
 
 
4683
 
 
4684
 
# Only define SubUnitBzrRunner if subunit is available.
4685
 
try:
4686
 
    from subunit import TestProtocolClient
4687
 
    from subunit.test_results import AutoTimingTestResultDecorator
4688
 
    class SubUnitBzrProtocolClient(TestProtocolClient):
4689
 
 
4690
 
        def addSuccess(self, test, details=None):
4691
 
            # The subunit client always includes the details in the subunit
4692
 
            # stream, but we don't want to include it in ours.
4693
 
            if details is not None and 'log' in details:
4694
 
                del details['log']
4695
 
            return super(SubUnitBzrProtocolClient, self).addSuccess(
4696
 
                test, details)
4697
 
 
4698
 
    class SubUnitBzrRunner(TextTestRunner):
4699
 
        def run(self, test):
4700
 
            result = AutoTimingTestResultDecorator(
4701
 
                SubUnitBzrProtocolClient(self.stream))
4702
 
            test.run(result)
4703
 
            return result
4704
 
except ImportError:
4705
 
    pass
4706
 
 
4707
 
class _PosixPermissionsFeature(Feature):
4708
 
 
4709
 
    def _probe(self):
4710
 
        def has_perms():
4711
 
            # create temporary file and check if specified perms are maintained.
4712
 
            import tempfile
4713
 
 
4714
 
            write_perms = stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR
4715
 
            f = tempfile.mkstemp(prefix='bzr_perms_chk_')
4716
 
            fd, name = f
4717
 
            os.close(fd)
4718
 
            os.chmod(name, write_perms)
4719
 
 
4720
 
            read_perms = os.stat(name).st_mode & 0777
4721
 
            os.unlink(name)
4722
 
            return (write_perms == read_perms)
4723
 
 
4724
 
        return (os.name == 'posix') and has_perms()
4725
 
 
4726
 
    def feature_name(self):
4727
 
        return 'POSIX permissions support'
4728
 
 
4729
 
posix_permissions_feature = _PosixPermissionsFeature()
 
286
                os.mkdir(name[:-1])
 
287
            else:
 
288
                f = file(name, 'wt')
 
289
                print >>f, "contents of", name
 
290
                f.close()
 
291
                
 
292
 
 
293
 
 
294
class MetaTestLog(TestCase):
 
295
    def test_logging(self):
 
296
        """Test logs are captured when a test fails."""
 
297
        logging.info('an info message')
 
298
        warning('something looks dodgy...')
 
299
        logging.debug('hello, test is running')
 
300
        ##assert 0
 
301
 
 
302
 
 
303
def selftest(verbose=False, pattern=".*"):
 
304
    return testsweet.run_suite(test_suite(), 'testbzr', verbose=verbose, pattern=pattern)
 
305
 
 
306
 
 
307
def test_suite():
 
308
    from bzrlib.selftest.TestUtil import TestLoader, TestSuite
 
309
    import bzrlib, bzrlib.store, bzrlib.inventory, bzrlib.branch
 
310
    import bzrlib.osutils, bzrlib.commands, bzrlib.merge3, bzrlib.plugin
 
311
    from doctest import DocTestSuite
 
312
 
 
313
    global MODULES_TO_TEST, MODULES_TO_DOCTEST
 
314
 
 
315
    testmod_names = \
 
316
                  ['bzrlib.selftest.MetaTestLog',
 
317
                   'bzrlib.selftest.test_parent',
 
318
                   'bzrlib.selftest.testinv',
 
319
                   'bzrlib.selftest.testfetch',
 
320
                   'bzrlib.selftest.versioning',
 
321
                   'bzrlib.selftest.whitebox',
 
322
                   'bzrlib.selftest.testmerge3',
 
323
                   'bzrlib.selftest.testmerge',
 
324
                   'bzrlib.selftest.testhashcache',
 
325
                   'bzrlib.selftest.teststatus',
 
326
                   'bzrlib.selftest.testlog',
 
327
                   'bzrlib.selftest.blackbox',
 
328
                   'bzrlib.selftest.testrevisionnamespaces',
 
329
                   'bzrlib.selftest.testbranch',
 
330
                   'bzrlib.selftest.testremotebranch',
 
331
                   'bzrlib.selftest.testrevision',
 
332
                   'bzrlib.selftest.test_merge_core',
 
333
                   'bzrlib.selftest.test_smart_add',
 
334
                   'bzrlib.selftest.testdiff',
 
335
                   'bzrlib.selftest.test_xml',
 
336
                   'bzrlib.fetch',
 
337
                   'bzrlib.selftest.teststore',
 
338
                   'bzrlib.selftest.testtransport',
 
339
                   'bzrlib.selftest.testgraph',
 
340
                   ]
 
341
 
 
342
    for m in (bzrlib.store, bzrlib.inventory, bzrlib.branch,
 
343
              bzrlib.osutils, bzrlib.commands, bzrlib.merge3):
 
344
        if m not in MODULES_TO_DOCTEST:
 
345
            MODULES_TO_DOCTEST.append(m)
 
346
 
 
347
    TestCase.BZRPATH = os.path.join(os.path.realpath(os.path.dirname(bzrlib.__path__[0])), 'bzr')
 
348
    print '%-30s %s' % ('bzr binary', TestCase.BZRPATH)
 
349
    print
 
350
    suite = TestSuite()
 
351
    suite.addTest(TestLoader().loadTestsFromNames(testmod_names))
 
352
    for m in MODULES_TO_TEST:
 
353
         suite.addTest(TestLoader().loadTestsFromModule(m))
 
354
    for m in (MODULES_TO_DOCTEST):
 
355
        suite.addTest(DocTestSuite(m))
 
356
    for p in bzrlib.plugin.all_plugins:
 
357
        if hasattr(p, 'test_suite'):
 
358
            suite.addTest(p.test_suite())
 
359
    return suite
 
360