~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/__init__.py

  • Committer: Jonathan Riddell
  • Date: 2011-06-17 09:07:12 UTC
  • mfrom: (5984 +trunk)
  • mto: (5971.2.3 bzr-gpgme)
  • mto: This revision was merged to the branch mainline in revision 6003.
  • Revision ID: jriddell@canonical.com-20110617090712-gbp520g89lwfhihn
mergeĀ inĀ trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
989
989
        # is addressed -- vila 20110219
990
990
        self.overrideAttr(config, '_expand_default_value', None)
991
991
        self._log_files = set()
 
992
        # Each key in the ``_counters`` dict holds a value for a different
 
993
        # counter. When the test ends, addDetail() should be used to output the
 
994
        # counter values. This happens in install_counter_hook().
 
995
        self._counters = {}
 
996
        if 'config_stats' in selftest_debug_flags:
 
997
            self._install_config_stats_hooks()
992
998
 
993
999
    def debug(self):
994
1000
        # debug a frame up.
1011
1017
        if name in details:
1012
1018
            del details[name]
1013
1019
 
 
1020
    def install_counter_hook(self, hooks, name, counter_name=None):
 
1021
        """Install a counting hook.
 
1022
 
 
1023
        Any hook can be counted as long as it doesn't need to return a value.
 
1024
 
 
1025
        :param hooks: Where the hook should be installed.
 
1026
 
 
1027
        :param name: The hook name that will be counted.
 
1028
 
 
1029
        :param counter_name: The counter identifier in ``_counters``, defaults
 
1030
            to ``name``.
 
1031
        """
 
1032
        _counters = self._counters # Avoid closing over self
 
1033
        if counter_name is None:
 
1034
            counter_name = name
 
1035
        if _counters.has_key(counter_name):
 
1036
            raise AssertionError('%s is already used as a counter name'
 
1037
                                  % (counter_name,))
 
1038
        _counters[counter_name] = 0
 
1039
        self.addDetail(counter_name, content.Content(content.UTF8_TEXT,
 
1040
            lambda: ['%d' % (_counters[counter_name],)]))
 
1041
        def increment_counter(*args, **kwargs):
 
1042
            _counters[counter_name] += 1
 
1043
        label = 'count %s calls' % (counter_name,)
 
1044
        hooks.install_named_hook(name, increment_counter, label)
 
1045
        self.addCleanup(hooks.uninstall_named_hook, name, label)
 
1046
 
 
1047
    def _install_config_stats_hooks(self):
 
1048
        """Install config hooks to count hook calls.
 
1049
 
 
1050
        """
 
1051
        for hook_name in ('get', 'set', 'remove', 'load', 'save'):
 
1052
            self.install_counter_hook(config.ConfigHooks, hook_name,
 
1053
                                       'config.%s' % (hook_name,))
 
1054
 
 
1055
        # The OldConfigHooks are private and need special handling to protect
 
1056
        # against recursive tests (tests that run other tests), so we just do
 
1057
        # manually what registering them into _builtin_known_hooks will provide
 
1058
        # us.
 
1059
        self.overrideAttr(config, 'OldConfigHooks', config._OldConfigHooks())
 
1060
        for hook_name in ('get', 'set', 'remove', 'load', 'save'):
 
1061
            self.install_counter_hook(config.OldConfigHooks, hook_name,
 
1062
                                      'old_config.%s' % (hook_name,))
 
1063
 
1014
1064
    def _clear_debug_flags(self):
1015
1065
        """Prevent externally set debug flags affecting tests.
1016
1066
 
3514
3564
#                           with proper exclusion rules.
3515
3565
#   -Ethreads               Will display thread ident at creation/join time to
3516
3566
#                           help track thread leaks
 
3567
 
 
3568
#   -Econfig_stats          Will collect statistics using addDetail
3517
3569
selftest_debug_flags = set()
3518
3570
 
3519
3571