提交 29f0fbd5 编写于 作者: K Kasper Timm Hansen

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.
上级 0894cb98
...@@ -2,11 +2,8 @@ ...@@ -2,11 +2,8 @@
require "minitest" require "minitest"
if Minitest.respond_to?(:run_with_rails_extension) if Minitest.respond_to?(:run_with_rails_extension) && !Minitest.run_with_rails_extension
unless Minitest.run_with_rails_extension Minitest.run_with_autorun = true
Minitest.run_with_autorun = true
Minitest.autorun
end
else
Minitest.autorun
end end
Minitest.autorun
require "rails/test_unit/minitest_plugin" require "rails/test_unit/minitest_plugin"
require "active_support/testing/autorun"
if defined?(ENGINE_ROOT) if defined?(ENGINE_ROOT)
$LOAD_PATH << File.expand_path("test", ENGINE_ROOT) $LOAD_PATH << File.expand_path("test", ENGINE_ROOT)
else else
$LOAD_PATH << File.expand_path("../../test", APP_PATH) $LOAD_PATH << File.expand_path("../../test", APP_PATH)
end end
Minitest.run_via[:rails] = true
require "active_support/testing/autorun"
...@@ -69,11 +69,12 @@ def self.rake_run(patterns) # :nodoc: ...@@ -69,11 +69,12 @@ def self.rake_run(patterns) # :nodoc:
# Owes great inspiration to test runner trailblazers like RSpec, # Owes great inspiration to test runner trailblazers like RSpec,
# minitest-reporters, maxitest and others. # minitest-reporters, maxitest and others.
def self.plugin_rails_init(options) def self.plugin_rails_init(options)
self.run_with_rails_extension = true
ENV["RAILS_ENV"] = options[:environment] || "test" 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"] unless options[:full_backtrace] || ENV["BACKTRACE"]
# Plugin can run without Rails loaded, check before filtering. # Plugin can run without Rails loaded, check before filtering.
...@@ -86,8 +87,7 @@ def self.plugin_rails_init(options) ...@@ -86,8 +87,7 @@ def self.plugin_rails_init(options)
reporter << ::Rails::TestUnitReporter.new(options[:io], options) reporter << ::Rails::TestUnitReporter.new(options[:io], options)
end end
mattr_accessor(:run_with_autorun) { false } mattr_accessor(:run_via) { Hash.new }
mattr_accessor(:run_with_rails_extension) { false }
end end
# Put Rails as the first plugin minitest initializes so other plugins # Put Rails as the first plugin minitest initializes so other plugins
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册