Handle `FrozenError` if it is available

This pull request handles `FrozenError` introduced by Ruby 2.5.
Refer https://bugs.ruby-lang.org/projects/ruby-trunk/repository/revisions/61131

Since `FrozenError` is a subclass of `RuntimeError` minitest used by master
branch can handle it, though it would be better to handle `FrozenError`
explicitly if possible.

`FrozenError` does not exist in Ruby 2.4 or lower, `frozen_error_class`
handles which exception is expected to be raised.

This pull request is intended to be merged to master,
then backported to `5-1-stable` to address #31508
This commit is contained in:
Yasuo Honda 2017-12-19 21:14:55 +00:00
parent 053a4c6990
commit 01efbc128d
8 changed files with 17 additions and 5 deletions

View File

@ -27,7 +27,7 @@ class AggregationsTest < ActiveRecord::TestCase
def test_immutable_value_objects
customers(:david).balance = Money.new(100)
assert_raise(RuntimeError) { customers(:david).balance.instance_eval { @amount = 20 } }
assert_raise(frozen_error_class) { customers(:david).balance.instance_eval { @amount = 20 } }
end
def test_inferred_mapping

View File

@ -283,7 +283,7 @@ class QueryCacheTest < ActiveRecord::TestCase
payload[:sql].downcase!
end
assert_raises RuntimeError do
assert_raises frozen_error_class do
ActiveRecord::Base.cache do
assert_queries(1) { Task.find(1); Task.find(1) }
end

View File

@ -77,6 +77,10 @@ module ActiveRecord
model.reset_column_information
model.column_names.include?(column_name.to_s)
end
def frozen_error_class
Object.const_defined?(:FrozenError) ? FrozenError : RuntimeError
end
end
class PostgreSQLTestCase < TestCase

View File

@ -576,7 +576,7 @@ class TransactionTest < ActiveRecord::TestCase
def test_rollback_when_saving_a_frozen_record
topic = Topic.new(title: "test")
topic.freeze
e = assert_raise(RuntimeError) { topic.save }
e = assert_raise(frozen_error_class) { topic.save }
# Not good enough, but we can't do much
# about it since there is no specific error
# for frozen objects.

View File

@ -38,4 +38,8 @@ class ActiveSupport::TestCase
private def jruby_skip(message = "")
skip message if defined?(JRUBY_VERSION)
end
def frozen_error_class
Object.const_defined?(:FrozenError) ? FrozenError : RuntimeError
end
end

View File

@ -446,7 +446,7 @@ class HashExtTest < ActiveSupport::TestCase
original.freeze
assert_nothing_raised { original.except(:a) }
assert_raise(RuntimeError) { original.except!(:a) }
assert_raise(frozen_error_class) { original.except!(:a) }
end
def test_except_does_not_delete_values_in_original

View File

@ -249,7 +249,7 @@ module ApplicationTests
test "can't change middleware after it's built" do
boot!
assert_raise RuntimeError do
assert_raise frozen_error_class do
app.config.middleware.use Rack::Config
end
end

View File

@ -406,6 +406,10 @@ class ActiveSupport::TestCase
include TestHelpers::Rack
include TestHelpers::Generation
include ActiveSupport::Testing::Stream
def frozen_error_class
Object.const_defined?(:FrozenError) ? FrozenError : RuntimeError
end
end
# Create a scope and build a fixture rails app