Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Changeset 3169

Show
Ignore:
Timestamp:
11/23/05 21:31:51 (3 years ago)
Author:
bitsweat
Message:

Enable warnings on first load only. File which are loaded but raise an exception are not added to loaded set.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/activesupport/CHANGELOG

    r3135 r3169  
    33* Active Support is warnings-safe.  #1792 [Eric Hodel] 
    44 
    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] 
    66 
    77* 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  
     1require 'set' 
    12require File.dirname(__FILE__) + '/module_attribute_accessors' 
    23require File.dirname(__FILE__) + '/core_ext/load_error' 
     
    67  extend self 
    78 
    8   @@loaded = [ ] 
     9  # All files ever loaded. 
     10  mattr_accessor :history 
     11  self.history = Set.new 
     12 
     13  # All files currently loaded. 
    914  mattr_accessor :loaded 
    10  
    11   @@mechanism = :load 
     15  self.loaded = Set.new 
     16 
     17  # Should we load files or require them? 
    1218  mattr_accessor :mechanism 
    13    
     19  self.mechanism = :load 
     20 
    1421  def load? 
    1522    mechanism == :load 
    1623  end 
    17    
     24 
    1825  def depend_on(file_name, swallow_load_errors = false) 
    1926    unless loaded.include?(file_name) 
    20       loaded << file_name 
    21  
    2227      begin 
    2328        require_or_load(file_name) 
     
    3136    depend_on(file_name, true) 
    3237  end 
    33    
     38 
    3439  def clear 
    35     self.loaded = [ ] 
     40    loaded.clear 
    3641  end 
    3742 
    3843  def require_or_load(file_name) 
    39     file_name = "#{file_name}.rb" unless ! load? || file_name[-3..-1] == '.rb' 
    4044    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 
    4254    else 
    4355      require file_name 
    4456    end 
     57 
     58    # Record that we've seen this file. 
     59    loaded  << file_name 
     60    history << file_name 
    4561  end 
    4662 
  • trunk/activesupport/test/dependencies_test.rb

    r2901 r3169  
    88  end 
    99 
    10   def test_require_dependency 
     10  def test_tracking_loaded_files 
    1111    require_dependency(File.dirname(__FILE__) + "/dependencies/service_one") 
    1212    require_dependency(File.dirname(__FILE__) + "/dependencies/service_two") 
    1313    assert_equal 2, Dependencies.loaded.size 
    1414  end 
    15    
    16   def test_require_dependency_two_times 
     15 
     16  def test_tracking_identical_loaded_files 
    1717    require_dependency(File.dirname(__FILE__) + "/dependencies/service_one") 
    1818    require_dependency(File.dirname(__FILE__) + "/dependencies/service_one") 
     
    2020  end 
    2121 
    22   def test_require_missing_dependency 
     22  def test_missing_dependency_raises_missing_source_file 
    2323    assert_raises(MissingSourceFile) { require_dependency("missing_service") } 
    2424  end 
    25    
    26   def test_require_missing_association 
     25 
     26  def test_missing_association_raises_nothing 
    2727    assert_nothing_raised { require_association("missing_model") } 
    2828  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 
    2982end