diff options
Diffstat (limited to 'activesupport/test/dependencies_test.rb')
-rw-r--r-- | activesupport/test/dependencies_test.rb | 216 |
1 files changed, 115 insertions, 101 deletions
diff --git a/activesupport/test/dependencies_test.rb b/activesupport/test/dependencies_test.rb index 757e600646..54068f5a08 100644 --- a/activesupport/test/dependencies_test.rb +++ b/activesupport/test/dependencies_test.rb @@ -1,7 +1,7 @@ -require 'abstract_unit' -require 'pp' -require 'active_support/dependencies' -require 'dependencies_test_helpers' +require "abstract_unit" +require "pp" +require "active_support/dependencies" +require "dependencies_test_helpers" module ModuleWithMissing mattr_accessor :missing_count @@ -29,18 +29,18 @@ class DependenciesTest < ActiveSupport::TestCase def test_depend_on_path expected = assert_raises(LoadError) do - Kernel.require 'omgwtfbbq' + Kernel.require "omgwtfbbq" end e = assert_raises(LoadError) do - ActiveSupport::Dependencies.depend_on 'omgwtfbbq' + ActiveSupport::Dependencies.depend_on "omgwtfbbq" end assert_equal expected.path, e.path end def test_require_dependency_accepts_an_object_which_implements_to_path o = Object.new - def o.to_path; 'dependencies/service_one'; end + def o.to_path; "dependencies/service_one"; end assert_nothing_raised { require_dependency o } @@ -51,8 +51,8 @@ class DependenciesTest < ActiveSupport::TestCase def test_tracking_loaded_files with_loading do - require_dependency 'dependencies/service_one' - require_dependency 'dependencies/service_two' + require_dependency "dependencies/service_one" + require_dependency "dependencies/service_two" assert_equal 2, ActiveSupport::Dependencies.loaded.size end ensure @@ -61,8 +61,8 @@ class DependenciesTest < ActiveSupport::TestCase def test_tracking_identical_loaded_files with_loading do - require_dependency 'dependencies/service_one' - require_dependency 'dependencies/service_one' + require_dependency "dependencies/service_one" + require_dependency "dependencies/service_one" assert_equal 1, ActiveSupport::Dependencies.loaded.size end ensure @@ -75,32 +75,33 @@ class DependenciesTest < ActiveSupport::TestCase def test_dependency_which_raises_exception_isnt_added_to_loaded_set with_loading do - filename = 'dependencies/raises_exception' + filename = "dependencies/raises_exception" + expanded = File.expand_path(filename) $raises_exception_load_count = 0 5.times do |count| - e = assert_raise Exception, 'should have loaded dependencies/raises_exception which raises an exception' do + e = assert_raise Exception, "should have loaded dependencies/raises_exception which raises an exception" do require_dependency filename end - assert_equal 'Loading me failed, so do not add to loaded or history.', e.message + assert_equal "Loading me failed, so do not add to loaded or history.", e.message assert_equal count + 1, $raises_exception_load_count - assert_not ActiveSupport::Dependencies.loaded.include?(filename) - assert_not ActiveSupport::Dependencies.history.include?(filename) + assert_not ActiveSupport::Dependencies.loaded.include?(expanded) + assert_not ActiveSupport::Dependencies.history.include?(expanded) end end end def test_dependency_which_raises_doesnt_blindly_call_blame_file! with_loading do - filename = 'dependencies/raises_exception_without_blame_file' + filename = "dependencies/raises_exception_without_blame_file" assert_raises(Exception) { require_dependency filename } end end def test_warnings_should_be_enabled_on_first_load - with_loading 'dependencies' do + 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}") @@ -111,41 +112,41 @@ class DependenciesTest < ActiveSupport::TestCase silence_warnings { require_dependency filename } assert_equal 1, $check_warnings_load_count - assert_equal true, $checked_verbose, 'On first load warnings should be enabled.' + assert_equal true, $checked_verbose, "On first load warnings should be enabled." - assert ActiveSupport::Dependencies.loaded.include?(expanded) + assert_includes ActiveSupport::Dependencies.loaded, expanded ActiveSupport::Dependencies.clear assert_not ActiveSupport::Dependencies.loaded.include?(expanded) - assert ActiveSupport::Dependencies.history.include?(expanded) + assert_includes ActiveSupport::Dependencies.history, expanded silence_warnings { require_dependency filename } assert_equal 2, $check_warnings_load_count - assert_equal nil, $checked_verbose, 'After first load warnings should be left alone.' + assert_equal nil, $checked_verbose, "After first load warnings should be left alone." - assert ActiveSupport::Dependencies.loaded.include?(expanded) + assert_includes ActiveSupport::Dependencies.loaded, expanded ActiveSupport::Dependencies.clear assert_not ActiveSupport::Dependencies.loaded.include?(expanded) - assert ActiveSupport::Dependencies.history.include?(expanded) + assert_includes ActiveSupport::Dependencies.history, expanded enable_warnings { require_dependency filename } assert_equal 3, $check_warnings_load_count - assert_equal true, $checked_verbose, 'After first load warnings should be left alone.' + assert_equal true, $checked_verbose, "After first load warnings should be left alone." - assert ActiveSupport::Dependencies.loaded.include?(expanded) + assert_includes ActiveSupport::Dependencies.loaded, expanded ActiveSupport::Dependencies.warnings_on_first_load = old_warnings end end def test_mutual_dependencies_dont_infinite_loop - with_loading 'dependencies' do + with_loading "dependencies" do $mutual_dependencies_count = 0 - assert_nothing_raised { require_dependency 'mutual_one' } + assert_nothing_raised { require_dependency "mutual_one" } assert_equal 2, $mutual_dependencies_count ActiveSupport::Dependencies.clear $mutual_dependencies_count = 0 - assert_nothing_raised { require_dependency 'mutual_two' } + assert_nothing_raised { require_dependency "mutual_two" } assert_equal 2, $mutual_dependencies_count end end @@ -166,7 +167,7 @@ class DependenciesTest < ActiveSupport::TestCase def test_require_dependency_does_not_assume_any_particular_constant_is_defined with_autoloading_fixtures do - require_dependency 'typo' + require_dependency "typo" assert_equal 1, TypO end end @@ -174,7 +175,7 @@ class DependenciesTest < ActiveSupport::TestCase # Regression, see https://github.com/rails/rails/issues/16468. def test_require_dependency_interaction_with_autoloading with_autoloading_fixtures do - require_dependency 'typo' + require_dependency "typo" assert_equal 1, TypO e = assert_raise(LoadError) { Typo } @@ -252,7 +253,7 @@ class DependenciesTest < ActiveSupport::TestCase with_autoloading_fixtures do assert_kind_of Class, ClassFolder::ClassFolderSubclass assert_kind_of Class, ClassFolder - assert_equal 'indeed', ClassFolder::ClassFolderSubclass::ConstantInClassFolder + assert_equal "indeed", ClassFolder::ClassFolderSubclass::ConstantInClassFolder end ensure remove_constants(:ClassFolder) @@ -268,6 +269,28 @@ class DependenciesTest < ActiveSupport::TestCase remove_constants(:ModuleFolder) end + def test_raising_discards_autoloaded_constants + with_autoloading_fixtures do + assert_raises(Exception, "arbitray exception message") { RaisesArbitraryException } + assert_not defined?(A) + assert_not defined?(RaisesArbitraryException) + end + ensure + remove_constants(:A, :RaisesArbitraryException) + end + + def test_throwing_discards_autoloaded_constants + with_autoloading_fixtures do + catch :t do + Throws + end + assert_not defined?(A) + assert_not defined?(Throws) + end + ensure + remove_constants(:A, :Throws) + end + def test_doesnt_break_normal_require path = File.expand_path("../autoloading_fixtures/load_path", __FILE__) original_path = $:.dup @@ -313,7 +336,7 @@ class DependenciesTest < ActiveSupport::TestCase $:.push(path) with_loading do - assert_equal true, require('loaded_constant') + assert_equal true, require("loaded_constant") end ensure remove_constants(:LoadedConstant) @@ -327,7 +350,7 @@ class DependenciesTest < ActiveSupport::TestCase with_loading do Object.module_eval "module LoadedConstant; end" - assert_equal true, require('loaded_constant') + assert_equal true, require("loaded_constant") end ensure remove_constants(:LoadedConstant) @@ -340,8 +363,8 @@ class DependenciesTest < ActiveSupport::TestCase $:.push(path) with_loading do - require 'loaded_constant' - assert_equal false, require('loaded_constant') + require "loaded_constant" + assert_equal false, require("loaded_constant") end ensure remove_constants(:LoadedConstant) @@ -350,7 +373,7 @@ class DependenciesTest < ActiveSupport::TestCase def test_require_raises_load_error_when_file_not_found with_loading do - assert_raise(LoadError) { require 'this_file_dont_exist_dude' } + assert_raise(LoadError) { require "this_file_dont_exist_dude" } end end @@ -360,8 +383,8 @@ class DependenciesTest < ActiveSupport::TestCase $:.push(path) with_loading do - assert_equal true, load('loaded_constant.rb') - assert_equal true, load('loaded_constant.rb') + assert_equal true, load("loaded_constant.rb") + assert_equal true, load("loaded_constant.rb") end ensure remove_constants(:LoadedConstant) @@ -370,7 +393,7 @@ class DependenciesTest < ActiveSupport::TestCase def test_load_raises_load_error_when_file_not_found with_loading do - assert_raise(LoadError) { load 'this_file_dont_exist_dude.rb' } + assert_raise(LoadError) { load "this_file_dont_exist_dude.rb" } end end @@ -406,38 +429,38 @@ class DependenciesTest < ActiveSupport::TestCase end def test_loadable_constants_for_path_should_handle_empty_autoloads - assert_equal [], ActiveSupport::Dependencies.loadable_constants_for_path('hello') + assert_equal [], ActiveSupport::Dependencies.loadable_constants_for_path("hello") end def test_loadable_constants_for_path_should_handle_relative_paths - fake_root = 'dependencies' - relative_root = File.dirname(__FILE__) + '/dependencies' - ['', '/'].each do |suffix| + fake_root = "dependencies" + relative_root = File.dirname(__FILE__) + "/dependencies" + ["", "/"].each do |suffix| with_loading fake_root + suffix do - assert_equal ["A::B"], ActiveSupport::Dependencies.loadable_constants_for_path(relative_root + '/a/b') + assert_equal ["A::B"], ActiveSupport::Dependencies.loadable_constants_for_path(relative_root + "/a/b") end end end def test_loadable_constants_for_path_should_provide_all_results - fake_root = '/usr/apps/backpack' - with_loading fake_root, fake_root + '/lib' do + fake_root = "/usr/apps/backpack" + with_loading fake_root, fake_root + "/lib" do root = ActiveSupport::Dependencies.autoload_paths.first - assert_equal ["Lib::A::B", "A::B"], ActiveSupport::Dependencies.loadable_constants_for_path(root + '/lib/a/b') + assert_equal ["Lib::A::B", "A::B"], ActiveSupport::Dependencies.loadable_constants_for_path(root + "/lib/a/b") end end def test_loadable_constants_for_path_should_uniq_results - fake_root = '/usr/apps/backpack/lib' - with_loading fake_root, fake_root + '/' do + fake_root = "/usr/apps/backpack/lib" + with_loading fake_root, fake_root + "/" do root = ActiveSupport::Dependencies.autoload_paths.first - assert_equal ["A::B"], ActiveSupport::Dependencies.loadable_constants_for_path(root + '/a/b') + assert_equal ["A::B"], ActiveSupport::Dependencies.loadable_constants_for_path(root + "/a/b") end end def test_loadable_constants_with_load_path_without_trailing_slash - path = File.dirname(__FILE__) + '/autoloading_fixtures/class_folder/inline_class.rb' - with_loading 'autoloading_fixtures/class/' do + path = File.dirname(__FILE__) + "/autoloading_fixtures/class_folder/inline_class.rb" + with_loading "autoloading_fixtures/class/" do assert_equal [], ActiveSupport::Dependencies.loadable_constants_for_path(path) end end @@ -501,29 +524,29 @@ class DependenciesTest < ActiveSupport::TestCase end def test_file_search - with_loading 'dependencies' do + with_loading "dependencies" do root = ActiveSupport::Dependencies.autoload_paths.first - assert_equal nil, ActiveSupport::Dependencies.search_for_file('service_three') - assert_equal nil, ActiveSupport::Dependencies.search_for_file('service_three.rb') - assert_equal root + '/service_one.rb', ActiveSupport::Dependencies.search_for_file('service_one') - assert_equal root + '/service_one.rb', ActiveSupport::Dependencies.search_for_file('service_one.rb') + assert_equal nil, ActiveSupport::Dependencies.search_for_file("service_three") + assert_equal nil, ActiveSupport::Dependencies.search_for_file("service_three.rb") + assert_equal root + "/service_one.rb", ActiveSupport::Dependencies.search_for_file("service_one") + assert_equal root + "/service_one.rb", ActiveSupport::Dependencies.search_for_file("service_one.rb") end end def test_file_search_uses_first_in_load_path - with_loading 'dependencies', 'autoloading_fixtures' do + with_loading "dependencies", "autoloading_fixtures" do deps, autoload = ActiveSupport::Dependencies.autoload_paths assert_match %r/dependencies/, deps assert_match %r/autoloading_fixtures/, autoload - assert_equal deps + '/conflict.rb', ActiveSupport::Dependencies.search_for_file('conflict') + assert_equal deps + "/conflict.rb", ActiveSupport::Dependencies.search_for_file("conflict") end - with_loading 'autoloading_fixtures', 'dependencies' do + with_loading "autoloading_fixtures", "dependencies" do autoload, deps = ActiveSupport::Dependencies.autoload_paths assert_match %r/dependencies/, deps assert_match %r/autoloading_fixtures/, autoload - assert_equal autoload + '/conflict.rb', ActiveSupport::Dependencies.search_for_file('conflict') + assert_equal autoload + "/conflict.rb", ActiveSupport::Dependencies.search_for_file("conflict") end end @@ -561,11 +584,11 @@ class DependenciesTest < ActiveSupport::TestCase def test_const_missing_in_anonymous_modules_raises_if_the_constant_belongs_to_Object with_autoloading_fixtures do - require_dependency 'em' + require_dependency "em" mod = Module.new e = assert_raise(NameError) { mod::EM } - assert_equal 'EM cannot be autoloaded from an anonymous class or module', e.message + assert_equal "EM cannot be autoloaded from an anonymous class or module", e.message assert_equal :EM, e.name end ensure @@ -573,7 +596,7 @@ class DependenciesTest < ActiveSupport::TestCase end def test_removal_from_tree_should_be_detected - with_loading 'dependencies' do + with_loading "dependencies" do c = ServiceOne ActiveSupport::Dependencies.clear assert_not defined?(ServiceOne) @@ -587,7 +610,7 @@ class DependenciesTest < ActiveSupport::TestCase end def test_references_should_work - with_loading 'dependencies' do + with_loading "dependencies" do c = ActiveSupport::Dependencies.reference("ServiceOne") service_one_first = ServiceOne assert_equal service_one_first, c.get("ServiceOne") @@ -602,7 +625,7 @@ class DependenciesTest < ActiveSupport::TestCase end def test_constantize_shortcut_for_cached_constant_lookups - with_loading 'dependencies' do + with_loading "dependencies" do assert_equal ServiceOne, ActiveSupport::Dependencies.constantize("ServiceOne") end ensure @@ -610,7 +633,7 @@ class DependenciesTest < ActiveSupport::TestCase end def test_nested_load_error_isnt_rescued - with_loading 'dependencies' do + with_loading "dependencies" do assert_raise(LoadError) do RequiresNonexistent1 end @@ -636,7 +659,7 @@ class DependenciesTest < ActiveSupport::TestCase def test_autoload_once_pathnames_do_not_add_to_autoloaded_constants with_autoloading_fixtures do - pathnames = ActiveSupport::Dependencies.autoload_paths.collect{|p| Pathname.new(p)} + pathnames = ActiveSupport::Dependencies.autoload_paths.collect { |p| Pathname.new(p) } ActiveSupport::Dependencies.autoload_paths = pathnames ActiveSupport::Dependencies.autoload_once_paths = pathnames @@ -654,7 +677,7 @@ class DependenciesTest < ActiveSupport::TestCase def test_application_should_special_case_application_controller with_autoloading_fixtures do - require_dependency 'application' + require_dependency "application" assert_equal 10, ApplicationController assert ActiveSupport::Dependencies.autoloaded?(:ApplicationController) end @@ -664,14 +687,14 @@ class DependenciesTest < ActiveSupport::TestCase def test_preexisting_constants_are_not_marked_as_autoloaded with_autoloading_fixtures do - require_dependency 'em' + require_dependency "em" assert ActiveSupport::Dependencies.autoloaded?(:EM) ActiveSupport::Dependencies.clear end Object.const_set :EM, Class.new with_autoloading_fixtures do - require_dependency 'em' + require_dependency "em" assert ! ActiveSupport::Dependencies.autoloaded?(:EM), "EM shouldn't be marked autoloaded!" ActiveSupport::Dependencies.clear end @@ -733,15 +756,15 @@ class DependenciesTest < ActiveSupport::TestCase end def test_new_contants_in_without_constants - assert_equal [], (ActiveSupport::Dependencies.new_constants_in(Object) { }) - assert ActiveSupport::Dependencies.constant_watch_stack.all? {|k,v| v.empty? } + assert_equal [], (ActiveSupport::Dependencies.new_constants_in(Object) {}) + assert ActiveSupport::Dependencies.constant_watch_stack.all? { |k,v| v.empty? } end def test_new_constants_in_with_a_single_constant assert_equal ["Hello"], ActiveSupport::Dependencies.new_constants_in(Object) { Object.const_set :Hello, 10 }.map(&:to_s) - assert ActiveSupport::Dependencies.constant_watch_stack.all? {|k,v| v.empty? } + assert ActiveSupport::Dependencies.constant_watch_stack.all? { |k,v| v.empty? } ensure remove_constants(:Hello) end @@ -758,7 +781,7 @@ class DependenciesTest < ActiveSupport::TestCase end assert_equal ["OuterAfter", "OuterBefore"], outer.sort.map(&:to_s) - assert ActiveSupport::Dependencies.constant_watch_stack.all? {|k,v| v.empty? } + assert ActiveSupport::Dependencies.constant_watch_stack.all? { |k,v| v.empty? } ensure remove_constants(:OuterBefore, :Inner, :OuterAfter) end @@ -777,7 +800,7 @@ class DependenciesTest < ActiveSupport::TestCase M.const_set :OuterAfter, 30 end assert_equal ["M::OuterAfter", "M::OuterBefore"], outer.sort - assert ActiveSupport::Dependencies.constant_watch_stack.all? {|k,v| v.empty? } + assert ActiveSupport::Dependencies.constant_watch_stack.all? { |k,v| v.empty? } ensure remove_constants(:M) end @@ -795,7 +818,7 @@ class DependenciesTest < ActiveSupport::TestCase M.const_set :OuterAfter, 30 end assert_equal ["M::OuterAfter", "M::OuterBefore"], outer.sort - assert ActiveSupport::Dependencies.constant_watch_stack.all? {|k,v| v.empty? } + assert ActiveSupport::Dependencies.constant_watch_stack.all? { |k,v| v.empty? } ensure remove_constants(:M) end @@ -818,7 +841,7 @@ class DependenciesTest < ActiveSupport::TestCase assert_not defined?(MultipleConstantFile) assert_not defined?(SiblingConstant) - require_dependency 'multiple_constant_file' + require_dependency "multiple_constant_file" assert defined?(MultipleConstantFile) assert defined?(SiblingConstant) assert ActiveSupport::Dependencies.autoloaded?(:MultipleConstantFile) @@ -858,7 +881,7 @@ class DependenciesTest < ActiveSupport::TestCase assert_not defined?(ClassFolder::NestedClass) assert_not defined?(ClassFolder::SiblingClass) - require_dependency 'class_folder/nested_class' + require_dependency "class_folder/nested_class" assert defined?(ClassFolder::NestedClass) assert defined?(ClassFolder::SiblingClass) @@ -936,7 +959,7 @@ class DependenciesTest < ActiveSupport::TestCase e = assert_raise NameError do ::RaisesNameError::FooBarBaz.object_id end - assert_equal 'uninitialized constant RaisesNameError::FooBarBaz', e.message + assert_equal "uninitialized constant RaisesNameError::FooBarBaz", e.message assert !defined?(::RaisesNameError), "::RaisesNameError is defined but it should have failed!" end @@ -951,8 +974,8 @@ class DependenciesTest < ActiveSupport::TestCase end def test_remove_constant_handles_double_colon_at_start - Object.const_set 'DeleteMe', Module.new - DeleteMe.const_set 'OrMe', Module.new + Object.const_set "DeleteMe", Module.new + DeleteMe.const_set "OrMe", Module.new ActiveSupport::Dependencies.remove_constant "::DeleteMe::OrMe" assert_not defined?(DeleteMe::OrMe) assert defined?(DeleteMe) @@ -963,9 +986,9 @@ class DependenciesTest < ActiveSupport::TestCase end def test_remove_constant_does_not_trigger_loading_autoloads - constant = 'ShouldNotBeAutoloaded' + constant = "ShouldNotBeAutoloaded" Object.class_eval do - autoload constant, File.expand_path('../autoloading_fixtures/should_not_be_required', __FILE__) + autoload constant, File.expand_path("../autoloading_fixtures/should_not_be_required", __FILE__) end assert_nil ActiveSupport::Dependencies.remove_constant(constant), "Kernel#autoload has been triggered by remove_constant" @@ -978,8 +1001,8 @@ class DependenciesTest < ActiveSupport::TestCase with_autoloading_fixtures do _ = ::A # assignment to silence parse-time warning "possibly useless use of :: in void context" _ = ::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') + ActiveSupport::Dependencies.remove_constant("A") + ActiveSupport::Dependencies.remove_constant("A::B") assert_not defined?(A) end ensure @@ -1013,10 +1036,9 @@ class DependenciesTest < ActiveSupport::TestCase 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 + with_loading "dependencies", "autoloading_fixtures" do ActiveSupport::Dependencies.autoload_once_paths = [ActiveSupport::Dependencies.autoload_paths.last] assert_not defined?(CrossSiteDependency) assert_nothing_raised { CrossSiteDepender.nil? } @@ -1037,21 +1059,13 @@ class DependenciesTest < ActiveSupport::TestCase end def test_load_and_require_stay_private - assert Object.private_methods.include?(:load) - assert Object.private_methods.include?(:require) + assert_includes Object.private_methods, :load + assert_includes Object.private_methods, :require ActiveSupport::Dependencies.unhook! - assert Object.private_methods.include?(:load) - assert Object.private_methods.include?(:require) - ensure - ActiveSupport::Dependencies.hook! - end - - def test_unhook - ActiveSupport::Dependencies.unhook! - assert !Module.new.respond_to?(:const_missing_without_dependencies) - assert !Module.new.respond_to?(:load_without_new_constant_marking) + assert_includes Object.private_methods, :load + assert_includes Object.private_methods, :require ensure ActiveSupport::Dependencies.hook! end |