From 9766eb4a833c26c64012230b96dd1157ebb8e8a2 Mon Sep 17 00:00:00 2001
From: eileencodes <eileencodes@gmail.com>
Date: Wed, 15 Jun 2022 12:44:11 -0400
Subject: [PATCH] Fix tests for minitest 5.16

In minitest/minitest@6e06ac9 minitest changed such that it now accepts
`kwargs` instead of requiring kwargs to be shoved into the args array.
This is a good change but required some updates to our test code to get
the new version of minitest passing.

Changes are as follows:

1) Lock minitest to 5.15 for Ruby 2.7. We don't love this change but
it's pretty difficult to get 2.7 and 3.0 to play nicely together with
the new kwargs changes. Dropping 2.7 support isn't an option right
now for Rails. This is safe because all of the code changes here are
internal methods to Rails like assert_called_with. Applications
shouldn't be consuming them as they are no-doc'd.
2) Update the `assert_called_with` method to take any kwargs but also
the returns kwarg.
3) Update callers of `assert_called_with` to move the kwargs outside the
args array.
4) Update the message from marshaled exceptions. In 5.16 the exception
message is "result not reported" instead of "Wrapped undumpable
exception".

Co-authored-by: Matthew Draper <matthew@trebex.net>
---
 activerecord/test/cases/fixtures_test.rb      |   6 +-
 .../test/cases/migration/change_table_test.rb | 100 ++++++++++--------
 .../test/cases/tasks/sqlite_rake_test.rb      |   5 +-
 3 files changed, 63 insertions(+), 48 deletions(-)

diff --git a/activerecord/test/cases/fixtures_test.rb b/activerecord/test/cases/fixtures_test.rb
index 0267da5116bdd..772f421f2c852 100644
--- a/activerecord/test/cases/fixtures_test.rb
+++ b/activerecord/test/cases/fixtures_test.rb
@@ -996,7 +996,7 @@ def rollback_transaction(*args); end
       def lock_thread=(lock_thread); end
     end.new
 
-    assert_called_with(connection, :begin_transaction, [joinable: false, _lazy: false]) do
+    assert_called_with(connection, :begin_transaction, [], joinable: false, _lazy: false) do
       fire_connection_notification(connection)
     end
   end
@@ -1036,14 +1036,14 @@ def rollback_transaction(*args); end
       def lock_thread=(lock_thread); end
     end.new
 
-    assert_called_with(connection, :begin_transaction, [joinable: false, _lazy: false]) do
+    assert_called_with(connection, :begin_transaction, [], joinable: false, _lazy: false) do
       fire_connection_notification(connection, shard: :shard_two)
     end
   end
 
   private
     def fire_connection_notification(connection, shard: ActiveRecord::Base.default_shard)
-      assert_called_with(ActiveRecord::Base.connection_handler, :retrieve_connection, ["book", { shard: shard }], returns: connection) do
+      assert_called_with(ActiveRecord::Base.connection_handler, :retrieve_connection, ["book"], returns: connection, shard: shard) do
         message_bus = ActiveSupport::Notifications.instrumenter
         payload = {
           spec_name: "book",
diff --git a/activerecord/test/cases/migration/change_table_test.rb b/activerecord/test/cases/migration/change_table_test.rb
index 5cf6493e52ba2..620319b38655c 100644
--- a/activerecord/test/cases/migration/change_table_test.rb
+++ b/activerecord/test/cases/migration/change_table_test.rb
@@ -17,117 +17,131 @@ def with_change_table
         yield ActiveRecord::Base.connection.update_table_definition(:delete_me, @connection)
       end
 
+      if Minitest::Mock.instance_method(:expect).parameters.map(&:first).include?(:keyrest)
+        def expect(method, returns, args, **kwargs)
+          @connection.expect(method, returns, args, **kwargs)
+        end
+      else
+        def expect(method, returns, args, **kwargs)
+          if !kwargs.empty?
+            @connection.expect(method, returns, [*args, kwargs])
+          else
+            @connection.expect(method, returns, args)
+          end
+        end
+      end
+
       def test_references_column_type_adds_id
         with_change_table do |t|
-          @connection.expect :add_reference, nil, [:delete_me, :customer]
+          expect :add_reference, nil, [:delete_me, :customer]
           t.references :customer
         end
       end
 
       def test_remove_references_column_type_removes_id
         with_change_table do |t|
-          @connection.expect :remove_reference, nil, [:delete_me, :customer]
+          expect :remove_reference, nil, [:delete_me, :customer]
           t.remove_references :customer
         end
       end
 
       def test_add_belongs_to_works_like_add_references
         with_change_table do |t|
-          @connection.expect :add_reference, nil, [:delete_me, :customer]
+          expect :add_reference, nil, [:delete_me, :customer]
           t.belongs_to :customer
         end
       end
 
       def test_remove_belongs_to_works_like_remove_references
         with_change_table do |t|
-          @connection.expect :remove_reference, nil, [:delete_me, :customer]
+          expect :remove_reference, nil, [:delete_me, :customer]
           t.remove_belongs_to :customer
         end
       end
 
       def test_references_column_type_with_polymorphic_adds_type
         with_change_table do |t|
-          @connection.expect :add_reference, nil, [:delete_me, :taggable, polymorphic: true]
+          expect :add_reference, nil, [:delete_me, :taggable], polymorphic: true
           t.references :taggable, polymorphic: true
         end
       end
 
       def test_remove_references_column_type_with_polymorphic_removes_type
         with_change_table do |t|
-          @connection.expect :remove_reference, nil, [:delete_me, :taggable, polymorphic: true]
+          expect :remove_reference, nil, [:delete_me, :taggable], polymorphic: true
           t.remove_references :taggable, polymorphic: true
         end
       end
 
       def test_references_column_type_with_polymorphic_and_options_null_is_false_adds_table_flag
         with_change_table do |t|
-          @connection.expect :add_reference, nil, [:delete_me, :taggable, polymorphic: true, null: false]
+          expect :add_reference, nil, [:delete_me, :taggable], polymorphic: true, null: false
           t.references :taggable, polymorphic: true, null: false
         end
       end
 
       def test_remove_references_column_type_with_polymorphic_and_options_null_is_false_removes_table_flag
         with_change_table do |t|
-          @connection.expect :remove_reference, nil, [:delete_me, :taggable, polymorphic: true, null: false]
+          expect :remove_reference, nil, [:delete_me, :taggable], polymorphic: true, null: false
           t.remove_references :taggable, polymorphic: true, null: false
         end
       end
 
       def test_references_column_type_with_polymorphic_and_type
         with_change_table do |t|
-          @connection.expect :add_reference, nil, [:delete_me, :taggable, polymorphic: true, type: :string]
+          expect :add_reference, nil, [:delete_me, :taggable], polymorphic: true, type: :string
           t.references :taggable, polymorphic: true, type: :string
         end
       end
 
       def test_remove_references_column_type_with_polymorphic_and_type
         with_change_table do |t|
-          @connection.expect :remove_reference, nil, [:delete_me, :taggable, polymorphic: true, type: :string]
+          expect :remove_reference, nil, [:delete_me, :taggable], polymorphic: true, type: :string
           t.remove_references :taggable, polymorphic: true, type: :string
         end
       end
 
       def test_timestamps_creates_updated_at_and_created_at
         with_change_table do |t|
-          @connection.expect :add_timestamps, nil, [:delete_me, null: true]
+          expect :add_timestamps, nil, [:delete_me], null: true
           t.timestamps null: true
         end
       end
 
       def test_remove_timestamps_creates_updated_at_and_created_at
         with_change_table do |t|
-          @connection.expect :remove_timestamps, nil, [:delete_me, { null: true }]
+          expect :remove_timestamps, nil, [:delete_me], null: true
           t.remove_timestamps(null: true)
         end
       end
 
       def test_primary_key_creates_primary_key_column
         with_change_table do |t|
-          @connection.expect :add_column, nil, [:delete_me, :id, :primary_key, primary_key: true, first: true]
+          expect :add_column, nil, [:delete_me, :id, :primary_key], primary_key: true, first: true
           t.primary_key :id, first: true
         end
       end
 
       def test_integer_creates_integer_column
         with_change_table do |t|
-          @connection.expect :add_column, nil, [:delete_me, :foo, :integer]
-          @connection.expect :add_column, nil, [:delete_me, :bar, :integer]
+          expect :add_column, nil, [:delete_me, :foo, :integer]
+          expect :add_column, nil, [:delete_me, :bar, :integer]
           t.integer :foo, :bar
         end
       end
 
       def test_bigint_creates_bigint_column
         with_change_table do |t|
-          @connection.expect :add_column, nil, [:delete_me, :foo, :bigint]
-          @connection.expect :add_column, nil, [:delete_me, :bar, :bigint]
+          expect :add_column, nil, [:delete_me, :foo, :bigint]
+          expect :add_column, nil, [:delete_me, :bar, :bigint]
           t.bigint :foo, :bar
         end
       end
 
       def test_string_creates_string_column
         with_change_table do |t|
-          @connection.expect :add_column, nil, [:delete_me, :foo, :string]
-          @connection.expect :add_column, nil, [:delete_me, :bar, :string]
+          expect :add_column, nil, [:delete_me, :foo, :string]
+          expect :add_column, nil, [:delete_me, :bar, :string]
           t.string :foo, :bar
         end
       end
@@ -135,16 +149,16 @@ def test_string_creates_string_column
       if current_adapter?(:PostgreSQLAdapter)
         def test_json_creates_json_column
           with_change_table do |t|
-            @connection.expect :add_column, nil, [:delete_me, :foo, :json]
-            @connection.expect :add_column, nil, [:delete_me, :bar, :json]
+            expect :add_column, nil, [:delete_me, :foo, :json]
+            expect :add_column, nil, [:delete_me, :bar, :json]
             t.json :foo, :bar
           end
         end
 
         def test_xml_creates_xml_column
           with_change_table do |t|
-            @connection.expect :add_column, nil, [:delete_me, :foo, :xml]
-            @connection.expect :add_column, nil, [:delete_me, :bar, :xml]
+            expect :add_column, nil, [:delete_me, :foo, :xml]
+            expect :add_column, nil, [:delete_me, :bar, :xml]
             t.xml :foo, :bar
           end
         end
@@ -152,120 +166,120 @@ def test_remove_exclusion_constraint_removes_exclusion_constraint
 
       def test_column_creates_column
         with_change_table do |t|
-          @connection.expect :add_column, nil, [:delete_me, :bar, :integer]
+          expect :add_column, nil, [:delete_me, :bar, :integer]
           t.column :bar, :integer
         end
       end
 
       def test_column_creates_column_with_options
         with_change_table do |t|
-          @connection.expect :add_column, nil, [:delete_me, :bar, :integer, { null: false }]
+          expect :add_column, nil, [:delete_me, :bar, :integer], null: false
           t.column :bar, :integer, null: false
         end
       end
 
       def test_column_creates_column_with_index
         with_change_table do |t|
-          @connection.expect :add_column, nil, [:delete_me, :bar, :integer]
-          @connection.expect :add_index, nil, [:delete_me, :bar]
+          expect :add_column, nil, [:delete_me, :bar, :integer]
+          expect :add_index, nil, [:delete_me, :bar]
           t.column :bar, :integer, index: true
         end
       end
 
       def test_index_creates_index
         with_change_table do |t|
-          @connection.expect :add_index, nil, [:delete_me, :bar]
+          expect :add_index, nil, [:delete_me, :bar]
           t.index :bar
         end
       end
 
       def test_index_creates_index_with_options
         with_change_table do |t|
-          @connection.expect :add_index, nil, [:delete_me, :bar, { unique: true }]
+          expect :add_index, nil, [:delete_me, :bar], unique: true
           t.index :bar, unique: true
         end
       end
 
       def test_index_exists
         with_change_table do |t|
-          @connection.expect :index_exists?, nil, [:delete_me, :bar, {}]
+          expect :index_exists?, nil, [:delete_me, :bar]
           t.index_exists?(:bar)
         end
       end
 
       def test_index_exists_with_options
         with_change_table do |t|
-          @connection.expect :index_exists?, nil, [:delete_me, :bar, { unique: true }]
+          expect :index_exists?, nil, [:delete_me, :bar], unique: true
           t.index_exists?(:bar, unique: true)
         end
       end
 
       def test_rename_index_renames_index
         with_change_table do |t|
-          @connection.expect :rename_index, nil, [:delete_me, :bar, :baz]
+          expect :rename_index, nil, [:delete_me, :bar, :baz]
           t.rename_index :bar, :baz
         end
       end
 
       def test_change_changes_column
         with_change_table do |t|
-          @connection.expect :change_column, nil, [:delete_me, :bar, :string]
+          expect :change_column, nil, [:delete_me, :bar, :string]
           t.change :bar, :string
         end
       end
 
       def test_change_changes_column_with_options
         with_change_table do |t|
-          @connection.expect :change_column, nil, [:delete_me, :bar, :string, { null: true }]
+          expect :change_column, nil, [:delete_me, :bar, :string], null: true
           t.change :bar, :string, null: true
         end
       end
 
       def test_change_default_changes_column
         with_change_table do |t|
-          @connection.expect :change_column_default, nil, [:delete_me, :bar, :string]
+          expect :change_column_default, nil, [:delete_me, :bar, :string]
           t.change_default :bar, :string
         end
       end
 
       def test_change_null_changes_column
         with_change_table do |t|
-          @connection.expect :change_column_null, nil, [:delete_me, :bar, true, nil]
+          expect :change_column_null, nil, [:delete_me, :bar, true, nil]
           t.change_null :bar, true
         end
       end
 
       def test_remove_drops_single_column
         with_change_table do |t|
-          @connection.expect :remove_columns, nil, [:delete_me, :bar]
+          expect :remove_columns, nil, [:delete_me, :bar]
           t.remove :bar
         end
       end
 
       def test_remove_drops_multiple_columns
         with_change_table do |t|
-          @connection.expect :remove_columns, nil, [:delete_me, :bar, :baz]
+          expect :remove_columns, nil, [:delete_me, :bar, :baz]
           t.remove :bar, :baz
         end
       end
 
       def test_remove_drops_multiple_columns_when_column_options_are_given
         with_change_table do |t|
-          @connection.expect :remove_columns, nil, [:delete_me, :bar, :baz, type: :string, null: false]
+          expect :remove_columns, nil, [:delete_me, :bar, :baz], type: :string, null: false
           t.remove :bar, :baz, type: :string, null: false
         end
       end
 
       def test_remove_index_removes_index_with_options
         with_change_table do |t|
-          @connection.expect :remove_index, nil, [:delete_me, :bar, { unique: true }]
+          expect :remove_index, nil, [:delete_me, :bar], unique: true
           t.remove_index :bar, unique: true
         end
       end
 
       def test_rename_renames_column
         with_change_table do |t|
-          @connection.expect :rename_column, nil, [:delete_me, :bar, :baz]
+          expect :rename_column, nil, [:delete_me, :bar, :baz]
           t.rename :bar, :baz
         end
       end
@@ -278,14 +292,14 @@ def test_table_name_set
 
       def test_check_constraint_creates_check_constraint
         with_change_table do |t|
-          @connection.expect :add_check_constraint, nil, [:delete_me, "price > discounted_price", name: "price_check"]
+          expect :add_check_constraint, nil, [:delete_me, "price > discounted_price"], name: "price_check"
           t.check_constraint "price > discounted_price", name: "price_check"
         end
       end
 
       def test_remove_check_constraint_removes_check_constraint
         with_change_table do |t|
-          @connection.expect :remove_check_constraint, nil, [:delete_me, name: "price_check"]
+          expect :remove_check_constraint, nil, [:delete_me], name: "price_check"
           t.remove_check_constraint name: "price_check"
         end
       end
diff --git a/activerecord/test/cases/tasks/sqlite_rake_test.rb b/activerecord/test/cases/tasks/sqlite_rake_test.rb
index 19daf8f1c8f05..98257867aa773 100644
--- a/activerecord/test/cases/tasks/sqlite_rake_test.rb
+++ b/activerecord/test/cases/tasks/sqlite_rake_test.rb
@@ -217,8 +217,9 @@ def test_structure_dump_execution_fails
         assert_called_with(
           Kernel,
           :system,
-          ["sqlite3", "--noop", "db_create.sqlite3", ".schema", out: "awesome-file.sql"],
-          returns: nil
+          ["sqlite3", "--noop", "db_create.sqlite3", ".schema"],
+          returns: nil,
+          out: "awesome-file.sql"
         ) do
           e = assert_raise(RuntimeError) do
             with_structure_dump_flags(["--noop"]) do
