Skip to content

Commit

Permalink
Use safe_constantize.
Browse files Browse the repository at this point in the history
Fixes rails#9933.
  • Loading branch information
tgxworld committed Sep 1, 2014
1 parent fcbdac7 commit 6e0f273
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 5 deletions.
6 changes: 1 addition & 5 deletions activesupport/lib/active_support/testing/constant_lookup.rb
Expand Up @@ -36,12 +36,8 @@ def determine_constant_from_test_name(test_name)
while names.size > 0 do
names.last.sub!(/Test$/, "")
begin
constant = names.join("::").constantize
constant = names.join("::").safe_constantize
break(constant) if yield(constant)
rescue NoMethodError # subclass of NameError
raise
rescue NameError
# Constant wasn't found, move on
ensure
names.pop
end
Expand Down
30 changes: 30 additions & 0 deletions activesupport/test/constantize_test_cases.rb
@@ -1,3 +1,5 @@
require 'dependencies_test_helpers'

module Ace
module Base
class Case
Expand All @@ -23,6 +25,8 @@ class Dice
end

module ConstantizeTestCases
include DependenciesTestHelpers

def run_constantize_tests_on
assert_equal Ace::Base::Case, yield("Ace::Base::Case")
assert_equal Ace::Base::Case, yield("::Ace::Base::Case")
Expand Down Expand Up @@ -56,6 +60,19 @@ def run_constantize_tests_on
assert_raises(NameError) { yield("Ace::Gas::ConstantizeTestCases") }
assert_raises(NameError) { yield("") }
assert_raises(NameError) { yield("::") }
assert_raises(NameError) { yield("Ace::gas") }

assert_raises(NameError) do
with_autoloading_fixtures do
yield("RaisesNameError")
end
end

assert_raises(NoMethodError) do
with_autoloading_fixtures do
yield("RaisesNoMethodError")
end
end
end

def run_safe_constantize_tests_on
Expand All @@ -82,5 +99,18 @@ def run_safe_constantize_tests_on
assert_nil yield("Ace::Gas::Base")
assert_nil yield("Ace::Gas::ConstantizeTestCases")
assert_nil yield("#<Class:0x7b8b718b>::Nested_1")
assert_nil yield("Ace::gas")

assert_raises(NameError) do
with_autoloading_fixtures do
yield("RaisesNameError")
end
end

assert_raises(NoMethodError) do
with_autoloading_fixtures do
yield("RaisesNoMethodError")
end
end
end
end
2 changes: 2 additions & 0 deletions activesupport/test/dependencies_test.rb
Expand Up @@ -888,6 +888,8 @@ def test_autoload_doesnt_shadow_error_when_mechanism_not_set_to_load
assert_raise(NameError) { assert_equal 123, ::RaisesNameError::FooBarBaz }
end
end
ensure
remove_constants(:RaisesNameError)
end

def test_autoload_doesnt_shadow_name_error
Expand Down
8 changes: 8 additions & 0 deletions activesupport/test/testing/constant_lookup_test.rb
Expand Up @@ -65,4 +65,12 @@ def test_does_not_swallow_exception_on_no_method_error
}
}
end

def test_does_not_swallow_exception_on_no_name_error_within_constant
assert_raises(NameError) do
with_autoloading_fixtures do
self.class.determine_constant_from_test_name('RaisesNameError')
end
end
end
end

0 comments on commit 6e0f273

Please sign in to comment.