Skip to content

Commit

Permalink
Merge pull request #38091 from kamipo/fix_middleware_stack_proxy
Browse files Browse the repository at this point in the history
Fix Ruby 2.7 warnings on `MiddlewareStackProxy`
  • Loading branch information
kamipo committed Dec 25, 2019
2 parents d8de75d + fedde23 commit 5e6c331
Show file tree
Hide file tree
Showing 10 changed files with 80 additions and 61 deletions.
17 changes: 15 additions & 2 deletions actionpack/lib/action_dispatch/middleware/stack.rb
Expand Up @@ -90,30 +90,43 @@ def [](i)
end

def unshift(klass, *args, &block)
middlewares.unshift(build_middleware(klass, args, block))
middlewares.unshift(
build_middleware(klass, args, block) do |app|
klass.new(app, *args, &block)
end
)
end
ruby2_keywords(:unshift) if respond_to?(:ruby2_keywords, true)

def initialize_copy(other)
self.middlewares = other.middlewares.dup
end

def insert(index, klass, *args, &block)
index = assert_index(index, :before)
middlewares.insert(index, build_middleware(klass, args, block))
middlewares.insert(
index,
build_middleware(klass, args, block) do |app|
klass.new(app, *args, &block)
end
)
end
ruby2_keywords(:insert) if respond_to?(:ruby2_keywords, true)

alias_method :insert_before, :insert

def insert_after(index, *args, &block)
index = assert_index(index, :after)
insert(index + 1, *args, &block)
end
ruby2_keywords(:insert_after) if respond_to?(:ruby2_keywords, true)

def swap(target, *args, &block)
index = assert_index(target, :before)
insert(index, *args, &block)
middlewares.delete_at(index + 1)
end
ruby2_keywords(:swap) if respond_to?(:ruby2_keywords, true)

def delete(target)
middlewares.delete_if { |m| m.klass == target }
Expand Down
21 changes: 13 additions & 8 deletions railties/lib/rails/configuration.rb
Expand Up @@ -41,34 +41,39 @@ def initialize(operations = [], delete_operations = [])
end

def insert_before(*args, &block)
@operations << [__method__, args, block]
@operations << -> middleware { middleware.send(__method__, *args, &block) }
end
ruby2_keywords(:insert_before) if respond_to?(:ruby2_keywords, true)

alias :insert :insert_before

def insert_after(*args, &block)
@operations << [__method__, args, block]
@operations << -> middleware { middleware.send(__method__, *args, &block) }
end
ruby2_keywords(:insert_after) if respond_to?(:ruby2_keywords, true)

def swap(*args, &block)
@operations << [__method__, args, block]
@operations << -> middleware { middleware.send(__method__, *args, &block) }
end
ruby2_keywords(:swap) if respond_to?(:ruby2_keywords, true)

def use(*args, &block)
@operations << [__method__, args, block]
@operations << -> middleware { middleware.send(__method__, *args, &block) }
end
ruby2_keywords(:use) if respond_to?(:ruby2_keywords, true)

def delete(*args, &block)
@delete_operations << [__method__, args, block]
@delete_operations << -> middleware { middleware.send(__method__, *args, &block) }
end

def unshift(*args, &block)
@operations << [__method__, args, block]
@operations << -> middleware { middleware.send(__method__, *args, &block) }
end
ruby2_keywords(:unshift) if respond_to?(:ruby2_keywords, true)

def merge_into(other) #:nodoc:
(@operations + @delete_operations).each do |operation, args, block|
other.send(operation, *args, &block)
(@operations + @delete_operations).each do |operation|
operation.call(other)
end

other
Expand Down
17 changes: 9 additions & 8 deletions railties/test/application/bin_setup_test.rb
Expand Up @@ -16,13 +16,13 @@ def test_bin_setup
list_tables = lambda { rails("runner", "p ActiveRecord::Base.connection.tables").strip }
File.write("log/test.log", "zomg!")

assert_match "[]", list_tables.call
assert_equal "[]", list_tables.call
assert_equal 5, File.size("log/test.log")
assert_not File.exist?("tmp/restart.txt")

`bin/setup 2>&1`
assert_equal 0, File.size("log/test.log")
assert_match '["schema_migrations", "ar_internal_metadata", "articles"]', list_tables.call
assert_equal '["schema_migrations", "ar_internal_metadata", "articles"]', list_tables.call
assert File.exist?("tmp/restart.txt")
end
end
Expand All @@ -44,17 +44,18 @@ def test_bin_setup_output
# Ignore warnings such as `Psych.safe_load is deprecated`
output.gsub!(/^warning:\s.*\n/, "")

assert_match(<<~OUTPUT, output)
assert_equal(<<~OUTPUT, output)
== Installing dependencies ==
The Gemfile's dependencies are satisfied
== Preparing database ==
OUTPUT
Created database 'app_development'
Created database 'app_test'
== Removing old logs and tempfiles ==
assert_match("Created database 'app_development'", output)
assert_match("Created database 'app_test'", output)
assert_match("== Removing old logs and tempfiles ==", output)
assert_match("== Restarting application server ==", output)
== Restarting application server ==
OUTPUT
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion railties/test/application/configuration_test.rb
Expand Up @@ -2441,7 +2441,7 @@ class MyLogger < ::Logger
RUBY

output = rails("routes", "-g", "active_storage")
assert_match <<~MESSAGE, output
assert_equal <<~MESSAGE, output
Prefix Verb URI Pattern Controller#Action
rails_service_blob GET /files/blobs/:signed_id/*filename(.:format) active_storage/blobs#show
rails_blob_representation GET /files/representations/:signed_blob_id/:variation_key/*filename(.:format) active_storage/representations#show
Expand Down
24 changes: 12 additions & 12 deletions railties/test/application/rake/dbs_test.rb
Expand Up @@ -420,8 +420,8 @@ def db_structure_dump_and_load(expected_database)
require "#{app_path}/config/environment"
db_structure_dump_and_load ActiveRecord::Base.configurations[Rails.env][:database]

assert_match "test", rails("runner", "-e", "test", "puts ActiveRecord::InternalMetadata[:environment]").strip
assert_match "development", rails("runner", "puts ActiveRecord::InternalMetadata[:environment]").strip
assert_equal "test", rails("runner", "-e", "test", "puts ActiveRecord::InternalMetadata[:environment]").strip
assert_equal "development", rails("runner", "puts ActiveRecord::InternalMetadata[:environment]").strip
end

test "db:structure:dump does not dump schema information when no migrations are used" do
Expand All @@ -445,16 +445,16 @@ def db_structure_dump_and_load(expected_database)

list_tables = lambda { rails("runner", "p ActiveRecord::Base.connection.tables").strip }

assert_match '["posts"]', list_tables[].to_s
assert_equal '["posts"]', list_tables[]
rails "db:schema:load"
assert_match '["posts", "comments", "schema_migrations", "ar_internal_metadata"]', list_tables[].to_s
assert_equal '["posts", "comments", "schema_migrations", "ar_internal_metadata"]', list_tables[]

app_file "db/structure.sql", <<-SQL
CREATE TABLE "users" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar(255));
SQL

rails "db:structure:load"
assert_match '["posts", "comments", "schema_migrations", "ar_internal_metadata", "users"]', list_tables[].to_s
assert_equal '["posts", "comments", "schema_migrations", "ar_internal_metadata", "users"]', list_tables[]
end

test "db:schema:load with inflections" do
Expand All @@ -480,7 +480,7 @@ def db_structure_dump_and_load(expected_database)
assert_match(/"geese"/, tables)

columns = rails("runner", "p ActiveRecord::Base.connection.columns('geese').map(&:name)").strip
assert_includes columns, '["gooseid", "name"]'
assert_equal columns, '["gooseid", "name"]'
end

test "db:schema:load fails if schema.rb doesn't exist yet" do
Expand Down Expand Up @@ -539,8 +539,8 @@ def db_test_load_structure
test_environment = lambda { rails("runner", "-e", "test", "puts ActiveRecord::InternalMetadata[:environment]").strip }
development_environment = lambda { rails("runner", "puts ActiveRecord::InternalMetadata[:environment]").strip }

assert_match "test", test_environment.call
assert_match "development", development_environment.call
assert_equal "test", test_environment.call
assert_equal "development", development_environment.call

app_file "db/structure.sql", ""
app_file "config/initializers/enable_sql_schema_format.rb", <<-RUBY
Expand All @@ -549,8 +549,8 @@ def db_test_load_structure

rails "db:setup"

assert_match "test", test_environment.call
assert_match "development", development_environment.call
assert_equal "test", test_environment.call
assert_equal "development", development_environment.call
end

test "db:test:prepare sets test ar_internal_metadata" do
Expand All @@ -559,7 +559,7 @@ def db_test_load_structure

test_environment = lambda { rails("runner", "-e", "test", "puts ActiveRecord::InternalMetadata[:environment]").strip }

assert_match "test", test_environment.call
assert_equal "test", test_environment.call

app_file "db/structure.sql", ""
app_file "config/initializers/enable_sql_schema_format.rb", <<-RUBY
Expand All @@ -568,7 +568,7 @@ def db_test_load_structure

rails "db:test:prepare"

assert_match "test", test_environment.call
assert_equal "test", test_environment.call
end

test "db:seed:replant truncates all non-internal tables and loads the seeds" do
Expand Down
2 changes: 1 addition & 1 deletion railties/test/application/rake/migrations_test.rb
Expand Up @@ -179,7 +179,7 @@ class TwoMigration < ActiveRecord::Migration::Current

test "migration status when schema migrations table is not present" do
output = rails("db:migrate:status", allow_failure: true)
assert_match "Schema migrations table does not exist yet.\n", output
assert_equal "Schema migrations table does not exist yet.\n", output
end

test "migration status" do
Expand Down
6 changes: 3 additions & 3 deletions railties/test/application/rake/multi_dbs_test.rb
Expand Up @@ -107,8 +107,8 @@ def db_migrate_and_schema_dump_and_load(format)
ar_tables = lambda { rails("runner", "p ActiveRecord::Base.connection.tables").strip }
animals_tables = lambda { rails("runner", "p AnimalsBase.connection.tables").strip }

assert_match '["schema_migrations", "ar_internal_metadata", "books"]', ar_tables[]
assert_match '["schema_migrations", "ar_internal_metadata", "dogs"]', animals_tables[]
assert_equal '["schema_migrations", "ar_internal_metadata", "books"]', ar_tables[]
assert_equal '["schema_migrations", "ar_internal_metadata", "dogs"]', animals_tables[]
end
end

Expand Down Expand Up @@ -380,7 +380,7 @@ class TwoMigration < ActiveRecord::Migration::Current
RUBY

output = rails("db:seed")
assert_includes output, "db/development.sqlite3"
assert_equal output, "db/development.sqlite3"
ensure
ENV["RAILS_ENV"] = @old_rails_env
ENV["RACK_ENV"] = @old_rack_env
Expand Down
2 changes: 1 addition & 1 deletion railties/test/application/runner_test.rb
Expand Up @@ -39,7 +39,7 @@ def test_should_run_ruby_statement

def test_should_set_argv_when_running_code
output = rails("runner", "puts ARGV.join(',')", "--foo", "a1", "-b", "a2", "a3", "--moo")
assert_match "--foo,a1,-b,a2,a3,--moo", output.chomp
assert_equal "--foo,a1,-b,a2,a3,--moo", output.chomp
end

def test_should_run_file
Expand Down
16 changes: 8 additions & 8 deletions railties/test/commands/notes_test.rb
Expand Up @@ -17,7 +17,7 @@ class Rails::Command::NotesTest < ActiveSupport::TestCase

app_file "some_other_dir/blah.rb", "# TODO: note in some_other directory"

assert_match <<~OUTPUT, run_notes_command
assert_equal <<~OUTPUT, run_notes_command
app/controllers/some_controller.rb:
* [ 1] [OPTIMIZE] note in app directory
Expand All @@ -37,7 +37,7 @@ class Rails::Command::NotesTest < ActiveSupport::TestCase
end

test "`rails notes` displays an empty string when no results were found" do
assert_match "", run_notes_command
assert_equal "", run_notes_command
end

test "`rails notes --annotations` displays results for a single annotation without being prefixed by a tag" do
Expand All @@ -47,7 +47,7 @@ class Rails::Command::NotesTest < ActiveSupport::TestCase
app_file "app/controllers/some_controller.rb", "# OPTIMIZE: note in app directory"
app_file "config/initializers/some_initializer.rb", "# TODO: note in config directory"

assert_match <<~OUTPUT, run_notes_command(["--annotations", "FIXME"])
assert_equal <<~OUTPUT, run_notes_command(["--annotations", "FIXME"])
db/some_seeds.rb:
* [1] note in db directory
Expand All @@ -64,7 +64,7 @@ class Rails::Command::NotesTest < ActiveSupport::TestCase

app_file "test/some_test.rb", "# FIXME: note in test directory"

assert_match <<~OUTPUT, run_notes_command(["--annotations", "FOOBAR", "TODO"])
assert_equal <<~OUTPUT, run_notes_command(["--annotations", "FOOBAR", "TODO"])
app/controllers/some_controller.rb:
* [1] [FOOBAR] note in app directory
Expand All @@ -85,7 +85,7 @@ class Rails::Command::NotesTest < ActiveSupport::TestCase

add_to_config "config.annotations.register_directories \"spec\""

assert_match <<~OUTPUT, run_notes_command
assert_equal <<~OUTPUT, run_notes_command
db/some_seeds.rb:
* [1] [FIXME] note in db directory
Expand All @@ -108,7 +108,7 @@ class Rails::Command::NotesTest < ActiveSupport::TestCase
app_file "app/assets/stylesheets/application.css.scss", "// TODO: note in scss"
app_file "app/assets/stylesheets/application.css.sass", "// TODO: note in sass"

assert_match <<~OUTPUT, run_notes_command
assert_equal <<~OUTPUT, run_notes_command
app/assets/stylesheets/application.css.sass:
* [1] [TODO] note in sass
Expand All @@ -128,7 +128,7 @@ class Rails::Command::NotesTest < ActiveSupport::TestCase

add_to_config 'config.annotations.register_tags "TESTME", "DEPRECATEME"'

assert_match <<~OUTPUT, run_notes_command
assert_equal <<~OUTPUT, run_notes_command
app/controllers/hello_controller.rb:
* [1] [DEPRECATEME] this action is no longer needed
Expand All @@ -149,7 +149,7 @@ class Rails::Command::NotesTest < ActiveSupport::TestCase

add_to_config 'config.annotations.register_tags "TESTME", "DEPRECATEME"'

assert_match <<~OUTPUT, run_notes_command
assert_equal <<~OUTPUT, run_notes_command
app/controllers/hello_controller.rb:
* [1] [DEPRECATEME] this action is no longer needed
Expand Down

0 comments on commit 5e6c331

Please sign in to comment.