Skip to content

Commit

Permalink
Merge pull request #44106 from etiennebarrie/no-double-boot-for-test-…
Browse files Browse the repository at this point in the history
…tasks

Avoid booting in development then test for test tasks
  • Loading branch information
byroot committed Jun 1, 2022
2 parents e0b58f6 + a627ef4 commit 44dd3d7
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 2 deletions.
8 changes: 8 additions & 0 deletions railties/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
* Avoid booting in development then test for test tasks.

Running one of the rails test subtasks (e.g. test:system, test:models) would
go through Rake and cause the app to be booted twice. Now all the test:*
subtasks are defined as Thor tasks and directly load the test environment.

*Étienne Barrié*

* Deprecate `Rails::Generators::Testing::Behaviour` in favor of `Rails::Generators::Testing::Behavior`.

*Gannon McGibbon*
Expand Down
3 changes: 2 additions & 1 deletion railties/lib/rails/command/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,8 @@ def relative_command_path
end

def namespaced_commands
commands.keys.map do |key|
commands.filter_map do |key, command|
next if command.hidden?
if command_root_namespace.match?(/(\A|:)#{key}\z/)
command_root_namespace
else
Expand Down
25 changes: 25 additions & 0 deletions railties/lib/rails/commands/test/test_command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,31 @@ def perform(*)
Rails::TestUnit::Runner.parse_options(args)
Rails::TestUnit::Runner.run(args)
end

# Define Thor tasks to avoid going through Rake and booting twice when using bin/rails test:*

Rails::TestUnit::Runner::TEST_FOLDERS.each do |name|
define_method(name) do |*|
self.args.prepend("test/#{name}")
perform
end
end

desc "test:all", "Runs all tests, including system tests", hide: true
def all
self.args = ["test/**/*_test.rb"]
perform
end

def system
self.args = ["test/system"]
perform
end

def generators
self.args = ["test/lib/generators"]
perform
end
end
end
end
1 change: 1 addition & 0 deletions railties/lib/rails/test_unit/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
module Rails
module TestUnit
class Runner
TEST_FOLDERS = [:models, :helpers, :channels, :controllers, :mailers, :integration, :jobs, :mailboxes]
mattr_reader :filters, default: []

class << self
Expand Down
2 changes: 1 addition & 1 deletion railties/lib/rails/test_unit/testing.rake
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ namespace :test do
success || exit(false)
end

["models", "helpers", "channels", "controllers", "mailers", "integration", "jobs", "mailboxes"].each do |name|
Rails::TestUnit::Runner::TEST_FOLDERS.each do |name|
task name => "test:prepare" do
Rails::TestUnit::Runner.rake_run(["test/#{name}"])
end
Expand Down
9 changes: 9 additions & 0 deletions railties/test/command/base_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@ class Rails::Command::BaseTest < ActiveSupport::TestCase
assert_equal %w(db:system:change), Rails::Command::Db::System::ChangeCommand.printing_commands
end

test "printing commands hides hidden commands" do
class Rails::Command::HiddenCommand < Rails::Command::Base
desc "command", "Hidden command", hide: true
def command
end
end
assert_equal [], Rails::Command::HiddenCommand.printing_commands
end

test "ARGV is populated" do
class Rails::Command::ArgvCommand < Rails::Command::Base
def check_populated(*args)
Expand Down

0 comments on commit 44dd3d7

Please sign in to comment.