Changeset 3169
- Timestamp:
- 11/23/05 21:31:51 (3 years ago)
- Files:
-
- trunk/activesupport/CHANGELOG (modified) (1 diff)
- trunk/activesupport/lib/active_support/dependencies.rb (modified) (3 diffs)
- trunk/activesupport/test/dependencies_test.rb (modified) (2 diffs)
- trunk/activesupport/test/dependencies/check_warnings.rb (added)
- trunk/activesupport/test/dependencies/raises_exception.rb (added)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/activesupport/CHANGELOG
r3135 r3169 3 3 * Active Support is warnings-safe. #1792 [Eric Hodel] 4 4 5 * Introduce enable_warnings counterpart to silence_warnings. Turn warnings on when loading a file if Dependencies.mechanism == :load. Common mistakes such as redefined methods will print warnings to stderr. [Jeremy Kemper]5 * Introduce enable_warnings counterpart to silence_warnings. Turn warnings on when loading a file for the first time if Dependencies.mechanism == :load. Common mistakes such as redefined methods will print warnings to stderr. [Jeremy Kemper] 6 6 7 7 * Add Symbol#to_proc, which allows for, e.g. [:foo, :bar].map(&:to_s). [Marcel Molina Jr.] trunk/activesupport/lib/active_support/dependencies.rb
r3134 r3169 1 require 'set' 1 2 require File.dirname(__FILE__) + '/module_attribute_accessors' 2 3 require File.dirname(__FILE__) + '/core_ext/load_error' … … 6 7 extend self 7 8 8 @@loaded = [ ] 9 # All files ever loaded. 10 mattr_accessor :history 11 self.history = Set.new 12 13 # All files currently loaded. 9 14 mattr_accessor :loaded 10 11 @@mechanism = :load 15 self.loaded = Set.new 16 17 # Should we load files or require them? 12 18 mattr_accessor :mechanism 13 19 self.mechanism = :load 20 14 21 def load? 15 22 mechanism == :load 16 23 end 17 24 18 25 def depend_on(file_name, swallow_load_errors = false) 19 26 unless loaded.include?(file_name) 20 loaded << file_name21 22 27 begin 23 28 require_or_load(file_name) … … 31 36 depend_on(file_name, true) 32 37 end 33 38 34 39 def clear 35 self.loaded = [ ]40 loaded.clear 36 41 end 37 42 38 43 def require_or_load(file_name) 39 file_name = "#{file_name}.rb" unless ! load? || file_name[-3..-1] == '.rb'40 44 if load? 41 enable_warnings { load file_name } 45 # Append .rb if we have a bare file name. 46 load_file_name = "#{file_name}.rb" unless file_name[-3..-1] == '.rb' 47 48 # Enable warnings iff this file has not been loaded before. 49 if history.include?(file_name) 50 load load_file_name 51 else 52 enable_warnings { load load_file_name } 53 end 42 54 else 43 55 require file_name 44 56 end 57 58 # Record that we've seen this file. 59 loaded << file_name 60 history << file_name 45 61 end 46 62 trunk/activesupport/test/dependencies_test.rb
r2901 r3169 8 8 end 9 9 10 def test_ require_dependency10 def test_tracking_loaded_files 11 11 require_dependency(File.dirname(__FILE__) + "/dependencies/service_one") 12 12 require_dependency(File.dirname(__FILE__) + "/dependencies/service_two") 13 13 assert_equal 2, Dependencies.loaded.size 14 14 end 15 16 def test_ require_dependency_two_times15 16 def test_tracking_identical_loaded_files 17 17 require_dependency(File.dirname(__FILE__) + "/dependencies/service_one") 18 18 require_dependency(File.dirname(__FILE__) + "/dependencies/service_one") … … 20 20 end 21 21 22 def test_ require_missing_dependency22 def test_missing_dependency_raises_missing_source_file 23 23 assert_raises(MissingSourceFile) { require_dependency("missing_service") } 24 24 end 25 26 def test_ require_missing_association25 26 def test_missing_association_raises_nothing 27 27 assert_nothing_raised { require_association("missing_model") } 28 28 end 29 30 def test_dependency_which_raises_exception_isnt_added_to_loaded_set 31 old_mechanism, Dependencies.mechanism = Dependencies.mechanism, :load 32 33 filename = "#{File.dirname(__FILE__)}/dependencies/raises_exception" 34 $raises_exception_load_count = 0 35 36 5.times do |count| 37 assert_raises(RuntimeError) { require_dependency filename } 38 assert_equal count + 1, $raises_exception_load_count 39 40 assert !Dependencies.loaded.include?(filename) 41 assert !Dependencies.history.include?(filename) 42 end 43 ensure 44 Dependencies.mechanism = old_mechanism 45 end 46 47 def test_warnings_should_be_enabled_on_first_load 48 old_mechanism, Dependencies.mechanism = Dependencies.mechanism, :load 49 50 filename = "#{File.dirname(__FILE__)}/dependencies/check_warnings" 51 $check_warnings_load_count = 0 52 53 assert !Dependencies.loaded.include?(filename) 54 assert !Dependencies.history.include?(filename) 55 56 silence_warnings { require_dependency filename } 57 assert_equal 1, $check_warnings_load_count 58 assert_equal true, $checked_verbose, 'On first load warnings should be enabled.' 59 60 assert Dependencies.loaded.include?(filename) 61 Dependencies.clear 62 assert !Dependencies.loaded.include?(filename) 63 assert Dependencies.history.include?(filename) 64 65 silence_warnings { require_dependency filename } 66 assert_equal 2, $check_warnings_load_count 67 assert_equal nil, $checked_verbose, 'After first load warnings should be left alone.' 68 69 assert Dependencies.loaded.include?(filename) 70 Dependencies.clear 71 assert !Dependencies.loaded.include?(filename) 72 assert Dependencies.history.include?(filename) 73 74 enable_warnings { require_dependency filename } 75 assert_equal 3, $check_warnings_load_count 76 assert_equal true, $checked_verbose, 'After first load warnings should be left alone.' 77 78 assert Dependencies.loaded.include?(filename) 79 ensure 80 Dependencies.mechanism = old_mechanism 81 end 29 82 end