diff options
author | Kasper Timm Hansen <kaspth@gmail.com> | 2016-09-25 20:21:30 +0200 |
---|---|---|
committer | Kasper Timm Hansen <kaspth@gmail.com> | 2016-09-25 20:36:30 +0200 |
commit | 29f0fbd5db643b885665e4a14c7de3bf6c0d9c96 (patch) | |
tree | 038216e9a43f47c8ecc6c54dd4f8d6ea5d91f200 | |
parent | 0894cb98d1019fd0c64de83129e946d188c40f07 (diff) | |
download | rails-29f0fbd5db643b885665e4a14c7de3bf6c0d9c96.tar.gz rails-29f0fbd5db643b885665e4a14c7de3bf6c0d9c96.tar.bz2 rails-29f0fbd5db643b885665e4a14c7de3bf6c0d9c96.zip |
Revise setting of run_with_rails_extension.
The Rails test runner supports three ways to run tests: directly, via rake, or ruby.
When Running with Ruby ala `ruby -Itest test/models/post_test.rb` our test file would
be evaluated first, requiring `test_helper` and then `active_support/testing/autorun`
that would then require the test file (which it hadn't been before) thus reevaluating
it. This caused exceptions if using Active Support's declarative syntax.
Fix this by shifting around when we set the how we're run to closer mimick the require
order.
If we're running with `bin/rails test` the test command file is run first and we then
set `run_with_rails_extension`, later we hit `active_support/testing/autorun` and do
nothing — because we've been run elsewhere.
If we at this point haven't set `run_with_rails_extension` we've been running with
`ruby` this whole time and thus we set that.
We should always trigger `Minitest.autorun` as it doesn't hurt to call it twice.
Consolidate the two methods into a single one that better brings out the intent of
why they're there.
-rw-r--r-- | activesupport/lib/active_support/testing/autorun.rb | 11 | ||||
-rw-r--r-- | railties/lib/rails/commands/test.rb | 5 | ||||
-rw-r--r-- | railties/lib/rails/test_unit/minitest_plugin.rb | 10 |
3 files changed, 13 insertions, 13 deletions
diff --git a/activesupport/lib/active_support/testing/autorun.rb b/activesupport/lib/active_support/testing/autorun.rb index 898ef209da..2e7b7246aa 100644 --- a/activesupport/lib/active_support/testing/autorun.rb +++ b/activesupport/lib/active_support/testing/autorun.rb @@ -2,11 +2,8 @@ gem "minitest" require "minitest" -if Minitest.respond_to?(:run_with_rails_extension) - unless Minitest.run_with_rails_extension - Minitest.run_with_autorun = true - Minitest.autorun - end -else - Minitest.autorun +if Minitest.respond_to?(:run_with_rails_extension) && !Minitest.run_with_rails_extension + Minitest.run_with_autorun = true end + +Minitest.autorun diff --git a/railties/lib/rails/commands/test.rb b/railties/lib/rails/commands/test.rb index 0ce2dd19b7..56d1021fab 100644 --- a/railties/lib/rails/commands/test.rb +++ b/railties/lib/rails/commands/test.rb @@ -1,8 +1,11 @@ require "rails/test_unit/minitest_plugin" -require "active_support/testing/autorun" if defined?(ENGINE_ROOT) $LOAD_PATH << File.expand_path("test", ENGINE_ROOT) else $LOAD_PATH << File.expand_path("../../test", APP_PATH) end + +Minitest.run_via[:rails] = true + +require "active_support/testing/autorun" diff --git a/railties/lib/rails/test_unit/minitest_plugin.rb b/railties/lib/rails/test_unit/minitest_plugin.rb index e15c6b3a38..0544847293 100644 --- a/railties/lib/rails/test_unit/minitest_plugin.rb +++ b/railties/lib/rails/test_unit/minitest_plugin.rb @@ -69,11 +69,12 @@ module Minitest # Owes great inspiration to test runner trailblazers like RSpec, # minitest-reporters, maxitest and others. def self.plugin_rails_init(options) - self.run_with_rails_extension = true - ENV["RAILS_ENV"] = options[:environment] || "test" - ::Rails::TestRequirer.require_files(options[:patterns]) unless run_with_autorun + # If run via `ruby` we've been passed the files to run directly. + unless run_via[:ruby] + ::Rails::TestRequirer.require_files(options[:patterns]) + end unless options[:full_backtrace] || ENV["BACKTRACE"] # Plugin can run without Rails loaded, check before filtering. @@ -86,8 +87,7 @@ module Minitest reporter << ::Rails::TestUnitReporter.new(options[:io], options) end - mattr_accessor(:run_with_autorun) { false } - mattr_accessor(:run_with_rails_extension) { false } + mattr_accessor(:run_via) { Hash.new } end # Put Rails as the first plugin minitest initializes so other plugins |