~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_selftest.py

  • Committer: Kit Randel
  • Date: 2014-12-15 20:24:42 UTC
  • mto: This revision was merged to the branch mainline in revision 6602.
  • Revision ID: kit.randel@canonical.com-20141215202442-usf2ixhypqg8yh6q
added a note for bug-1400567 to the 2.7b release notes

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005-2010 Canonical Ltd
 
1
# Copyright (C) 2005-2011 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
 
from doctest import ELLIPSIS
 
20
import gc
 
21
import doctest
21
22
import os
22
23
import signal
23
24
import sys
26
27
import unittest
27
28
import warnings
28
29
 
29
 
from testtools import MultiTestResult
 
30
from testtools import (
 
31
    ExtendedToOriginalDecorator,
 
32
    MultiTestResult,
 
33
    )
30
34
from testtools.content import Content
31
35
from testtools.content_type import ContentType
32
36
from testtools.matchers import (
33
37
    DocTestMatches,
34
38
    Equals,
35
39
    )
36
 
import testtools.tests.helpers
 
40
import testtools.testresult.doubles
37
41
 
38
42
import bzrlib
39
43
from bzrlib import (
40
44
    branchbuilder,
41
45
    bzrdir,
42
 
    debug,
 
46
    controldir,
43
47
    errors,
 
48
    hooks,
44
49
    lockdir,
45
50
    memorytree,
46
51
    osutils,
50
55
    tests,
51
56
    transport,
52
57
    workingtree,
 
58
    workingtree_3,
 
59
    workingtree_4,
53
60
    )
54
61
from bzrlib.repofmt import (
55
62
    groupcompress_repo,
56
 
    pack_repo,
57
 
    weaverepo,
58
63
    )
59
64
from bzrlib.symbol_versioning import (
60
65
    deprecated_function,
65
70
    features,
66
71
    test_lsprof,
67
72
    test_server,
68
 
    test_sftp_transport,
69
73
    TestUtil,
70
74
    )
71
75
from bzrlib.trace import note, mutter
72
76
from bzrlib.transport import memory
73
 
from bzrlib.version import _get_bzr_source_tree
74
77
 
75
78
 
76
79
def _test_ids(test_suite):
78
81
    return [t.id() for t in tests.iter_suite_tests(test_suite)]
79
82
 
80
83
 
81
 
class SelftestTests(tests.TestCase):
82
 
 
83
 
    def test_import_tests(self):
84
 
        mod = TestUtil._load_module_by_name('bzrlib.tests.test_selftest')
85
 
        self.assertEqual(mod.SelftestTests, SelftestTests)
86
 
 
87
 
    def test_import_test_failure(self):
88
 
        self.assertRaises(ImportError,
89
 
                          TestUtil._load_module_by_name,
90
 
                          'bzrlib.no-name-yet')
91
 
 
92
 
 
93
84
class MetaTestLog(tests.TestCase):
94
85
 
95
86
    def test_logging(self):
101
92
            "text", "plain", {"charset": "utf8"})))
102
93
        self.assertThat(u"".join(log.iter_text()), Equals(self.get_log()))
103
94
        self.assertThat(self.get_log(),
104
 
            DocTestMatches(u"...a test message\n", ELLIPSIS))
105
 
 
106
 
 
107
 
class TestUnicodeFilename(tests.TestCase):
108
 
 
109
 
    def test_probe_passes(self):
110
 
        """UnicodeFilename._probe passes."""
111
 
        # We can't test much more than that because the behaviour depends
112
 
        # on the platform.
113
 
        tests.UnicodeFilename._probe()
 
95
            DocTestMatches(u"...a test message\n", doctest.ELLIPSIS))
114
96
 
115
97
 
116
98
class TestTreeShape(tests.TestCaseInTempDir):
117
99
 
118
100
    def test_unicode_paths(self):
119
 
        self.requireFeature(tests.UnicodeFilename)
 
101
        self.requireFeature(features.UnicodeFilenameFeature)
120
102
 
121
103
        filename = u'hell\u00d8'
122
104
        self.build_tree_contents([(filename, 'contents of hello')])
123
 
        self.failUnlessExists(filename)
 
105
        self.assertPathExists(filename)
124
106
 
125
107
 
126
108
class TestClassesAvailable(tests.TestCase):
352
334
        from bzrlib.tests.per_workingtree import make_scenarios
353
335
        server1 = "a"
354
336
        server2 = "b"
355
 
        formats = [workingtree.WorkingTreeFormat2(),
356
 
                   workingtree.WorkingTreeFormat3(),]
357
 
        scenarios = make_scenarios(server1, server2, formats)
 
337
        formats = [workingtree_4.WorkingTreeFormat4(),
 
338
                   workingtree_3.WorkingTreeFormat3(),
 
339
                   workingtree_4.WorkingTreeFormat6()]
 
340
        scenarios = make_scenarios(server1, server2, formats,
 
341
            remote_server='c', remote_readonly_server='d',
 
342
            remote_backing_server='e')
358
343
        self.assertEqual([
359
 
            ('WorkingTreeFormat2',
 
344
            ('WorkingTreeFormat4',
360
345
             {'bzrdir_format': formats[0]._matchingbzrdir,
361
346
              'transport_readonly_server': 'b',
362
347
              'transport_server': 'a',
365
350
             {'bzrdir_format': formats[1]._matchingbzrdir,
366
351
              'transport_readonly_server': 'b',
367
352
              'transport_server': 'a',
368
 
              'workingtree_format': formats[1]})],
369
 
            scenarios)
 
353
              'workingtree_format': formats[1]}),
 
354
            ('WorkingTreeFormat6',
 
355
             {'bzrdir_format': formats[2]._matchingbzrdir,
 
356
              'transport_readonly_server': 'b',
 
357
              'transport_server': 'a',
 
358
              'workingtree_format': formats[2]}),
 
359
            ('WorkingTreeFormat6,remote',
 
360
             {'bzrdir_format': formats[2]._matchingbzrdir,
 
361
              'repo_is_remote': True,
 
362
              'transport_readonly_server': 'd',
 
363
              'transport_server': 'c',
 
364
              'vfs_transport_factory': 'e',
 
365
              'workingtree_format': formats[2]}),
 
366
            ], scenarios)
370
367
 
371
368
 
372
369
class TestTreeScenarios(tests.TestCase):
373
370
 
374
371
    def test_scenarios(self):
375
372
        # the tree implementation scenario generator is meant to setup one
376
 
        # instance for each working tree format, and one additional instance
 
373
        # instance for each working tree format, one additional instance
377
374
        # that will use the default wt format, but create a revision tree for
378
 
        # the tests.  this means that the wt ones should have the
379
 
        # workingtree_to_test_tree attribute set to 'return_parameter' and the
380
 
        # revision one set to revision_tree_from_workingtree.
 
375
        # the tests, and one more that uses the default wt format as a
 
376
        # lightweight checkout of a remote repository.  This means that the wt
 
377
        # ones should have the workingtree_to_test_tree attribute set to
 
378
        # 'return_parameter' and the revision one set to
 
379
        # revision_tree_from_workingtree.
381
380
 
382
381
        from bzrlib.tests.per_tree import (
383
382
            _dirstate_tree_from_workingtree,
389
388
            )
390
389
        server1 = "a"
391
390
        server2 = "b"
392
 
        formats = [workingtree.WorkingTreeFormat2(),
393
 
                   workingtree.WorkingTreeFormat3(),]
 
391
        smart_server = test_server.SmartTCPServer_for_testing
 
392
        smart_readonly_server = test_server.ReadonlySmartTCPServer_for_testing
 
393
        mem_server = memory.MemoryServer
 
394
        formats = [workingtree_4.WorkingTreeFormat4(),
 
395
                   workingtree_3.WorkingTreeFormat3(),]
394
396
        scenarios = make_scenarios(server1, server2, formats)
395
 
        self.assertEqual(7, len(scenarios))
396
 
        default_wt_format = workingtree.WorkingTreeFormat4._default_format
397
 
        wt4_format = workingtree.WorkingTreeFormat4()
398
 
        wt5_format = workingtree.WorkingTreeFormat5()
 
397
        self.assertEqual(8, len(scenarios))
 
398
        default_wt_format = workingtree.format_registry.get_default()
 
399
        wt4_format = workingtree_4.WorkingTreeFormat4()
 
400
        wt5_format = workingtree_4.WorkingTreeFormat5()
 
401
        wt6_format = workingtree_4.WorkingTreeFormat6()
399
402
        expected_scenarios = [
400
 
            ('WorkingTreeFormat2',
 
403
            ('WorkingTreeFormat4',
401
404
             {'bzrdir_format': formats[0]._matchingbzrdir,
402
405
              'transport_readonly_server': 'b',
403
406
              'transport_server': 'a',
411
414
              'workingtree_format': formats[1],
412
415
              '_workingtree_to_test_tree': return_parameter,
413
416
             }),
 
417
            ('WorkingTreeFormat6,remote',
 
418
             {'bzrdir_format': wt6_format._matchingbzrdir,
 
419
              'repo_is_remote': True,
 
420
              'transport_readonly_server': smart_readonly_server,
 
421
              'transport_server': smart_server,
 
422
              'vfs_transport_factory': mem_server,
 
423
              'workingtree_format': wt6_format,
 
424
              '_workingtree_to_test_tree': return_parameter,
 
425
             }),
414
426
            ('RevisionTree',
415
427
             {'_workingtree_to_test_tree': revision_tree_from_workingtree,
416
428
              'bzrdir_format': default_wt_format._matchingbzrdir,
463
475
        # ones to add.
464
476
        from bzrlib.tests.per_tree import (
465
477
            return_parameter,
466
 
            revision_tree_from_workingtree
467
478
            )
468
479
        from bzrlib.tests.per_intertree import (
469
480
            make_scenarios,
470
481
            )
471
 
        from bzrlib.workingtree import WorkingTreeFormat2, WorkingTreeFormat3
 
482
        from bzrlib.workingtree_3 import WorkingTreeFormat3
 
483
        from bzrlib.workingtree_4 import WorkingTreeFormat4
472
484
        input_test = TestInterTreeScenarios(
473
485
            "test_scenarios")
474
486
        server1 = "a"
475
487
        server2 = "b"
476
 
        format1 = WorkingTreeFormat2()
 
488
        format1 = WorkingTreeFormat4()
477
489
        format2 = WorkingTreeFormat3()
478
490
        formats = [("1", str, format1, format2, "converter1"),
479
491
            ("2", int, format2, format1, "converter2")]
525
537
        self.assertRaises(AssertionError, self.assertEqualStat,
526
538
            os.lstat("foo"), os.lstat("longname"))
527
539
 
 
540
    def test_failUnlessExists(self):
 
541
        """Deprecated failUnlessExists and failIfExists"""
 
542
        self.applyDeprecated(
 
543
            deprecated_in((2, 4)),
 
544
            self.failUnlessExists, '.')
 
545
        self.build_tree(['foo/', 'foo/bar'])
 
546
        self.applyDeprecated(
 
547
            deprecated_in((2, 4)),
 
548
            self.failUnlessExists, 'foo/bar')
 
549
        self.applyDeprecated(
 
550
            deprecated_in((2, 4)),
 
551
            self.failIfExists, 'foo/foo')
 
552
 
 
553
    def test_assertPathExists(self):
 
554
        self.assertPathExists('.')
 
555
        self.build_tree(['foo/', 'foo/bar'])
 
556
        self.assertPathExists('foo/bar')
 
557
        self.assertPathDoesNotExist('foo/foo')
 
558
 
528
559
 
529
560
class TestTestCaseWithMemoryTransport(tests.TestCaseWithMemoryTransport):
530
561
 
564
595
        tree = self.make_branch_and_memory_tree('dir')
565
596
        # Guard against regression into MemoryTransport leaking
566
597
        # files to disk instead of keeping them in memory.
567
 
        self.failIf(osutils.lexists('dir'))
 
598
        self.assertFalse(osutils.lexists('dir'))
568
599
        self.assertIsInstance(tree, memorytree.MemoryTree)
569
600
 
570
601
    def test_make_branch_and_memory_tree_with_format(self):
571
602
        """make_branch_and_memory_tree should accept a format option."""
572
603
        format = bzrdir.BzrDirMetaFormat1()
573
 
        format.repository_format = weaverepo.RepositoryFormat7()
 
604
        format.repository_format = repository.format_registry.get_default()
574
605
        tree = self.make_branch_and_memory_tree('dir', format=format)
575
606
        # Guard against regression into MemoryTransport leaking
576
607
        # files to disk instead of keeping them in memory.
577
 
        self.failIf(osutils.lexists('dir'))
 
608
        self.assertFalse(osutils.lexists('dir'))
578
609
        self.assertIsInstance(tree, memorytree.MemoryTree)
579
610
        self.assertEqual(format.repository_format.__class__,
580
611
            tree.branch.repository._format.__class__)
584
615
        self.assertIsInstance(builder, branchbuilder.BranchBuilder)
585
616
        # Guard against regression into MemoryTransport leaking
586
617
        # files to disk instead of keeping them in memory.
587
 
        self.failIf(osutils.lexists('dir'))
 
618
        self.assertFalse(osutils.lexists('dir'))
588
619
 
589
620
    def test_make_branch_builder_with_format(self):
590
621
        # Use a repo layout that doesn't conform to a 'named' layout, to ensure
591
622
        # that the format objects are used.
592
623
        format = bzrdir.BzrDirMetaFormat1()
593
 
        repo_format = weaverepo.RepositoryFormat7()
 
624
        repo_format = repository.format_registry.get_default()
594
625
        format.repository_format = repo_format
595
626
        builder = self.make_branch_builder('dir', format=format)
596
627
        the_branch = builder.get_branch()
597
628
        # Guard against regression into MemoryTransport leaking
598
629
        # files to disk instead of keeping them in memory.
599
 
        self.failIf(osutils.lexists('dir'))
 
630
        self.assertFalse(osutils.lexists('dir'))
600
631
        self.assertEqual(format.repository_format.__class__,
601
632
                         the_branch.repository._format.__class__)
602
633
        self.assertEqual(repo_format.get_format_string(),
608
639
        the_branch = builder.get_branch()
609
640
        # Guard against regression into MemoryTransport leaking
610
641
        # files to disk instead of keeping them in memory.
611
 
        self.failIf(osutils.lexists('dir'))
612
 
        dir_format = bzrdir.format_registry.make_bzrdir('knit')
 
642
        self.assertFalse(osutils.lexists('dir'))
 
643
        dir_format = controldir.format_registry.make_bzrdir('knit')
613
644
        self.assertEqual(dir_format.repository_format.__class__,
614
645
                         the_branch.repository._format.__class__)
615
646
        self.assertEqual('Bazaar-NG Knit Repository Format 1',
619
650
    def test_dangling_locks_cause_failures(self):
620
651
        class TestDanglingLock(tests.TestCaseWithMemoryTransport):
621
652
            def test_function(self):
622
 
                t = self.get_transport('.')
 
653
                t = self.get_transport_from_path('.')
623
654
                l = lockdir.LockDir(t, 'lock')
624
655
                l.create()
625
656
                l.attempt_lock()
645
676
        # for the server
646
677
        url = self.get_readonly_url()
647
678
        url2 = self.get_readonly_url('foo/bar')
648
 
        t = transport.get_transport(url)
649
 
        t2 = transport.get_transport(url2)
650
 
        self.failUnless(isinstance(t, ReadonlyTransportDecorator))
651
 
        self.failUnless(isinstance(t2, ReadonlyTransportDecorator))
 
679
        t = transport.get_transport_from_url(url)
 
680
        t2 = transport.get_transport_from_url(url2)
 
681
        self.assertIsInstance(t, ReadonlyTransportDecorator)
 
682
        self.assertIsInstance(t2, ReadonlyTransportDecorator)
652
683
        self.assertEqual(t2.base[:-1], t.abspath('foo/bar'))
653
684
 
654
685
    def test_get_readonly_url_http(self):
660
691
        url = self.get_readonly_url()
661
692
        url2 = self.get_readonly_url('foo/bar')
662
693
        # the transport returned may be any HttpTransportBase subclass
663
 
        t = transport.get_transport(url)
664
 
        t2 = transport.get_transport(url2)
665
 
        self.failUnless(isinstance(t, HttpTransportBase))
666
 
        self.failUnless(isinstance(t2, HttpTransportBase))
 
694
        t = transport.get_transport_from_url(url)
 
695
        t2 = transport.get_transport_from_url(url2)
 
696
        self.assertIsInstance(t, HttpTransportBase)
 
697
        self.assertIsInstance(t2, HttpTransportBase)
667
698
        self.assertEqual(t2.base[:-1], t.abspath('foo/bar'))
668
699
 
669
700
    def test_is_directory(self):
677
708
    def test_make_branch_builder(self):
678
709
        builder = self.make_branch_builder('dir')
679
710
        rev_id = builder.build_commit()
680
 
        self.failUnlessExists('dir')
681
 
        a_dir = bzrdir.BzrDir.open('dir')
 
711
        self.assertPathExists('dir')
 
712
        a_dir = controldir.ControlDir.open('dir')
682
713
        self.assertRaises(errors.NoWorkingTree, a_dir.open_workingtree)
683
714
        a_branch = a_dir.open_branch()
684
715
        builder_branch = builder.get_branch()
699
730
        self.assertIsInstance(result_bzrdir.transport,
700
731
                              memory.MemoryTransport)
701
732
        # should not be on disk, should only be in memory
702
 
        self.failIfExists('subdir')
 
733
        self.assertPathDoesNotExist('subdir')
703
734
 
704
735
 
705
736
class TestChrootedTest(tests.ChrootedTestCase):
706
737
 
707
738
    def test_root_is_root(self):
708
 
        t = transport.get_transport(self.get_readonly_url())
 
739
        t = transport.get_transport_from_url(self.get_readonly_url())
709
740
        url = t.base
710
741
        self.assertEqual(url, t.clone('..').base)
711
742
 
713
744
class TestProfileResult(tests.TestCase):
714
745
 
715
746
    def test_profiles_tests(self):
716
 
        self.requireFeature(test_lsprof.LSProfFeature)
717
 
        terminal = testtools.tests.helpers.ExtendedTestResult()
 
747
        self.requireFeature(features.lsprof_feature)
 
748
        terminal = testtools.testresult.doubles.ExtendedTestResult()
718
749
        result = tests.ProfileResult(terminal)
719
750
        class Sample(tests.TestCase):
720
751
            def a(self):
737
768
                descriptions=0,
738
769
                verbosity=1,
739
770
                )
740
 
        capture = testtools.tests.helpers.ExtendedTestResult()
 
771
        capture = testtools.testresult.doubles.ExtendedTestResult()
741
772
        test_case.run(MultiTestResult(result, capture))
742
773
        run_case = capture._events[0][1]
743
774
        timed_string = result._testTimeString(run_case)
764
795
        self.check_timing(ShortDelayTestCase('test_short_delay'),
765
796
                          r"^ +[0-9]+ms$")
766
797
 
767
 
    def _patch_get_bzr_source_tree(self):
768
 
        # Reading from the actual source tree breaks isolation, but we don't
769
 
        # want to assume that thats *all* that would happen.
770
 
        self.overrideAttr(bzrlib.version, '_get_bzr_source_tree', lambda: None)
771
 
 
772
 
    def test_assigned_benchmark_file_stores_date(self):
773
 
        self._patch_get_bzr_source_tree()
774
 
        output = StringIO()
775
 
        result = bzrlib.tests.TextTestResult(self._log_file,
776
 
                                        descriptions=0,
777
 
                                        verbosity=1,
778
 
                                        bench_history=output
779
 
                                        )
780
 
        output_string = output.getvalue()
781
 
        # if you are wondering about the regexp please read the comment in
782
 
        # test_bench_history (bzrlib.tests.test_selftest.TestRunner)
783
 
        # XXX: what comment?  -- Andrew Bennetts
784
 
        self.assertContainsRe(output_string, "--date [0-9.]+")
785
 
 
786
 
    def test_benchhistory_records_test_times(self):
787
 
        self._patch_get_bzr_source_tree()
788
 
        result_stream = StringIO()
789
 
        result = bzrlib.tests.TextTestResult(
790
 
            self._log_file,
791
 
            descriptions=0,
792
 
            verbosity=1,
793
 
            bench_history=result_stream
794
 
            )
795
 
 
796
 
        # we want profile a call and check that its test duration is recorded
797
 
        # make a new test instance that when run will generate a benchmark
798
 
        example_test_case = TestTestResult("_time_hello_world_encoding")
799
 
        # execute the test, which should succeed and record times
800
 
        example_test_case.run(result)
801
 
        lines = result_stream.getvalue().splitlines()
802
 
        self.assertEqual(2, len(lines))
803
 
        self.assertContainsRe(lines[1],
804
 
            " *[0-9]+ms bzrlib.tests.test_selftest.TestTestResult"
805
 
            "._time_hello_world_encoding")
806
 
 
807
798
    def _time_hello_world_encoding(self):
808
799
        """Profile two sleep calls
809
800
 
814
805
 
815
806
    def test_lsprofiling(self):
816
807
        """Verbose test result prints lsprof statistics from test cases."""
817
 
        self.requireFeature(test_lsprof.LSProfFeature)
 
808
        self.requireFeature(features.lsprof_feature)
818
809
        result_stream = StringIO()
819
810
        result = bzrlib.tests.VerboseTestResult(
820
811
            result_stream,
849
840
        self.assertContainsRe(output,
850
841
            r"LSProf output for <type 'unicode'>\(\('world',\), {'errors': 'replace'}\)\n")
851
842
 
 
843
    def test_uses_time_from_testtools(self):
 
844
        """Test case timings in verbose results should use testtools times"""
 
845
        import datetime
 
846
        class TimeAddedVerboseTestResult(tests.VerboseTestResult):
 
847
            def startTest(self, test):
 
848
                self.time(datetime.datetime.utcfromtimestamp(1.145))
 
849
                super(TimeAddedVerboseTestResult, self).startTest(test)
 
850
            def addSuccess(self, test):
 
851
                self.time(datetime.datetime.utcfromtimestamp(51.147))
 
852
                super(TimeAddedVerboseTestResult, self).addSuccess(test)
 
853
            def report_tests_starting(self): pass
 
854
        sio = StringIO()
 
855
        self.get_passing_test().run(TimeAddedVerboseTestResult(sio, 0, 2))
 
856
        self.assertEndsWith(sio.getvalue(), "OK    50002ms\n")
 
857
 
852
858
    def test_known_failure(self):
853
 
        """A KnownFailure being raised should trigger several result actions."""
 
859
        """Using knownFailure should trigger several result actions."""
854
860
        class InstrumentedTestResult(tests.ExtendedTestResult):
855
861
            def stopTestRun(self): pass
856
862
            def report_tests_starting(self): pass
859
865
        result = InstrumentedTestResult(None, None, None, None)
860
866
        class Test(tests.TestCase):
861
867
            def test_function(self):
862
 
                raise tests.KnownFailure('failed!')
 
868
                self.knownFailure('failed!')
863
869
        test = Test("test_function")
864
870
        test.run(result)
865
871
        # it should invoke 'report_known_failure'.
881
887
            descriptions=0,
882
888
            verbosity=2,
883
889
            )
884
 
        test = self.get_passing_test()
885
 
        result.startTest(test)
886
 
        prefix = len(result_stream.getvalue())
887
 
        # the err parameter has the shape:
888
 
        # (class, exception object, traceback)
889
 
        # KnownFailures dont get their tracebacks shown though, so we
890
 
        # can skip that.
891
 
        err = (tests.KnownFailure, tests.KnownFailure('foo'), None)
892
 
        result.report_known_failure(test, err)
893
 
        output = result_stream.getvalue()[prefix:]
894
 
        lines = output.splitlines()
895
 
        self.assertContainsRe(lines[0], r'XFAIL *\d+ms$')
896
 
        if sys.version_info > (2, 7):
897
 
            self.expectFailure("_ExpectedFailure on 2.7 loses the message",
898
 
                self.assertNotEqual, lines[1], '    ')
899
 
        self.assertEqual(lines[1], '    foo')
900
 
        self.assertEqual(2, len(lines))
 
890
        _get_test("test_xfail").run(result)
 
891
        self.assertContainsRe(result_stream.getvalue(),
 
892
            "\n\\S+\\.test_xfail\\s+XFAIL\\s+\\d+ms\n"
 
893
            "\\s*(?:Text attachment: )?reason"
 
894
            "(?:\n-+\n|: {{{)"
 
895
            "this_fails"
 
896
            "(?:\n-+\n|}}}\n)")
901
897
 
902
898
    def get_passing_test(self):
903
899
        """Return a test object that can't be run usefully."""
914
910
                self._call = test, feature
915
911
        result = InstrumentedTestResult(None, None, None, None)
916
912
        test = SampleTestCase('_test_pass')
917
 
        feature = tests.Feature()
 
913
        feature = features.Feature()
918
914
        result.startTest(test)
919
915
        result.addNotSupported(test, feature)
920
916
        # it should invoke 'report_unsupported'.
939
935
            verbosity=2,
940
936
            )
941
937
        test = self.get_passing_test()
942
 
        feature = tests.Feature()
 
938
        feature = features.Feature()
943
939
        result.startTest(test)
944
940
        prefix = len(result_stream.getvalue())
945
941
        result.report_unsupported(test, feature)
958
954
            def addNotSupported(self, test, feature):
959
955
                self._call = test, feature
960
956
        result = InstrumentedTestResult(None, None, None, None)
961
 
        feature = tests.Feature()
 
957
        feature = features.Feature()
962
958
        class Test(tests.TestCase):
963
959
            def test_function(self):
964
960
                raise tests.UnavailableFeature(feature)
983
979
    def test_strict_with_known_failure(self):
984
980
        result = bzrlib.tests.TextTestResult(self._log_file, descriptions=0,
985
981
                                             verbosity=1)
986
 
        test = self.get_passing_test()
987
 
        err = (tests.KnownFailure, tests.KnownFailure('foo'), None)
988
 
        result.addExpectedFailure(test, err)
 
982
        test = _get_test("test_xfail")
 
983
        test.run(result)
989
984
        self.assertFalse(result.wasStrictlySuccessful())
990
985
        self.assertEqual(None, result._extractBenchmarkTime(test))
991
986
 
1023
1018
        self.assertEquals(2, result.count)
1024
1019
 
1025
1020
 
1026
 
class TestUnicodeFilenameFeature(tests.TestCase):
1027
 
 
1028
 
    def test_probe_passes(self):
1029
 
        """UnicodeFilenameFeature._probe passes."""
1030
 
        # We can't test much more than that because the behaviour depends
1031
 
        # on the platform.
1032
 
        tests.UnicodeFilenameFeature._probe()
1033
 
 
1034
 
 
1035
1021
class TestRunner(tests.TestCase):
1036
1022
 
1037
1023
    def dummy_test(self):
1063
1049
        test = unittest.TestSuite()
1064
1050
        test.addTest(Test("known_failure_test"))
1065
1051
        def failing_test():
1066
 
            self.fail('foo')
 
1052
            raise AssertionError('foo')
1067
1053
        test.addTest(unittest.FunctionTestCase(failing_test))
1068
1054
        stream = StringIO()
1069
1055
        runner = tests.TextTestRunner(stream=stream)
1077
1063
            '^----------------------------------------------------------------------\n'
1078
1064
            'Traceback \\(most recent call last\\):\n'
1079
1065
            '  .*' # File .*, line .*, in failing_test' - but maybe not from .pyc
1080
 
            '    self.fail\\(\'foo\'\\)\n'
 
1066
            '    raise AssertionError\\(\'foo\'\\)\n'
1081
1067
            '.*'
1082
1068
            '^----------------------------------------------------------------------\n'
1083
1069
            '.*'
1089
1075
        # the final output.
1090
1076
        class Test(tests.TestCase):
1091
1077
            def known_failure_test(self):
1092
 
                self.expectFailure('failed', self.assertTrue, False)
 
1078
                self.knownFailure("Never works...")
1093
1079
        test = Test("known_failure_test")
1094
1080
        stream = StringIO()
1095
1081
        runner = tests.TextTestRunner(stream=stream)
1101
1087
            '\n'
1102
1088
            'OK \\(known_failures=1\\)\n')
1103
1089
 
 
1090
    def test_unexpected_success_bad(self):
 
1091
        class Test(tests.TestCase):
 
1092
            def test_truth(self):
 
1093
                self.expectFailure("No absolute truth", self.assertTrue, True)
 
1094
        runner = tests.TextTestRunner(stream=StringIO())
 
1095
        result = self.run_test_runner(runner, Test("test_truth"))
 
1096
        self.assertContainsRe(runner.stream.getvalue(),
 
1097
            "=+\n"
 
1098
            "FAIL: \\S+\.test_truth\n"
 
1099
            "-+\n"
 
1100
            "(?:.*\n)*"
 
1101
            "\\s*(?:Text attachment: )?reason"
 
1102
            "(?:\n-+\n|: {{{)"
 
1103
            "No absolute truth"
 
1104
            "(?:\n-+\n|}}}\n)"
 
1105
            "(?:.*\n)*"
 
1106
            "-+\n"
 
1107
            "Ran 1 test in .*\n"
 
1108
            "\n"
 
1109
            "FAILED \\(failures=1\\)\n\\Z")
 
1110
 
1104
1111
    def test_result_decorator(self):
1105
1112
        # decorate results
1106
1113
        calls = []
1107
 
        class LoggingDecorator(tests.ForwardingResult):
 
1114
        class LoggingDecorator(ExtendedToOriginalDecorator):
1108
1115
            def startTest(self, test):
1109
 
                tests.ForwardingResult.startTest(self, test)
 
1116
                ExtendedToOriginalDecorator.startTest(self, test)
1110
1117
                calls.append('start')
1111
1118
        test = unittest.FunctionTestCase(lambda:None)
1112
1119
        stream = StringIO()
1154
1161
        class SkippedTest(tests.TestCase):
1155
1162
 
1156
1163
            def setUp(self):
1157
 
                tests.TestCase.setUp(self)
 
1164
                super(SkippedTest, self).setUp()
1158
1165
                calls.append('setUp')
1159
1166
                self.addCleanup(self.cleanup)
1160
1167
 
1190
1197
 
1191
1198
    def test_unsupported_features_listed(self):
1192
1199
        """When unsupported features are encountered they are detailed."""
1193
 
        class Feature1(tests.Feature):
 
1200
        class Feature1(features.Feature):
1194
1201
            def _probe(self): return False
1195
 
        class Feature2(tests.Feature):
 
1202
        class Feature2(features.Feature):
1196
1203
            def _probe(self): return False
1197
1204
        # create sample tests
1198
1205
        test1 = SampleTestCase('_test_pass')
1213
1220
            ],
1214
1221
            lines[-3:])
1215
1222
 
1216
 
    def _patch_get_bzr_source_tree(self):
1217
 
        # Reading from the actual source tree breaks isolation, but we don't
1218
 
        # want to assume that thats *all* that would happen.
1219
 
        self._get_source_tree_calls = []
1220
 
        def new_get():
1221
 
            self._get_source_tree_calls.append("called")
1222
 
            return None
1223
 
        self.overrideAttr(bzrlib.version, '_get_bzr_source_tree',  new_get)
1224
 
 
1225
 
    def test_bench_history(self):
1226
 
        # tests that the running the benchmark passes bench_history into
1227
 
        # the test result object. We can tell that happens if
1228
 
        # _get_bzr_source_tree is called.
1229
 
        self._patch_get_bzr_source_tree()
1230
 
        test = TestRunner('dummy_test')
1231
 
        output = StringIO()
1232
 
        runner = tests.TextTestRunner(stream=self._log_file,
1233
 
                                      bench_history=output)
1234
 
        result = self.run_test_runner(runner, test)
1235
 
        output_string = output.getvalue()
1236
 
        self.assertContainsRe(output_string, "--date [0-9.]+")
1237
 
        self.assertLength(1, self._get_source_tree_calls)
1238
 
 
1239
1223
    def test_verbose_test_count(self):
1240
1224
        """A verbose test run reports the right test count at the start"""
1241
1225
        suite = TestUtil.TestSuite([
1251
1235
    def test_startTestRun(self):
1252
1236
        """run should call result.startTestRun()"""
1253
1237
        calls = []
1254
 
        class LoggingDecorator(tests.ForwardingResult):
 
1238
        class LoggingDecorator(ExtendedToOriginalDecorator):
1255
1239
            def startTestRun(self):
1256
 
                tests.ForwardingResult.startTestRun(self)
 
1240
                ExtendedToOriginalDecorator.startTestRun(self)
1257
1241
                calls.append('startTestRun')
1258
1242
        test = unittest.FunctionTestCase(lambda:None)
1259
1243
        stream = StringIO()
1265
1249
    def test_stopTestRun(self):
1266
1250
        """run should call result.stopTestRun()"""
1267
1251
        calls = []
1268
 
        class LoggingDecorator(tests.ForwardingResult):
 
1252
        class LoggingDecorator(ExtendedToOriginalDecorator):
1269
1253
            def stopTestRun(self):
1270
 
                tests.ForwardingResult.stopTestRun(self)
 
1254
                ExtendedToOriginalDecorator.stopTestRun(self)
1271
1255
                calls.append('stopTestRun')
1272
1256
        test = unittest.FunctionTestCase(lambda:None)
1273
1257
        stream = StringIO()
1276
1260
        result = self.run_test_runner(runner, test)
1277
1261
        self.assertLength(1, calls)
1278
1262
 
 
1263
    def test_unicode_test_output_on_ascii_stream(self):
 
1264
        """Showing results should always succeed even on an ascii console"""
 
1265
        class FailureWithUnicode(tests.TestCase):
 
1266
            def test_log_unicode(self):
 
1267
                self.log(u"\u2606")
 
1268
                self.fail("Now print that log!")
 
1269
        out = StringIO()
 
1270
        self.overrideAttr(osutils, "get_terminal_encoding",
 
1271
            lambda trace=False: "ascii")
 
1272
        result = self.run_test_runner(tests.TextTestRunner(stream=out),
 
1273
            FailureWithUnicode("test_log_unicode"))
 
1274
        self.assertContainsRe(out.getvalue(),
 
1275
            "(?:Text attachment: )?log"
 
1276
            "(?:\n-+\n|: {{{)"
 
1277
            "\d+\.\d+  \\\\u2606"
 
1278
            "(?:\n-+\n|}}}\n)")
 
1279
 
1279
1280
 
1280
1281
class SampleTestCase(tests.TestCase):
1281
1282
 
1469
1470
        # Note this test won't fail with hooks that the core library doesn't
1470
1471
        # use - but it trigger with a plugin that adds hooks, so its still a
1471
1472
        # useful warning in that case.
1472
 
        self.assertEqual(bzrlib.branch.BranchHooks(),
1473
 
            bzrlib.branch.Branch.hooks)
1474
 
        self.assertEqual(bzrlib.smart.server.SmartServerHooks(),
 
1473
        self.assertEqual(bzrlib.branch.BranchHooks(), bzrlib.branch.Branch.hooks)
 
1474
        self.assertEqual(
 
1475
            bzrlib.smart.server.SmartServerHooks(),
1475
1476
            bzrlib.smart.server.SmartTCPServer.hooks)
1476
 
        self.assertEqual(bzrlib.commands.CommandHooks(),
1477
 
            bzrlib.commands.Command.hooks)
 
1477
        self.assertEqual(
 
1478
            bzrlib.commands.CommandHooks(), bzrlib.commands.Command.hooks)
1478
1479
 
1479
1480
    def test__gather_lsprof_in_benchmarks(self):
1480
1481
        """When _gather_lsprof_in_benchmarks is on, accumulate profile data.
1481
1482
 
1482
1483
        Each self.time() call is individually and separately profiled.
1483
1484
        """
1484
 
        self.requireFeature(test_lsprof.LSProfFeature)
 
1485
        self.requireFeature(features.lsprof_feature)
1485
1486
        # overrides the class member with an instance member so no cleanup
1486
1487
        # needed.
1487
1488
        self._gather_lsprof_in_benchmarks = True
1506
1507
        transport_server = memory.MemoryServer()
1507
1508
        transport_server.start_server()
1508
1509
        self.addCleanup(transport_server.stop_server)
1509
 
        t = transport.get_transport(transport_server.get_url())
1510
 
        bzrdir.BzrDir.create(t.base)
 
1510
        t = transport.get_transport_from_url(transport_server.get_url())
 
1511
        controldir.ControlDir.create(t.base)
1511
1512
        self.assertRaises(errors.BzrError,
1512
 
            bzrdir.BzrDir.open_from_transport, t)
 
1513
            controldir.ControlDir.open_from_transport, t)
1513
1514
        # But if we declare this as safe, we can open the bzrdir.
1514
1515
        self.permit_url(t.base)
1515
1516
        self._bzr_selftest_roots.append(t.base)
1516
 
        bzrdir.BzrDir.open_from_transport(t)
 
1517
        controldir.ControlDir.open_from_transport(t)
1517
1518
 
1518
1519
    def test_requireFeature_available(self):
1519
1520
        """self.requireFeature(available) is a no-op."""
1520
 
        class Available(tests.Feature):
 
1521
        class Available(features.Feature):
1521
1522
            def _probe(self):return True
1522
1523
        feature = Available()
1523
1524
        self.requireFeature(feature)
1524
1525
 
1525
1526
    def test_requireFeature_unavailable(self):
1526
1527
        """self.requireFeature(unavailable) raises UnavailableFeature."""
1527
 
        class Unavailable(tests.Feature):
 
1528
        class Unavailable(features.Feature):
1528
1529
            def _probe(self):return False
1529
1530
        feature = Unavailable()
1530
1531
        self.assertRaises(tests.UnavailableFeature,
1653
1654
        self.assertRaises(AssertionError,
1654
1655
            self.assertListRaises, _TestException, success_generator)
1655
1656
 
 
1657
    def _run_successful_test(self, test):
 
1658
        result = testtools.TestResult()
 
1659
        test.run(result)
 
1660
        self.assertTrue(result.wasSuccessful())
 
1661
        return result
 
1662
 
1656
1663
    def test_overrideAttr_without_value(self):
1657
1664
        self.test_attr = 'original' # Define a test attribute
1658
1665
        obj = self # Make 'obj' visible to the embedded test
1659
1666
        class Test(tests.TestCase):
1660
1667
 
1661
1668
            def setUp(self):
1662
 
                tests.TestCase.setUp(self)
 
1669
                super(Test, self).setUp()
1663
1670
                self.orig = self.overrideAttr(obj, 'test_attr')
1664
1671
 
1665
1672
            def test_value(self):
1668
1675
                obj.test_attr = 'modified'
1669
1676
                self.assertEqual('modified', obj.test_attr)
1670
1677
 
1671
 
        test = Test('test_value')
1672
 
        test.run(unittest.TestResult())
 
1678
        self._run_successful_test(Test('test_value'))
1673
1679
        self.assertEqual('original', obj.test_attr)
1674
1680
 
1675
1681
    def test_overrideAttr_with_value(self):
1678
1684
        class Test(tests.TestCase):
1679
1685
 
1680
1686
            def setUp(self):
1681
 
                tests.TestCase.setUp(self)
 
1687
                super(Test, self).setUp()
1682
1688
                self.orig = self.overrideAttr(obj, 'test_attr', new='modified')
1683
1689
 
1684
1690
            def test_value(self):
1685
1691
                self.assertEqual('original', self.orig)
1686
1692
                self.assertEqual('modified', obj.test_attr)
1687
1693
 
1688
 
        test = Test('test_value')
1689
 
        test.run(unittest.TestResult())
 
1694
        self._run_successful_test(Test('test_value'))
1690
1695
        self.assertEqual('original', obj.test_attr)
1691
1696
 
1692
 
 
1693
 
class _MissingFeature(tests.Feature):
 
1697
    def test_overrideAttr_with_no_existing_value_and_value(self):
 
1698
        # Do not define the test_attribute
 
1699
        obj = self # Make 'obj' visible to the embedded test
 
1700
        class Test(tests.TestCase):
 
1701
 
 
1702
            def setUp(self):
 
1703
                tests.TestCase.setUp(self)
 
1704
                self.orig = self.overrideAttr(obj, 'test_attr', new='modified')
 
1705
 
 
1706
            def test_value(self):
 
1707
                self.assertEqual(tests._unitialized_attr, self.orig)
 
1708
                self.assertEqual('modified', obj.test_attr)
 
1709
 
 
1710
        self._run_successful_test(Test('test_value'))
 
1711
        self.assertRaises(AttributeError, getattr, obj, 'test_attr')
 
1712
 
 
1713
    def test_overrideAttr_with_no_existing_value_and_no_value(self):
 
1714
        # Do not define the test_attribute
 
1715
        obj = self # Make 'obj' visible to the embedded test
 
1716
        class Test(tests.TestCase):
 
1717
 
 
1718
            def setUp(self):
 
1719
                tests.TestCase.setUp(self)
 
1720
                self.orig = self.overrideAttr(obj, 'test_attr')
 
1721
 
 
1722
            def test_value(self):
 
1723
                self.assertEqual(tests._unitialized_attr, self.orig)
 
1724
                self.assertRaises(AttributeError, getattr, obj, 'test_attr')
 
1725
 
 
1726
        self._run_successful_test(Test('test_value'))
 
1727
        self.assertRaises(AttributeError, getattr, obj, 'test_attr')
 
1728
 
 
1729
    def test_recordCalls(self):
 
1730
        from bzrlib.tests import test_selftest
 
1731
        calls = self.recordCalls(
 
1732
            test_selftest, '_add_numbers')
 
1733
        self.assertEqual(test_selftest._add_numbers(2, 10),
 
1734
            12)
 
1735
        self.assertEquals(calls, [((2, 10), {})])
 
1736
 
 
1737
 
 
1738
def _add_numbers(a, b):
 
1739
    return a + b
 
1740
 
 
1741
 
 
1742
class _MissingFeature(features.Feature):
1694
1743
    def _probe(self):
1695
1744
        return False
1696
1745
missing_feature = _MissingFeature()
1747
1796
        result = self._run_test('test_fail')
1748
1797
        self.assertEqual(1, len(result.failures))
1749
1798
        result_content = result.failures[0][1]
1750
 
        self.assertContainsRe(result_content, 'Text attachment: log')
 
1799
        self.assertContainsRe(result_content,
 
1800
            '(?m)^(?:Text attachment: )?log(?:$|: )')
1751
1801
        self.assertContainsRe(result_content, 'this was a failing test')
1752
1802
 
1753
1803
    def test_error_has_log(self):
1754
1804
        result = self._run_test('test_error')
1755
1805
        self.assertEqual(1, len(result.errors))
1756
1806
        result_content = result.errors[0][1]
1757
 
        self.assertContainsRe(result_content, 'Text attachment: log')
 
1807
        self.assertContainsRe(result_content,
 
1808
            '(?m)^(?:Text attachment: )?log(?:$|: )')
1758
1809
        self.assertContainsRe(result_content, 'this test errored')
1759
1810
 
1760
1811
    def test_skip_has_no_log(self):
1779
1830
        result = self._run_test('test_xfail')
1780
1831
        self.assertEqual(1, len(result.expectedFailures))
1781
1832
        result_content = result.expectedFailures[0][1]
1782
 
        self.assertNotContainsRe(result_content, 'Text attachment: log')
 
1833
        self.assertNotContainsRe(result_content,
 
1834
            '(?m)^(?:Text attachment: )?log(?:$|: )')
1783
1835
        self.assertNotContainsRe(result_content, 'test with expected failure')
1784
1836
 
1785
1837
    def test_unexpected_success_has_log(self):
1958
2010
    def test_make_branch_and_tree_with_format(self):
1959
2011
        # we should be able to supply a format to make_branch_and_tree
1960
2012
        self.make_branch_and_tree('a', format=bzrlib.bzrdir.BzrDirMetaFormat1())
1961
 
        self.make_branch_and_tree('b', format=bzrlib.bzrdir.BzrDirFormat6())
1962
 
        self.assertIsInstance(bzrlib.bzrdir.BzrDir.open('a')._format,
 
2013
        self.assertIsInstance(bzrlib.controldir.ControlDir.open('a')._format,
1963
2014
                              bzrlib.bzrdir.BzrDirMetaFormat1)
1964
 
        self.assertIsInstance(bzrlib.bzrdir.BzrDir.open('b')._format,
1965
 
                              bzrlib.bzrdir.BzrDirFormat6)
1966
2015
 
1967
2016
    def test_make_branch_and_memory_tree(self):
1968
2017
        # we should be able to get a new branch and a mutable tree from
2045
2094
        self.assertLength(2, output.readlines())
2046
2095
 
2047
2096
    def test_lsprof_tests(self):
2048
 
        self.requireFeature(test_lsprof.LSProfFeature)
2049
 
        calls = []
 
2097
        self.requireFeature(features.lsprof_feature)
 
2098
        results = []
2050
2099
        class Test(object):
2051
2100
            def __call__(test, result):
2052
2101
                test.run(result)
2053
2102
            def run(test, result):
2054
 
                self.assertIsInstance(result, tests.ForwardingResult)
2055
 
                calls.append("called")
 
2103
                results.append(result)
2056
2104
            def countTestCases(self):
2057
2105
                return 1
2058
2106
        self.run_selftest(test_suite_factory=Test, lsprof_tests=True)
2059
 
        self.assertLength(1, calls)
 
2107
        self.assertLength(1, results)
 
2108
        self.assertIsInstance(results.pop(), ExtendedToOriginalDecorator)
2060
2109
 
2061
2110
    def test_random(self):
2062
2111
        # test randomising by listing a number of tests.
2197
2246
        self.assertNotContainsRe(content, 'test with expected failure')
2198
2247
        self.assertEqual(1, len(result.expectedFailures))
2199
2248
        result_content = result.expectedFailures[0][1]
2200
 
        self.assertNotContainsRe(result_content, 'Text attachment: log')
 
2249
        self.assertNotContainsRe(result_content,
 
2250
            '(?m)^(?:Text attachment: )?log(?:$|: )')
2201
2251
        self.assertNotContainsRe(result_content, 'test with expected failure')
2202
2252
 
2203
2253
    def test_unexpected_success_has_log(self):
2204
2254
        content, result = self.run_subunit_stream('test_unexpected_success')
2205
2255
        self.assertContainsRe(content, '(?m)^log$')
2206
2256
        self.assertContainsRe(content, 'test with unexpected success')
2207
 
        self.expectFailure('subunit treats "unexpectedSuccess"'
2208
 
                           ' as a plain success',
2209
 
            self.assertEqual, 1, len(result.unexpectedSuccesses))
 
2257
        # GZ 2011-05-18: Old versions of subunit treat unexpected success as a
 
2258
        #                success, if a min version check is added remove this
 
2259
        from subunit import TestProtocolClient as _Client
 
2260
        if _Client.addUnexpectedSuccess.im_func is _Client.addSuccess.im_func:
 
2261
            self.expectFailure('subunit treats "unexpectedSuccess"'
 
2262
                               ' as a plain success',
 
2263
                self.assertEqual, 1, len(result.unexpectedSuccesses))
2210
2264
        self.assertEqual(1, len(result.unexpectedSuccesses))
2211
2265
        test = result.unexpectedSuccesses[0]
2212
2266
        # RemotedTestCase doesn't preserve the "details"
2347
2401
        # stdout and stderr of the invoked run_bzr
2348
2402
        current_factory = bzrlib.ui.ui_factory
2349
2403
        self.run_bzr(['foo'])
2350
 
        self.failIf(current_factory is self.factory)
 
2404
        self.assertFalse(current_factory is self.factory)
2351
2405
        self.assertNotEqual(sys.stdout, self.factory.stdout)
2352
2406
        self.assertNotEqual(sys.stderr, self.factory.stderr)
2353
2407
        self.assertEqual('foo\n', self.factory.stdout.getvalue())
2394
2448
    """Base class for tests testing how we might run bzr."""
2395
2449
 
2396
2450
    def setUp(self):
2397
 
        tests.TestCaseWithTransport.setUp(self)
 
2451
        super(TestWithFakedStartBzrSubprocess, self).setUp()
2398
2452
        self.subprocess_calls = []
2399
2453
 
2400
2454
    def start_bzr_subprocess(self, process_args, env_changes=None,
2510
2564
 
2511
2565
 
2512
2566
class TestStartBzrSubProcess(tests.TestCase):
 
2567
    """Stub test start_bzr_subprocess."""
2513
2568
 
2514
 
    def check_popen_state(self):
2515
 
        """Replace to make assertions when popen is called."""
 
2569
    def _subprocess_log_cleanup(self):
 
2570
        """Inhibits the base version as we don't produce a log file."""
2516
2571
 
2517
2572
    def _popen(self, *args, **kwargs):
2518
 
        """Record the command that is run, so that we can ensure it is correct"""
 
2573
        """Override the base version to record the command that is run.
 
2574
 
 
2575
        From there we can ensure it is correct without spawning a real process.
 
2576
        """
2519
2577
        self.check_popen_state()
2520
2578
        self._popen_args = args
2521
2579
        self._popen_kwargs = kwargs
2522
2580
        raise _DontSpawnProcess()
2523
2581
 
 
2582
    def check_popen_state(self):
 
2583
        """Replace to make assertions when popen is called."""
 
2584
 
2524
2585
    def test_run_bzr_subprocess_no_plugins(self):
2525
2586
        self.assertRaises(_DontSpawnProcess, self.start_bzr_subprocess, [])
2526
2587
        command = self._popen_args[0]
2530
2591
 
2531
2592
    def test_allow_plugins(self):
2532
2593
        self.assertRaises(_DontSpawnProcess, self.start_bzr_subprocess, [],
2533
 
            allow_plugins=True)
 
2594
                          allow_plugins=True)
2534
2595
        command = self._popen_args[0]
2535
2596
        self.assertEqual([], command[2:])
2536
2597
 
2537
2598
    def test_set_env(self):
2538
 
        self.failIf('EXISTANT_ENV_VAR' in os.environ)
 
2599
        self.assertFalse('EXISTANT_ENV_VAR' in os.environ)
2539
2600
        # set in the child
2540
2601
        def check_environment():
2541
2602
            self.assertEqual('set variable', os.environ['EXISTANT_ENV_VAR'])
2542
2603
        self.check_popen_state = check_environment
2543
2604
        self.assertRaises(_DontSpawnProcess, self.start_bzr_subprocess, [],
2544
 
            env_changes={'EXISTANT_ENV_VAR':'set variable'})
 
2605
                          env_changes={'EXISTANT_ENV_VAR':'set variable'})
2545
2606
        # not set in theparent
2546
2607
        self.assertFalse('EXISTANT_ENV_VAR' in os.environ)
2547
2608
 
2548
2609
    def test_run_bzr_subprocess_env_del(self):
2549
2610
        """run_bzr_subprocess can remove environment variables too."""
2550
 
        self.failIf('EXISTANT_ENV_VAR' in os.environ)
 
2611
        self.assertFalse('EXISTANT_ENV_VAR' in os.environ)
2551
2612
        def check_environment():
2552
2613
            self.assertFalse('EXISTANT_ENV_VAR' in os.environ)
2553
2614
        os.environ['EXISTANT_ENV_VAR'] = 'set variable'
2554
2615
        self.check_popen_state = check_environment
2555
2616
        self.assertRaises(_DontSpawnProcess, self.start_bzr_subprocess, [],
2556
 
            env_changes={'EXISTANT_ENV_VAR':None})
 
2617
                          env_changes={'EXISTANT_ENV_VAR':None})
2557
2618
        # Still set in parent
2558
2619
        self.assertEqual('set variable', os.environ['EXISTANT_ENV_VAR'])
2559
2620
        del os.environ['EXISTANT_ENV_VAR']
2560
2621
 
2561
2622
    def test_env_del_missing(self):
2562
 
        self.failIf('NON_EXISTANT_ENV_VAR' in os.environ)
 
2623
        self.assertFalse('NON_EXISTANT_ENV_VAR' in os.environ)
2563
2624
        def check_environment():
2564
2625
            self.assertFalse('NON_EXISTANT_ENV_VAR' in os.environ)
2565
2626
        self.check_popen_state = check_environment
2566
2627
        self.assertRaises(_DontSpawnProcess, self.start_bzr_subprocess, [],
2567
 
            env_changes={'NON_EXISTANT_ENV_VAR':None})
 
2628
                          env_changes={'NON_EXISTANT_ENV_VAR':None})
2568
2629
 
2569
2630
    def test_working_dir(self):
2570
2631
        """Test that we can specify the working dir for the child"""
2573
2634
        chdirs = []
2574
2635
        def chdir(path):
2575
2636
            chdirs.append(path)
2576
 
        os.chdir = chdir
2577
 
        try:
2578
 
            def getcwd():
2579
 
                return 'current'
2580
 
            osutils.getcwd = getcwd
2581
 
            try:
2582
 
                self.assertRaises(_DontSpawnProcess, self.start_bzr_subprocess, [],
2583
 
                    working_dir='foo')
2584
 
            finally:
2585
 
                osutils.getcwd = orig_getcwd
2586
 
        finally:
2587
 
            os.chdir = orig_chdir
 
2637
        self.overrideAttr(os, 'chdir', chdir)
 
2638
        def getcwd():
 
2639
            return 'current'
 
2640
        self.overrideAttr(osutils, 'getcwd', getcwd)
 
2641
        self.assertRaises(_DontSpawnProcess, self.start_bzr_subprocess, [],
 
2642
                          working_dir='foo')
2588
2643
        self.assertEqual(['foo', 'current'], chdirs)
2589
2644
 
2590
2645
    def test_get_bzr_path_with_cwd_bzrlib(self):
2610
2665
        self.assertEqual('bzr: interrupted\n', result[1])
2611
2666
 
2612
2667
 
2613
 
class TestFeature(tests.TestCase):
2614
 
 
2615
 
    def test_caching(self):
2616
 
        """Feature._probe is called by the feature at most once."""
2617
 
        class InstrumentedFeature(tests.Feature):
2618
 
            def __init__(self):
2619
 
                super(InstrumentedFeature, self).__init__()
2620
 
                self.calls = []
2621
 
            def _probe(self):
2622
 
                self.calls.append('_probe')
2623
 
                return False
2624
 
        feature = InstrumentedFeature()
2625
 
        feature.available()
2626
 
        self.assertEqual(['_probe'], feature.calls)
2627
 
        feature.available()
2628
 
        self.assertEqual(['_probe'], feature.calls)
2629
 
 
2630
 
    def test_named_str(self):
2631
 
        """Feature.__str__ should thunk to feature_name()."""
2632
 
        class NamedFeature(tests.Feature):
2633
 
            def feature_name(self):
2634
 
                return 'symlinks'
2635
 
        feature = NamedFeature()
2636
 
        self.assertEqual('symlinks', str(feature))
2637
 
 
2638
 
    def test_default_str(self):
2639
 
        """Feature.__str__ should default to __class__.__name__."""
2640
 
        class NamedFeature(tests.Feature):
2641
 
            pass
2642
 
        feature = NamedFeature()
2643
 
        self.assertEqual('NamedFeature', str(feature))
2644
 
 
2645
 
 
2646
 
class TestUnavailableFeature(tests.TestCase):
2647
 
 
2648
 
    def test_access_feature(self):
2649
 
        feature = tests.Feature()
2650
 
        exception = tests.UnavailableFeature(feature)
2651
 
        self.assertIs(feature, exception.args[0])
2652
 
 
2653
 
 
2654
 
simple_thunk_feature = tests._CompatabilityThunkFeature(
2655
 
    deprecated_in((2, 1, 0)),
2656
 
    'bzrlib.tests.test_selftest',
2657
 
    'simple_thunk_feature','UnicodeFilename',
2658
 
    replacement_module='bzrlib.tests'
2659
 
    )
2660
 
 
2661
 
class Test_CompatibilityFeature(tests.TestCase):
2662
 
 
2663
 
    def test_does_thunk(self):
2664
 
        res = self.callDeprecated(
2665
 
            ['bzrlib.tests.test_selftest.simple_thunk_feature was deprecated'
2666
 
             ' in version 2.1.0. Use bzrlib.tests.UnicodeFilename instead.'],
2667
 
            simple_thunk_feature.available)
2668
 
        self.assertEqual(tests.UnicodeFilename.available(), res)
2669
 
 
2670
 
 
2671
 
class TestModuleAvailableFeature(tests.TestCase):
2672
 
 
2673
 
    def test_available_module(self):
2674
 
        feature = tests.ModuleAvailableFeature('bzrlib.tests')
2675
 
        self.assertEqual('bzrlib.tests', feature.module_name)
2676
 
        self.assertEqual('bzrlib.tests', str(feature))
2677
 
        self.assertTrue(feature.available())
2678
 
        self.assertIs(tests, feature.module)
2679
 
 
2680
 
    def test_unavailable_module(self):
2681
 
        feature = tests.ModuleAvailableFeature('bzrlib.no_such_module_exists')
2682
 
        self.assertEqual('bzrlib.no_such_module_exists', str(feature))
2683
 
        self.assertFalse(feature.available())
2684
 
        self.assertIs(None, feature.module)
2685
 
 
2686
 
 
2687
2668
class TestSelftestFiltering(tests.TestCase):
2688
2669
 
2689
2670
    def setUp(self):
2690
 
        tests.TestCase.setUp(self)
 
2671
        super(TestSelftestFiltering, self).setUp()
2691
2672
        self.suite = TestUtil.TestSuite()
2692
2673
        self.loader = TestUtil.TestLoader()
2693
2674
        self.suite.addTest(self.loader.loadTestsFromModule(
2840
2821
        self.assertEqual(remaining_names, _test_ids(split_suite[1]))
2841
2822
 
2842
2823
 
2843
 
class TestCheckInventoryShape(tests.TestCaseWithTransport):
 
2824
class TestCheckTreeShape(tests.TestCaseWithTransport):
2844
2825
 
2845
 
    def test_check_inventory_shape(self):
 
2826
    def test_check_tree_shape(self):
2846
2827
        files = ['a', 'b/', 'b/c']
2847
2828
        tree = self.make_branch_and_tree('.')
2848
2829
        self.build_tree(files)
2849
2830
        tree.add(files)
2850
2831
        tree.lock_read()
2851
2832
        try:
2852
 
            self.check_inventory_shape(tree.inventory, files)
 
2833
            self.check_tree_shape(tree, files)
2853
2834
        finally:
2854
2835
            tree.unlock()
2855
2836
 
3187
3168
        tpr.register('bar', 'bBB.aAA.rRR')
3188
3169
        self.assertEquals('bbb.aaa.rrr', tpr.get('bar'))
3189
3170
        self.assertThat(self.get_log(),
3190
 
            DocTestMatches("...bar...bbb.aaa.rrr...BB.aAA.rRR", ELLIPSIS))
 
3171
            DocTestMatches("...bar...bbb.aaa.rrr...BB.aAA.rRR",
 
3172
                           doctest.ELLIPSIS))
3191
3173
 
3192
3174
    def test_get_unknown_prefix(self):
3193
3175
        tpr = self._get_registry()
3228
3210
        class Test(unittest.TestCase):
3229
3211
            def runTest(self):
3230
3212
                pass
3231
 
            addCleanup = None # for when on Python 2.7 with native addCleanup
3232
3213
        result = self.LeakRecordingResult()
3233
3214
        test = Test()
3234
 
        self.assertIs(getattr(test, "addCleanup", None), None)
3235
3215
        result.startTestRun()
3236
3216
        test.run(result)
3237
3217
        result.stopTestRun()
3301
3281
        self.assertContainsString(result.stream.getvalue(), "leaking threads")
3302
3282
 
3303
3283
 
 
3284
class TestPostMortemDebugging(tests.TestCase):
 
3285
    """Check post mortem debugging works when tests fail or error"""
 
3286
 
 
3287
    class TracebackRecordingResult(tests.ExtendedTestResult):
 
3288
        def __init__(self):
 
3289
            tests.ExtendedTestResult.__init__(self, StringIO(), 0, 1)
 
3290
            self.postcode = None
 
3291
        def _post_mortem(self, tb=None):
 
3292
            """Record the code object at the end of the current traceback"""
 
3293
            tb = tb or sys.exc_info()[2]
 
3294
            if tb is not None:
 
3295
                next = tb.tb_next
 
3296
                while next is not None:
 
3297
                    tb = next
 
3298
                    next = next.tb_next
 
3299
                self.postcode = tb.tb_frame.f_code
 
3300
        def report_error(self, test, err):
 
3301
            pass
 
3302
        def report_failure(self, test, err):
 
3303
            pass
 
3304
 
 
3305
    def test_location_unittest_error(self):
 
3306
        """Needs right post mortem traceback with erroring unittest case"""
 
3307
        class Test(unittest.TestCase):
 
3308
            def runTest(self):
 
3309
                raise RuntimeError
 
3310
        result = self.TracebackRecordingResult()
 
3311
        Test().run(result)
 
3312
        self.assertEqual(result.postcode, Test.runTest.func_code)
 
3313
 
 
3314
    def test_location_unittest_failure(self):
 
3315
        """Needs right post mortem traceback with failing unittest case"""
 
3316
        class Test(unittest.TestCase):
 
3317
            def runTest(self):
 
3318
                raise self.failureException
 
3319
        result = self.TracebackRecordingResult()
 
3320
        Test().run(result)
 
3321
        self.assertEqual(result.postcode, Test.runTest.func_code)
 
3322
 
 
3323
    def test_location_bt_error(self):
 
3324
        """Needs right post mortem traceback with erroring bzrlib.tests case"""
 
3325
        class Test(tests.TestCase):
 
3326
            def test_error(self):
 
3327
                raise RuntimeError
 
3328
        result = self.TracebackRecordingResult()
 
3329
        Test("test_error").run(result)
 
3330
        self.assertEqual(result.postcode, Test.test_error.func_code)
 
3331
 
 
3332
    def test_location_bt_failure(self):
 
3333
        """Needs right post mortem traceback with failing bzrlib.tests case"""
 
3334
        class Test(tests.TestCase):
 
3335
            def test_failure(self):
 
3336
                raise self.failureException
 
3337
        result = self.TracebackRecordingResult()
 
3338
        Test("test_failure").run(result)
 
3339
        self.assertEqual(result.postcode, Test.test_failure.func_code)
 
3340
 
 
3341
    def test_env_var_triggers_post_mortem(self):
 
3342
        """Check pdb.post_mortem is called iff BZR_TEST_PDB is set"""
 
3343
        import pdb
 
3344
        result = tests.ExtendedTestResult(StringIO(), 0, 1)
 
3345
        post_mortem_calls = []
 
3346
        self.overrideAttr(pdb, "post_mortem", post_mortem_calls.append)
 
3347
        self.overrideEnv('BZR_TEST_PDB', None)
 
3348
        result._post_mortem(1)
 
3349
        self.overrideEnv('BZR_TEST_PDB', 'on')
 
3350
        result._post_mortem(2)
 
3351
        self.assertEqual([2], post_mortem_calls)
 
3352
 
 
3353
 
3304
3354
class TestRunSuite(tests.TestCase):
3305
3355
 
3306
3356
    def test_runner_class(self):
3317
3367
                                                self.verbosity)
3318
3368
        tests.run_suite(suite, runner_class=MyRunner, stream=StringIO())
3319
3369
        self.assertLength(1, calls)
 
3370
 
 
3371
 
 
3372
class _Selftest(object):
 
3373
    """Mixin for tests needing full selftest output"""
 
3374
 
 
3375
    def _inject_stream_into_subunit(self, stream):
 
3376
        """To be overridden by subclasses that run tests out of process"""
 
3377
 
 
3378
    def _run_selftest(self, **kwargs):
 
3379
        sio = StringIO()
 
3380
        self._inject_stream_into_subunit(sio)
 
3381
        tests.selftest(stream=sio, stop_on_failure=False, **kwargs)
 
3382
        return sio.getvalue()
 
3383
 
 
3384
 
 
3385
class _ForkedSelftest(_Selftest):
 
3386
    """Mixin for tests needing full selftest output with forked children"""
 
3387
 
 
3388
    _test_needs_features = [features.subunit]
 
3389
 
 
3390
    def _inject_stream_into_subunit(self, stream):
 
3391
        """Monkey-patch subunit so the extra output goes to stream not stdout
 
3392
 
 
3393
        Some APIs need rewriting so this kind of bogus hackery can be replaced
 
3394
        by passing the stream param from run_tests down into ProtocolTestCase.
 
3395
        """
 
3396
        from subunit import ProtocolTestCase
 
3397
        _original_init = ProtocolTestCase.__init__
 
3398
        def _init_with_passthrough(self, *args, **kwargs):
 
3399
            _original_init(self, *args, **kwargs)
 
3400
            self._passthrough = stream
 
3401
        self.overrideAttr(ProtocolTestCase, "__init__", _init_with_passthrough)
 
3402
 
 
3403
    def _run_selftest(self, **kwargs):
 
3404
        # GZ 2011-05-26: Add a PosixSystem feature so this check can go away
 
3405
        if getattr(os, "fork", None) is None:
 
3406
            raise tests.TestNotApplicable("Platform doesn't support forking")
 
3407
        # Make sure the fork code is actually invoked by claiming two cores
 
3408
        self.overrideAttr(osutils, "local_concurrency", lambda: 2)
 
3409
        kwargs.setdefault("suite_decorators", []).append(tests.fork_decorator)
 
3410
        return super(_ForkedSelftest, self)._run_selftest(**kwargs)
 
3411
 
 
3412
 
 
3413
class TestParallelFork(_ForkedSelftest, tests.TestCase):
 
3414
    """Check operation of --parallel=fork selftest option"""
 
3415
 
 
3416
    def test_error_in_child_during_fork(self):
 
3417
        """Error in a forked child during test setup should get reported"""
 
3418
        class Test(tests.TestCase):
 
3419
            def testMethod(self):
 
3420
                pass
 
3421
        # We don't care what, just break something that a child will run
 
3422
        self.overrideAttr(tests, "workaround_zealous_crypto_random", None)
 
3423
        out = self._run_selftest(test_suite_factory=Test)
 
3424
        # Lines from the tracebacks of the two child processes may be mixed
 
3425
        # together due to the way subunit parses and forwards the streams,
 
3426
        # so permit extra lines between each part of the error output.
 
3427
        self.assertContainsRe(out,
 
3428
            "Traceback.*:\n"
 
3429
            "(?:.*\n)*"
 
3430
            ".+ in fork_for_tests\n"
 
3431
            "(?:.*\n)*"
 
3432
            "\s*workaround_zealous_crypto_random\(\)\n"
 
3433
            "(?:.*\n)*"
 
3434
            "TypeError:")
 
3435
 
 
3436
 
 
3437
class TestUncollectedWarnings(_Selftest, tests.TestCase):
 
3438
    """Check a test case still alive after being run emits a warning"""
 
3439
 
 
3440
    class Test(tests.TestCase):
 
3441
        def test_pass(self):
 
3442
            pass
 
3443
        def test_self_ref(self):
 
3444
            self.also_self = self.test_self_ref
 
3445
        def test_skip(self):
 
3446
            self.skip("Don't need")
 
3447
 
 
3448
    def _get_suite(self):
 
3449
        return TestUtil.TestSuite([
 
3450
            self.Test("test_pass"),
 
3451
            self.Test("test_self_ref"),
 
3452
            self.Test("test_skip"),
 
3453
            ])
 
3454
 
 
3455
    def _run_selftest_with_suite(self, **kwargs):
 
3456
        old_flags = tests.selftest_debug_flags
 
3457
        tests.selftest_debug_flags = old_flags.union(["uncollected_cases"])
 
3458
        gc_on = gc.isenabled()
 
3459
        if gc_on:
 
3460
            gc.disable()
 
3461
        try:
 
3462
            output = self._run_selftest(test_suite_factory=self._get_suite,
 
3463
                **kwargs)
 
3464
        finally:
 
3465
            if gc_on:
 
3466
                gc.enable()
 
3467
            tests.selftest_debug_flags = old_flags
 
3468
        self.assertNotContainsRe(output, "Uncollected test case.*test_pass")
 
3469
        self.assertContainsRe(output, "Uncollected test case.*test_self_ref")
 
3470
        return output
 
3471
 
 
3472
    def test_testsuite(self):
 
3473
        self._run_selftest_with_suite()
 
3474
 
 
3475
    def test_pattern(self):
 
3476
        out = self._run_selftest_with_suite(pattern="test_(?:pass|self_ref)$")
 
3477
        self.assertNotContainsRe(out, "test_skip")
 
3478
 
 
3479
    def test_exclude_pattern(self):
 
3480
        out = self._run_selftest_with_suite(exclude_pattern="test_skip$")
 
3481
        self.assertNotContainsRe(out, "test_skip")
 
3482
 
 
3483
    def test_random_seed(self):
 
3484
        self._run_selftest_with_suite(random_seed="now")
 
3485
 
 
3486
    def test_matching_tests_first(self):
 
3487
        self._run_selftest_with_suite(matching_tests_first=True,
 
3488
            pattern="test_self_ref$")
 
3489
 
 
3490
    def test_starting_with_and_exclude(self):
 
3491
        out = self._run_selftest_with_suite(starting_with=["bt."],
 
3492
            exclude_pattern="test_skip$")
 
3493
        self.assertNotContainsRe(out, "test_skip")
 
3494
 
 
3495
    def test_additonal_decorator(self):
 
3496
        out = self._run_selftest_with_suite(
 
3497
            suite_decorators=[tests.TestDecorator])
 
3498
 
 
3499
 
 
3500
class TestUncollectedWarningsSubunit(TestUncollectedWarnings):
 
3501
    """Check warnings from tests staying alive are emitted with subunit"""
 
3502
 
 
3503
    _test_needs_features = [features.subunit]
 
3504
 
 
3505
    def _run_selftest_with_suite(self, **kwargs):
 
3506
        return TestUncollectedWarnings._run_selftest_with_suite(self,
 
3507
            runner_class=tests.SubUnitBzrRunner, **kwargs)
 
3508
 
 
3509
 
 
3510
class TestUncollectedWarningsForked(_ForkedSelftest, TestUncollectedWarnings):
 
3511
    """Check warnings from tests staying alive are emitted when forking"""
 
3512
 
 
3513
 
 
3514
class TestEnvironHandling(tests.TestCase):
 
3515
 
 
3516
    def test_overrideEnv_None_called_twice_doesnt_leak(self):
 
3517
        self.assertFalse('MYVAR' in os.environ)
 
3518
        self.overrideEnv('MYVAR', '42')
 
3519
        # We use an embedded test to make sure we fix the _captureVar bug
 
3520
        class Test(tests.TestCase):
 
3521
            def test_me(self):
 
3522
                # The first call save the 42 value
 
3523
                self.overrideEnv('MYVAR', None)
 
3524
                self.assertEquals(None, os.environ.get('MYVAR'))
 
3525
                # Make sure we can call it twice
 
3526
                self.overrideEnv('MYVAR', None)
 
3527
                self.assertEquals(None, os.environ.get('MYVAR'))
 
3528
        output = StringIO()
 
3529
        result = tests.TextTestResult(output, 0, 1)
 
3530
        Test('test_me').run(result)
 
3531
        if not result.wasStrictlySuccessful():
 
3532
            self.fail(output.getvalue())
 
3533
        # We get our value back
 
3534
        self.assertEquals('42', os.environ.get('MYVAR'))
 
3535
 
 
3536
 
 
3537
class TestIsolatedEnv(tests.TestCase):
 
3538
    """Test isolating tests from os.environ.
 
3539
 
 
3540
    Since we use tests that are already isolated from os.environ a bit of care
 
3541
    should be taken when designing the tests to avoid bootstrap side-effects.
 
3542
    The tests start an already clean os.environ which allow doing valid
 
3543
    assertions about which variables are present or not and design tests around
 
3544
    these assertions.
 
3545
    """
 
3546
 
 
3547
    class ScratchMonkey(tests.TestCase):
 
3548
 
 
3549
        def test_me(self):
 
3550
            pass
 
3551
 
 
3552
    def test_basics(self):
 
3553
        # Make sure we know the definition of BZR_HOME: not part of os.environ
 
3554
        # for tests.TestCase.
 
3555
        self.assertTrue('BZR_HOME' in tests.isolated_environ)
 
3556
        self.assertEquals(None, tests.isolated_environ['BZR_HOME'])
 
3557
        # Being part of isolated_environ, BZR_HOME should not appear here
 
3558
        self.assertFalse('BZR_HOME' in os.environ)
 
3559
        # Make sure we know the definition of LINES: part of os.environ for
 
3560
        # tests.TestCase
 
3561
        self.assertTrue('LINES' in tests.isolated_environ)
 
3562
        self.assertEquals('25', tests.isolated_environ['LINES'])
 
3563
        self.assertEquals('25', os.environ['LINES'])
 
3564
 
 
3565
    def test_injecting_unknown_variable(self):
 
3566
        # BZR_HOME is known to be absent from os.environ
 
3567
        test = self.ScratchMonkey('test_me')
 
3568
        tests.override_os_environ(test, {'BZR_HOME': 'foo'})
 
3569
        self.assertEquals('foo', os.environ['BZR_HOME'])
 
3570
        tests.restore_os_environ(test)
 
3571
        self.assertFalse('BZR_HOME' in os.environ)
 
3572
 
 
3573
    def test_injecting_known_variable(self):
 
3574
        test = self.ScratchMonkey('test_me')
 
3575
        # LINES is known to be present in os.environ
 
3576
        tests.override_os_environ(test, {'LINES': '42'})
 
3577
        self.assertEquals('42', os.environ['LINES'])
 
3578
        tests.restore_os_environ(test)
 
3579
        self.assertEquals('25', os.environ['LINES'])
 
3580
 
 
3581
    def test_deleting_variable(self):
 
3582
        test = self.ScratchMonkey('test_me')
 
3583
        # LINES is known to be present in os.environ
 
3584
        tests.override_os_environ(test, {'LINES': None})
 
3585
        self.assertTrue('LINES' not in os.environ)
 
3586
        tests.restore_os_environ(test)
 
3587
        self.assertEquals('25', os.environ['LINES'])
 
3588
 
 
3589
 
 
3590
class TestDocTestSuiteIsolation(tests.TestCase):
 
3591
    """Test that `tests.DocTestSuite` isolates doc tests from os.environ.
 
3592
 
 
3593
    Since tests.TestCase alreay provides an isolation from os.environ, we use
 
3594
    the clean environment as a base for testing. To precisely capture the
 
3595
    isolation provided by tests.DocTestSuite, we use doctest.DocTestSuite to
 
3596
    compare against.
 
3597
 
 
3598
    We want to make sure `tests.DocTestSuite` respect `tests.isolated_environ`,
 
3599
    not `os.environ` so each test overrides it to suit its needs.
 
3600
 
 
3601
    """
 
3602
 
 
3603
    def get_doctest_suite_for_string(self, klass, string):
 
3604
        class Finder(doctest.DocTestFinder):
 
3605
 
 
3606
            def find(*args, **kwargs):
 
3607
                test = doctest.DocTestParser().get_doctest(
 
3608
                    string, {}, 'foo', 'foo.py', 0)
 
3609
                return [test]
 
3610
 
 
3611
        suite = klass(test_finder=Finder())
 
3612
        return suite
 
3613
 
 
3614
    def run_doctest_suite_for_string(self, klass, string):
 
3615
        suite = self.get_doctest_suite_for_string(klass, string)
 
3616
        output = StringIO()
 
3617
        result = tests.TextTestResult(output, 0, 1)
 
3618
        suite.run(result)
 
3619
        return result, output
 
3620
 
 
3621
    def assertDocTestStringSucceds(self, klass, string):
 
3622
        result, output = self.run_doctest_suite_for_string(klass, string)
 
3623
        if not result.wasStrictlySuccessful():
 
3624
            self.fail(output.getvalue())
 
3625
 
 
3626
    def assertDocTestStringFails(self, klass, string):
 
3627
        result, output = self.run_doctest_suite_for_string(klass, string)
 
3628
        if result.wasStrictlySuccessful():
 
3629
            self.fail(output.getvalue())
 
3630
 
 
3631
    def test_injected_variable(self):
 
3632
        self.overrideAttr(tests, 'isolated_environ', {'LINES': '42'})
 
3633
        test = """
 
3634
            >>> import os
 
3635
            >>> os.environ['LINES']
 
3636
            '42'
 
3637
            """
 
3638
        # doctest.DocTestSuite fails as it sees '25'
 
3639
        self.assertDocTestStringFails(doctest.DocTestSuite, test)
 
3640
        # tests.DocTestSuite sees '42'
 
3641
        self.assertDocTestStringSucceds(tests.IsolatedDocTestSuite, test)
 
3642
 
 
3643
    def test_deleted_variable(self):
 
3644
        self.overrideAttr(tests, 'isolated_environ', {'LINES': None})
 
3645
        test = """
 
3646
            >>> import os
 
3647
            >>> os.environ.get('LINES')
 
3648
            """
 
3649
        # doctest.DocTestSuite fails as it sees '25'
 
3650
        self.assertDocTestStringFails(doctest.DocTestSuite, test)
 
3651
        # tests.DocTestSuite sees None
 
3652
        self.assertDocTestStringSucceds(tests.IsolatedDocTestSuite, test)
 
3653
 
 
3654
 
 
3655
class TestSelftestExcludePatterns(tests.TestCase):
 
3656
 
 
3657
    def setUp(self):
 
3658
        super(TestSelftestExcludePatterns, self).setUp()
 
3659
        self.overrideAttr(tests, 'test_suite', self.suite_factory)
 
3660
 
 
3661
    def suite_factory(self, keep_only=None, starting_with=None):
 
3662
        """A test suite factory with only a few tests."""
 
3663
        class Test(tests.TestCase):
 
3664
            def id(self):
 
3665
                # We don't need the full class path
 
3666
                return self._testMethodName
 
3667
            def a(self):
 
3668
                pass
 
3669
            def b(self):
 
3670
                pass
 
3671
            def c(self):
 
3672
                pass
 
3673
        return TestUtil.TestSuite([Test("a"), Test("b"), Test("c")])
 
3674
 
 
3675
    def assertTestList(self, expected, *selftest_args):
 
3676
        # We rely on setUp installing the right test suite factory so we can
 
3677
        # test at the command level without loading the whole test suite
 
3678
        out, err = self.run_bzr(('selftest', '--list') + selftest_args)
 
3679
        actual = out.splitlines()
 
3680
        self.assertEquals(expected, actual)
 
3681
 
 
3682
    def test_full_list(self):
 
3683
        self.assertTestList(['a', 'b', 'c'])
 
3684
 
 
3685
    def test_single_exclude(self):
 
3686
        self.assertTestList(['b', 'c'], '-x', 'a')
 
3687
 
 
3688
    def test_mutiple_excludes(self):
 
3689
        self.assertTestList(['c'], '-x', 'a', '-x', 'b')
 
3690
 
 
3691
 
 
3692
class TestCounterHooks(tests.TestCase, SelfTestHelper):
 
3693
 
 
3694
    _test_needs_features = [features.subunit]
 
3695
 
 
3696
    def setUp(self):
 
3697
        super(TestCounterHooks, self).setUp()
 
3698
        class Test(tests.TestCase):
 
3699
 
 
3700
            def setUp(self):
 
3701
                super(Test, self).setUp()
 
3702
                self.hooks = hooks.Hooks()
 
3703
                self.hooks.add_hook('myhook', 'Foo bar blah', (2,4))
 
3704
                self.install_counter_hook(self.hooks, 'myhook')
 
3705
 
 
3706
            def no_hook(self):
 
3707
                pass
 
3708
 
 
3709
            def run_hook_once(self):
 
3710
                for hook in self.hooks['myhook']:
 
3711
                    hook(self)
 
3712
 
 
3713
        self.test_class = Test
 
3714
 
 
3715
    def assertHookCalls(self, expected_calls, test_name):
 
3716
        test = self.test_class(test_name)
 
3717
        result = unittest.TestResult()
 
3718
        test.run(result)
 
3719
        self.assertTrue(hasattr(test, '_counters'))
 
3720
        self.assertTrue(test._counters.has_key('myhook'))
 
3721
        self.assertEquals(expected_calls, test._counters['myhook'])
 
3722
 
 
3723
    def test_no_hook(self):
 
3724
        self.assertHookCalls(0, 'no_hook')
 
3725
 
 
3726
    def test_run_hook_once(self):
 
3727
        tt = features.testtools
 
3728
        if tt.module.__version__ < (0, 9, 8):
 
3729
            raise tests.TestSkipped('testtools-0.9.8 required for addDetail')
 
3730
        self.assertHookCalls(1, 'run_hook_once')