Skip to content
This repository
Browse code

Fixed Dependencies so load errors are not masked behind a 'Expected x…

….rb to define X' message when mechanism is not set to :load [#87 state:resolved]

Signed-off-by: Joshua Peek <josh@joshpeek.com>
  • Loading branch information...
commit 77e45352e7e947f1df1bfb8fe7d9e4e133224dd9 1 parent f701533
Tom Ward authored May 31, 2008 josh committed May 31, 2008
19  activesupport/lib/active_support/dependencies.rb
@@ -82,9 +82,10 @@ def require_or_load(file_name, const_path = nil)
82 82
     # infinite loop with mutual dependencies.
83 83
     loaded << expanded
84 84
 
85  
-    if load?
86  
-      log "loading #{file_name}"
87  
-      begin
  85
+    begin
  86
+      if load?
  87
+        log "loading #{file_name}"
  88
+
88 89
         # Enable warnings iff this file has not been loaded before and
89 90
         # warnings_on_first_load is set.
90 91
         load_args = ["#{file_name}.rb"]
@@ -95,13 +96,13 @@ def require_or_load(file_name, const_path = nil)
95 96
         else
96 97
           enable_warnings { result = load_file(*load_args) }
97 98
         end
98  
-      rescue Exception
99  
-        loaded.delete expanded
100  
-        raise
  99
+      else
  100
+        log "requiring #{file_name}"
  101
+        result = require file_name
101 102
       end
102  
-    else
103  
-      log "requiring #{file_name}"
104  
-      result = require file_name
  103
+    rescue Exception
  104
+      loaded.delete expanded
  105
+      raise
105 106
     end
106 107
 
107 108
     # Record history *after* loading so first load gets warnings.
20  activesupport/test/dependencies_test.rb
@@ -673,7 +673,7 @@ def test_autoload_doesnt_shadow_no_method_error_with_relative_constant
673 673
         assert !defined?(::RaisesNoMethodError), "::RaisesNoMethodError is defined but it should have failed!"
674 674
       end
675 675
     end
676  
-  
  676
+
677 677
   ensure
678 678
     Object.class_eval { remove_const :RaisesNoMethodError if const_defined?(:RaisesNoMethodError) }
679 679
   end
@@ -686,11 +686,20 @@ def test_autoload_doesnt_shadow_no_method_error_with_absolute_constant
686 686
         assert !defined?(::RaisesNoMethodError), "::RaisesNoMethodError is defined but it should have failed!"
687 687
       end
688 688
     end
689  
-  
  689
+
690 690
   ensure
691 691
     Object.class_eval { remove_const :RaisesNoMethodError if const_defined?(:RaisesNoMethodError) }
692 692
   end
693 693
 
  694
+  def test_autoload_doesnt_shadow_error_when_mechanism_not_set_to_load
  695
+    with_loading 'autoloading_fixtures' do
  696
+      Dependencies.mechanism = :require
  697
+      2.times do
  698
+        assert_raise(NameError) {"RaisesNameError".constantize}
  699
+      end
  700
+    end
  701
+  end
  702
+
694 703
   def test_autoload_doesnt_shadow_name_error
695 704
     with_loading 'autoloading_fixtures' do
696 705
       assert !defined?(::RaisesNameError), "::RaisesNameError is defined but it hasn't been referenced yet!"
@@ -714,7 +723,7 @@ def test_autoload_doesnt_shadow_name_error
714 723
   ensure
715 724
     Object.class_eval { remove_const :RaisesNoMethodError if const_defined?(:RaisesNoMethodError) }
716 725
   end
717  
-  
  726
+
718 727
   def test_remove_constant_handles_double_colon_at_start
719 728
     Object.const_set 'DeleteMe', Module.new
720 729
     DeleteMe.const_set 'OrMe', Module.new
@@ -724,7 +733,7 @@ def test_remove_constant_handles_double_colon_at_start
724 733
     Dependencies.remove_constant "::DeleteMe"
725 734
     assert ! defined?(DeleteMe)
726 735
   end
727  
-  
  736
+
728 737
   def test_load_once_constants_should_not_be_unloaded
729 738
     with_loading 'autoloading_fixtures' do
730 739
       Dependencies.load_once_paths = Dependencies.load_paths
@@ -737,7 +746,7 @@ def test_load_once_constants_should_not_be_unloaded
737 746
     Dependencies.load_once_paths = []
738 747
     Object.class_eval { remove_const :A if const_defined?(:A) }
739 748
   end
740  
-  
  749
+
741 750
   def test_load_once_paths_should_behave_when_recursively_loading
742 751
     with_loading 'dependencies', 'autoloading_fixtures' do
743 752
       Dependencies.load_once_paths = [Dependencies.load_paths.last]
@@ -753,5 +762,4 @@ def test_load_once_paths_should_behave_when_recursively_loading
753 762
   ensure
754 763
     Dependencies.load_once_paths = []
755 764
   end
756  
-  
757 765
 end

0 notes on commit 77e4535

Please sign in to comment.
Something went wrong with that request. Please try again.