/brz/remove-bazaar

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

« back to all changes in this revision

Viewing changes to bzrlib/tests/__init__.py

  • Committer: Jelmer Vernooij
  • Date: 2011-06-18 13:57:17 UTC
  • mfrom: (5985 +trunk)
  • mto: This revision was merged to the branch mainline in revision 5987.
  • Revision ID: jelmer@samba.org-20110618135717-wo0eadh0dydpbemc
Merge bzr.dev.

Show diffs side-by-side

added added

removed removed

Lines of Context:
142
142
    'BZREMAIL': None, # may still be present in the environment
143
143
    'EMAIL': 'jrandom@example.com', # set EMAIL as bzr does not guess
144
144
    'BZR_PROGRESS_BAR': None,
145
 
    'BZR_LOG': None,
 
145
    # This should trap leaks to ~/.bzr.log. This occurs when tests use TestCase
 
146
    # as a base class instead of TestCaseInTempDir. Tests inheriting from
 
147
    # TestCase should not use disk resources, BZR_LOG is one.
 
148
    'BZR_LOG': '/you-should-use-TestCaseInTempDir-if-you-need-a-log-file',
146
149
    'BZR_PLUGIN_PATH': None,
147
150
    'BZR_DISABLE_PLUGINS': None,
148
151
    'BZR_PLUGINS_AT': None,
928
931
 
929
932
    The method is really a factory and users are expected to use it as such.
930
933
    """
931
 
    
 
934
 
932
935
    kwargs['setUp'] = isolated_doctest_setUp
933
936
    kwargs['tearDown'] = isolated_doctest_tearDown
934
937
    return doctest.DocTestSuite(*args, **kwargs)
989
992
        # is addressed -- vila 20110219
990
993
        self.overrideAttr(config, '_expand_default_value', None)
991
994
        self._log_files = set()
 
995
        # Each key in the ``_counters`` dict holds a value for a different
 
996
        # counter. When the test ends, addDetail() should be used to output the
 
997
        # counter values. This happens in install_counter_hook().
 
998
        self._counters = {}
 
999
        if 'config_stats' in selftest_debug_flags:
 
1000
            self._install_config_stats_hooks()
992
1001
 
993
1002
    def debug(self):
994
1003
        # debug a frame up.
1011
1020
        if name in details:
1012
1021
            del details[name]
1013
1022
 
 
1023
    def install_counter_hook(self, hooks, name, counter_name=None):
 
1024
        """Install a counting hook.
 
1025
 
 
1026
        Any hook can be counted as long as it doesn't need to return a value.
 
1027
 
 
1028
        :param hooks: Where the hook should be installed.
 
1029
 
 
1030
        :param name: The hook name that will be counted.
 
1031
 
 
1032
        :param counter_name: The counter identifier in ``_counters``, defaults
 
1033
            to ``name``.
 
1034
        """
 
1035
        _counters = self._counters # Avoid closing over self
 
1036
        if counter_name is None:
 
1037
            counter_name = name
 
1038
        if _counters.has_key(counter_name):
 
1039
            raise AssertionError('%s is already used as a counter name'
 
1040
                                  % (counter_name,))
 
1041
        _counters[counter_name] = 0
 
1042
        self.addDetail(counter_name, content.Content(content.UTF8_TEXT,
 
1043
            lambda: ['%d' % (_counters[counter_name],)]))
 
1044
        def increment_counter(*args, **kwargs):
 
1045
            _counters[counter_name] += 1
 
1046
        label = 'count %s calls' % (counter_name,)
 
1047
        hooks.install_named_hook(name, increment_counter, label)
 
1048
        self.addCleanup(hooks.uninstall_named_hook, name, label)
 
1049
 
 
1050
    def _install_config_stats_hooks(self):
 
1051
        """Install config hooks to count hook calls.
 
1052
 
 
1053
        """
 
1054
        for hook_name in ('get', 'set', 'remove', 'load', 'save'):
 
1055
            self.install_counter_hook(config.ConfigHooks, hook_name,
 
1056
                                       'config.%s' % (hook_name,))
 
1057
 
 
1058
        # The OldConfigHooks are private and need special handling to protect
 
1059
        # against recursive tests (tests that run other tests), so we just do
 
1060
        # manually what registering them into _builtin_known_hooks will provide
 
1061
        # us.
 
1062
        self.overrideAttr(config, 'OldConfigHooks', config._OldConfigHooks())
 
1063
        for hook_name in ('get', 'set', 'remove', 'load', 'save'):
 
1064
            self.install_counter_hook(config.OldConfigHooks, hook_name,
 
1065
                                      'old_config.%s' % (hook_name,))
 
1066
 
1014
1067
    def _clear_debug_flags(self):
1015
1068
        """Prevent externally set debug flags affecting tests.
1016
1069
 
1654
1707
    def _finishLogFile(self):
1655
1708
        """Finished with the log file.
1656
1709
 
1657
 
        Close the file and delete it, unless setKeepLogfile was called.
 
1710
        Close the file and delete it.
1658
1711
        """
1659
1712
        if trace._trace_file:
1660
1713
            # flush the log file, to get all content
2275
2328
class TestCaseWithMemoryTransport(TestCase):
2276
2329
    """Common test class for tests that do not need disk resources.
2277
2330
 
2278
 
    Tests that need disk resources should derive from TestCaseWithTransport.
 
2331
    Tests that need disk resources should derive from TestCaseInTempDir
 
2332
    orTestCaseWithTransport.
2279
2333
 
2280
2334
    TestCaseWithMemoryTransport sets the TEST_ROOT variable for all bzr tests.
2281
2335
 
2282
 
    For TestCaseWithMemoryTransport the test_home_dir is set to the name of
 
2336
    For TestCaseWithMemoryTransport the ``test_home_dir`` is set to the name of
2283
2337
    a directory which does not exist. This serves to help ensure test isolation
2284
 
    is preserved. test_dir is set to the TEST_ROOT, as is cwd, because they
2285
 
    must exist. However, TestCaseWithMemoryTransport does not offer local
2286
 
    file defaults for the transport in tests, nor does it obey the command line
 
2338
    is preserved. ``test_dir`` is set to the TEST_ROOT, as is cwd, because they
 
2339
    must exist. However, TestCaseWithMemoryTransport does not offer local file
 
2340
    defaults for the transport in tests, nor does it obey the command line
2287
2341
    override, so tests that accidentally write to the common directory should
2288
2342
    be rare.
2289
2343
 
2290
 
    :cvar TEST_ROOT: Directory containing all temporary directories, plus
2291
 
    a .bzr directory that stops us ascending higher into the filesystem.
 
2344
    :cvar TEST_ROOT: Directory containing all temporary directories, plus a
 
2345
        ``.bzr`` directory that stops us ascending higher into the filesystem.
2292
2346
    """
2293
2347
 
2294
2348
    TEST_ROOT = None
2618
2672
 
2619
2673
    OVERRIDE_PYTHON = 'python'
2620
2674
 
 
2675
    def setUp(self):
 
2676
        super(TestCaseInTempDir, self).setUp()
 
2677
        # Remove the protection set in isolated_environ, we have a proper
 
2678
        # access to disk resources now.
 
2679
        self.overrideEnv('BZR_LOG', None)
 
2680
 
2621
2681
    def check_file_contents(self, filename, expect):
2622
2682
        self.log("check contents of file %s" % filename)
2623
2683
        f = file(filename)
3514
3574
#                           with proper exclusion rules.
3515
3575
#   -Ethreads               Will display thread ident at creation/join time to
3516
3576
#                           help track thread leaks
 
3577
 
 
3578
#   -Econfig_stats          Will collect statistics using addDetail
3517
3579
selftest_debug_flags = set()
3518
3580
 
3519
3581