/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_selftest.py

  • Committer: Robert Collins
  • Date: 2010-05-06 11:08:10 UTC
  • mto: This revision was merged to the branch mainline in revision 5223.
  • Revision ID: robertc@robertcollins.net-20100506110810-h3j07fh5gmw54s25
Cleaner matcher matching revised unlocking protocol.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005-2011 Canonical Ltd
 
1
# Copyright (C) 2005-2010 Canonical Ltd
2
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
17
17
"""Tests for the test framework."""
18
18
 
19
19
from cStringIO import StringIO
20
 
import doctest
 
20
from doctest import ELLIPSIS
21
21
import os
22
22
import signal
23
23
import sys
24
 
import threading
25
24
import time
26
25
import unittest
27
26
import warnings
28
27
 
29
 
from testtools import (
30
 
    ExtendedToOriginalDecorator,
31
 
    MultiTestResult,
32
 
    )
33
 
from testtools.content import Content
 
28
from testtools import MultiTestResult
34
29
from testtools.content_type import ContentType
35
30
from testtools.matchers import (
36
31
    DocTestMatches,
37
32
    Equals,
38
33
    )
39
 
import testtools.testresult.doubles
 
34
import testtools.tests.helpers
40
35
 
41
36
import bzrlib
42
37
from bzrlib import (
43
38
    branchbuilder,
44
39
    bzrdir,
 
40
    debug,
45
41
    errors,
46
 
    hooks,
47
42
    lockdir,
48
43
    memorytree,
49
44
    osutils,
 
45
    progress,
50
46
    remote,
51
47
    repository,
52
48
    symbol_versioning,
53
49
    tests,
54
50
    transport,
55
51
    workingtree,
56
 
    workingtree_3,
57
 
    workingtree_4,
58
52
    )
59
53
from bzrlib.repofmt import (
60
54
    groupcompress_repo,
 
55
    pack_repo,
 
56
    weaverepo,
61
57
    )
62
58
from bzrlib.symbol_versioning import (
63
59
    deprecated_function,
68
64
    features,
69
65
    test_lsprof,
70
66
    test_server,
 
67
    test_sftp_transport,
71
68
    TestUtil,
72
69
    )
73
 
from bzrlib.trace import note, mutter
 
70
from bzrlib.trace import note
74
71
from bzrlib.transport import memory
 
72
from bzrlib.version import _get_bzr_source_tree
75
73
 
76
74
 
77
75
def _test_ids(test_suite):
79
77
    return [t.id() for t in tests.iter_suite_tests(test_suite)]
80
78
 
81
79
 
 
80
class SelftestTests(tests.TestCase):
 
81
 
 
82
    def test_import_tests(self):
 
83
        mod = TestUtil._load_module_by_name('bzrlib.tests.test_selftest')
 
84
        self.assertEqual(mod.SelftestTests, SelftestTests)
 
85
 
 
86
    def test_import_test_failure(self):
 
87
        self.assertRaises(ImportError,
 
88
                          TestUtil._load_module_by_name,
 
89
                          'bzrlib.no-name-yet')
 
90
 
 
91
 
82
92
class MetaTestLog(tests.TestCase):
83
93
 
84
94
    def test_logging(self):
90
100
            "text", "plain", {"charset": "utf8"})))
91
101
        self.assertThat(u"".join(log.iter_text()), Equals(self.get_log()))
92
102
        self.assertThat(self.get_log(),
93
 
            DocTestMatches(u"...a test message\n", doctest.ELLIPSIS))
 
103
            DocTestMatches(u"...a test message\n", ELLIPSIS))
94
104
 
95
105
 
96
106
class TestUnicodeFilename(tests.TestCase):
109
119
 
110
120
        filename = u'hell\u00d8'
111
121
        self.build_tree_contents([(filename, 'contents of hello')])
112
 
        self.assertPathExists(filename)
113
 
 
114
 
 
115
 
class TestClassesAvailable(tests.TestCase):
116
 
    """As a convenience we expose Test* classes from bzrlib.tests"""
117
 
 
118
 
    def test_test_case(self):
119
 
        from bzrlib.tests import TestCase
120
 
 
121
 
    def test_test_loader(self):
122
 
        from bzrlib.tests import TestLoader
123
 
 
124
 
    def test_test_suite(self):
125
 
        from bzrlib.tests import TestSuite
 
122
        self.failUnlessExists(filename)
126
123
 
127
124
 
128
125
class TestTransportScenarios(tests.TestCase):
211
208
    def test_scenarios(self):
212
209
        # check that constructor parameters are passed through to the adapted
213
210
        # test.
214
 
        from bzrlib.tests.per_controldir import make_scenarios
 
211
        from bzrlib.tests.per_bzrdir import make_scenarios
215
212
        vfs_factory = "v"
216
213
        server1 = "a"
217
214
        server2 = "b"
315
312
        from bzrlib.tests.per_interrepository import make_scenarios
316
313
        server1 = "a"
317
314
        server2 = "b"
318
 
        formats = [("C0", "C1", "C2", "C3"), ("D0", "D1", "D2", "D3")]
 
315
        formats = [("C0", "C1", "C2"), ("D0", "D1", "D2")]
319
316
        scenarios = make_scenarios(server1, server2, formats)
320
317
        self.assertEqual([
321
318
            ('C0,str,str',
322
319
             {'repository_format': 'C1',
323
320
              'repository_format_to': 'C2',
324
321
              'transport_readonly_server': 'b',
325
 
              'transport_server': 'a',
326
 
              'extra_setup': 'C3'}),
 
322
              'transport_server': 'a'}),
327
323
            ('D0,str,str',
328
324
             {'repository_format': 'D1',
329
325
              'repository_format_to': 'D2',
330
326
              'transport_readonly_server': 'b',
331
 
              'transport_server': 'a',
332
 
              'extra_setup': 'D3'})],
 
327
              'transport_server': 'a'})],
333
328
            scenarios)
334
329
 
335
330
 
341
336
        from bzrlib.tests.per_workingtree import make_scenarios
342
337
        server1 = "a"
343
338
        server2 = "b"
344
 
        formats = [workingtree_4.WorkingTreeFormat4(),
345
 
                   workingtree_3.WorkingTreeFormat3(),]
 
339
        formats = [workingtree.WorkingTreeFormat2(),
 
340
                   workingtree.WorkingTreeFormat3(),]
346
341
        scenarios = make_scenarios(server1, server2, formats)
347
342
        self.assertEqual([
348
 
            ('WorkingTreeFormat4',
 
343
            ('WorkingTreeFormat2',
349
344
             {'bzrdir_format': formats[0]._matchingbzrdir,
350
345
              'transport_readonly_server': 'b',
351
346
              'transport_server': 'a',
378
373
            )
379
374
        server1 = "a"
380
375
        server2 = "b"
381
 
        formats = [workingtree_4.WorkingTreeFormat4(),
382
 
                   workingtree_3.WorkingTreeFormat3(),]
 
376
        formats = [workingtree.WorkingTreeFormat2(),
 
377
                   workingtree.WorkingTreeFormat3(),]
383
378
        scenarios = make_scenarios(server1, server2, formats)
384
379
        self.assertEqual(7, len(scenarios))
385
 
        default_wt_format = workingtree.format_registry.get_default()
386
 
        wt4_format = workingtree_4.WorkingTreeFormat4()
387
 
        wt5_format = workingtree_4.WorkingTreeFormat5()
 
380
        default_wt_format = workingtree.WorkingTreeFormat4._default_format
 
381
        wt4_format = workingtree.WorkingTreeFormat4()
 
382
        wt5_format = workingtree.WorkingTreeFormat5()
388
383
        expected_scenarios = [
389
 
            ('WorkingTreeFormat4',
 
384
            ('WorkingTreeFormat2',
390
385
             {'bzrdir_format': formats[0]._matchingbzrdir,
391
386
              'transport_readonly_server': 'b',
392
387
              'transport_server': 'a',
452
447
        # ones to add.
453
448
        from bzrlib.tests.per_tree import (
454
449
            return_parameter,
 
450
            revision_tree_from_workingtree
455
451
            )
456
452
        from bzrlib.tests.per_intertree import (
457
453
            make_scenarios,
458
454
            )
459
 
        from bzrlib.workingtree_3 import WorkingTreeFormat3
460
 
        from bzrlib.workingtree_4 import WorkingTreeFormat4
 
455
        from bzrlib.workingtree import WorkingTreeFormat2, WorkingTreeFormat3
461
456
        input_test = TestInterTreeScenarios(
462
457
            "test_scenarios")
463
458
        server1 = "a"
464
459
        server2 = "b"
465
 
        format1 = WorkingTreeFormat4()
 
460
        format1 = WorkingTreeFormat2()
466
461
        format2 = WorkingTreeFormat3()
467
462
        formats = [("1", str, format1, format2, "converter1"),
468
463
            ("2", int, format2, format1, "converter2")]
514
509
        self.assertRaises(AssertionError, self.assertEqualStat,
515
510
            os.lstat("foo"), os.lstat("longname"))
516
511
 
517
 
    def test_failUnlessExists(self):
518
 
        """Deprecated failUnlessExists and failIfExists"""
519
 
        self.applyDeprecated(
520
 
            deprecated_in((2, 4)),
521
 
            self.failUnlessExists, '.')
522
 
        self.build_tree(['foo/', 'foo/bar'])
523
 
        self.applyDeprecated(
524
 
            deprecated_in((2, 4)),
525
 
            self.failUnlessExists, 'foo/bar')
526
 
        self.applyDeprecated(
527
 
            deprecated_in((2, 4)),
528
 
            self.failIfExists, 'foo/foo')
529
 
 
530
 
    def test_assertPathExists(self):
531
 
        self.assertPathExists('.')
532
 
        self.build_tree(['foo/', 'foo/bar'])
533
 
        self.assertPathExists('foo/bar')
534
 
        self.assertPathDoesNotExist('foo/foo')
535
 
 
536
512
 
537
513
class TestTestCaseWithMemoryTransport(tests.TestCaseWithMemoryTransport):
538
514
 
572
548
        tree = self.make_branch_and_memory_tree('dir')
573
549
        # Guard against regression into MemoryTransport leaking
574
550
        # files to disk instead of keeping them in memory.
575
 
        self.assertFalse(osutils.lexists('dir'))
 
551
        self.failIf(osutils.lexists('dir'))
576
552
        self.assertIsInstance(tree, memorytree.MemoryTree)
577
553
 
578
554
    def test_make_branch_and_memory_tree_with_format(self):
579
555
        """make_branch_and_memory_tree should accept a format option."""
580
556
        format = bzrdir.BzrDirMetaFormat1()
581
 
        format.repository_format = repository.format_registry.get_default()
 
557
        format.repository_format = weaverepo.RepositoryFormat7()
582
558
        tree = self.make_branch_and_memory_tree('dir', format=format)
583
559
        # Guard against regression into MemoryTransport leaking
584
560
        # files to disk instead of keeping them in memory.
585
 
        self.assertFalse(osutils.lexists('dir'))
 
561
        self.failIf(osutils.lexists('dir'))
586
562
        self.assertIsInstance(tree, memorytree.MemoryTree)
587
563
        self.assertEqual(format.repository_format.__class__,
588
564
            tree.branch.repository._format.__class__)
592
568
        self.assertIsInstance(builder, branchbuilder.BranchBuilder)
593
569
        # Guard against regression into MemoryTransport leaking
594
570
        # files to disk instead of keeping them in memory.
595
 
        self.assertFalse(osutils.lexists('dir'))
 
571
        self.failIf(osutils.lexists('dir'))
596
572
 
597
573
    def test_make_branch_builder_with_format(self):
598
574
        # Use a repo layout that doesn't conform to a 'named' layout, to ensure
599
575
        # that the format objects are used.
600
576
        format = bzrdir.BzrDirMetaFormat1()
601
 
        repo_format = repository.format_registry.get_default()
 
577
        repo_format = weaverepo.RepositoryFormat7()
602
578
        format.repository_format = repo_format
603
579
        builder = self.make_branch_builder('dir', format=format)
604
580
        the_branch = builder.get_branch()
605
581
        # Guard against regression into MemoryTransport leaking
606
582
        # files to disk instead of keeping them in memory.
607
 
        self.assertFalse(osutils.lexists('dir'))
 
583
        self.failIf(osutils.lexists('dir'))
608
584
        self.assertEqual(format.repository_format.__class__,
609
585
                         the_branch.repository._format.__class__)
610
586
        self.assertEqual(repo_format.get_format_string(),
616
592
        the_branch = builder.get_branch()
617
593
        # Guard against regression into MemoryTransport leaking
618
594
        # files to disk instead of keeping them in memory.
619
 
        self.assertFalse(osutils.lexists('dir'))
 
595
        self.failIf(osutils.lexists('dir'))
620
596
        dir_format = bzrdir.format_registry.make_bzrdir('knit')
621
597
        self.assertEqual(dir_format.repository_format.__class__,
622
598
                         the_branch.repository._format.__class__)
633
609
                l.attempt_lock()
634
610
        test = TestDanglingLock('test_function')
635
611
        result = test.run()
636
 
        total_failures = result.errors + result.failures
637
612
        if self._lock_check_thorough:
638
 
            self.assertEqual(1, len(total_failures))
 
613
            self.assertEqual(1, len(result.errors))
639
614
        else:
640
615
            # When _lock_check_thorough is disabled, then we don't trigger a
641
616
            # failure
642
 
            self.assertEqual(0, len(total_failures))
 
617
            self.assertEqual(0, len(result.errors))
643
618
 
644
619
 
645
620
class TestTestCaseWithTransport(tests.TestCaseWithTransport):
646
621
    """Tests for the convenience functions TestCaseWithTransport introduces."""
647
622
 
648
623
    def test_get_readonly_url_none(self):
 
624
        from bzrlib.transport import get_transport
649
625
        from bzrlib.transport.readonly import ReadonlyTransportDecorator
650
626
        self.vfs_transport_factory = memory.MemoryServer
651
627
        self.transport_readonly_server = None
653
629
        # for the server
654
630
        url = self.get_readonly_url()
655
631
        url2 = self.get_readonly_url('foo/bar')
656
 
        t = transport.get_transport(url)
657
 
        t2 = transport.get_transport(url2)
658
 
        self.assertIsInstance(t, ReadonlyTransportDecorator)
659
 
        self.assertIsInstance(t2, ReadonlyTransportDecorator)
 
632
        t = get_transport(url)
 
633
        t2 = get_transport(url2)
 
634
        self.failUnless(isinstance(t, ReadonlyTransportDecorator))
 
635
        self.failUnless(isinstance(t2, ReadonlyTransportDecorator))
660
636
        self.assertEqual(t2.base[:-1], t.abspath('foo/bar'))
661
637
 
662
638
    def test_get_readonly_url_http(self):
663
639
        from bzrlib.tests.http_server import HttpServer
 
640
        from bzrlib.transport import get_transport
664
641
        from bzrlib.transport.http import HttpTransportBase
665
642
        self.transport_server = test_server.LocalURLServer
666
643
        self.transport_readonly_server = HttpServer
668
645
        url = self.get_readonly_url()
669
646
        url2 = self.get_readonly_url('foo/bar')
670
647
        # the transport returned may be any HttpTransportBase subclass
671
 
        t = transport.get_transport(url)
672
 
        t2 = transport.get_transport(url2)
673
 
        self.assertIsInstance(t, HttpTransportBase)
674
 
        self.assertIsInstance(t2, HttpTransportBase)
 
648
        t = get_transport(url)
 
649
        t2 = get_transport(url2)
 
650
        self.failUnless(isinstance(t, HttpTransportBase))
 
651
        self.failUnless(isinstance(t2, HttpTransportBase))
675
652
        self.assertEqual(t2.base[:-1], t.abspath('foo/bar'))
676
653
 
677
654
    def test_is_directory(self):
685
662
    def test_make_branch_builder(self):
686
663
        builder = self.make_branch_builder('dir')
687
664
        rev_id = builder.build_commit()
688
 
        self.assertPathExists('dir')
 
665
        self.failUnlessExists('dir')
689
666
        a_dir = bzrdir.BzrDir.open('dir')
690
667
        self.assertRaises(errors.NoWorkingTree, a_dir.open_workingtree)
691
668
        a_branch = a_dir.open_branch()
707
684
        self.assertIsInstance(result_bzrdir.transport,
708
685
                              memory.MemoryTransport)
709
686
        # should not be on disk, should only be in memory
710
 
        self.assertPathDoesNotExist('subdir')
 
687
        self.failIfExists('subdir')
711
688
 
712
689
 
713
690
class TestChrootedTest(tests.ChrootedTestCase):
714
691
 
715
692
    def test_root_is_root(self):
716
 
        t = transport.get_transport(self.get_readonly_url())
 
693
        from bzrlib.transport import get_transport
 
694
        t = get_transport(self.get_readonly_url())
717
695
        url = t.base
718
696
        self.assertEqual(url, t.clone('..').base)
719
697
 
722
700
 
723
701
    def test_profiles_tests(self):
724
702
        self.requireFeature(test_lsprof.LSProfFeature)
725
 
        terminal = testtools.testresult.doubles.ExtendedTestResult()
 
703
        terminal = testtools.tests.helpers.ExtendedTestResult()
726
704
        result = tests.ProfileResult(terminal)
727
705
        class Sample(tests.TestCase):
728
706
            def a(self):
745
723
                descriptions=0,
746
724
                verbosity=1,
747
725
                )
748
 
        capture = testtools.testresult.doubles.ExtendedTestResult()
 
726
        capture = testtools.tests.helpers.ExtendedTestResult()
749
727
        test_case.run(MultiTestResult(result, capture))
750
728
        run_case = capture._events[0][1]
751
729
        timed_string = result._testTimeString(run_case)
772
750
        self.check_timing(ShortDelayTestCase('test_short_delay'),
773
751
                          r"^ +[0-9]+ms$")
774
752
 
 
753
    def _patch_get_bzr_source_tree(self):
 
754
        # Reading from the actual source tree breaks isolation, but we don't
 
755
        # want to assume that thats *all* that would happen.
 
756
        self.overrideAttr(bzrlib.version, '_get_bzr_source_tree', lambda: None)
 
757
 
 
758
    def test_assigned_benchmark_file_stores_date(self):
 
759
        self._patch_get_bzr_source_tree()
 
760
        output = StringIO()
 
761
        result = bzrlib.tests.TextTestResult(self._log_file,
 
762
                                        descriptions=0,
 
763
                                        verbosity=1,
 
764
                                        bench_history=output
 
765
                                        )
 
766
        output_string = output.getvalue()
 
767
        # if you are wondering about the regexp please read the comment in
 
768
        # test_bench_history (bzrlib.tests.test_selftest.TestRunner)
 
769
        # XXX: what comment?  -- Andrew Bennetts
 
770
        self.assertContainsRe(output_string, "--date [0-9.]+")
 
771
 
 
772
    def test_benchhistory_records_test_times(self):
 
773
        self._patch_get_bzr_source_tree()
 
774
        result_stream = StringIO()
 
775
        result = bzrlib.tests.TextTestResult(
 
776
            self._log_file,
 
777
            descriptions=0,
 
778
            verbosity=1,
 
779
            bench_history=result_stream
 
780
            )
 
781
 
 
782
        # we want profile a call and check that its test duration is recorded
 
783
        # make a new test instance that when run will generate a benchmark
 
784
        example_test_case = TestTestResult("_time_hello_world_encoding")
 
785
        # execute the test, which should succeed and record times
 
786
        example_test_case.run(result)
 
787
        lines = result_stream.getvalue().splitlines()
 
788
        self.assertEqual(2, len(lines))
 
789
        self.assertContainsRe(lines[1],
 
790
            " *[0-9]+ms bzrlib.tests.test_selftest.TestTestResult"
 
791
            "._time_hello_world_encoding")
 
792
 
775
793
    def _time_hello_world_encoding(self):
776
794
        """Profile two sleep calls
777
795
 
785
803
        self.requireFeature(test_lsprof.LSProfFeature)
786
804
        result_stream = StringIO()
787
805
        result = bzrlib.tests.VerboseTestResult(
788
 
            result_stream,
 
806
            unittest._WritelnDecorator(result_stream),
789
807
            descriptions=0,
790
808
            verbosity=2,
791
809
            )
817
835
        self.assertContainsRe(output,
818
836
            r"LSProf output for <type 'unicode'>\(\('world',\), {'errors': 'replace'}\)\n")
819
837
 
820
 
    def test_uses_time_from_testtools(self):
821
 
        """Test case timings in verbose results should use testtools times"""
822
 
        import datetime
823
 
        class TimeAddedVerboseTestResult(tests.VerboseTestResult):
824
 
            def startTest(self, test):
825
 
                self.time(datetime.datetime.utcfromtimestamp(1.145))
826
 
                super(TimeAddedVerboseTestResult, self).startTest(test)
827
 
            def addSuccess(self, test):
828
 
                self.time(datetime.datetime.utcfromtimestamp(51.147))
829
 
                super(TimeAddedVerboseTestResult, self).addSuccess(test)
830
 
            def report_tests_starting(self): pass
831
 
        sio = StringIO()
832
 
        self.get_passing_test().run(TimeAddedVerboseTestResult(sio, 0, 2))
833
 
        self.assertEndsWith(sio.getvalue(), "OK    50002ms\n")
834
 
 
835
838
    def test_known_failure(self):
836
839
        """A KnownFailure being raised should trigger several result actions."""
837
840
        class InstrumentedTestResult(tests.ExtendedTestResult):
838
841
            def stopTestRun(self): pass
839
 
            def report_tests_starting(self): pass
 
842
            def startTests(self): pass
 
843
            def report_test_start(self, test): pass
840
844
            def report_known_failure(self, test, err=None, details=None):
841
845
                self._call = test, 'known failure'
842
846
        result = InstrumentedTestResult(None, None, None, None)
860
864
        # verbose test output formatting
861
865
        result_stream = StringIO()
862
866
        result = bzrlib.tests.VerboseTestResult(
863
 
            result_stream,
 
867
            unittest._WritelnDecorator(result_stream),
864
868
            descriptions=0,
865
869
            verbosity=2,
866
870
            )
876
880
        output = result_stream.getvalue()[prefix:]
877
881
        lines = output.splitlines()
878
882
        self.assertContainsRe(lines[0], r'XFAIL *\d+ms$')
879
 
        if sys.version_info > (2, 7):
880
 
            self.expectFailure("_ExpectedFailure on 2.7 loses the message",
881
 
                self.assertNotEqual, lines[1], '    ')
882
883
        self.assertEqual(lines[1], '    foo')
883
884
        self.assertEqual(2, len(lines))
884
885
 
892
893
        """Test the behaviour of invoking addNotSupported."""
893
894
        class InstrumentedTestResult(tests.ExtendedTestResult):
894
895
            def stopTestRun(self): pass
895
 
            def report_tests_starting(self): pass
 
896
            def startTests(self): pass
 
897
            def report_test_start(self, test): pass
896
898
            def report_unsupported(self, test, feature):
897
899
                self._call = test, feature
898
900
        result = InstrumentedTestResult(None, None, None, None)
917
919
        # verbose test output formatting
918
920
        result_stream = StringIO()
919
921
        result = bzrlib.tests.VerboseTestResult(
920
 
            result_stream,
 
922
            unittest._WritelnDecorator(result_stream),
921
923
            descriptions=0,
922
924
            verbosity=2,
923
925
            )
937
939
        """An UnavailableFeature being raised should invoke addNotSupported."""
938
940
        class InstrumentedTestResult(tests.ExtendedTestResult):
939
941
            def stopTestRun(self): pass
940
 
            def report_tests_starting(self): pass
 
942
            def startTests(self): pass
 
943
            def report_test_start(self, test): pass
941
944
            def addNotSupported(self, test, feature):
942
945
                self._call = test, feature
943
946
        result = InstrumentedTestResult(None, None, None, None)
985
988
        class InstrumentedTestResult(tests.ExtendedTestResult):
986
989
            calls = 0
987
990
            def startTests(self): self.calls += 1
 
991
            def report_test_start(self, test): pass
988
992
        result = InstrumentedTestResult(None, None, None, None)
989
993
        def test_function():
990
994
            pass
992
996
        test.run(result)
993
997
        self.assertEquals(1, result.calls)
994
998
 
995
 
    def test_startTests_only_once(self):
996
 
        """With multiple tests startTests should still only be called once"""
997
 
        class InstrumentedTestResult(tests.ExtendedTestResult):
998
 
            calls = 0
999
 
            def startTests(self): self.calls += 1
1000
 
        result = InstrumentedTestResult(None, None, None, None)
1001
 
        suite = unittest.TestSuite([
1002
 
            unittest.FunctionTestCase(lambda: None),
1003
 
            unittest.FunctionTestCase(lambda: None)])
1004
 
        suite.run(result)
1005
 
        self.assertEquals(1, result.calls)
1006
 
        self.assertEquals(2, result.count)
1007
 
 
1008
999
 
1009
1000
class TestUnicodeFilenameFeature(tests.TestCase):
1010
1001
 
1031
1022
        because of our use of global state.
1032
1023
        """
1033
1024
        old_root = tests.TestCaseInTempDir.TEST_ROOT
 
1025
        old_leak = tests.TestCase._first_thread_leaker_id
1034
1026
        try:
1035
1027
            tests.TestCaseInTempDir.TEST_ROOT = None
 
1028
            tests.TestCase._first_thread_leaker_id = None
1036
1029
            return testrunner.run(test)
1037
1030
        finally:
1038
1031
            tests.TestCaseInTempDir.TEST_ROOT = old_root
 
1032
            tests.TestCase._first_thread_leaker_id = old_leak
1039
1033
 
1040
1034
    def test_known_failure_failed_run(self):
1041
1035
        # run a test that generates a known failure which should be printed in
1046
1040
        test = unittest.TestSuite()
1047
1041
        test.addTest(Test("known_failure_test"))
1048
1042
        def failing_test():
1049
 
            raise AssertionError('foo')
 
1043
            self.fail('foo')
1050
1044
        test.addTest(unittest.FunctionTestCase(failing_test))
1051
1045
        stream = StringIO()
1052
1046
        runner = tests.TextTestRunner(stream=stream)
1060
1054
            '^----------------------------------------------------------------------\n'
1061
1055
            'Traceback \\(most recent call last\\):\n'
1062
1056
            '  .*' # File .*, line .*, in failing_test' - but maybe not from .pyc
1063
 
            '    raise AssertionError\\(\'foo\'\\)\n'
 
1057
            '    self.fail\\(\'foo\'\\)\n'
1064
1058
            '.*'
1065
1059
            '^----------------------------------------------------------------------\n'
1066
1060
            '.*'
1072
1066
        # the final output.
1073
1067
        class Test(tests.TestCase):
1074
1068
            def known_failure_test(self):
1075
 
                self.knownFailure("Never works...")
 
1069
                self.expectFailure('failed', self.assertTrue, False)
1076
1070
        test = Test("known_failure_test")
1077
1071
        stream = StringIO()
1078
1072
        runner = tests.TextTestRunner(stream=stream)
1084
1078
            '\n'
1085
1079
            'OK \\(known_failures=1\\)\n')
1086
1080
 
1087
 
    def test_unexpected_success_bad(self):
1088
 
        class Test(tests.TestCase):
1089
 
            def test_truth(self):
1090
 
                self.expectFailure("No absolute truth", self.assertTrue, True)
1091
 
        runner = tests.TextTestRunner(stream=StringIO())
1092
 
        result = self.run_test_runner(runner, Test("test_truth"))
1093
 
        self.assertContainsRe(runner.stream.getvalue(),
1094
 
            "=+\n"
1095
 
            "FAIL: \\S+\.test_truth\n"
1096
 
            "-+\n"
1097
 
            "(?:.*\n)*"
1098
 
            "No absolute truth\n"
1099
 
            "(?:.*\n)*"
1100
 
            "-+\n"
1101
 
            "Ran 1 test in .*\n"
1102
 
            "\n"
1103
 
            "FAILED \\(failures=1\\)\n\\Z")
1104
 
 
1105
1081
    def test_result_decorator(self):
1106
1082
        # decorate results
1107
1083
        calls = []
1108
 
        class LoggingDecorator(ExtendedToOriginalDecorator):
 
1084
        class LoggingDecorator(tests.ForwardingResult):
1109
1085
            def startTest(self, test):
1110
 
                ExtendedToOriginalDecorator.startTest(self, test)
 
1086
                tests.ForwardingResult.startTest(self, test)
1111
1087
                calls.append('start')
1112
1088
        test = unittest.FunctionTestCase(lambda:None)
1113
1089
        stream = StringIO()
1214
1190
            ],
1215
1191
            lines[-3:])
1216
1192
 
1217
 
    def test_verbose_test_count(self):
1218
 
        """A verbose test run reports the right test count at the start"""
1219
 
        suite = TestUtil.TestSuite([
1220
 
            unittest.FunctionTestCase(lambda:None),
1221
 
            unittest.FunctionTestCase(lambda:None)])
1222
 
        self.assertEqual(suite.countTestCases(), 2)
1223
 
        stream = StringIO()
1224
 
        runner = tests.TextTestRunner(stream=stream, verbosity=2)
1225
 
        # Need to use the CountingDecorator as that's what sets num_tests
1226
 
        result = self.run_test_runner(runner, tests.CountingDecorator(suite))
1227
 
        self.assertStartsWith(stream.getvalue(), "running 2 tests")
 
1193
    def _patch_get_bzr_source_tree(self):
 
1194
        # Reading from the actual source tree breaks isolation, but we don't
 
1195
        # want to assume that thats *all* that would happen.
 
1196
        self._get_source_tree_calls = []
 
1197
        def new_get():
 
1198
            self._get_source_tree_calls.append("called")
 
1199
            return None
 
1200
        self.overrideAttr(bzrlib.version, '_get_bzr_source_tree',  new_get)
 
1201
 
 
1202
    def test_bench_history(self):
 
1203
        # tests that the running the benchmark passes bench_history into
 
1204
        # the test result object. We can tell that happens if
 
1205
        # _get_bzr_source_tree is called.
 
1206
        self._patch_get_bzr_source_tree()
 
1207
        test = TestRunner('dummy_test')
 
1208
        output = StringIO()
 
1209
        runner = tests.TextTestRunner(stream=self._log_file,
 
1210
                                      bench_history=output)
 
1211
        result = self.run_test_runner(runner, test)
 
1212
        output_string = output.getvalue()
 
1213
        self.assertContainsRe(output_string, "--date [0-9.]+")
 
1214
        self.assertLength(1, self._get_source_tree_calls)
1228
1215
 
1229
1216
    def test_startTestRun(self):
1230
1217
        """run should call result.startTestRun()"""
1231
1218
        calls = []
1232
 
        class LoggingDecorator(ExtendedToOriginalDecorator):
 
1219
        class LoggingDecorator(tests.ForwardingResult):
1233
1220
            def startTestRun(self):
1234
 
                ExtendedToOriginalDecorator.startTestRun(self)
 
1221
                tests.ForwardingResult.startTestRun(self)
1235
1222
                calls.append('startTestRun')
1236
1223
        test = unittest.FunctionTestCase(lambda:None)
1237
1224
        stream = StringIO()
1243
1230
    def test_stopTestRun(self):
1244
1231
        """run should call result.stopTestRun()"""
1245
1232
        calls = []
1246
 
        class LoggingDecorator(ExtendedToOriginalDecorator):
 
1233
        class LoggingDecorator(tests.ForwardingResult):
1247
1234
            def stopTestRun(self):
1248
 
                ExtendedToOriginalDecorator.stopTestRun(self)
 
1235
                tests.ForwardingResult.stopTestRun(self)
1249
1236
                calls.append('stopTestRun')
1250
1237
        test = unittest.FunctionTestCase(lambda:None)
1251
1238
        stream = StringIO()
1254
1241
        result = self.run_test_runner(runner, test)
1255
1242
        self.assertLength(1, calls)
1256
1243
 
1257
 
    def test_unicode_test_output_on_ascii_stream(self):
1258
 
        """Showing results should always succeed even on an ascii console"""
1259
 
        class FailureWithUnicode(tests.TestCase):
1260
 
            def test_log_unicode(self):
1261
 
                self.log(u"\u2606")
1262
 
                self.fail("Now print that log!")
1263
 
        out = StringIO()
1264
 
        self.overrideAttr(osutils, "get_terminal_encoding",
1265
 
            lambda trace=False: "ascii")
1266
 
        result = self.run_test_runner(tests.TextTestRunner(stream=out),
1267
 
            FailureWithUnicode("test_log_unicode"))
1268
 
        self.assertContainsRe(out.getvalue(),
1269
 
            "Text attachment: log\n"
1270
 
            "-+\n"
1271
 
            "\d+\.\d+  \\\\u2606\n"
1272
 
            "-+\n")
1273
 
 
1274
1244
 
1275
1245
class SampleTestCase(tests.TestCase):
1276
1246
 
1451
1421
        sample_test = TestTestCase("method_that_times_a_bit_twice")
1452
1422
        output_stream = StringIO()
1453
1423
        result = bzrlib.tests.VerboseTestResult(
1454
 
            output_stream,
 
1424
            unittest._WritelnDecorator(output_stream),
1455
1425
            descriptions=0,
1456
1426
            verbosity=2)
1457
1427
        sample_test.run(result)
1464
1434
        # Note this test won't fail with hooks that the core library doesn't
1465
1435
        # use - but it trigger with a plugin that adds hooks, so its still a
1466
1436
        # useful warning in that case.
1467
 
        self.assertEqual(bzrlib.branch.BranchHooks(), bzrlib.branch.Branch.hooks)
1468
 
        self.assertEqual(
1469
 
            bzrlib.smart.server.SmartServerHooks(),
 
1437
        self.assertEqual(bzrlib.branch.BranchHooks(),
 
1438
            bzrlib.branch.Branch.hooks)
 
1439
        self.assertEqual(bzrlib.smart.server.SmartServerHooks(),
1470
1440
            bzrlib.smart.server.SmartTCPServer.hooks)
1471
 
        self.assertEqual(
1472
 
            bzrlib.commands.CommandHooks(), bzrlib.commands.Command.hooks)
 
1441
        self.assertEqual(bzrlib.commands.CommandHooks(),
 
1442
            bzrlib.commands.Command.hooks)
1473
1443
 
1474
1444
    def test__gather_lsprof_in_benchmarks(self):
1475
1445
        """When _gather_lsprof_in_benchmarks is on, accumulate profile data.
1684
1654
        test.run(unittest.TestResult())
1685
1655
        self.assertEqual('original', obj.test_attr)
1686
1656
 
1687
 
    def test_recordCalls(self):
1688
 
        from bzrlib.tests import test_selftest
1689
 
        calls = self.recordCalls(
1690
 
            test_selftest, '_add_numbers')
1691
 
        self.assertEqual(test_selftest._add_numbers(2, 10),
1692
 
            12)
1693
 
        self.assertEquals(calls, [((2, 10), {})])
1694
 
 
1695
 
 
1696
 
def _add_numbers(a, b):
1697
 
    return a + b
1698
 
 
1699
 
 
1700
 
class _MissingFeature(tests.Feature):
1701
 
    def _probe(self):
1702
 
        return False
1703
 
missing_feature = _MissingFeature()
1704
 
 
1705
 
 
1706
 
def _get_test(name):
1707
 
    """Get an instance of a specific example test.
1708
 
 
1709
 
    We protect this in a function so that they don't auto-run in the test
1710
 
    suite.
1711
 
    """
1712
 
 
1713
 
    class ExampleTests(tests.TestCase):
1714
 
 
1715
 
        def test_fail(self):
1716
 
            mutter('this was a failing test')
1717
 
            self.fail('this test will fail')
1718
 
 
1719
 
        def test_error(self):
1720
 
            mutter('this test errored')
1721
 
            raise RuntimeError('gotcha')
1722
 
 
1723
 
        def test_missing_feature(self):
1724
 
            mutter('missing the feature')
1725
 
            self.requireFeature(missing_feature)
1726
 
 
1727
 
        def test_skip(self):
1728
 
            mutter('this test will be skipped')
1729
 
            raise tests.TestSkipped('reason')
1730
 
 
1731
 
        def test_success(self):
1732
 
            mutter('this test succeeds')
1733
 
 
1734
 
        def test_xfail(self):
1735
 
            mutter('test with expected failure')
1736
 
            self.knownFailure('this_fails')
1737
 
 
1738
 
        def test_unexpected_success(self):
1739
 
            mutter('test with unexpected success')
1740
 
            self.expectFailure('should_fail', lambda: None)
1741
 
 
1742
 
    return ExampleTests(name)
1743
 
 
1744
 
 
1745
 
class TestTestCaseLogDetails(tests.TestCase):
1746
 
 
1747
 
    def _run_test(self, test_name):
1748
 
        test = _get_test(test_name)
1749
 
        result = testtools.TestResult()
1750
 
        test.run(result)
1751
 
        return result
1752
 
 
1753
 
    def test_fail_has_log(self):
1754
 
        result = self._run_test('test_fail')
1755
 
        self.assertEqual(1, len(result.failures))
1756
 
        result_content = result.failures[0][1]
1757
 
        self.assertContainsRe(result_content, 'Text attachment: log')
1758
 
        self.assertContainsRe(result_content, 'this was a failing test')
1759
 
 
1760
 
    def test_error_has_log(self):
1761
 
        result = self._run_test('test_error')
1762
 
        self.assertEqual(1, len(result.errors))
1763
 
        result_content = result.errors[0][1]
1764
 
        self.assertContainsRe(result_content, 'Text attachment: log')
1765
 
        self.assertContainsRe(result_content, 'this test errored')
1766
 
 
1767
 
    def test_skip_has_no_log(self):
1768
 
        result = self._run_test('test_skip')
1769
 
        self.assertEqual(['reason'], result.skip_reasons.keys())
1770
 
        skips = result.skip_reasons['reason']
1771
 
        self.assertEqual(1, len(skips))
1772
 
        test = skips[0]
1773
 
        self.assertFalse('log' in test.getDetails())
1774
 
 
1775
 
    def test_missing_feature_has_no_log(self):
1776
 
        # testtools doesn't know about addNotSupported, so it just gets
1777
 
        # considered as a skip
1778
 
        result = self._run_test('test_missing_feature')
1779
 
        self.assertEqual([missing_feature], result.skip_reasons.keys())
1780
 
        skips = result.skip_reasons[missing_feature]
1781
 
        self.assertEqual(1, len(skips))
1782
 
        test = skips[0]
1783
 
        self.assertFalse('log' in test.getDetails())
1784
 
 
1785
 
    def test_xfail_has_no_log(self):
1786
 
        result = self._run_test('test_xfail')
1787
 
        self.assertEqual(1, len(result.expectedFailures))
1788
 
        result_content = result.expectedFailures[0][1]
1789
 
        self.assertNotContainsRe(result_content, 'Text attachment: log')
1790
 
        self.assertNotContainsRe(result_content, 'test with expected failure')
1791
 
 
1792
 
    def test_unexpected_success_has_log(self):
1793
 
        result = self._run_test('test_unexpected_success')
1794
 
        self.assertEqual(1, len(result.unexpectedSuccesses))
1795
 
        # Inconsistency, unexpectedSuccesses is a list of tests,
1796
 
        # expectedFailures is a list of reasons?
1797
 
        test = result.unexpectedSuccesses[0]
1798
 
        details = test.getDetails()
1799
 
        self.assertTrue('log' in details)
1800
 
 
1801
 
 
1802
 
class TestTestCloning(tests.TestCase):
1803
 
    """Tests that test cloning of TestCases (as used by multiply_tests)."""
1804
 
 
1805
 
    def test_cloned_testcase_does_not_share_details(self):
1806
 
        """A TestCase cloned with clone_test does not share mutable attributes
1807
 
        such as details or cleanups.
1808
 
        """
1809
 
        class Test(tests.TestCase):
1810
 
            def test_foo(self):
1811
 
                self.addDetail('foo', Content('text/plain', lambda: 'foo'))
1812
 
        orig_test = Test('test_foo')
1813
 
        cloned_test = tests.clone_test(orig_test, orig_test.id() + '(cloned)')
1814
 
        orig_test.run(unittest.TestResult())
1815
 
        self.assertEqual('foo', orig_test.getDetails()['foo'].iter_bytes())
1816
 
        self.assertEqual(None, cloned_test.getDetails().get('foo'))
1817
 
 
1818
 
    def test_double_apply_scenario_preserves_first_scenario(self):
1819
 
        """Applying two levels of scenarios to a test preserves the attributes
1820
 
        added by both scenarios.
1821
 
        """
1822
 
        class Test(tests.TestCase):
1823
 
            def test_foo(self):
1824
 
                pass
1825
 
        test = Test('test_foo')
1826
 
        scenarios_x = [('x=1', {'x': 1}), ('x=2', {'x': 2})]
1827
 
        scenarios_y = [('y=1', {'y': 1}), ('y=2', {'y': 2})]
1828
 
        suite = tests.multiply_tests(test, scenarios_x, unittest.TestSuite())
1829
 
        suite = tests.multiply_tests(suite, scenarios_y, unittest.TestSuite())
1830
 
        all_tests = list(tests.iter_suite_tests(suite))
1831
 
        self.assertLength(4, all_tests)
1832
 
        all_xys = sorted((t.x, t.y) for t in all_tests)
1833
 
        self.assertEqual([(1, 1), (1, 2), (2, 1), (2, 2)], all_xys)
1834
 
 
1835
1657
 
1836
1658
# NB: Don't delete this; it's not actually from 0.11!
1837
1659
@deprecated_function(deprecated_in((0, 11, 0)))
1965
1787
    def test_make_branch_and_tree_with_format(self):
1966
1788
        # we should be able to supply a format to make_branch_and_tree
1967
1789
        self.make_branch_and_tree('a', format=bzrlib.bzrdir.BzrDirMetaFormat1())
 
1790
        self.make_branch_and_tree('b', format=bzrlib.bzrdir.BzrDirFormat6())
1968
1791
        self.assertIsInstance(bzrlib.bzrdir.BzrDir.open('a')._format,
1969
1792
                              bzrlib.bzrdir.BzrDirMetaFormat1)
 
1793
        self.assertIsInstance(bzrlib.bzrdir.BzrDir.open('b')._format,
 
1794
                              bzrlib.bzrdir.BzrDirFormat6)
1970
1795
 
1971
1796
    def test_make_branch_and_memory_tree(self):
1972
1797
        # we should be able to get a new branch and a mutable tree from
1989
1814
                tree.branch.repository.bzrdir.root_transport)
1990
1815
 
1991
1816
 
1992
 
class SelfTestHelper(object):
 
1817
class SelfTestHelper:
1993
1818
 
1994
1819
    def run_selftest(self, **kwargs):
1995
1820
        """Run selftest returning its output."""
2050
1875
 
2051
1876
    def test_lsprof_tests(self):
2052
1877
        self.requireFeature(test_lsprof.LSProfFeature)
2053
 
        results = []
 
1878
        calls = []
2054
1879
        class Test(object):
2055
1880
            def __call__(test, result):
2056
1881
                test.run(result)
2057
1882
            def run(test, result):
2058
 
                results.append(result)
 
1883
                self.assertIsInstance(result, tests.ForwardingResult)
 
1884
                calls.append("called")
2059
1885
            def countTestCases(self):
2060
1886
                return 1
2061
1887
        self.run_selftest(test_suite_factory=Test, lsprof_tests=True)
2062
 
        self.assertLength(1, results)
2063
 
        self.assertIsInstance(results.pop(), ExtendedToOriginalDecorator)
 
1888
        self.assertLength(1, calls)
2064
1889
 
2065
1890
    def test_random(self):
2066
1891
        # test randomising by listing a number of tests.
2146
1971
            load_list='missing file name', list_only=True)
2147
1972
 
2148
1973
 
2149
 
class TestSubunitLogDetails(tests.TestCase, SelfTestHelper):
2150
 
 
2151
 
    _test_needs_features = [features.subunit]
2152
 
 
2153
 
    def run_subunit_stream(self, test_name):
2154
 
        from subunit import ProtocolTestCase
2155
 
        def factory():
2156
 
            return TestUtil.TestSuite([_get_test(test_name)])
2157
 
        stream = self.run_selftest(runner_class=tests.SubUnitBzrRunner,
2158
 
            test_suite_factory=factory)
2159
 
        test = ProtocolTestCase(stream)
2160
 
        result = testtools.TestResult()
2161
 
        test.run(result)
2162
 
        content = stream.getvalue()
2163
 
        return content, result
2164
 
 
2165
 
    def test_fail_has_log(self):
2166
 
        content, result = self.run_subunit_stream('test_fail')
2167
 
        self.assertEqual(1, len(result.failures))
2168
 
        self.assertContainsRe(content, '(?m)^log$')
2169
 
        self.assertContainsRe(content, 'this test will fail')
2170
 
 
2171
 
    def test_error_has_log(self):
2172
 
        content, result = self.run_subunit_stream('test_error')
2173
 
        self.assertContainsRe(content, '(?m)^log$')
2174
 
        self.assertContainsRe(content, 'this test errored')
2175
 
 
2176
 
    def test_skip_has_no_log(self):
2177
 
        content, result = self.run_subunit_stream('test_skip')
2178
 
        self.assertNotContainsRe(content, '(?m)^log$')
2179
 
        self.assertNotContainsRe(content, 'this test will be skipped')
2180
 
        self.assertEqual(['reason'], result.skip_reasons.keys())
2181
 
        skips = result.skip_reasons['reason']
2182
 
        self.assertEqual(1, len(skips))
2183
 
        test = skips[0]
2184
 
        # RemotedTestCase doesn't preserve the "details"
2185
 
        ## self.assertFalse('log' in test.getDetails())
2186
 
 
2187
 
    def test_missing_feature_has_no_log(self):
2188
 
        content, result = self.run_subunit_stream('test_missing_feature')
2189
 
        self.assertNotContainsRe(content, '(?m)^log$')
2190
 
        self.assertNotContainsRe(content, 'missing the feature')
2191
 
        self.assertEqual(['_MissingFeature\n'], result.skip_reasons.keys())
2192
 
        skips = result.skip_reasons['_MissingFeature\n']
2193
 
        self.assertEqual(1, len(skips))
2194
 
        test = skips[0]
2195
 
        # RemotedTestCase doesn't preserve the "details"
2196
 
        ## self.assertFalse('log' in test.getDetails())
2197
 
 
2198
 
    def test_xfail_has_no_log(self):
2199
 
        content, result = self.run_subunit_stream('test_xfail')
2200
 
        self.assertNotContainsRe(content, '(?m)^log$')
2201
 
        self.assertNotContainsRe(content, 'test with expected failure')
2202
 
        self.assertEqual(1, len(result.expectedFailures))
2203
 
        result_content = result.expectedFailures[0][1]
2204
 
        self.assertNotContainsRe(result_content, 'Text attachment: log')
2205
 
        self.assertNotContainsRe(result_content, 'test with expected failure')
2206
 
 
2207
 
    def test_unexpected_success_has_log(self):
2208
 
        content, result = self.run_subunit_stream('test_unexpected_success')
2209
 
        self.assertContainsRe(content, '(?m)^log$')
2210
 
        self.assertContainsRe(content, 'test with unexpected success')
2211
 
        # GZ 2011-05-18: Old versions of subunit treat unexpected success as a
2212
 
        #                success, if a min version check is added remove this
2213
 
        from subunit import TestProtocolClient as _Client
2214
 
        if _Client.addUnexpectedSuccess.im_func is _Client.addSuccess.im_func:
2215
 
            self.expectFailure('subunit treats "unexpectedSuccess"'
2216
 
                               ' as a plain success',
2217
 
                self.assertEqual, 1, len(result.unexpectedSuccesses))
2218
 
        self.assertEqual(1, len(result.unexpectedSuccesses))
2219
 
        test = result.unexpectedSuccesses[0]
2220
 
        # RemotedTestCase doesn't preserve the "details"
2221
 
        ## self.assertTrue('log' in test.getDetails())
2222
 
 
2223
 
    def test_success_has_no_log(self):
2224
 
        content, result = self.run_subunit_stream('test_success')
2225
 
        self.assertEqual(1, result.testsRun)
2226
 
        self.assertNotContainsRe(content, '(?m)^log$')
2227
 
        self.assertNotContainsRe(content, 'this test succeeds')
2228
 
 
2229
 
 
2230
1974
class TestRunBzr(tests.TestCase):
2231
1975
 
2232
1976
    out = ''
2355
2099
        # stdout and stderr of the invoked run_bzr
2356
2100
        current_factory = bzrlib.ui.ui_factory
2357
2101
        self.run_bzr(['foo'])
2358
 
        self.assertFalse(current_factory is self.factory)
 
2102
        self.failIf(current_factory is self.factory)
2359
2103
        self.assertNotEqual(sys.stdout, self.factory.stdout)
2360
2104
        self.assertNotEqual(sys.stderr, self.factory.stderr)
2361
2105
        self.assertEqual('foo\n', self.factory.stdout.getvalue())
2518
2262
 
2519
2263
 
2520
2264
class TestStartBzrSubProcess(tests.TestCase):
2521
 
    """Stub test start_bzr_subprocess."""
2522
2265
 
2523
 
    def _subprocess_log_cleanup(self):
2524
 
        """Inhibits the base version as we don't produce a log file."""
 
2266
    def check_popen_state(self):
 
2267
        """Replace to make assertions when popen is called."""
2525
2268
 
2526
2269
    def _popen(self, *args, **kwargs):
2527
 
        """Override the base version to record the command that is run.
2528
 
 
2529
 
        From there we can ensure it is correct without spawning a real process.
2530
 
        """
 
2270
        """Record the command that is run, so that we can ensure it is correct"""
2531
2271
        self.check_popen_state()
2532
2272
        self._popen_args = args
2533
2273
        self._popen_kwargs = kwargs
2534
2274
        raise _DontSpawnProcess()
2535
2275
 
2536
 
    def check_popen_state(self):
2537
 
        """Replace to make assertions when popen is called."""
2538
 
 
2539
2276
    def test_run_bzr_subprocess_no_plugins(self):
2540
2277
        self.assertRaises(_DontSpawnProcess, self.start_bzr_subprocess, [])
2541
2278
        command = self._popen_args[0]
2545
2282
 
2546
2283
    def test_allow_plugins(self):
2547
2284
        self.assertRaises(_DontSpawnProcess, self.start_bzr_subprocess, [],
2548
 
                          allow_plugins=True)
 
2285
            allow_plugins=True)
2549
2286
        command = self._popen_args[0]
2550
2287
        self.assertEqual([], command[2:])
2551
2288
 
2552
2289
    def test_set_env(self):
2553
 
        self.assertFalse('EXISTANT_ENV_VAR' in os.environ)
 
2290
        self.failIf('EXISTANT_ENV_VAR' in os.environ)
2554
2291
        # set in the child
2555
2292
        def check_environment():
2556
2293
            self.assertEqual('set variable', os.environ['EXISTANT_ENV_VAR'])
2557
2294
        self.check_popen_state = check_environment
2558
2295
        self.assertRaises(_DontSpawnProcess, self.start_bzr_subprocess, [],
2559
 
                          env_changes={'EXISTANT_ENV_VAR':'set variable'})
 
2296
            env_changes={'EXISTANT_ENV_VAR':'set variable'})
2560
2297
        # not set in theparent
2561
2298
        self.assertFalse('EXISTANT_ENV_VAR' in os.environ)
2562
2299
 
2563
2300
    def test_run_bzr_subprocess_env_del(self):
2564
2301
        """run_bzr_subprocess can remove environment variables too."""
2565
 
        self.assertFalse('EXISTANT_ENV_VAR' in os.environ)
 
2302
        self.failIf('EXISTANT_ENV_VAR' in os.environ)
2566
2303
        def check_environment():
2567
2304
            self.assertFalse('EXISTANT_ENV_VAR' in os.environ)
2568
2305
        os.environ['EXISTANT_ENV_VAR'] = 'set variable'
2569
2306
        self.check_popen_state = check_environment
2570
2307
        self.assertRaises(_DontSpawnProcess, self.start_bzr_subprocess, [],
2571
 
                          env_changes={'EXISTANT_ENV_VAR':None})
 
2308
            env_changes={'EXISTANT_ENV_VAR':None})
2572
2309
        # Still set in parent
2573
2310
        self.assertEqual('set variable', os.environ['EXISTANT_ENV_VAR'])
2574
2311
        del os.environ['EXISTANT_ENV_VAR']
2575
2312
 
2576
2313
    def test_env_del_missing(self):
2577
 
        self.assertFalse('NON_EXISTANT_ENV_VAR' in os.environ)
 
2314
        self.failIf('NON_EXISTANT_ENV_VAR' in os.environ)
2578
2315
        def check_environment():
2579
2316
            self.assertFalse('NON_EXISTANT_ENV_VAR' in os.environ)
2580
2317
        self.check_popen_state = check_environment
2581
2318
        self.assertRaises(_DontSpawnProcess, self.start_bzr_subprocess, [],
2582
 
                          env_changes={'NON_EXISTANT_ENV_VAR':None})
 
2319
            env_changes={'NON_EXISTANT_ENV_VAR':None})
2583
2320
 
2584
2321
    def test_working_dir(self):
2585
2322
        """Test that we can specify the working dir for the child"""
2588
2325
        chdirs = []
2589
2326
        def chdir(path):
2590
2327
            chdirs.append(path)
2591
 
        self.overrideAttr(os, 'chdir', chdir)
2592
 
        def getcwd():
2593
 
            return 'current'
2594
 
        self.overrideAttr(osutils, 'getcwd', getcwd)
2595
 
        self.assertRaises(_DontSpawnProcess, self.start_bzr_subprocess, [],
2596
 
                          working_dir='foo')
 
2328
        os.chdir = chdir
 
2329
        try:
 
2330
            def getcwd():
 
2331
                return 'current'
 
2332
            osutils.getcwd = getcwd
 
2333
            try:
 
2334
                self.assertRaises(_DontSpawnProcess, self.start_bzr_subprocess, [],
 
2335
                    working_dir='foo')
 
2336
            finally:
 
2337
                osutils.getcwd = orig_getcwd
 
2338
        finally:
 
2339
            os.chdir = orig_chdir
2597
2340
        self.assertEqual(['foo', 'current'], chdirs)
2598
2341
 
2599
 
    def test_get_bzr_path_with_cwd_bzrlib(self):
2600
 
        self.get_source_path = lambda: ""
2601
 
        self.overrideAttr(os.path, "isfile", lambda path: True)
2602
 
        self.assertEqual(self.get_bzr_path(), "bzr")
2603
 
 
2604
2342
 
2605
2343
class TestActuallyStartBzrSubprocess(tests.TestCaseWithTransport):
2606
2344
    """Tests that really need to do things with an external bzr."""
2849
2587
        self.assertEqual(remaining_names, _test_ids(split_suite[1]))
2850
2588
 
2851
2589
 
2852
 
class TestCheckTreeShape(tests.TestCaseWithTransport):
 
2590
class TestCheckInventoryShape(tests.TestCaseWithTransport):
2853
2591
 
2854
 
    def test_check_tree_shape(self):
 
2592
    def test_check_inventory_shape(self):
2855
2593
        files = ['a', 'b/', 'b/c']
2856
2594
        tree = self.make_branch_and_tree('.')
2857
2595
        self.build_tree(files)
2858
2596
        tree.add(files)
2859
2597
        tree.lock_read()
2860
2598
        try:
2861
 
            self.check_tree_shape(tree, files)
 
2599
            self.check_inventory_shape(tree.inventory, files)
2862
2600
        finally:
2863
2601
            tree.unlock()
2864
2602
 
3196
2934
        tpr.register('bar', 'bBB.aAA.rRR')
3197
2935
        self.assertEquals('bbb.aaa.rrr', tpr.get('bar'))
3198
2936
        self.assertThat(self.get_log(),
3199
 
            DocTestMatches("...bar...bbb.aaa.rrr...BB.aAA.rRR",
3200
 
                           doctest.ELLIPSIS))
 
2937
            DocTestMatches("...bar...bbb.aaa.rrr...BB.aAA.rRR", ELLIPSIS))
3201
2938
 
3202
2939
    def test_get_unknown_prefix(self):
3203
2940
        tpr = self._get_registry()
3223
2960
        self.assertEquals('bzrlib.plugins', tpr.resolve_alias('bp'))
3224
2961
 
3225
2962
 
3226
 
class TestThreadLeakDetection(tests.TestCase):
3227
 
    """Ensure when tests leak threads we detect and report it"""
3228
 
 
3229
 
    class LeakRecordingResult(tests.ExtendedTestResult):
3230
 
        def __init__(self):
3231
 
            tests.ExtendedTestResult.__init__(self, StringIO(), 0, 1)
3232
 
            self.leaks = []
3233
 
        def _report_thread_leak(self, test, leaks, alive):
3234
 
            self.leaks.append((test, leaks))
3235
 
 
3236
 
    def test_testcase_without_addCleanups(self):
3237
 
        """Check old TestCase instances don't break with leak detection"""
3238
 
        class Test(unittest.TestCase):
3239
 
            def runTest(self):
3240
 
                pass
3241
 
        result = self.LeakRecordingResult()
3242
 
        test = Test()
3243
 
        result.startTestRun()
3244
 
        test.run(result)
3245
 
        result.stopTestRun()
3246
 
        self.assertEqual(result._tests_leaking_threads_count, 0)
3247
 
        self.assertEqual(result.leaks, [])
3248
 
        
3249
 
    def test_thread_leak(self):
3250
 
        """Ensure a thread that outlives the running of a test is reported
3251
 
 
3252
 
        Uses a thread that blocks on an event, and is started by the inner
3253
 
        test case. As the thread outlives the inner case's run, it should be
3254
 
        detected as a leak, but the event is then set so that the thread can
3255
 
        be safely joined in cleanup so it's not leaked for real.
3256
 
        """
3257
 
        event = threading.Event()
3258
 
        thread = threading.Thread(name="Leaker", target=event.wait)
3259
 
        class Test(tests.TestCase):
3260
 
            def test_leak(self):
3261
 
                thread.start()
3262
 
        result = self.LeakRecordingResult()
3263
 
        test = Test("test_leak")
3264
 
        self.addCleanup(thread.join)
3265
 
        self.addCleanup(event.set)
3266
 
        result.startTestRun()
3267
 
        test.run(result)
3268
 
        result.stopTestRun()
3269
 
        self.assertEqual(result._tests_leaking_threads_count, 1)
3270
 
        self.assertEqual(result._first_thread_leaker_id, test.id())
3271
 
        self.assertEqual(result.leaks, [(test, set([thread]))])
3272
 
        self.assertContainsString(result.stream.getvalue(), "leaking threads")
3273
 
 
3274
 
    def test_multiple_leaks(self):
3275
 
        """Check multiple leaks are blamed on the test cases at fault
3276
 
 
3277
 
        Same concept as the previous test, but has one inner test method that
3278
 
        leaks two threads, and one that doesn't leak at all.
3279
 
        """
3280
 
        event = threading.Event()
3281
 
        thread_a = threading.Thread(name="LeakerA", target=event.wait)
3282
 
        thread_b = threading.Thread(name="LeakerB", target=event.wait)
3283
 
        thread_c = threading.Thread(name="LeakerC", target=event.wait)
3284
 
        class Test(tests.TestCase):
3285
 
            def test_first_leak(self):
3286
 
                thread_b.start()
3287
 
            def test_second_no_leak(self):
3288
 
                pass
3289
 
            def test_third_leak(self):
3290
 
                thread_c.start()
3291
 
                thread_a.start()
3292
 
        result = self.LeakRecordingResult()
3293
 
        first_test = Test("test_first_leak")
3294
 
        third_test = Test("test_third_leak")
3295
 
        self.addCleanup(thread_a.join)
3296
 
        self.addCleanup(thread_b.join)
3297
 
        self.addCleanup(thread_c.join)
3298
 
        self.addCleanup(event.set)
3299
 
        result.startTestRun()
3300
 
        unittest.TestSuite(
3301
 
            [first_test, Test("test_second_no_leak"), third_test]
3302
 
            ).run(result)
3303
 
        result.stopTestRun()
3304
 
        self.assertEqual(result._tests_leaking_threads_count, 2)
3305
 
        self.assertEqual(result._first_thread_leaker_id, first_test.id())
3306
 
        self.assertEqual(result.leaks, [
3307
 
            (first_test, set([thread_b])),
3308
 
            (third_test, set([thread_a, thread_c]))])
3309
 
        self.assertContainsString(result.stream.getvalue(), "leaking threads")
3310
 
 
3311
 
 
3312
 
class TestPostMortemDebugging(tests.TestCase):
3313
 
    """Check post mortem debugging works when tests fail or error"""
3314
 
 
3315
 
    class TracebackRecordingResult(tests.ExtendedTestResult):
3316
 
        def __init__(self):
3317
 
            tests.ExtendedTestResult.__init__(self, StringIO(), 0, 1)
3318
 
            self.postcode = None
3319
 
        def _post_mortem(self, tb=None):
3320
 
            """Record the code object at the end of the current traceback"""
3321
 
            tb = tb or sys.exc_info()[2]
3322
 
            if tb is not None:
3323
 
                next = tb.tb_next
3324
 
                while next is not None:
3325
 
                    tb = next
3326
 
                    next = next.tb_next
3327
 
                self.postcode = tb.tb_frame.f_code
3328
 
        def report_error(self, test, err):
3329
 
            pass
3330
 
        def report_failure(self, test, err):
3331
 
            pass
3332
 
 
3333
 
    def test_location_unittest_error(self):
3334
 
        """Needs right post mortem traceback with erroring unittest case"""
3335
 
        class Test(unittest.TestCase):
3336
 
            def runTest(self):
3337
 
                raise RuntimeError
3338
 
        result = self.TracebackRecordingResult()
3339
 
        Test().run(result)
3340
 
        self.assertEqual(result.postcode, Test.runTest.func_code)
3341
 
 
3342
 
    def test_location_unittest_failure(self):
3343
 
        """Needs right post mortem traceback with failing unittest case"""
3344
 
        class Test(unittest.TestCase):
3345
 
            def runTest(self):
3346
 
                raise self.failureException
3347
 
        result = self.TracebackRecordingResult()
3348
 
        Test().run(result)
3349
 
        self.assertEqual(result.postcode, Test.runTest.func_code)
3350
 
 
3351
 
    def test_location_bt_error(self):
3352
 
        """Needs right post mortem traceback with erroring bzrlib.tests case"""
3353
 
        class Test(tests.TestCase):
3354
 
            def test_error(self):
3355
 
                raise RuntimeError
3356
 
        result = self.TracebackRecordingResult()
3357
 
        Test("test_error").run(result)
3358
 
        self.assertEqual(result.postcode, Test.test_error.func_code)
3359
 
 
3360
 
    def test_location_bt_failure(self):
3361
 
        """Needs right post mortem traceback with failing bzrlib.tests case"""
3362
 
        class Test(tests.TestCase):
3363
 
            def test_failure(self):
3364
 
                raise self.failureException
3365
 
        result = self.TracebackRecordingResult()
3366
 
        Test("test_failure").run(result)
3367
 
        self.assertEqual(result.postcode, Test.test_failure.func_code)
3368
 
 
3369
 
    def test_env_var_triggers_post_mortem(self):
3370
 
        """Check pdb.post_mortem is called iff BZR_TEST_PDB is set"""
3371
 
        import pdb
3372
 
        result = tests.ExtendedTestResult(StringIO(), 0, 1)
3373
 
        post_mortem_calls = []
3374
 
        self.overrideAttr(pdb, "post_mortem", post_mortem_calls.append)
3375
 
        self.overrideEnv('BZR_TEST_PDB', None)
3376
 
        result._post_mortem(1)
3377
 
        self.overrideEnv('BZR_TEST_PDB', 'on')
3378
 
        result._post_mortem(2)
3379
 
        self.assertEqual([2], post_mortem_calls)
3380
 
 
3381
 
 
3382
2963
class TestRunSuite(tests.TestCase):
3383
2964
 
3384
2965
    def test_runner_class(self):
3395
2976
                                                self.verbosity)
3396
2977
        tests.run_suite(suite, runner_class=MyRunner, stream=StringIO())
3397
2978
        self.assertLength(1, calls)
3398
 
 
3399
 
 
3400
 
class TestEnvironHandling(tests.TestCase):
3401
 
 
3402
 
    def test_overrideEnv_None_called_twice_doesnt_leak(self):
3403
 
        self.assertFalse('MYVAR' in os.environ)
3404
 
        self.overrideEnv('MYVAR', '42')
3405
 
        # We use an embedded test to make sure we fix the _captureVar bug
3406
 
        class Test(tests.TestCase):
3407
 
            def test_me(self):
3408
 
                # The first call save the 42 value
3409
 
                self.overrideEnv('MYVAR', None)
3410
 
                self.assertEquals(None, os.environ.get('MYVAR'))
3411
 
                # Make sure we can call it twice
3412
 
                self.overrideEnv('MYVAR', None)
3413
 
                self.assertEquals(None, os.environ.get('MYVAR'))
3414
 
        output = StringIO()
3415
 
        result = tests.TextTestResult(output, 0, 1)
3416
 
        Test('test_me').run(result)
3417
 
        if not result.wasStrictlySuccessful():
3418
 
            self.fail(output.getvalue())
3419
 
        # We get our value back
3420
 
        self.assertEquals('42', os.environ.get('MYVAR'))
3421
 
 
3422
 
 
3423
 
class TestIsolatedEnv(tests.TestCase):
3424
 
    """Test isolating tests from os.environ.
3425
 
 
3426
 
    Since we use tests that are already isolated from os.environ a bit of care
3427
 
    should be taken when designing the tests to avoid bootstrap side-effects.
3428
 
    The tests start an already clean os.environ which allow doing valid
3429
 
    assertions about which variables are present or not and design tests around
3430
 
    these assertions.
3431
 
    """
3432
 
 
3433
 
    class ScratchMonkey(tests.TestCase):
3434
 
 
3435
 
        def test_me(self):
3436
 
            pass
3437
 
 
3438
 
    def test_basics(self):
3439
 
        # Make sure we know the definition of BZR_HOME: not part of os.environ
3440
 
        # for tests.TestCase.
3441
 
        self.assertTrue('BZR_HOME' in tests.isolated_environ)
3442
 
        self.assertEquals(None, tests.isolated_environ['BZR_HOME'])
3443
 
        # Being part of isolated_environ, BZR_HOME should not appear here
3444
 
        self.assertFalse('BZR_HOME' in os.environ)
3445
 
        # Make sure we know the definition of LINES: part of os.environ for
3446
 
        # tests.TestCase
3447
 
        self.assertTrue('LINES' in tests.isolated_environ)
3448
 
        self.assertEquals('25', tests.isolated_environ['LINES'])
3449
 
        self.assertEquals('25', os.environ['LINES'])
3450
 
 
3451
 
    def test_injecting_unknown_variable(self):
3452
 
        # BZR_HOME is known to be absent from os.environ
3453
 
        test = self.ScratchMonkey('test_me')
3454
 
        tests.override_os_environ(test, {'BZR_HOME': 'foo'})
3455
 
        self.assertEquals('foo', os.environ['BZR_HOME'])
3456
 
        tests.restore_os_environ(test)
3457
 
        self.assertFalse('BZR_HOME' in os.environ)
3458
 
 
3459
 
    def test_injecting_known_variable(self):
3460
 
        test = self.ScratchMonkey('test_me')
3461
 
        # LINES is known to be present in os.environ
3462
 
        tests.override_os_environ(test, {'LINES': '42'})
3463
 
        self.assertEquals('42', os.environ['LINES'])
3464
 
        tests.restore_os_environ(test)
3465
 
        self.assertEquals('25', os.environ['LINES'])
3466
 
 
3467
 
    def test_deleting_variable(self):
3468
 
        test = self.ScratchMonkey('test_me')
3469
 
        # LINES is known to be present in os.environ
3470
 
        tests.override_os_environ(test, {'LINES': None})
3471
 
        self.assertTrue('LINES' not in os.environ)
3472
 
        tests.restore_os_environ(test)
3473
 
        self.assertEquals('25', os.environ['LINES'])
3474
 
 
3475
 
 
3476
 
class TestDocTestSuiteIsolation(tests.TestCase):
3477
 
    """Test that `tests.DocTestSuite` isolates doc tests from os.environ.
3478
 
 
3479
 
    Since tests.TestCase alreay provides an isolation from os.environ, we use
3480
 
    the clean environment as a base for testing. To precisely capture the
3481
 
    isolation provided by tests.DocTestSuite, we use doctest.DocTestSuite to
3482
 
    compare against.
3483
 
 
3484
 
    We want to make sure `tests.DocTestSuite` respect `tests.isolated_environ`,
3485
 
    not `os.environ` so each test overrides it to suit its needs.
3486
 
 
3487
 
    """
3488
 
 
3489
 
    def get_doctest_suite_for_string(self, klass, string):
3490
 
        class Finder(doctest.DocTestFinder):
3491
 
 
3492
 
            def find(*args, **kwargs):
3493
 
                test = doctest.DocTestParser().get_doctest(
3494
 
                    string, {}, 'foo', 'foo.py', 0)
3495
 
                return [test]
3496
 
 
3497
 
        suite = klass(test_finder=Finder())
3498
 
        return suite
3499
 
 
3500
 
    def run_doctest_suite_for_string(self, klass, string):
3501
 
        suite = self.get_doctest_suite_for_string(klass, string)
3502
 
        output = StringIO()
3503
 
        result = tests.TextTestResult(output, 0, 1)
3504
 
        suite.run(result)
3505
 
        return result, output
3506
 
 
3507
 
    def assertDocTestStringSucceds(self, klass, string):
3508
 
        result, output = self.run_doctest_suite_for_string(klass, string)
3509
 
        if not result.wasStrictlySuccessful():
3510
 
            self.fail(output.getvalue())
3511
 
 
3512
 
    def assertDocTestStringFails(self, klass, string):
3513
 
        result, output = self.run_doctest_suite_for_string(klass, string)
3514
 
        if result.wasStrictlySuccessful():
3515
 
            self.fail(output.getvalue())
3516
 
 
3517
 
    def test_injected_variable(self):
3518
 
        self.overrideAttr(tests, 'isolated_environ', {'LINES': '42'})
3519
 
        test = """
3520
 
            >>> import os
3521
 
            >>> os.environ['LINES']
3522
 
            '42'
3523
 
            """
3524
 
        # doctest.DocTestSuite fails as it sees '25'
3525
 
        self.assertDocTestStringFails(doctest.DocTestSuite, test)
3526
 
        # tests.DocTestSuite sees '42'
3527
 
        self.assertDocTestStringSucceds(tests.IsolatedDocTestSuite, test)
3528
 
 
3529
 
    def test_deleted_variable(self):
3530
 
        self.overrideAttr(tests, 'isolated_environ', {'LINES': None})
3531
 
        test = """
3532
 
            >>> import os
3533
 
            >>> os.environ.get('LINES')
3534
 
            """
3535
 
        # doctest.DocTestSuite fails as it sees '25'
3536
 
        self.assertDocTestStringFails(doctest.DocTestSuite, test)
3537
 
        # tests.DocTestSuite sees None
3538
 
        self.assertDocTestStringSucceds(tests.IsolatedDocTestSuite, test)
3539
 
 
3540
 
 
3541
 
class TestSelftestExcludePatterns(tests.TestCase):
3542
 
 
3543
 
    def setUp(self):
3544
 
        super(TestSelftestExcludePatterns, self).setUp()
3545
 
        self.overrideAttr(tests, 'test_suite', self.suite_factory)
3546
 
 
3547
 
    def suite_factory(self, keep_only=None, starting_with=None):
3548
 
        """A test suite factory with only a few tests."""
3549
 
        class Test(tests.TestCase):
3550
 
            def id(self):
3551
 
                # We don't need the full class path
3552
 
                return self._testMethodName
3553
 
            def a(self):
3554
 
                pass
3555
 
            def b(self):
3556
 
                pass
3557
 
            def c(self):
3558
 
                pass
3559
 
        return TestUtil.TestSuite([Test("a"), Test("b"), Test("c")])
3560
 
 
3561
 
    def assertTestList(self, expected, *selftest_args):
3562
 
        # We rely on setUp installing the right test suite factory so we can
3563
 
        # test at the command level without loading the whole test suite
3564
 
        out, err = self.run_bzr(('selftest', '--list') + selftest_args)
3565
 
        actual = out.splitlines()
3566
 
        self.assertEquals(expected, actual)
3567
 
 
3568
 
    def test_full_list(self):
3569
 
        self.assertTestList(['a', 'b', 'c'])
3570
 
 
3571
 
    def test_single_exclude(self):
3572
 
        self.assertTestList(['b', 'c'], '-x', 'a')
3573
 
 
3574
 
    def test_mutiple_excludes(self):
3575
 
        self.assertTestList(['c'], '-x', 'a', '-x', 'b')
3576
 
 
3577
 
 
3578
 
class TestCounterHooks(tests.TestCase, SelfTestHelper):
3579
 
 
3580
 
    _test_needs_features = [features.subunit]
3581
 
 
3582
 
    def setUp(self):
3583
 
        super(TestCounterHooks, self).setUp()
3584
 
        class Test(tests.TestCase):
3585
 
 
3586
 
            def setUp(self):
3587
 
                super(Test, self).setUp()
3588
 
                self.hooks = hooks.Hooks()
3589
 
                self.hooks.add_hook('myhook', 'Foo bar blah', (2,4))
3590
 
                self.install_counter_hook(self.hooks, 'myhook')
3591
 
 
3592
 
            def no_hook(self):
3593
 
                pass
3594
 
 
3595
 
            def run_hook_once(self):
3596
 
                for hook in self.hooks['myhook']:
3597
 
                    hook(self)
3598
 
 
3599
 
        self.test_class = Test
3600
 
 
3601
 
    def assertHookCalls(self, expected_calls, test_name):
3602
 
        test = self.test_class(test_name)
3603
 
        result = unittest.TestResult()
3604
 
        test.run(result)
3605
 
        self.assertTrue(hasattr(test, '_counters'))
3606
 
        self.assertTrue(test._counters.has_key('myhook'))
3607
 
        self.assertEquals(expected_calls, test._counters['myhook'])
3608
 
 
3609
 
    def test_no_hook(self):
3610
 
        self.assertHookCalls(0, 'no_hook')
3611
 
 
3612
 
    def test_run_hook_once(self):
3613
 
        tt = features.testtools
3614
 
        if tt.module.__version__ < (0, 9, 8):
3615
 
            raise tests.TestSkipped('testtools-0.9.8 required for addDetail')
3616
 
        self.assertHookCalls(1, 'run_hook_once')