~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/__init__.py

Merge updated set_parents api.

Show diffs side-by-side

added added

removed removed

Lines of Context:
63
63
import bzrlib.plugin
64
64
import bzrlib.progress as progress
65
65
from bzrlib.revision import common_ancestor
 
66
from bzrlib.revisionspec import RevisionSpec
66
67
import bzrlib.store
 
68
from bzrlib import symbol_versioning
67
69
import bzrlib.trace
68
70
from bzrlib.transport import get_transport
69
71
import bzrlib.transport
136
138
    """
137
139
    stop_early = False
138
140
    
139
 
    def __init__(self, stream, descriptions, verbosity, pb=None):
 
141
    def __init__(self, stream, descriptions, verbosity, pb=None,
 
142
                 bench_history=None):
 
143
        """Construct new TestResult.
 
144
 
 
145
        :param bench_history: Optionally, a writable file object to accumulate
 
146
            benchmark results.
 
147
        """
140
148
        unittest._TextTestResult.__init__(self, stream, descriptions, verbosity)
141
149
        self.pb = pb
 
150
        if bench_history is not None:
 
151
            from bzrlib.version import _get_bzr_source_tree
 
152
            src_tree = _get_bzr_source_tree()
 
153
            if src_tree:
 
154
                revision_id = src_tree.last_revision()
 
155
            else:
 
156
                # XXX: If there's no branch, what should we do?
 
157
                revision_id = ''
 
158
            bench_history.write("--date %s %s\n" % (time.time(), revision_id))
 
159
        self._bench_history = bench_history
142
160
    
143
161
    def extractBenchmarkTime(self, testCase):
144
162
        """Add a benchmark time for the current test case."""
248
266
 
249
267
    def addSuccess(self, test):
250
268
        self.extractBenchmarkTime(test)
 
269
        if self._bench_history is not None:
 
270
            if self._benchmarkTime is not None:
 
271
                self._bench_history.write("%s %s\n" % (
 
272
                    self._formatTime(self._benchmarkTime),
 
273
                    test.id()))
251
274
        if self.showAll:
252
275
            self.stream.writeln('   OK %s' % self._testTimeString())
253
276
            for bench_called, stats in getattr(test, '_benchcalls', []):
304
327
                 descriptions=0,
305
328
                 verbosity=1,
306
329
                 keep_output=False,
307
 
                 pb=None):
 
330
                 pb=None,
 
331
                 bench_history=None):
308
332
        self.stream = unittest._WritelnDecorator(stream)
309
333
        self.descriptions = descriptions
310
334
        self.verbosity = verbosity
311
335
        self.keep_output = keep_output
312
336
        self.pb = pb
 
337
        self._bench_history = bench_history
313
338
 
314
339
    def _makeResult(self):
315
340
        result = _MyResult(self.stream,
316
341
                           self.descriptions,
317
342
                           self.verbosity,
318
 
                           pb=self.pb)
 
343
                           pb=self.pb,
 
344
                           bench_history=self._bench_history)
319
345
        result.stop_early = self.stop_on_failure
320
346
        return result
321
347
 
395
421
 
396
422
class TestSkipped(Exception):
397
423
    """Indicates that a test was intentionally skipped, rather than failing."""
398
 
    # XXX: Not used yet
399
424
 
400
425
 
401
426
class CommandFailed(Exception):
552
577
            self.fail("%r is an instance of %s rather than %s" % (
553
578
                obj, obj.__class__, kls))
554
579
 
 
580
    def assertDeprecated(self, expected, callable, *args, **kwargs):
 
581
        """Assert that a callable is deprecated in a particular way.
 
582
 
 
583
        :param expected: a list of the deprecation warnings expected, in order
 
584
        :param callable: The callable to call
 
585
        :param args: The positional arguments for the callable
 
586
        :param kwargs: The keyword arguments for the callable
 
587
        """
 
588
        local_warnings = []
 
589
        def capture_warnings(msg, cls, stacklevel=None):
 
590
            self.assertEqual(cls, DeprecationWarning)
 
591
            local_warnings.append(msg)
 
592
        method = symbol_versioning.warn
 
593
        symbol_versioning.set_warning_method(capture_warnings)
 
594
        try:
 
595
            callable(*args, **kwargs)
 
596
        finally:
 
597
            result = symbol_versioning.set_warning_method(method)
 
598
        self.assertEqual(expected, local_warnings)
 
599
        return result
 
600
 
555
601
    def _startLogFile(self):
556
602
        """Send bzr and test log messages to a temporary file.
557
603
 
1209
1255
 
1210
1256
def run_suite(suite, name='test', verbose=False, pattern=".*",
1211
1257
              stop_on_failure=False, keep_output=False,
1212
 
              transport=None, lsprof_timed=None):
 
1258
              transport=None, lsprof_timed=None, bench_history=None):
1213
1259
    TestCaseInTempDir._TEST_NAME = name
1214
1260
    TestCase._gather_lsprof_in_benchmarks = lsprof_timed
1215
1261
    if verbose:
1222
1268
                            descriptions=0,
1223
1269
                            verbosity=verbosity,
1224
1270
                            keep_output=keep_output,
1225
 
                            pb=pb)
 
1271
                            pb=pb,
 
1272
                            bench_history=bench_history)
1226
1273
    runner.stop_on_failure=stop_on_failure
1227
1274
    if pattern != '.*':
1228
1275
        suite = filter_suite_by_re(suite, pattern)
1234
1281
             keep_output=False,
1235
1282
             transport=None,
1236
1283
             test_suite_factory=None,
1237
 
             lsprof_timed=None):
 
1284
             lsprof_timed=None,
 
1285
             bench_history=None):
1238
1286
    """Run the whole test suite under the enhanced runner"""
1239
1287
    # XXX: Very ugly way to do this...
1240
1288
    # Disable warning about old formats because we don't want it to disturb
1255
1303
        return run_suite(suite, 'testbzr', verbose=verbose, pattern=pattern,
1256
1304
                     stop_on_failure=stop_on_failure, keep_output=keep_output,
1257
1305
                     transport=transport,
1258
 
                     lsprof_timed=lsprof_timed)
 
1306
                     lsprof_timed=lsprof_timed,
 
1307
                     bench_history=bench_history)
1259
1308
    finally:
1260
1309
        default_transport = old_transport
1261
1310
 
1340
1389
                   'bzrlib.tests.test_upgrade',
1341
1390
                   'bzrlib.tests.test_urlutils',
1342
1391
                   'bzrlib.tests.test_versionedfile',
 
1392
                   'bzrlib.tests.test_version',
1343
1393
                   'bzrlib.tests.test_weave',
1344
1394
                   'bzrlib.tests.test_whitebox',
1345
1395
                   'bzrlib.tests.test_workingtree',