~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_selftest.py

Late bind to PatienceSequenceMatcher to allow plugin to override.

Show diffs side-by-side

added added

removed removed

Lines of Context:
22
22
import unittest
23
23
import warnings
24
24
 
25
 
from bzrlib import osutils
26
25
import bzrlib
27
26
from bzrlib.progress import _BaseProgressBar
28
27
from bzrlib.tests import (
36
35
                          )
37
36
from bzrlib.tests.TestUtil import _load_module_by_name
38
37
import bzrlib.errors as errors
39
 
from bzrlib.trace import note
40
38
 
41
39
 
42
40
class SelftestTests(TestCase):
324
322
        self.assertEqual(tests[1].transport_readonly_server, server2)
325
323
 
326
324
 
327
 
class TestTreeProviderAdapter(TestCase):
328
 
    """Test the setup of tree_implementation tests."""
329
 
 
330
 
    def test_adapted_tests(self):
331
 
        # the tree implementation adapter is meant to setup one instance for
332
 
        # each working tree format, and one additional instance that will
333
 
        # use the default wt format, but create a revision tree for the tests.
334
 
        # this means that the wt ones should have the workingtree_to_test_tree
335
 
        # attribute set to 'return_parameter' and the revision one set to
336
 
        # revision_tree_from_workingtree.
337
 
 
338
 
        from bzrlib.tests.tree_implementations import (
339
 
            TreeTestProviderAdapter,
340
 
            return_parameter,
341
 
            revision_tree_from_workingtree
342
 
            )
343
 
        from bzrlib.workingtree import WorkingTreeFormat
344
 
        input_test = TestTreeProviderAdapter(
345
 
            "test_adapted_tests")
346
 
        server1 = "a"
347
 
        server2 = "b"
348
 
        formats = [("c", "C"), ("d", "D")]
349
 
        adapter = TreeTestProviderAdapter(server1, server2, formats)
350
 
        suite = adapter.adapt(input_test)
351
 
        tests = list(iter(suite))
352
 
        self.assertEqual(3, len(tests))
353
 
        default_format = WorkingTreeFormat.get_default_format()
354
 
        self.assertEqual(tests[0].workingtree_format, formats[0][0])
355
 
        self.assertEqual(tests[0].bzrdir_format, formats[0][1])
356
 
        self.assertEqual(tests[0].transport_server, server1)
357
 
        self.assertEqual(tests[0].transport_readonly_server, server2)
358
 
        self.assertEqual(tests[0].workingtree_to_test_tree, return_parameter)
359
 
        self.assertEqual(tests[1].workingtree_format, formats[1][0])
360
 
        self.assertEqual(tests[1].bzrdir_format, formats[1][1])
361
 
        self.assertEqual(tests[1].transport_server, server1)
362
 
        self.assertEqual(tests[1].transport_readonly_server, server2)
363
 
        self.assertEqual(tests[1].workingtree_to_test_tree, return_parameter)
364
 
        self.assertEqual(tests[2].workingtree_format, default_format)
365
 
        self.assertEqual(tests[2].bzrdir_format, default_format._matchingbzrdir)
366
 
        self.assertEqual(tests[2].transport_server, server1)
367
 
        self.assertEqual(tests[2].transport_readonly_server, server2)
368
 
        self.assertEqual(tests[2].workingtree_to_test_tree,
369
 
            revision_tree_from_workingtree)
370
 
 
371
 
 
372
 
class TestInterTreeProviderAdapter(TestCase):
373
 
    """A group of tests that test the InterTreeTestAdapter."""
374
 
 
375
 
    def test_adapted_tests(self):
376
 
        # check that constructor parameters are passed through to the adapted
377
 
        # test.
378
 
        # for InterTree tests we want the machinery to bring up two trees in
379
 
        # each instance: the base one, and the one we are interacting with.
380
 
        # because each optimiser can be direction specific, we need to test
381
 
        # each optimiser in its chosen direction.
382
 
        # unlike the TestProviderAdapter we dont want to automatically add a
383
 
        # parameterised one for WorkingTree - the optimisers will tell us what
384
 
        # ones to add.
385
 
        from bzrlib.tests.tree_implementations import (
386
 
            return_parameter,
387
 
            revision_tree_from_workingtree
388
 
            )
389
 
        from bzrlib.tests.intertree_implementations import (
390
 
            InterTreeTestProviderAdapter,
391
 
            )
392
 
        from bzrlib.workingtree import WorkingTreeFormat2, WorkingTreeFormat3
393
 
        input_test = TestInterTreeProviderAdapter(
394
 
            "test_adapted_tests")
395
 
        server1 = "a"
396
 
        server2 = "b"
397
 
        format1 = WorkingTreeFormat2()
398
 
        format2 = WorkingTreeFormat3()
399
 
        formats = [(str, format1, format2, False, True),
400
 
            (int, format2, format1, False, True)]
401
 
        adapter = InterTreeTestProviderAdapter(server1, server2, formats)
402
 
        suite = adapter.adapt(input_test)
403
 
        tests = list(iter(suite))
404
 
        self.assertEqual(2, len(tests))
405
 
        self.assertEqual(tests[0].intertree_class, formats[0][0])
406
 
        self.assertEqual(tests[0].workingtree_format, formats[0][1])
407
 
        self.assertEqual(tests[0].workingtree_to_test_tree, formats[0][2])
408
 
        self.assertEqual(tests[0].workingtree_format_to, formats[0][3])
409
 
        self.assertEqual(tests[0].workingtree_to_test_tree_to, formats[0][4])
410
 
        self.assertEqual(tests[0].transport_server, server1)
411
 
        self.assertEqual(tests[0].transport_readonly_server, server2)
412
 
        self.assertEqual(tests[1].intertree_class, formats[1][0])
413
 
        self.assertEqual(tests[1].workingtree_format, formats[1][1])
414
 
        self.assertEqual(tests[1].workingtree_to_test_tree, formats[1][2])
415
 
        self.assertEqual(tests[1].workingtree_format_to, formats[1][3])
416
 
        self.assertEqual(tests[1].workingtree_to_test_tree_to, formats[1][4])
417
 
        self.assertEqual(tests[1].transport_server, server1)
418
 
        self.assertEqual(tests[1].transport_readonly_server, server2)
419
 
 
420
325
class TestTestCaseWithTransport(TestCaseWithTransport):
421
326
    """Tests for the convenience functions TestCaseWithTransport introduces."""
422
327
 
489
394
    def clear(self):
490
395
        self.calls.append(('clear',))
491
396
 
492
 
    def note(self, msg, *args):
493
 
        self.calls.append(('note', msg, args))
494
 
 
495
397
 
496
398
class TestTestResult(TestCase):
497
399
 
502
404
        mypb = MockProgress()
503
405
        mypb.update('Running tests', 0, 4)
504
406
        last_calls = mypb.calls[:]
505
 
 
506
407
        result = bzrlib.tests._MyResult(self._log_file,
507
408
                                        descriptions=0,
508
409
                                        verbosity=1,
509
410
                                        pb=mypb)
510
411
        self.assertEqual(last_calls, mypb.calls)
511
412
 
512
 
        def shorten(s):
513
 
            """Shorten a string based on the terminal width"""
514
 
            return result._ellipsise_unimportant_words(s,
515
 
                                 osutils.terminal_width())
516
 
 
517
413
        # an error 
518
414
        result.startTest(dummy_test)
519
415
        # starting a test prints the test name
520
 
        last_calls += [('update', '...tyle_quiet', 0, None)]
521
 
        self.assertEqual(last_calls, mypb.calls)
 
416
        self.assertEqual(last_calls + [('update', '...tyle_quiet', 0, None)], mypb.calls)
 
417
        last_calls = mypb.calls[:]
522
418
        result.addError(dummy_test, dummy_error)
523
 
        last_calls += [('update', 'ERROR        ', 1, None),
524
 
                       ('note', shorten(dummy_test.id() + ': ERROR'), ())
525
 
                      ]
526
 
        self.assertEqual(last_calls, mypb.calls)
 
419
        self.assertEqual(last_calls + [('update', 'ERROR        ', 1, None)], mypb.calls)
 
420
        last_calls = mypb.calls[:]
527
421
 
528
422
        # a failure
529
423
        result.startTest(dummy_test)
530
 
        last_calls += [('update', '...tyle_quiet', 1, None)]
531
 
        self.assertEqual(last_calls, mypb.calls)
532
 
        last_calls += [('update', 'FAIL         ', 2, None),
533
 
                       ('note', shorten(dummy_test.id() + ': FAIL'), ())
534
 
                      ]
 
424
        self.assertEqual(last_calls + [('update', '...tyle_quiet', 1, None)], mypb.calls)
 
425
        last_calls = mypb.calls[:]
535
426
        result.addFailure(dummy_test, dummy_error)
536
 
        self.assertEqual(last_calls, mypb.calls)
 
427
        self.assertEqual(last_calls + [('update', 'FAIL         ', 2, None)], mypb.calls)
 
428
        last_calls = mypb.calls[:]
537
429
 
538
430
        # a success
539
431
        result.startTest(dummy_test)
540
 
        last_calls += [('update', '...tyle_quiet', 2, None)]
541
 
        self.assertEqual(last_calls, mypb.calls)
 
432
        self.assertEqual(last_calls + [('update', '...tyle_quiet', 2, None)], mypb.calls)
 
433
        last_calls = mypb.calls[:]
542
434
        result.addSuccess(dummy_test)
543
 
        last_calls += [('update', 'OK           ', 3, None)]
544
 
        self.assertEqual(last_calls, mypb.calls)
 
435
        self.assertEqual(last_calls + [('update', 'OK           ', 3, None)], mypb.calls)
 
436
        last_calls = mypb.calls[:]
545
437
 
546
438
        # a skip
547
439
        result.startTest(dummy_test)
548
 
        last_calls += [('update', '...tyle_quiet', 3, None)]
549
 
        self.assertEqual(last_calls, mypb.calls)
 
440
        self.assertEqual(last_calls + [('update', '...tyle_quiet', 3, None)], mypb.calls)
 
441
        last_calls = mypb.calls[:]
550
442
        result.addSkipped(dummy_test, dummy_error)
551
 
        last_calls += [('update', 'SKIP         ', 4, None)]
552
 
        self.assertEqual(last_calls, mypb.calls)
 
443
        self.assertEqual(last_calls + [('update', 'SKIP         ', 4, None)], mypb.calls)
 
444
        last_calls = mypb.calls[:]
553
445
 
554
446
    def test_elapsed_time_with_benchmarking(self):
555
447
        result = bzrlib.tests._MyResult(self._log_file,
566
458
        self.time(time.sleep, 0.001)
567
459
        result.extractBenchmarkTime(self)
568
460
        timed_string = result._testTimeString()
569
 
        self.assertContainsRe(timed_string, "^   [ 1-9][0-9]ms/   [ 1-9][0-9]ms$")
 
461
        self.assertContainsRe(timed_string, "^    [0-9]ms/   [ 1-9][0-9]ms$")
570
462
        # extracting the time from a non-bzrlib testcase sets to None
571
463
        result._recordTestStartTime()
572
464
        result.extractBenchmarkTime(
573
465
            unittest.FunctionTestCase(self.test_elapsed_time_with_benchmarking))
574
466
        timed_string = result._testTimeString()
575
 
        self.assertContainsRe(timed_string, "^         [ 1-9][0-9]ms$")
 
467
        self.assertContainsRe(timed_string, "^          [0-9]ms$")
576
468
        # cheat. Yes, wash thy mouth out with soap.
577
469
        self._benchtime = None
578
470
 
614
506
        #           1        0            ???         ???       ???(sleep) 
615
507
        # and then repeated but with 'world', rather than 'hello'.
616
508
        # this should appear in the output stream of our test result.
617
 
        output = result_stream.getvalue()
618
 
        self.assertContainsRe(output,
619
 
            r"LSProf output for <type 'unicode'>\(\('hello',\), {'errors': 'replace'}\)")
620
 
        self.assertContainsRe(output,
621
 
            r" *CallCount *Recursive *Total\(ms\) *Inline\(ms\) *module:lineno\(function\)\n")
622
 
        self.assertContainsRe(output,
623
 
            r"( +1 +0 +0\.\d+ +0\.\d+ +<method 'disable' of '_lsprof\.Profiler' objects>\n)?")
624
 
        self.assertContainsRe(output,
625
 
            r"LSProf output for <type 'unicode'>\(\('world',\), {'errors': 'replace'}\)\n")
 
509
        self.assertContainsRe(result_stream.getvalue(), 
 
510
            r"LSProf output for <type 'unicode'>\(\('hello',\), {'errors': 'replace'}\)\n"
 
511
            r" *CallCount *Recursive *Total\(ms\) *Inline\(ms\) *module:lineno\(function\)\n"
 
512
            r" +1 +0 +0\.\d+ +0\.\d+ +<method 'disable' of '_lsprof\.Profiler' objects>\n"
 
513
            r"LSProf output for <type 'unicode'>\(\('world',\), {'errors': 'replace'}\)\n"
 
514
            r" *CallCount *Recursive *Total\(ms\) *Inline\(ms\) *module:lineno\(function\)\n"
 
515
            r" +1 +0 +0\.\d+ +0\.\d+ +<method 'disable' of '_lsprof\.Profiler' objects>\n"
 
516
            )
626
517
 
627
518
 
628
519
class TestRunner(TestCase):