~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_selftest.py

  • Committer: Vincent Ladeuil
  • Date: 2011-06-27 15:42:09 UTC
  • mfrom: (5993 +trunk)
  • mto: (5993.1.1 trunk)
  • mto: This revision was merged to the branch mainline in revision 5994.
  • Revision ID: v.ladeuil+lp@free.fr-20110627154209-azubuhbuxsz109hq
Merge trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
43
43
    branchbuilder,
44
44
    bzrdir,
45
45
    errors,
 
46
    hooks,
46
47
    lockdir,
47
48
    memorytree,
48
49
    osutils,
2505
2506
 
2506
2507
 
2507
2508
class TestStartBzrSubProcess(tests.TestCase):
 
2509
    """Stub test start_bzr_subprocess."""
2508
2510
 
2509
 
    def check_popen_state(self):
2510
 
        """Replace to make assertions when popen is called."""
 
2511
    def _subprocess_log_cleanup(self):
 
2512
        """Inhibits the base version as we don't produce a log file."""
2511
2513
 
2512
2514
    def _popen(self, *args, **kwargs):
2513
 
        """Record the command that is run, so that we can ensure it is correct"""
 
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
        """
2514
2519
        self.check_popen_state()
2515
2520
        self._popen_args = args
2516
2521
        self._popen_kwargs = kwargs
2517
2522
        raise _DontSpawnProcess()
2518
2523
 
 
2524
    def check_popen_state(self):
 
2525
        """Replace to make assertions when popen is called."""
 
2526
 
2519
2527
    def test_run_bzr_subprocess_no_plugins(self):
2520
2528
        self.assertRaises(_DontSpawnProcess, self.start_bzr_subprocess, [])
2521
2529
        command = self._popen_args[0]
2525
2533
 
2526
2534
    def test_allow_plugins(self):
2527
2535
        self.assertRaises(_DontSpawnProcess, self.start_bzr_subprocess, [],
2528
 
            allow_plugins=True)
 
2536
                          allow_plugins=True)
2529
2537
        command = self._popen_args[0]
2530
2538
        self.assertEqual([], command[2:])
2531
2539
 
2536
2544
            self.assertEqual('set variable', os.environ['EXISTANT_ENV_VAR'])
2537
2545
        self.check_popen_state = check_environment
2538
2546
        self.assertRaises(_DontSpawnProcess, self.start_bzr_subprocess, [],
2539
 
            env_changes={'EXISTANT_ENV_VAR':'set variable'})
 
2547
                          env_changes={'EXISTANT_ENV_VAR':'set variable'})
2540
2548
        # not set in theparent
2541
2549
        self.assertFalse('EXISTANT_ENV_VAR' in os.environ)
2542
2550
 
2548
2556
        os.environ['EXISTANT_ENV_VAR'] = 'set variable'
2549
2557
        self.check_popen_state = check_environment
2550
2558
        self.assertRaises(_DontSpawnProcess, self.start_bzr_subprocess, [],
2551
 
            env_changes={'EXISTANT_ENV_VAR':None})
 
2559
                          env_changes={'EXISTANT_ENV_VAR':None})
2552
2560
        # Still set in parent
2553
2561
        self.assertEqual('set variable', os.environ['EXISTANT_ENV_VAR'])
2554
2562
        del os.environ['EXISTANT_ENV_VAR']
2559
2567
            self.assertFalse('NON_EXISTANT_ENV_VAR' in os.environ)
2560
2568
        self.check_popen_state = check_environment
2561
2569
        self.assertRaises(_DontSpawnProcess, self.start_bzr_subprocess, [],
2562
 
            env_changes={'NON_EXISTANT_ENV_VAR':None})
 
2570
                          env_changes={'NON_EXISTANT_ENV_VAR':None})
2563
2571
 
2564
2572
    def test_working_dir(self):
2565
2573
        """Test that we can specify the working dir for the child"""
2568
2576
        chdirs = []
2569
2577
        def chdir(path):
2570
2578
            chdirs.append(path)
2571
 
        os.chdir = chdir
2572
 
        try:
2573
 
            def getcwd():
2574
 
                return 'current'
2575
 
            osutils.getcwd = getcwd
2576
 
            try:
2577
 
                self.assertRaises(_DontSpawnProcess, self.start_bzr_subprocess, [],
2578
 
                    working_dir='foo')
2579
 
            finally:
2580
 
                osutils.getcwd = orig_getcwd
2581
 
        finally:
2582
 
            os.chdir = orig_chdir
 
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')
2583
2585
        self.assertEqual(['foo', 'current'], chdirs)
2584
2586
 
2585
2587
    def test_get_bzr_path_with_cwd_bzrlib(self):
3559
3561
 
3560
3562
    def test_mutiple_excludes(self):
3561
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')