Skip to content

Commit

Permalink
Only track new constant definitions when we're reloading dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremy committed Nov 11, 2008
1 parent 278b6cd commit cbb38bb
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
12 changes: 10 additions & 2 deletions activesupport/lib/active_support/dependencies.rb
Original file line number Diff line number Diff line change
Expand Up @@ -138,14 +138,22 @@ def require_association(file_name)
end

def load_with_new_constant_marking(file, *extras) #:nodoc:
Dependencies.new_constants_in(Object) { load_without_new_constant_marking(file, *extras) }
if Dependencies.load?
Dependencies.new_constants_in(Object) { load_without_new_constant_marking(file, *extras) }
else
load_without_new_constant_marking(file, *extras)
end
rescue Exception => exception # errors from loading file
exception.blame_file! file
raise
end

def require(file, *extras) #:nodoc:
Dependencies.new_constants_in(Object) { super }
if Dependencies.load?
Dependencies.new_constants_in(Object) { super }
else
super
end
rescue Exception => exception # errors from required file
exception.blame_file! file
raise
Expand Down
10 changes: 5 additions & 5 deletions activesupport/test/dependencies_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -694,27 +694,27 @@ def test_autoload_doesnt_shadow_error_when_mechanism_not_set_to_load
with_loading 'autoloading_fixtures' do
ActiveSupport::Dependencies.mechanism = :require
2.times do
assert_raise(NameError) {"RaisesNameError".constantize}
assert_raise(NameError) { assert_equal 123, ::RaisesNameError::FooBarBaz }
end
end
end

def test_autoload_doesnt_shadow_name_error
with_loading 'autoloading_fixtures' do
assert !defined?(::RaisesNameError), "::RaisesNameError is defined but it hasn't been referenced yet!"
Object.send(:remove_const, :RaisesNameError) if defined?(::RaisesNameError)
2.times do
begin
::RaisesNameError.object_id
::RaisesNameError::FooBarBaz.object_id
flunk 'should have raised NameError when autoloaded file referenced FooBarBaz'
rescue NameError => e
assert_equal 'uninitialized constant RaisesNameError::FooBarBaz', e.message
end
assert !defined?(::RaisesNameError), "::RaisesNameError is defined but it should have failed!"
end

assert !defined?(RaisesNameError)
assert !defined?(::RaisesNameError)
2.times do
assert_raise(NameError) { RaisesNameError }
assert_raise(NameError) { ::RaisesNameError }
assert !defined?(::RaisesNameError), "::RaisesNameError is defined but it should have failed!"
end
end
Expand Down

0 comments on commit cbb38bb

Please sign in to comment.