Index: includes/module.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/module.inc,v
retrieving revision 1.206
diff -u -p -r1.206 module.inc
--- includes/module.inc	21 Nov 2010 10:19:48 -0000	1.206
+++ includes/module.inc	24 Nov 2010 01:46:29 -0000
@@ -595,6 +595,32 @@ function module_hook($module, $hook) {
 }
 
 /**
+ * Ensure that a hook implementation is loaded and available to call.
+ *
+ * If the hook is defined in hook_hook_info(), the appropriate include file
+ * will be loaded.
+ *
+ * @param $module
+ *   The name of the module (without the .module extension).
+ * @param $hook
+ *   The name of the hook (e.g. "help" or "menu").
+ *
+ * @return
+ *   The function name of the hook if it is defined, or FALSE if the hook
+ *   implementation does not exist.
+ */
+function module_load_hook($module, $hook) {
+  $function = $module . '_' . $hook;
+  $hook_info = module_hook_info();
+  if (isset($hook_info[$hook]['group'])) {
+    $group = $hook_info[$hook]['group'];
+    module_load_include('inc', $module, "$module.$group");
+  }
+
+  return module_hook($module, $hook) ? $function : FALSE;
+}
+
+/**
  * Determine which modules are implementing a hook.
  *
  * @param $hook
@@ -695,11 +721,12 @@ function module_implements($hook, $sort 
  * Retrieve a list of what hooks are explicitly declared.
  */
 function module_hook_info() {
-  $hook_info = &drupal_static(__FUNCTION__, array());
+  $static_cache = &drupal_static(__FUNCTION__, array());
 
-  if (empty($hook_info)) {
+  if (empty($static_cache)) {
     $cache = cache_get('hook_info', 'cache_bootstrap');
     if ($cache === FALSE) {
+      $hook_info = array();
       // Rebuild the cache and save it.
       // We can't use module_invoke_all() here or it would cause an infinite
       // loop.
@@ -719,12 +746,19 @@ function module_hook_info() {
           $function($hook_info);
         }
       }
-      cache_set('hook_info', $hook_info, 'cache_bootstrap');
+      // Only set the cache when all modules are loaded and we aren't in a test.
+      if (drupal_bootstrap(NULL, FALSE) == DRUPAL_BOOTSTRAP_FULL && empty($GLOBALS['drupal_test_info'])) {
+        cache_set('hook_info', $hook_info, 'cache_bootstrap');
+      }
+      $static_cache = $hook_info;
     }
     else {
-      $hook_info = $cache->data;
+      $hook_info = $static_cache = $cache->data;
     }
   }
+  else {
+    $hook_info = $static_cache;
+  }
 
   return $hook_info;
 }
@@ -763,10 +797,11 @@ function module_invoke() {
   $module = $args[0];
   $hook = $args[1];
   unset($args[0], $args[1]);
-  if (module_hook($module, $hook)) {
-    return call_user_func_array($module . '_' . $hook, $args);
+  if ($function = module_load_hook($module, $hook)) {
+    return call_user_func_array($function, $args);
   }
 }
+
 /**
  * Invoke a hook in all enabled modules that implement it.
  *
Index: modules/simpletest/tests/module.test
===================================================================
RCS file: /cvs/drupal/drupal/modules/simpletest/tests/module.test,v
retrieving revision 1.24
diff -u -p -r1.24 module.test
--- modules/simpletest/tests/module.test	20 Nov 2010 03:34:30 -0000	1.24
+++ modules/simpletest/tests/module.test	24 Nov 2010 01:46:30 -0000
@@ -112,6 +112,26 @@ class ModuleUnitTest extends DrupalWebTe
   }
 
   /**
+   * Test that module_invoke() can load a hook defined in hook_hook_info().
+   */
+  function testModuleInvokeDynamicLoading() {
+    module_enable(array('module_test'), FALSE);
+    $this->resetAll();
+    $this->drupalGet('module-test/hook-dynamic-loading-invoke');
+    $this->assertText('success!', t('module_invoke() dynamically loads a hook defined in hook_hook_info().'));
+  }
+
+  /**
+   * Test that module_invoke_all() can load a hook defined in hook_hook_info().
+   */
+  function testModuleInvokeAllDynamicLoading() {
+    module_enable(array('module_test'), FALSE);
+    $this->resetAll();
+    $this->drupalGet('module-test/hook-dynamic-loading-invoke-all');
+    $this->assertText('success!', t('module_invoke_all() dynamically loads a hook defined in hook_hook_info().'));
+  }
+
+  /**
    * Test dependency resolution.
    */
   function testDependencyResolution() {
Index: modules/simpletest/tests/module_test.file.inc
===================================================================
RCS file: /cvs/drupal/drupal/modules/simpletest/tests/module_test.file.inc,v
retrieving revision 1.2
diff -u -p -r1.2 module_test.file.inc
--- modules/simpletest/tests/module_test.file.inc	26 May 2010 19:51:01 -0000	1.2
+++ modules/simpletest/tests/module_test.file.inc	24 Nov 2010 01:46:30 -0000
@@ -10,5 +10,5 @@
  * Implements hook_test_hook().
  */
 function module_test_test_hook() {
-
+  return array('module_test' => 'success!');
 }
Index: modules/simpletest/tests/module_test.module
===================================================================
RCS file: /cvs/drupal/drupal/modules/simpletest/tests/module_test.module,v
retrieving revision 1.9
diff -u -p -r1.9 module_test.module
--- modules/simpletest/tests/module_test.module	20 Nov 2010 03:34:30 -0000	1.9
+++ modules/simpletest/tests/module_test.module	24 Nov 2010 01:46:30 -0000
@@ -52,6 +52,45 @@ function module_test_hook_info() {
 }
 
 /**
+ * Implements hook_menu().
+ */
+function module_test_menu() {
+  $items['module-test/hook-dynamic-loading-invoke'] = array(
+    'title' => 'Test hook dynamic loading (invoke)',
+    'page callback' => 'module_test_hook_dynamic_loading_invoke',
+    'access arguments' => array('access content'),
+  );
+  $items['module-test/hook-dynamic-loading-invoke-all'] = array(
+    'title' => 'Test hook dynamic loading (invoke_all)',
+    'page callback' => 'module_test_hook_dynamic_loading_invoke_all',
+    'access arguments' => array('access content'),
+  );
+  return $items;
+}
+
+/**
+ * Page callback for 'hook dynamic loading' test.
+ *
+ * If the hook is dynamically loaded correctly, the menu callback should
+ * return 'success!'.
+ */
+function module_test_hook_dynamic_loading_invoke() {
+  $result = module_invoke('module_test', 'test_hook');
+  return $result['module_test'];
+}
+
+/**
+ * Page callback for 'hook dynamic loading' test.
+ *
+ * If the hook is dynamically loaded correctly, the menu callback should
+ * return 'success!'.
+ */
+function module_test_hook_dynamic_loading_invoke_all() {
+  $result = module_invoke_all('test_hook');
+  return $result['module_test'];
+}
+
+/**
  * Implements hook_modules_enabled().
  */
 function module_test_modules_enabled($modules) {
