Skip to content

Commit

Permalink
Fix did you mean tests for ruby-trunk (3.2)
Browse files Browse the repository at this point in the history
Manual backport of #45322

Original commit message:
In ruby/ruby@f075be3
did_you_mean and error_highlight now use `detailed_message` over
`message` to display errors.

For cases where we are testing `message`, in 3.2 and above we need to
test against `detailed_message` instead.

As far as I can tell in a Rails console when these errors are raised the
`detailed_message` is used so we shouldn't need to make other changes to
Rails. The only case where this isn't true is in the Railties changes -
we are explicitly formatting the did you mean message so we need to be
sure to call `detailed_message` here.

This fixes most of the failing tests for ruby-trunk.
  • Loading branch information
eileencodes committed Jul 12, 2022
1 parent 16a966a commit 41b0776
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 9 deletions.
6 changes: 5 additions & 1 deletion actionpack/test/controller/base_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,11 @@ def test_exceptions_have_suggestions_for_fix
exception = assert_raise AbstractController::ActionNotFound do
get :non_existent
end
assert_match "Did you mean?", exception.message
if exception.respond_to?(:detailed_message)
assert_match "Did you mean?", exception.detailed_message
else
assert_match "Did you mean?", exception.message
end
end
end

Expand Down
4 changes: 3 additions & 1 deletion actionpack/test/controller/helper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,9 @@ class HelpersTypoControllerTest < ActiveSupport::TestCase
def test_helper_typo_error_message
e = assert_raise(NameError) { HelpersTypoController.helper "admin/users" }
# This message is better if autoloading.
if RUBY_VERSION >= "2.6"
if e.respond_to?(:detailed_message)
assert_includes e.detailed_message, "Did you mean? Admin::UsersHelpeR"
elsif RUBY_VERSION >= "2.6"
assert_equal "uninitialized constant Admin::UsersHelper\nDid you mean? Admin::UsersHelpeR", e.message
else
assert_equal "uninitialized constant Admin::UsersHelper", e.message
Expand Down
12 changes: 10 additions & 2 deletions actionpack/test/controller/required_params_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,20 @@ class ActionControllerRequiredParamsTest < ActionController::TestCase
error = assert_raise ActionController::ParameterMissing do
post :create, params: { magazine: { name: "Mjallo!" } }
end
assert_match "Did you mean?", error.message
if error.respond_to?(:detailed_message)
assert_match "Did you mean?", error.detailed_message
else
assert_match "Did you mean?", error.message
end

error = assert_raise ActionController::ParameterMissing do
post :create, params: { book: { title: "Mjallo!" } }
end
assert_match "Did you mean?", error.message
if error.respond_to?(:detailed_message)
assert_match "Did you mean?", error.detailed_message
else
assert_match "Did you mean?", error.message
end
end
end

Expand Down
6 changes: 5 additions & 1 deletion actionpack/test/dispatch/routing_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4904,7 +4904,11 @@ def app; APP end
if defined?(DidYouMean) && DidYouMean.respond_to?(:correct_error)
test "exceptions have suggestions for fix" do
error = assert_raises(ActionController::UrlGenerationError) { product_path(nil, "id" => "url-tested") }
assert_match "Did you mean?", error.message
if error.respond_to?(:detailed_message)
assert_match "Did you mean?", error.detailed_message
else
assert_match "Did you mean?", error.message
end
end
end

Expand Down
6 changes: 5 additions & 1 deletion activerecord/test/cases/associations/eager_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -853,7 +853,11 @@ def test_eager_with_invalid_association_reference
error = assert_raise(ActiveRecord::AssociationNotFoundError) {
Post.all.merge!(includes: :monkeys).find(6)
}
assert_match "Did you mean?", error.message
if error.respond_to?(:detailed_message)
assert_match "Did you mean?", error.detailed_message
else
assert_match "Did you mean?", error.message
end
end
end

Expand Down
12 changes: 10 additions & 2 deletions activerecord/test/cases/associations/inverse_associations_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,11 @@ def test_trying_to_use_inverses_that_dont_exist_should_have_suggestions_for_fix
Human.first.confused_face
}

assert_match "Did you mean?", error.message
if error.respond_to?(:detailed_message)
assert_match "Did you mean?", error.detailed_message
else
assert_match "Did you mean?", error.message
end
assert_equal "super_human", error.corrections.first
end
end
Expand Down Expand Up @@ -770,7 +774,11 @@ def test_trying_to_use_inverses_that_dont_exist_should_have_suggestions_for_fix
Face.first.puzzled_human
}

assert_match "Did you mean?", error.message
if error.respond_to?(:detailed_message)
assert_match "Did you mean?", error.detailed_message
else
assert_match "Did you mean?", error.message
end
assert_equal "confused_face", error.corrections.first
end
end
Expand Down
6 changes: 5 additions & 1 deletion activerecord/test/cases/associations/join_model_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,11 @@ def test_exceptions_have_suggestions_for_fix
error = assert_raise(ActiveRecord::HasManyThroughAssociationNotFoundError) {
authors(:david).nothings
}
assert_match "Did you mean?", error.message
if error.respond_to?(:detailed_message)
assert_match "Did you mean?", error.detailed_message
else
assert_match "Did you mean?", error.message
end
end
end

Expand Down

0 comments on commit 41b0776

Please sign in to comment.