From 1b736a8720f401d9f436d558c3fe9e78dc2a1375 Mon Sep 17 00:00:00 2001 From: Yasuo Honda Date: Fri, 16 Sep 2022 14:45:36 +0900 Subject: [PATCH] Do not run tests that have `ensure` instead of `skip` Even if tests are "skipped" by `Minitest::Assertions#skip` `ensure` is executed because tests methods are Ruby methods. Adding conditions outside of the test methods, entire test methods are executed only when they are necessary. --- .../connection_handler_test.rb | 46 ++++++++--------- activerecord/test/cases/query_cache_test.rb | 47 ++++++++--------- railties/test/console_helpers.rb | 8 +-- railties/test/engine/commands_test.rb | 50 +++++++++---------- 4 files changed, 74 insertions(+), 77 deletions(-) diff --git a/activerecord/test/cases/connection_adapters/connection_handler_test.rb b/activerecord/test/cases/connection_adapters/connection_handler_test.rb index d9eeaf2482d..ac46d30b298 100644 --- a/activerecord/test/cases/connection_adapters/connection_handler_test.rb +++ b/activerecord/test/cases/connection_adapters/connection_handler_test.rb @@ -423,38 +423,38 @@ module ActiveRecord rd.close end - def test_pool_from_any_process_for_uses_most_recent_spec - skip unless current_adapter?(:SQLite3Adapter) + if current_adapter?(:SQLite3Adapter) + def test_pool_from_any_process_for_uses_most_recent_spec + file = Tempfile.new "lol.sqlite3" - file = Tempfile.new "lol.sqlite3" + rd, wr = IO.pipe + rd.binmode + wr.binmode - rd, wr = IO.pipe - rd.binmode - wr.binmode + pid = fork do + config_hash = ActiveRecord::Base.configurations.configs_for(env_name: "arunit", name: "primary").configuration_hash.merge(database: file.path) + ActiveRecord::Base.establish_connection(config_hash) - pid = fork do - config_hash = ActiveRecord::Base.configurations.configs_for(env_name: "arunit", name: "primary").configuration_hash.merge(database: file.path) - ActiveRecord::Base.establish_connection(config_hash) + pid2 = fork do + wr.write ActiveRecord::Base.connection_db_config.database + wr.close + end - pid2 = fork do - wr.write ActiveRecord::Base.connection_db_config.database - wr.close + Process.waitpid pid2 end - Process.waitpid pid2 - end + Process.waitpid pid - Process.waitpid pid + wr.close - wr.close + assert_equal file.path, rd.read - assert_equal file.path, rd.read - - rd.close - ensure - if file - file.close - file.unlink + rd.close + ensure + if file + file.close + file.unlink + end end end end diff --git a/activerecord/test/cases/query_cache_test.rb b/activerecord/test/cases/query_cache_test.rb index 51094d1cda0..7cd6f590d0e 100644 --- a/activerecord/test/cases/query_cache_test.rb +++ b/activerecord/test/cases/query_cache_test.rb @@ -565,37 +565,38 @@ class QueryCacheTest < ActiveRecord::TestCase }.call({}) end - def test_clear_query_cache_is_called_on_all_connections - skip "with in memory db, reading role won't be able to see database on writing role" if in_memory_db? - - ActiveRecord::Base.connected_to(role: :reading) do - db_config = ActiveRecord::Base.configurations.configs_for(env_name: "arunit", name: "primary") - ActiveRecord::Base.establish_connection(db_config) - end - - mw = middleware { |env| + # with in memory db, reading role won't be able to see database on writing role + unless in_memory_db? + def test_clear_query_cache_is_called_on_all_connections ActiveRecord::Base.connected_to(role: :reading) do - @topic = Topic.first + db_config = ActiveRecord::Base.configurations.configs_for(env_name: "arunit", name: "primary") + ActiveRecord::Base.establish_connection(db_config) end - assert @topic + mw = middleware { |env| + ActiveRecord::Base.connected_to(role: :reading) do + @topic = Topic.first + end - ActiveRecord::Base.connected_to(role: :writing) do - @topic.title = "Topic title" - @topic.save! - end + assert @topic - assert_equal "Topic title", @topic.title + ActiveRecord::Base.connected_to(role: :writing) do + @topic.title = "Topic title" + @topic.save! + end - ActiveRecord::Base.connected_to(role: :reading) do - @topic = Topic.first assert_equal "Topic title", @topic.title - end - } - mw.call({}) - ensure - clean_up_connection_handler + ActiveRecord::Base.connected_to(role: :reading) do + @topic = Topic.first + assert_equal "Topic title", @topic.title + end + } + + mw.call({}) + ensure + clean_up_connection_handler + end end test "query cache is enabled in threads with shared connection" do diff --git a/railties/test/console_helpers.rb b/railties/test/console_helpers.rb index 67f55fdc45b..10d3a546025 100644 --- a/railties/test/console_helpers.rb +++ b/railties/test/console_helpers.rb @@ -18,8 +18,8 @@ module ConsoleHelpers assert_includes output, expected, "#{expected.inspect} expected, but got:\n\n#{output}" end - - def available_pty? - defined?(PTY) && PTY.respond_to?(:open) - end +end + +def available_pty? + defined?(PTY) && PTY.respond_to?(:open) end diff --git a/railties/test/engine/commands_test.rb b/railties/test/engine/commands_test.rb index a7534772a58..da750ba6043 100644 --- a/railties/test/engine/commands_test.rb +++ b/railties/test/engine/commands_test.rb @@ -30,35 +30,31 @@ class Rails::Engine::CommandsTest < ActiveSupport::TestCase assert_equal "test", output.strip end - def test_console_command_work_inside_engine - skip "PTY unavailable" unless available_pty? + if available_pty? + def test_console_command_work_inside_engine + primary, replica = PTY.open + cmd = "console --singleline" + spawn_command(cmd, replica) + assert_output(">", primary) + ensure + primary.puts "quit" + end - primary, replica = PTY.open - cmd = "console --singleline" - spawn_command(cmd, replica) - assert_output(">", primary) - ensure - primary.puts "quit" - end + def test_dbconsole_command_work_inside_engine + primary, replica = PTY.open + spawn_command("dbconsole", replica) + assert_output("sqlite>", primary) + ensure + primary.puts ".exit" + end - def test_dbconsole_command_work_inside_engine - skip "PTY unavailable" unless available_pty? - - primary, replica = PTY.open - spawn_command("dbconsole", replica) - assert_output("sqlite>", primary) - ensure - primary.puts ".exit" - end - - def test_server_command_work_inside_engine - skip "PTY unavailable" unless available_pty? - - primary, replica = PTY.open - pid = spawn_command("server", replica) - assert_output("Listening on", primary) - ensure - kill(pid) + def test_server_command_work_inside_engine + primary, replica = PTY.open + pid = spawn_command("server", replica) + assert_output("Listening on", primary) + ensure + kill(pid) + end end private