~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_selftest.py

  • Committer: INADA Naoki
  • Date: 2011-05-17 00:45:09 UTC
  • mfrom: (5875 +trunk)
  • mto: This revision was merged to the branch mainline in revision 5891.
  • Revision ID: songofacandy@gmail.com-20110517004509-q58negjbdjh7t6u1
mergeĀ fromĀ lp:bzr

Show diffs side-by-side

added added

removed removed

Lines of Context:
36
36
    DocTestMatches,
37
37
    Equals,
38
38
    )
39
 
import testtools.testresult.doubles
 
39
import testtools.tests.helpers
40
40
 
41
41
import bzrlib
42
42
from bzrlib import (
43
43
    branchbuilder,
44
44
    bzrdir,
45
45
    errors,
46
 
    hooks,
47
46
    lockdir,
48
47
    memorytree,
49
48
    osutils,
722
721
 
723
722
    def test_profiles_tests(self):
724
723
        self.requireFeature(test_lsprof.LSProfFeature)
725
 
        terminal = testtools.testresult.doubles.ExtendedTestResult()
 
724
        terminal = testtools.tests.helpers.ExtendedTestResult()
726
725
        result = tests.ProfileResult(terminal)
727
726
        class Sample(tests.TestCase):
728
727
            def a(self):
745
744
                descriptions=0,
746
745
                verbosity=1,
747
746
                )
748
 
        capture = testtools.testresult.doubles.ExtendedTestResult()
 
747
        capture = testtools.tests.helpers.ExtendedTestResult()
749
748
        test_case.run(MultiTestResult(result, capture))
750
749
        run_case = capture._events[0][1]
751
750
        timed_string = result._testTimeString(run_case)
1046
1045
        test = unittest.TestSuite()
1047
1046
        test.addTest(Test("known_failure_test"))
1048
1047
        def failing_test():
1049
 
            raise AssertionError('foo')
 
1048
            self.fail('foo')
1050
1049
        test.addTest(unittest.FunctionTestCase(failing_test))
1051
1050
        stream = StringIO()
1052
1051
        runner = tests.TextTestRunner(stream=stream)
1060
1059
            '^----------------------------------------------------------------------\n'
1061
1060
            'Traceback \\(most recent call last\\):\n'
1062
1061
            '  .*' # File .*, line .*, in failing_test' - but maybe not from .pyc
1063
 
            '    raise AssertionError\\(\'foo\'\\)\n'
 
1062
            '    self.fail\\(\'foo\'\\)\n'
1064
1063
            '.*'
1065
1064
            '^----------------------------------------------------------------------\n'
1066
1065
            '.*'
1072
1071
        # the final output.
1073
1072
        class Test(tests.TestCase):
1074
1073
            def known_failure_test(self):
1075
 
                self.knownFailure("Never works...")
 
1074
                self.expectFailure('failed', self.assertTrue, False)
1076
1075
        test = Test("known_failure_test")
1077
1076
        stream = StringIO()
1078
1077
        runner = tests.TextTestRunner(stream=stream)
1084
1083
            '\n'
1085
1084
            'OK \\(known_failures=1\\)\n')
1086
1085
 
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
1086
    def test_result_decorator(self):
1106
1087
        # decorate results
1107
1088
        calls = []
2038
2019
 
2039
2020
    def test_lsprof_tests(self):
2040
2021
        self.requireFeature(test_lsprof.LSProfFeature)
2041
 
        results = []
 
2022
        calls = []
2042
2023
        class Test(object):
2043
2024
            def __call__(test, result):
2044
2025
                test.run(result)
2045
2026
            def run(test, result):
2046
 
                results.append(result)
 
2027
                self.assertIsInstance(result, ExtendedToOriginalDecorator)
 
2028
                calls.append("called")
2047
2029
            def countTestCases(self):
2048
2030
                return 1
2049
2031
        self.run_selftest(test_suite_factory=Test, lsprof_tests=True)
2050
 
        self.assertLength(1, results)
2051
 
        self.assertIsInstance(results.pop(), ExtendedToOriginalDecorator)
 
2032
        self.assertLength(1, calls)
2052
2033
 
2053
2034
    def test_random(self):
2054
2035
        # test randomising by listing a number of tests.
2196
2177
        content, result = self.run_subunit_stream('test_unexpected_success')
2197
2178
        self.assertContainsRe(content, '(?m)^log$')
2198
2179
        self.assertContainsRe(content, 'test with unexpected success')
2199
 
        # GZ 2011-05-18: Old versions of subunit treat unexpected success as a
2200
 
        #                success, if a min version check is added remove this
2201
 
        from subunit import TestProtocolClient as _Client
2202
 
        if _Client.addUnexpectedSuccess.im_func is _Client.addSuccess.im_func:
2203
 
            self.expectFailure('subunit treats "unexpectedSuccess"'
2204
 
                               ' as a plain success',
2205
 
                self.assertEqual, 1, len(result.unexpectedSuccesses))
 
2180
        self.expectFailure('subunit treats "unexpectedSuccess"'
 
2181
                           ' as a plain success',
 
2182
            self.assertEqual, 1, len(result.unexpectedSuccesses))
2206
2183
        self.assertEqual(1, len(result.unexpectedSuccesses))
2207
2184
        test = result.unexpectedSuccesses[0]
2208
2185
        # RemotedTestCase doesn't preserve the "details"
2506
2483
 
2507
2484
 
2508
2485
class TestStartBzrSubProcess(tests.TestCase):
2509
 
    """Stub test start_bzr_subprocess."""
2510
2486
 
2511
 
    def _subprocess_log_cleanup(self):
2512
 
        """Inhibits the base version as we don't produce a log file."""
 
2487
    def check_popen_state(self):
 
2488
        """Replace to make assertions when popen is called."""
2513
2489
 
2514
2490
    def _popen(self, *args, **kwargs):
2515
 
        """Override the base version to record the command that is run.
2516
 
 
2517
 
        From there we can ensure it is correct without spawning a real process.
2518
 
        """
 
2491
        """Record the command that is run, so that we can ensure it is correct"""
2519
2492
        self.check_popen_state()
2520
2493
        self._popen_args = args
2521
2494
        self._popen_kwargs = kwargs
2522
2495
        raise _DontSpawnProcess()
2523
2496
 
2524
 
    def check_popen_state(self):
2525
 
        """Replace to make assertions when popen is called."""
2526
 
 
2527
2497
    def test_run_bzr_subprocess_no_plugins(self):
2528
2498
        self.assertRaises(_DontSpawnProcess, self.start_bzr_subprocess, [])
2529
2499
        command = self._popen_args[0]
2533
2503
 
2534
2504
    def test_allow_plugins(self):
2535
2505
        self.assertRaises(_DontSpawnProcess, self.start_bzr_subprocess, [],
2536
 
                          allow_plugins=True)
 
2506
            allow_plugins=True)
2537
2507
        command = self._popen_args[0]
2538
2508
        self.assertEqual([], command[2:])
2539
2509
 
2544
2514
            self.assertEqual('set variable', os.environ['EXISTANT_ENV_VAR'])
2545
2515
        self.check_popen_state = check_environment
2546
2516
        self.assertRaises(_DontSpawnProcess, self.start_bzr_subprocess, [],
2547
 
                          env_changes={'EXISTANT_ENV_VAR':'set variable'})
 
2517
            env_changes={'EXISTANT_ENV_VAR':'set variable'})
2548
2518
        # not set in theparent
2549
2519
        self.assertFalse('EXISTANT_ENV_VAR' in os.environ)
2550
2520
 
2556
2526
        os.environ['EXISTANT_ENV_VAR'] = 'set variable'
2557
2527
        self.check_popen_state = check_environment
2558
2528
        self.assertRaises(_DontSpawnProcess, self.start_bzr_subprocess, [],
2559
 
                          env_changes={'EXISTANT_ENV_VAR':None})
 
2529
            env_changes={'EXISTANT_ENV_VAR':None})
2560
2530
        # Still set in parent
2561
2531
        self.assertEqual('set variable', os.environ['EXISTANT_ENV_VAR'])
2562
2532
        del os.environ['EXISTANT_ENV_VAR']
2567
2537
            self.assertFalse('NON_EXISTANT_ENV_VAR' in os.environ)
2568
2538
        self.check_popen_state = check_environment
2569
2539
        self.assertRaises(_DontSpawnProcess, self.start_bzr_subprocess, [],
2570
 
                          env_changes={'NON_EXISTANT_ENV_VAR':None})
 
2540
            env_changes={'NON_EXISTANT_ENV_VAR':None})
2571
2541
 
2572
2542
    def test_working_dir(self):
2573
2543
        """Test that we can specify the working dir for the child"""
2576
2546
        chdirs = []
2577
2547
        def chdir(path):
2578
2548
            chdirs.append(path)
2579
 
        self.overrideAttr(os, 'chdir', chdir)
2580
 
        def getcwd():
2581
 
            return 'current'
2582
 
        self.overrideAttr(osutils, 'getcwd', getcwd)
2583
 
        self.assertRaises(_DontSpawnProcess, self.start_bzr_subprocess, [],
2584
 
                          working_dir='foo')
 
2549
        os.chdir = chdir
 
2550
        try:
 
2551
            def getcwd():
 
2552
                return 'current'
 
2553
            osutils.getcwd = getcwd
 
2554
            try:
 
2555
                self.assertRaises(_DontSpawnProcess, self.start_bzr_subprocess, [],
 
2556
                    working_dir='foo')
 
2557
            finally:
 
2558
                osutils.getcwd = orig_getcwd
 
2559
        finally:
 
2560
            os.chdir = orig_chdir
2585
2561
        self.assertEqual(['foo', 'current'], chdirs)
2586
2562
 
2587
2563
    def test_get_bzr_path_with_cwd_bzrlib(self):
3524
3500
        self.assertDocTestStringFails(doctest.DocTestSuite, test)
3525
3501
        # tests.DocTestSuite sees None
3526
3502
        self.assertDocTestStringSucceds(tests.IsolatedDocTestSuite, test)
3527
 
 
3528
 
 
3529
 
class TestSelftestExcludePatterns(tests.TestCase):
3530
 
 
3531
 
    def setUp(self):
3532
 
        super(TestSelftestExcludePatterns, self).setUp()
3533
 
        self.overrideAttr(tests, 'test_suite', self.suite_factory)
3534
 
 
3535
 
    def suite_factory(self, keep_only=None, starting_with=None):
3536
 
        """A test suite factory with only a few tests."""
3537
 
        class Test(tests.TestCase):
3538
 
            def id(self):
3539
 
                # We don't need the full class path
3540
 
                return self._testMethodName
3541
 
            def a(self):
3542
 
                pass
3543
 
            def b(self):
3544
 
                pass
3545
 
            def c(self):
3546
 
                pass
3547
 
        return TestUtil.TestSuite([Test("a"), Test("b"), Test("c")])
3548
 
 
3549
 
    def assertTestList(self, expected, *selftest_args):
3550
 
        # We rely on setUp installing the right test suite factory so we can
3551
 
        # test at the command level without loading the whole test suite
3552
 
        out, err = self.run_bzr(('selftest', '--list') + selftest_args)
3553
 
        actual = out.splitlines()
3554
 
        self.assertEquals(expected, actual)
3555
 
 
3556
 
    def test_full_list(self):
3557
 
        self.assertTestList(['a', 'b', 'c'])
3558
 
 
3559
 
    def test_single_exclude(self):
3560
 
        self.assertTestList(['b', 'c'], '-x', 'a')
3561
 
 
3562
 
    def test_mutiple_excludes(self):
3563
 
        self.assertTestList(['c'], '-x', 'a', '-x', 'b')
3564
 
 
3565
 
 
3566
 
class TestCounterHooks(tests.TestCase, SelfTestHelper):
3567
 
 
3568
 
    _test_needs_features = [features.subunit]
3569
 
 
3570
 
    def setUp(self):
3571
 
        super(TestCounterHooks, self).setUp()
3572
 
        class Test(tests.TestCase):
3573
 
 
3574
 
            def setUp(self):
3575
 
                super(Test, self).setUp()
3576
 
                self.hooks = hooks.Hooks()
3577
 
                self.hooks.add_hook('myhook', 'Foo bar blah', (2,4))
3578
 
                self.install_counter_hook(self.hooks, 'myhook')
3579
 
 
3580
 
            def no_hook(self):
3581
 
                pass
3582
 
 
3583
 
            def run_hook_once(self):
3584
 
                for hook in self.hooks['myhook']:
3585
 
                    hook(self)
3586
 
 
3587
 
        self.test_class = Test
3588
 
 
3589
 
    def assertHookCalls(self, expected_calls, test_name):
3590
 
        test = self.test_class(test_name)
3591
 
        result = unittest.TestResult()
3592
 
        test.run(result)
3593
 
        self.assertTrue(hasattr(test, '_counters'))
3594
 
        self.assertTrue(test._counters.has_key('myhook'))
3595
 
        self.assertEquals(expected_calls, test._counters['myhook'])
3596
 
 
3597
 
    def test_no_hook(self):
3598
 
        self.assertHookCalls(0, 'no_hook')
3599
 
 
3600
 
    def test_run_hook_once(self):
3601
 
        tt = features.testtools
3602
 
        if tt.module.__version__ < (0, 9, 8):
3603
 
            raise tests.TestSkipped('testtools-0.9.8 required for addDetail')
3604
 
        self.assertHookCalls(1, 'run_hook_once')