Rev 2554: Give Hooks names. in sftp://rookery/~/public_html/baz2.0/commit-hook-names

Robert Collins robertc at robertcollins.net
Thu Jun 28 04:08:56 BST 2007


At sftp://rookery/~/public_html/baz2.0/commit-hook-names

------------------------------------------------------------
revno: 2554
revision-id: robertc at robertcollins.net-20070628030853-fjeb7ii5euyvi7c1
parent: pqm at pqm.ubuntu.com-20070626191000-flte155pupv54bcs
committer: Robert Collins <robertc at robertcollins.net>
branch nick: commit-hook-names
timestamp: Thu 2007-06-28 13:08:53 +1000
message:
  Give Hooks names.
added:
  bzrlib/tests/test_hooks.py     test_hooks.py-20070628030849-89rtsbe5dmer5npz-1
modified:
  NEWS                           NEWS-20050323055033-4e00b5db738777ff
  bzrlib/hooks.py                hooks.py-20070325015548-ix4np2q0kd8452au-1
  bzrlib/tests/__init__.py       selftest.py-20050531073622-8d0e3c8845c97a64
  bzrlib/tests/test_branch.py    test_branch.py-20060116013032-97819aa07b8ab3b5
=== added file 'bzrlib/tests/test_hooks.py'
--- a/bzrlib/tests/test_hooks.py	1970-01-01 00:00:00 +0000
+++ b/bzrlib/tests/test_hooks.py	2007-06-28 03:08:53 +0000
@@ -0,0 +1,55 @@
+# Copyright (C) 2007 Canonical Ltd
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+
+"""Tests for the core Hooks logic."""
+
+from bzrlib.hooks import (
+    Hooks,
+    )
+from bzrlib.errors import (
+    UnknownHook,
+    )
+
+from bzrlib.tests import TestCase
+
+
+class TestHooks(TestCase):
+
+    def test_install_hook_raises_unknown_hook(self):
+        """install_hook should raise UnknownHook if a hook is unknown."""
+        hooks = Hooks()
+        self.assertRaises(UnknownHook, hooks.install_hook, 'silly', None)
+
+    def test_install_hook_appends_known_hook(self):
+        """install_hook should append the callable for known hooks."""
+        hooks = Hooks()
+        hooks['set_rh'] = []
+        hooks.install_hook('set_rh', None)
+        self.assertEqual(hooks['set_rh'], [None])
+
+    def test_name_hook_and_retrieve_name(self):
+        """name_hook puts the name in the names mapping."""
+        hooks = Hooks()
+        hooks['set_rh'] = []
+        hooks.install_hook('set_rh', None)
+        hooks.name_hook(None, 'demo')
+        self.assertEqual("demo", hooks.get_hook_name(None))
+
+    def test_get_unnamed_hook_name_is_unnamed(self):
+        hooks = Hooks()
+        hooks['set_rh'] = []
+        hooks.install_hook('set_rh', None)
+        self.assertEqual("No hook name", hooks.get_hook_name(None))

=== modified file 'NEWS'
--- a/NEWS	2007-06-25 17:46:47 +0000
+++ b/NEWS	2007-06-28 03:08:53 +0000
@@ -50,6 +50,10 @@
 
     * New SMTPConnection class to unify email handling.  (Adeodato Simó)
 
+    * ``bzrlib`` Hooks are now nameable using ``Hooks.name_hook``. This 
+      allows a nicer UI when hooks are running as the current hook can
+      be displayed. (Robert Collins)
+
   TESTING:
 
     * Removed the ``--keep-output`` option from selftest and clean up test

=== modified file 'bzrlib/hooks.py'
--- a/bzrlib/hooks.py	2007-03-25 08:59:56 +0000
+++ b/bzrlib/hooks.py	2007-06-28 03:08:53 +0000
@@ -31,6 +31,17 @@
     FOO hook is triggered.
     """
 
+    def __init__(self):
+        dict.__init__(self)
+        self._callable_names = {}
+
+    def get_hook_name(self, a_callable):
+        """Get the name for a_callable for UI display.
+
+        If no name has been registered, the string 'No hook name' is returned.
+        """
+        return self._callable_names.get(a_callable, "No hook name")
+
     def install_hook(self, hook_name, a_callable):
         """Install a_callable in to the hook hook_name.
 
@@ -44,3 +55,7 @@
             self[hook_name].append(a_callable)
         except KeyError:
             raise errors.UnknownHook(self.__class__.__name__, hook_name)
+
+    def name_hook(self, a_callable, name):
+        """Associate name with a_callable to show users what is running."""
+        self._callable_names[a_callable] = name

=== modified file 'bzrlib/tests/__init__.py'
--- a/bzrlib/tests/__init__.py	2007-06-19 16:11:26 +0000
+++ b/bzrlib/tests/__init__.py	2007-06-28 03:08:53 +0000
@@ -2253,6 +2253,7 @@
                    'bzrlib.tests.test_graph',
                    'bzrlib.tests.test_hashcache',
                    'bzrlib.tests.test_help',
+                   'bzrlib.tests.test_hooks',
                    'bzrlib.tests.test_http',
                    'bzrlib.tests.test_http_response',
                    'bzrlib.tests.test_https_ca_bundle',

=== modified file 'bzrlib/tests/test_branch.py'
--- a/bzrlib/tests/test_branch.py	2007-04-10 15:54:15 +0000
+++ b/bzrlib/tests/test_branch.py	2007-06-28 03:08:53 +0000
@@ -326,17 +326,6 @@
         # the installed hooks are saved in self._preserved_hooks.
         self.assertIsInstance(self._preserved_hooks[_mod_branch.Branch], BranchHooks)
 
-    def test_install_hook_raises_unknown_hook(self):
-        """install_hook should raise UnknownHook if a hook is unknown."""
-        hooks = BranchHooks()
-        self.assertRaises(UnknownHook, hooks.install_hook, 'silly', None)
-
-    def test_install_hook_appends_known_hook(self):
-        """install_hook should append the callable for known hooks."""
-        hooks = BranchHooks()
-        hooks.install_hook('set_rh', None)
-        self.assertEqual(hooks['set_rh'], [None])
-
 
 class TestPullResult(TestCase):
 



More information about the bazaar-commits mailing list