From f38a660a60ecc5e2f0e0f91e07a18344598cff5e Mon Sep 17 00:00:00 2001 From: "yuuji.yaginuma" Date: Wed, 8 Feb 2017 18:24:50 +0900 Subject: Prevent multiple values being set to `run_via` When executing the test via rake, since `rake` is set for `run_via`, `ruby` should not be set. Related 2cb6c27310452da11b93d729c3b760ce988106e1 --- railties/lib/rails/commands/test/test_command.rb | 2 +- .../generators/rails/plugin/templates/bin/test.tt | 2 +- railties/lib/rails/test_unit/minitest_plugin.rb | 34 ++++++++++++++++++---- 3 files changed, 31 insertions(+), 7 deletions(-) (limited to 'railties/lib/rails') diff --git a/railties/lib/rails/commands/test/test_command.rb b/railties/lib/rails/commands/test/test_command.rb index 7bf8f61137..629fb5b425 100644 --- a/railties/lib/rails/commands/test/test_command.rb +++ b/railties/lib/rails/commands/test/test_command.rb @@ -11,7 +11,7 @@ module Rails def perform(*) $LOAD_PATH << Rails::Command.root.join("test") - Minitest.run_via[:rails] = true + Minitest.run_via = :rails require "active_support/testing/autorun" end diff --git a/railties/lib/rails/generators/rails/plugin/templates/bin/test.tt b/railties/lib/rails/generators/rails/plugin/templates/bin/test.tt index c0fbb84a93..35a9bf8c8b 100644 --- a/railties/lib/rails/generators/rails/plugin/templates/bin/test.tt +++ b/railties/lib/rails/generators/rails/plugin/templates/bin/test.tt @@ -5,6 +5,6 @@ require 'rails/test_unit/minitest_plugin' Rails::TestUnitReporter.executable = 'bin/test' -Minitest.run_via[:rails] = true +Minitest.run_via = :rails 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 4df3e7f0f2..7d3da6b529 100644 --- a/railties/lib/rails/test_unit/minitest_plugin.rb +++ b/railties/lib/rails/test_unit/minitest_plugin.rb @@ -59,18 +59,18 @@ module Minitest options[:color] = true options[:output_inline] = true - options[:patterns] = opts.order! unless run_via[:rake] + options[:patterns] = opts.order! unless run_via.rake? end def self.rake_run(patterns) # :nodoc: - run_via[:rake] = true + self.run_via = :rake unless run_via.set? ::Rails::TestRequirer.require_files(patterns) autorun end module RunRespectingRakeTestopts def run(args = []) - if run_via[:rake] + if run_via.rake? args = Shellwords.split(ENV["TESTOPTS"] || "") end @@ -87,7 +87,7 @@ module Minitest # If run via `ruby` we've been passed the files to run directly, or if run # via `rake` then they have already been eagerly required. - unless run_via[:ruby] || run_via[:rake] + unless run_via.ruby? || run_via.rake? ::Rails::TestRequirer.require_files(options[:patterns]) end @@ -102,7 +102,31 @@ module Minitest reporter << ::Rails::TestUnitReporter.new(options[:io], options) end - mattr_accessor(:run_via) { Hash.new } + def self.run_via=(runner) + if run_via.set? + raise ArgumentError, "run_via already assigned" + else + run_via.runner = runner + end + end + + class RunVia + attr_accessor :runner + alias set? runner + + # Backwardscompatibility with Rails 5.0 generated plugin test scripts. + alias []= runner= + + def ruby? + runner == :ruby + end + + def rake? + runner == :rake + end + end + + mattr_reader(:run_via) { RunVia.new } end # Put Rails as the first plugin minitest initializes so other plugins -- cgit v1.2.3