1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Merge pull request #15956 from zuhao/refactor_activesupport_dependencies_test

Cleanup loaded features and constants after dependency tests.
This commit is contained in:
Yves Senn 2014-11-05 15:20:26 +01:00
commit 00ae750b23

View file

@ -16,11 +16,16 @@ module ModuleWithConstant
end
class DependenciesTest < ActiveSupport::TestCase
def teardown
ActiveSupport::Dependencies.clear
include DependenciesTestHelpers
setup do
@loaded_features_copy = $LOADED_FEATURES.dup
end
include DependenciesTestHelpers
teardown do
ActiveSupport::Dependencies.clear
$LOADED_FEATURES.replace(@loaded_features_copy)
end
def test_depend_on_path
skip "LoadError#path does not exist" if RUBY_VERSION < '2.0.0'
@ -53,8 +58,7 @@ class DependenciesTest < ActiveSupport::TestCase
assert_equal 2, ActiveSupport::Dependencies.loaded.size
end
ensure
Object.send(:remove_const, :ServiceOne) if Object.const_defined?(:ServiceOne)
Object.send(:remove_const, :ServiceTwo) if Object.const_defined?(:ServiceTwo)
remove_constants(:ServiceOne, :ServiceTwo)
end
def test_tracking_identical_loaded_files
@ -64,7 +68,7 @@ class DependenciesTest < ActiveSupport::TestCase
assert_equal 1, ActiveSupport::Dependencies.loaded.size
end
ensure
Object.send(:remove_const, :ServiceOne) if Object.const_defined?(:ServiceOne)
remove_constants(:ServiceOne)
end
def test_missing_dependency_raises_missing_source_file
@ -84,8 +88,8 @@ class DependenciesTest < ActiveSupport::TestCase
assert_equal 'Loading me failed, so do not add to loaded or history.', e.message
assert_equal count + 1, $raises_exception_load_count
assert !ActiveSupport::Dependencies.loaded.include?(filename)
assert !ActiveSupport::Dependencies.history.include?(filename)
assert_not ActiveSupport::Dependencies.loaded.include?(filename)
assert_not ActiveSupport::Dependencies.history.include?(filename)
end
end
end
@ -93,7 +97,6 @@ class DependenciesTest < ActiveSupport::TestCase
def test_dependency_which_raises_doesnt_blindly_call_blame_file!
with_loading do
filename = 'dependencies/raises_exception_without_blame_file'
assert_raises(Exception) { require_dependency filename }
end
end
@ -101,13 +104,12 @@ class DependenciesTest < ActiveSupport::TestCase
def test_warnings_should_be_enabled_on_first_load
with_loading 'dependencies' do
old_warnings, ActiveSupport::Dependencies.warnings_on_first_load = ActiveSupport::Dependencies.warnings_on_first_load, true
filename = "check_warnings"
expanded = File.expand_path("#{File.dirname(__FILE__)}/dependencies/#{filename}")
$check_warnings_load_count = 0
assert !ActiveSupport::Dependencies.loaded.include?(expanded)
assert !ActiveSupport::Dependencies.history.include?(expanded)
assert_not ActiveSupport::Dependencies.loaded.include?(expanded)
assert_not ActiveSupport::Dependencies.history.include?(expanded)
silence_warnings { require_dependency filename }
assert_equal 1, $check_warnings_load_count
@ -115,7 +117,7 @@ class DependenciesTest < ActiveSupport::TestCase
assert ActiveSupport::Dependencies.loaded.include?(expanded)
ActiveSupport::Dependencies.clear
assert !ActiveSupport::Dependencies.loaded.include?(expanded)
assert_not ActiveSupport::Dependencies.loaded.include?(expanded)
assert ActiveSupport::Dependencies.history.include?(expanded)
silence_warnings { require_dependency filename }
@ -124,7 +126,7 @@ class DependenciesTest < ActiveSupport::TestCase
assert ActiveSupport::Dependencies.loaded.include?(expanded)
ActiveSupport::Dependencies.clear
assert !ActiveSupport::Dependencies.loaded.include?(expanded)
assert_not ActiveSupport::Dependencies.loaded.include?(expanded)
assert ActiveSupport::Dependencies.history.include?(expanded)
enable_warnings { require_dependency filename }
@ -203,43 +205,49 @@ class DependenciesTest < ActiveSupport::TestCase
def test_directories_manifest_as_modules_unless_const_defined
with_autoloading_fixtures do
assert_kind_of Module, ModuleFolder
Object.__send__ :remove_const, :ModuleFolder
end
ensure
remove_constants(:ModuleFolder)
end
def test_module_with_nested_class
with_autoloading_fixtures do
assert_kind_of Class, ModuleFolder::NestedClass
Object.__send__ :remove_const, :ModuleFolder
end
ensure
remove_constants(:ModuleFolder)
end
def test_module_with_nested_inline_class
with_autoloading_fixtures do
assert_kind_of Class, ModuleFolder::InlineClass
Object.__send__ :remove_const, :ModuleFolder
end
ensure
remove_constants(:ModuleFolder)
end
def test_directories_may_manifest_as_nested_classes
with_autoloading_fixtures do
assert_kind_of Class, ClassFolder
Object.__send__ :remove_const, :ClassFolder
end
ensure
remove_constants(:ClassFolder)
end
def test_class_with_nested_class
with_autoloading_fixtures do
assert_kind_of Class, ClassFolder::NestedClass
Object.__send__ :remove_const, :ClassFolder
end
ensure
remove_constants(:ClassFolder)
end
def test_class_with_nested_inline_class
with_autoloading_fixtures do
assert_kind_of Class, ClassFolder::InlineClass
Object.__send__ :remove_const, :ClassFolder
end
ensure
remove_constants(:ClassFolder)
end
def test_class_with_nested_inline_subclass_of_parent
@ -247,8 +255,9 @@ class DependenciesTest < ActiveSupport::TestCase
assert_kind_of Class, ClassFolder::ClassFolderSubclass
assert_kind_of Class, ClassFolder
assert_equal 'indeed', ClassFolder::ClassFolderSubclass::ConstantInClassFolder
Object.__send__ :remove_const, :ClassFolder
end
ensure
remove_constants(:ClassFolder)
end
def test_nested_class_can_access_sibling
@ -256,16 +265,15 @@ class DependenciesTest < ActiveSupport::TestCase
sibling = ModuleFolder::NestedClass.class_eval "NestedSibling"
assert defined?(ModuleFolder::NestedSibling)
assert_equal ModuleFolder::NestedSibling, sibling
Object.__send__ :remove_const, :ModuleFolder
end
ensure
remove_constants(:ModuleFolder)
end
def test_doesnt_break_normal_require
path = File.expand_path("../autoloading_fixtures/load_path", __FILE__)
original_path = $:.dup
original_features = $".dup
$:.push(path)
with_autoloading_fixtures do
# The _ = assignments are to prevent warnings
_ = RequiresConstant
@ -277,15 +285,13 @@ class DependenciesTest < ActiveSupport::TestCase
assert defined?(LoadedConstant)
end
ensure
remove_constants(:RequiresConstant, :LoadedConstant, :LoadsConstant)
$".replace(original_features)
remove_constants(:RequiresConstant, :LoadedConstant)
$:.replace(original_path)
end
def test_doesnt_break_normal_require_nested
path = File.expand_path("../autoloading_fixtures/load_path", __FILE__)
original_path = $:.dup
original_features = $".dup
$:.push(path)
with_autoloading_fixtures do
@ -300,14 +306,12 @@ class DependenciesTest < ActiveSupport::TestCase
end
ensure
remove_constants(:RequiresConstant, :LoadedConstant, :LoadsConstant)
$".replace(original_features)
$:.replace(original_path)
end
def test_require_returns_true_when_file_not_yet_required
path = File.expand_path("../autoloading_fixtures/load_path", __FILE__)
original_path = $:.dup
original_features = $".dup
$:.push(path)
with_loading do
@ -315,14 +319,12 @@ class DependenciesTest < ActiveSupport::TestCase
end
ensure
remove_constants(:LoadedConstant)
$".replace(original_features)
$:.replace(original_path)
end
def test_require_returns_true_when_file_not_yet_required_even_when_no_new_constants_added
path = File.expand_path("../autoloading_fixtures/load_path", __FILE__)
original_path = $:.dup
original_features = $".dup
$:.push(path)
with_loading do
@ -331,14 +333,12 @@ class DependenciesTest < ActiveSupport::TestCase
end
ensure
remove_constants(:LoadedConstant)
$".replace(original_features)
$:.replace(original_path)
end
def test_require_returns_false_when_file_already_required
path = File.expand_path("../autoloading_fixtures/load_path", __FILE__)
original_path = $:.dup
original_features = $".dup
$:.push(path)
with_loading do
@ -347,7 +347,6 @@ class DependenciesTest < ActiveSupport::TestCase
end
ensure
remove_constants(:LoadedConstant)
$".replace(original_features)
$:.replace(original_path)
end
@ -355,14 +354,11 @@ class DependenciesTest < ActiveSupport::TestCase
with_loading do
assert_raise(LoadError) { require 'this_file_dont_exist_dude' }
end
ensure
remove_constants(:LoadedConstant)
end
def test_load_returns_true_when_file_found
path = File.expand_path("../autoloading_fixtures/load_path", __FILE__)
original_path = $:.dup
original_features = $".dup
$:.push(path)
with_loading do
@ -371,7 +367,6 @@ class DependenciesTest < ActiveSupport::TestCase
end
ensure
remove_constants(:LoadedConstant)
$".replace(original_features)
$:.replace(original_path)
end
@ -379,17 +374,16 @@ class DependenciesTest < ActiveSupport::TestCase
with_loading do
assert_raise(LoadError) { load 'this_file_dont_exist_dude.rb' }
end
ensure
remove_constants(:LoadedConstant)
end
def failing_test_access_thru_and_upwards_fails
with_autoloading_fixtures do
assert ! defined?(ModuleFolder)
assert_not defined?(ModuleFolder)
assert_raise(NameError) { ModuleFolder::Object }
assert_raise(NameError) { ModuleFolder::NestedClass::Object }
Object.__send__ :remove_const, :ModuleFolder
end
ensure
remove_constants(:ModuleFolder)
end
def test_non_existing_const_raises_name_error_with_fully_qualified_name
@ -402,6 +396,8 @@ class DependenciesTest < ActiveSupport::TestCase
assert_equal "uninitialized constant A::B::DoesNotExist", e.message
assert_equal :DoesNotExist, e.name
end
ensure
remove_constants(:A)
end
def test_smart_name_error_strings
@ -491,9 +487,9 @@ class DependenciesTest < ActiveSupport::TestCase
nil_name = Module.new
def nil_name.name() nil end
assert !ActiveSupport::Dependencies.autoloaded?(nil_name)
Object.class_eval { remove_const :ModuleFolder }
end
ensure
remove_constants(:ModuleFolder)
end
def test_qualified_name_for
@ -551,6 +547,8 @@ class DependenciesTest < ActiveSupport::TestCase
assert_kind_of Module, ::ModuleWithCustomConstMissing::A
assert_kind_of String, ::ModuleWithCustomConstMissing::A::B
end
ensure
remove_constants(:ModuleWithCustomConstMissing)
end
def test_const_missing_in_anonymous_modules_loads_top_level_constants
@ -559,6 +557,8 @@ class DependenciesTest < ActiveSupport::TestCase
klass = Class.new.class_eval "E"
assert_equal E, klass
end
ensure
remove_constants(:E)
end
def test_const_missing_in_anonymous_modules_raises_if_the_constant_belongs_to_Object
@ -570,18 +570,22 @@ class DependenciesTest < ActiveSupport::TestCase
assert_equal 'E cannot be autoloaded from an anonymous class or module', e.message
assert_equal :E, e.name
end
ensure
remove_constants(:E)
end
def test_removal_from_tree_should_be_detected
with_loading 'dependencies' do
c = ServiceOne
ActiveSupport::Dependencies.clear
assert ! defined?(ServiceOne)
assert_not defined?(ServiceOne)
e = assert_raise ArgumentError do
ActiveSupport::Dependencies.load_missing_constant(c, :FakeMissing)
end
assert_match %r{ServiceOne has been removed from the module tree}i, e.message
end
ensure
remove_constants(:ServiceOne)
end
def test_references_should_work
@ -590,18 +594,21 @@ class DependenciesTest < ActiveSupport::TestCase
service_one_first = ServiceOne
assert_equal service_one_first, c.get("ServiceOne")
ActiveSupport::Dependencies.clear
assert ! defined?(ServiceOne)
assert_not defined?(ServiceOne)
service_one_second = ServiceOne
assert_not_equal service_one_first, c.get("ServiceOne")
assert_equal service_one_second, c.get("ServiceOne")
end
ensure
remove_constants(:ServiceOne)
end
def test_constantize_shortcut_for_cached_constant_lookups
with_loading 'dependencies' do
assert_equal ServiceOne, ActiveSupport::Dependencies.constantize("ServiceOne")
end
ensure
remove_constants(:ServiceOne)
end
def test_nested_load_error_isnt_rescued
@ -613,19 +620,20 @@ class DependenciesTest < ActiveSupport::TestCase
end
def test_autoload_once_paths_do_not_add_to_autoloaded_constants
old_path = ActiveSupport::Dependencies.autoload_once_paths
with_autoloading_fixtures do
ActiveSupport::Dependencies.autoload_once_paths = ActiveSupport::Dependencies.autoload_paths.dup
assert ! ActiveSupport::Dependencies.autoloaded?("ModuleFolder")
assert ! ActiveSupport::Dependencies.autoloaded?("ModuleFolder::NestedClass")
assert ! ActiveSupport::Dependencies.autoloaded?(ModuleFolder)
assert_not ActiveSupport::Dependencies.autoloaded?("ModuleFolder")
assert_not ActiveSupport::Dependencies.autoloaded?("ModuleFolder::NestedClass")
assert_not ActiveSupport::Dependencies.autoloaded?(ModuleFolder)
1 if ModuleFolder::NestedClass # 1 if to avoid warning
assert ! ActiveSupport::Dependencies.autoloaded?(ModuleFolder::NestedClass)
assert_not ActiveSupport::Dependencies.autoloaded?(ModuleFolder::NestedClass)
end
ensure
Object.class_eval { remove_const :ModuleFolder }
ActiveSupport::Dependencies.autoload_once_paths = []
remove_constants(:ModuleFolder)
ActiveSupport::Dependencies.autoload_once_paths = old_path
end
def test_autoload_once_pathnames_do_not_add_to_autoloaded_constants
@ -634,15 +642,15 @@ class DependenciesTest < ActiveSupport::TestCase
ActiveSupport::Dependencies.autoload_paths = pathnames
ActiveSupport::Dependencies.autoload_once_paths = pathnames
assert ! ActiveSupport::Dependencies.autoloaded?("ModuleFolder")
assert ! ActiveSupport::Dependencies.autoloaded?("ModuleFolder::NestedClass")
assert ! ActiveSupport::Dependencies.autoloaded?(ModuleFolder)
assert_not ActiveSupport::Dependencies.autoloaded?("ModuleFolder")
assert_not ActiveSupport::Dependencies.autoloaded?("ModuleFolder::NestedClass")
assert_not ActiveSupport::Dependencies.autoloaded?(ModuleFolder)
1 if ModuleFolder::NestedClass # 1 if to avoid warning
assert ! ActiveSupport::Dependencies.autoloaded?(ModuleFolder::NestedClass)
assert_not ActiveSupport::Dependencies.autoloaded?(ModuleFolder::NestedClass)
end
ensure
Object.class_eval { remove_const :ModuleFolder }
remove_constants(:ModuleFolder)
ActiveSupport::Dependencies.autoload_once_paths = []
end
@ -652,6 +660,8 @@ class DependenciesTest < ActiveSupport::TestCase
assert_equal 10, ApplicationController
assert ActiveSupport::Dependencies.autoloaded?(:ApplicationController)
end
ensure
remove_constants(:ApplicationController)
end
def test_preexisting_constants_are_not_marked_as_autoloaded
@ -667,9 +677,8 @@ class DependenciesTest < ActiveSupport::TestCase
assert ! ActiveSupport::Dependencies.autoloaded?(:E), "E shouldn't be marked autoloaded!"
ActiveSupport::Dependencies.clear
end
ensure
Object.class_eval { remove_const :E }
remove_constants(:E)
end
def test_constants_in_capitalized_nesting_marked_as_autoloaded
@ -678,6 +687,8 @@ class DependenciesTest < ActiveSupport::TestCase
assert ActiveSupport::Dependencies.autoloaded?("HTML::SomeClass")
end
ensure
remove_constants(:HTML)
end
def test_unloadable
@ -708,7 +719,7 @@ class DependenciesTest < ActiveSupport::TestCase
assert_equal false, M.unloadable
end
ensure
Object.class_eval { remove_const :M }
remove_constants(:M)
end
def test_unloadable_constants_should_receive_callback
@ -719,7 +730,7 @@ class DependenciesTest < ActiveSupport::TestCase
ActiveSupport::Dependencies.clear
assert !defined?(C)
ensure
Object.class_eval { remove_const :C } if defined?(C)
remove_constants(:C)
end
def test_new_contants_in_without_constants
@ -733,7 +744,7 @@ class DependenciesTest < ActiveSupport::TestCase
}.map(&:to_s)
assert ActiveSupport::Dependencies.constant_watch_stack.all? {|k,v| v.empty? }
ensure
Object.class_eval { remove_const :Hello }
remove_constants(:Hello)
end
def test_new_constants_in_with_nesting
@ -750,9 +761,7 @@ class DependenciesTest < ActiveSupport::TestCase
assert_equal ["OuterAfter", "OuterBefore"], outer.sort.map(&:to_s)
assert ActiveSupport::Dependencies.constant_watch_stack.all? {|k,v| v.empty? }
ensure
%w(OuterBefore Inner OuterAfter).each do |name|
Object.class_eval { remove_const name if const_defined?(name) }
end
remove_constants(:OuterBefore, :Inner, :OuterAfter)
end
def test_new_constants_in_module
@ -771,7 +780,7 @@ class DependenciesTest < ActiveSupport::TestCase
assert_equal ["M::OuterAfter", "M::OuterBefore"], outer.sort
assert ActiveSupport::Dependencies.constant_watch_stack.all? {|k,v| v.empty? }
ensure
Object.class_eval { remove_const :M }
remove_constants(:M)
end
def test_new_constants_in_module_using_name
@ -789,7 +798,7 @@ class DependenciesTest < ActiveSupport::TestCase
assert_equal ["M::OuterAfter", "M::OuterBefore"], outer.sort
assert ActiveSupport::Dependencies.constant_watch_stack.all? {|k,v| v.empty? }
ensure
Object.class_eval { remove_const :M }
remove_constants(:M)
end
def test_new_constants_in_with_inherited_constants
@ -807,26 +816,27 @@ class DependenciesTest < ActiveSupport::TestCase
def test_file_with_multiple_constants_and_require_dependency
with_autoloading_fixtures do
assert ! defined?(MultipleConstantFile)
assert ! defined?(SiblingConstant)
assert_not defined?(MultipleConstantFile)
assert_not defined?(SiblingConstant)
require_dependency 'multiple_constant_file'
assert defined?(MultipleConstantFile)
assert defined?(SiblingConstant)
assert ActiveSupport::Dependencies.autoloaded?(:MultipleConstantFile)
assert ActiveSupport::Dependencies.autoloaded?(:SiblingConstant)
ActiveSupport::Dependencies.clear
assert ! defined?(MultipleConstantFile)
assert ! defined?(SiblingConstant)
assert_not defined?(MultipleConstantFile)
assert_not defined?(SiblingConstant)
end
ensure
remove_constants(:MultipleConstantFile, :SiblingConstant)
end
def test_file_with_multiple_constants_and_auto_loading
with_autoloading_fixtures do
assert ! defined?(MultipleConstantFile)
assert ! defined?(SiblingConstant)
assert_not defined?(MultipleConstantFile)
assert_not defined?(SiblingConstant)
assert_equal 10, MultipleConstantFile
@ -837,15 +847,17 @@ class DependenciesTest < ActiveSupport::TestCase
ActiveSupport::Dependencies.clear
assert ! defined?(MultipleConstantFile)
assert ! defined?(SiblingConstant)
assert_not defined?(MultipleConstantFile)
assert_not defined?(SiblingConstant)
end
ensure
remove_constants(:MultipleConstantFile, :SiblingConstant)
end
def test_nested_file_with_multiple_constants_and_require_dependency
with_autoloading_fixtures do
assert ! defined?(ClassFolder::NestedClass)
assert ! defined?(ClassFolder::SiblingClass)
assert_not defined?(ClassFolder::NestedClass)
assert_not defined?(ClassFolder::SiblingClass)
require_dependency 'class_folder/nested_class'
@ -856,15 +868,17 @@ class DependenciesTest < ActiveSupport::TestCase
ActiveSupport::Dependencies.clear
assert ! defined?(ClassFolder::NestedClass)
assert ! defined?(ClassFolder::SiblingClass)
assert_not defined?(ClassFolder::NestedClass)
assert_not defined?(ClassFolder::SiblingClass)
end
ensure
remove_constants(:ClassFolder)
end
def test_nested_file_with_multiple_constants_and_auto_loading
with_autoloading_fixtures do
assert ! defined?(ClassFolder::NestedClass)
assert ! defined?(ClassFolder::SiblingClass)
assert_not defined?(ClassFolder::NestedClass)
assert_not defined?(ClassFolder::SiblingClass)
assert_kind_of Class, ClassFolder::NestedClass
@ -875,9 +889,11 @@ class DependenciesTest < ActiveSupport::TestCase
ActiveSupport::Dependencies.clear
assert ! defined?(ClassFolder::NestedClass)
assert ! defined?(ClassFolder::SiblingClass)
assert_not defined?(ClassFolder::NestedClass)
assert_not defined?(ClassFolder::SiblingClass)
end
ensure
remove_constants(:ClassFolder)
end
def test_autoload_doesnt_shadow_no_method_error_with_relative_constant
@ -888,9 +904,8 @@ class DependenciesTest < ActiveSupport::TestCase
assert !defined?(::RaisesNoMethodError), "::RaisesNoMethodError is defined but it should have failed!"
end
end
ensure
Object.class_eval { remove_const :RaisesNoMethodError if const_defined?(:RaisesNoMethodError) }
remove_constants(:RaisesNoMethodError)
end
def test_autoload_doesnt_shadow_no_method_error_with_absolute_constant
@ -901,9 +916,8 @@ class DependenciesTest < ActiveSupport::TestCase
assert !defined?(::RaisesNoMethodError), "::RaisesNoMethodError is defined but it should have failed!"
end
end
ensure
Object.class_eval { remove_const :RaisesNoMethodError if const_defined?(:RaisesNoMethodError) }
remove_constants(:RaisesNoMethodError)
end
def test_autoload_doesnt_shadow_error_when_mechanism_not_set_to_load
@ -919,7 +933,6 @@ class DependenciesTest < ActiveSupport::TestCase
def test_autoload_doesnt_shadow_name_error
with_autoloading_fixtures do
Object.send(:remove_const, :RaisesNameError) if defined?(::RaisesNameError)
2.times do
e = assert_raise NameError do
::RaisesNameError::FooBarBaz.object_id
@ -934,19 +947,20 @@ class DependenciesTest < ActiveSupport::TestCase
assert !defined?(::RaisesNameError), "::RaisesNameError is defined but it should have failed!"
end
end
ensure
Object.class_eval { remove_const :RaisesNoMethodError if const_defined?(:RaisesNoMethodError) }
remove_constants(:RaisesNameError)
end
def test_remove_constant_handles_double_colon_at_start
Object.const_set 'DeleteMe', Module.new
DeleteMe.const_set 'OrMe', Module.new
ActiveSupport::Dependencies.remove_constant "::DeleteMe::OrMe"
assert ! defined?(DeleteMe::OrMe)
assert_not defined?(DeleteMe::OrMe)
assert defined?(DeleteMe)
ActiveSupport::Dependencies.remove_constant "::DeleteMe"
assert ! defined?(DeleteMe)
assert_not defined?(DeleteMe)
ensure
remove_constants(:DeleteMe)
end
def test_remove_constant_does_not_trigger_loading_autoloads
@ -956,7 +970,9 @@ class DependenciesTest < ActiveSupport::TestCase
end
assert_nil ActiveSupport::Dependencies.remove_constant(constant), "Kernel#autoload has been triggered by remove_constant"
assert !defined?(ShouldNotBeAutoloaded)
assert_not defined?(ShouldNotBeAutoloaded)
ensure
remove_constants(constant)
end
def test_remove_constant_does_not_autoload_already_removed_parents_as_a_side_effect
@ -965,11 +981,14 @@ class DependenciesTest < ActiveSupport::TestCase
_ = ::A::B # assignment to silence parse-time warning "possibly useless use of :: in void context"
ActiveSupport::Dependencies.remove_constant('A')
ActiveSupport::Dependencies.remove_constant('A::B')
assert !defined?(A)
assert_not defined?(A)
end
ensure
remove_constants(:A)
end
def test_load_once_constants_should_not_be_unloaded
old_path = ActiveSupport::Dependencies.autoload_once_paths
with_autoloading_fixtures do
ActiveSupport::Dependencies.autoload_once_paths = ActiveSupport::Dependencies.autoload_paths
_ = ::A # assignment to silence parse-time warning "possibly useless use of :: in void context"
@ -978,8 +997,8 @@ class DependenciesTest < ActiveSupport::TestCase
assert defined?(A)
end
ensure
ActiveSupport::Dependencies.autoload_once_paths = []
Object.class_eval { remove_const :A if const_defined?(:A) }
ActiveSupport::Dependencies.autoload_once_paths = old_path
remove_constants(:A)
end
def test_access_unloaded_constants_for_reload
@ -991,23 +1010,27 @@ class DependenciesTest < ActiveSupport::TestCase
A::B # Make sure no circular dependency error
end
ensure
remove_constants(:A)
end
def test_autoload_once_paths_should_behave_when_recursively_loading
old_path = ActiveSupport::Dependencies.autoload_once_paths
with_loading 'dependencies', 'autoloading_fixtures' do
ActiveSupport::Dependencies.autoload_once_paths = [ActiveSupport::Dependencies.autoload_paths.last]
assert !defined?(CrossSiteDependency)
assert_not defined?(CrossSiteDependency)
assert_nothing_raised { CrossSiteDepender.nil? }
assert defined?(CrossSiteDependency)
assert !ActiveSupport::Dependencies.autoloaded?(CrossSiteDependency),
assert_not ActiveSupport::Dependencies.autoloaded?(CrossSiteDependency),
"CrossSiteDependency shouldn't be marked as autoloaded!"
ActiveSupport::Dependencies.clear
assert defined?(CrossSiteDependency),
"CrossSiteDependency shouldn't have been unloaded!"
end
ensure
ActiveSupport::Dependencies.autoload_once_paths = []
ActiveSupport::Dependencies.autoload_once_paths = old_path
remove_constants(:CrossSiteDependency)
end
def test_hook_called_multiple_times