mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
prevent Dependencies#remove_const from autoloading parents [fixes #8301]
This commit is contained in:
parent
5aade8245a
commit
46ebce6b49
2 changed files with 54 additions and 32 deletions
|
@ -646,9 +646,22 @@ module ActiveSupport #:nodoc:
|
|||
|
||||
constants = normalized.split('::')
|
||||
to_remove = constants.pop
|
||||
parent_name = constants.empty? ? 'Object' : constants.join('::')
|
||||
|
||||
if parent = safe_constantize(parent_name)
|
||||
if constants.empty?
|
||||
parent = Object
|
||||
else
|
||||
# This method is robust to non-reachable constants.
|
||||
#
|
||||
# Non-reachable constants may be passed if some of the parents were
|
||||
# autoloaded and already removed. It is easier to do a sanity check
|
||||
# here than require the caller to be clever. We check the parent
|
||||
# rather than the very const argument because we do not want to
|
||||
# trigger Kernel#autoloads, see the comment below.
|
||||
parent_name = constants.join('::')
|
||||
return unless qualified_const_defined?(parent_name)
|
||||
parent = constantize(parent_name)
|
||||
end
|
||||
|
||||
log "removing constant #{const}"
|
||||
|
||||
# In an autoloaded user.rb like this
|
||||
|
@ -684,7 +697,6 @@ module ActiveSupport #:nodoc:
|
|||
log "the constant #{const} is not reachable anymore, skipping"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
protected
|
||||
def log_call(*args)
|
||||
|
|
|
@ -938,6 +938,16 @@ class DependenciesTest < ActiveSupport::TestCase
|
|||
assert !defined?(ShouldNotBeAutoloaded)
|
||||
end
|
||||
|
||||
def test_remove_constant_does_not_autoload_already_removed_parents_as_a_side_effect
|
||||
with_autoloading_fixtures do
|
||||
::A
|
||||
::A::B
|
||||
ActiveSupport::Dependencies.remove_constant('A')
|
||||
ActiveSupport::Dependencies.remove_constant('A::B')
|
||||
assert !defined?(A)
|
||||
end
|
||||
end
|
||||
|
||||
def test_load_once_constants_should_not_be_unloaded
|
||||
with_autoloading_fixtures do
|
||||
ActiveSupport::Dependencies.autoload_once_paths = ActiveSupport::Dependencies.autoload_paths
|
||||
|
|
Loading…
Reference in a new issue