From c5532e06d9ef8b75f7443019aa1f34a3b697efdc Mon Sep 17 00:00:00 2001 From: Yves Senn Date: Tue, 30 Dec 2014 10:17:30 +0100 Subject: spike of a `OptionParser` backed test runner. --- railties/lib/rails/commands.rb | 3 +- railties/lib/rails/commands/commands_tasks.rb | 7 ++- railties/lib/rails/commands/test.rb | 9 +++ railties/lib/rails/test_unit/runner.rb | 83 +++++++++++++++++++++++++++ 4 files changed, 100 insertions(+), 2 deletions(-) create mode 100644 railties/lib/rails/commands/test.rb create mode 100644 railties/lib/rails/test_unit/runner.rb (limited to 'railties/lib/rails') diff --git a/railties/lib/rails/commands.rb b/railties/lib/rails/commands.rb index f32bf772a5..12bd73db24 100644 --- a/railties/lib/rails/commands.rb +++ b/railties/lib/rails/commands.rb @@ -6,7 +6,8 @@ aliases = { "c" => "console", "s" => "server", "db" => "dbconsole", - "r" => "runner" + "r" => "runner", + "t" => "test", } command = ARGV.shift diff --git a/railties/lib/rails/commands/commands_tasks.rb b/railties/lib/rails/commands/commands_tasks.rb index 8bae08e44e..d8d4080c3e 100644 --- a/railties/lib/rails/commands/commands_tasks.rb +++ b/railties/lib/rails/commands/commands_tasks.rb @@ -14,6 +14,7 @@ The most common rails commands are: generate Generate new code (short-cut alias: "g") console Start the Rails console (short-cut alias: "c") server Start the Rails server (short-cut alias: "s") + test Run tests (short-cut alias: "t") dbconsole Start a console for the database specified in config/database.yml (short-cut alias: "db") new Create a new Rails application. "rails new my_app" creates a @@ -27,7 +28,7 @@ In addition to those, there are: All commands can be run with -h (or --help) for more information. EOT - COMMAND_WHITELIST = %w(plugin generate destroy console server dbconsole runner new version help) + COMMAND_WHITELIST = %w(plugin generate destroy console server dbconsole runner new version help test) def initialize(argv) @argv = argv @@ -81,6 +82,10 @@ EOT end end + def test + require_command!("test") + end + def dbconsole require_command!("dbconsole") Rails::DBConsole.start diff --git a/railties/lib/rails/commands/test.rb b/railties/lib/rails/commands/test.rb new file mode 100644 index 0000000000..f75b07cf73 --- /dev/null +++ b/railties/lib/rails/commands/test.rb @@ -0,0 +1,9 @@ +ENV["RAILS_ENV"] = "test" + +require "rails/test_unit/runner" +options = Rails::TestRunner::Options.parse(ARGV) + +$: << File.expand_path("../../test", APP_PATH) + +# Config Rails backtrace in tests. +Rails::TestRunner.new(options).run diff --git a/railties/lib/rails/test_unit/runner.rb b/railties/lib/rails/test_unit/runner.rb new file mode 100644 index 0000000000..25f30f6b80 --- /dev/null +++ b/railties/lib/rails/test_unit/runner.rb @@ -0,0 +1,83 @@ +require "ostruct" +require "optparse" +require "rake/file_list" + +module Rails + class TestRunner + class Options + def self.parse(args) + options = { backtrace: false, name: nil } + + opt_parser = ::OptionParser.new do |opts| + opts.banner = "Usage: bin/rails test [options]" + + opts.separator "" + opts.separator "Filter options:" + + opts.on("-n", "--name [NAME]", + "Only run tests matching NAME") do |name| + options[:name] = name + end + + opts.separator "" + opts.separator "Output options:" + + opts.on("-b", "--backtrace", + "show the complte backtrace") do + options[:backtrace] = true + end + + opts.separator "" + opts.separator "Common options:" + + opts.on_tail("-h", "--help", "Show this message") do + puts opts + exit + end + end + + opt_parser.order!(args) + options[:pattern] = ARGV.shift + options + end + end + + def initialize(options = {}) + @options = options + end + + def run + enable_backtrace if @options[:backtrace] + filter_tests_by_name if @options[:name] + + run_tests + end + + private + def run_tests + test_files.to_a.each do |file| + require File.expand_path file + end + end + + NAMED_PATTERNS = { + "models" => "test/models/**/*_test.rb" + } + def test_files + if @options[:pattern] + pattern = NAMED_PATTERNS[@options[:pattern]] + else + pattern = "test/**/*_test.rb" + end + Rake::FileList[pattern] + end + + def enable_backtrace + ENV["BACKTRACE"] = "1" + end + + def filter_tests_by_name + ARGV.push("-n", @options[:name]) + end + end +end -- cgit v1.2.3 From ff0a7611cf912ce7ae32966cc42da5dbdff3a0bc Mon Sep 17 00:00:00 2001 From: Arthur Neves Date: Fri, 23 Jan 2015 16:05:31 -0500 Subject: Add test run by line, and report error by line --- railties/lib/rails/commands/test.rb | 16 +++++++++++++--- railties/lib/rails/test_unit/reporter.rb | 22 ++++++++++++++++++++++ railties/lib/rails/test_unit/runner.rb | 27 +++++++++++++++++++++++++++ 3 files changed, 62 insertions(+), 3 deletions(-) create mode 100644 railties/lib/rails/test_unit/reporter.rb (limited to 'railties/lib/rails') diff --git a/railties/lib/rails/commands/test.rb b/railties/lib/rails/commands/test.rb index f75b07cf73..5c997336a0 100644 --- a/railties/lib/rails/commands/test.rb +++ b/railties/lib/rails/commands/test.rb @@ -1,9 +1,19 @@ ENV["RAILS_ENV"] = "test" - require "rails/test_unit/runner" -options = Rails::TestRunner::Options.parse(ARGV) +require "rails/test_unit/reporter" +options = Rails::TestRunner::Options.parse(ARGV) $: << File.expand_path("../../test", APP_PATH) +$runner = Rails::TestRunner.new(options) + +def Minitest.plugin_rails_init(options) + self.reporter << Rails::TestUnitReporter.new(options[:io], options) + if method = $runner.find_method + options[:filter] = "/^(#{method})$/" + end +end +Minitest.extensions << 'rails' + # Config Rails backtrace in tests. -Rails::TestRunner.new(options).run +$runner.run diff --git a/railties/lib/rails/test_unit/reporter.rb b/railties/lib/rails/test_unit/reporter.rb new file mode 100644 index 0000000000..bd4d455589 --- /dev/null +++ b/railties/lib/rails/test_unit/reporter.rb @@ -0,0 +1,22 @@ +require "minitest" + +module Rails + class TestUnitReporter < Minitest::StatisticsReporter + def report + io.puts + io.puts "Failed test:" + io.puts + io.puts aggregated_results + end + + def aggregated_results # :nodoc: + filtered_results = results.dup + filtered_results.reject!(&:skipped?) unless options[:verbose] + filtered_results.map do |result| + result.failures.map { |failure| + "bin/rails test #{failure.location}\n" + }.join "\n" + end.join + end + end +end diff --git a/railties/lib/rails/test_unit/runner.rb b/railties/lib/rails/test_unit/runner.rb index 25f30f6b80..6f9fdab68a 100644 --- a/railties/lib/rails/test_unit/runner.rb +++ b/railties/lib/rails/test_unit/runner.rb @@ -1,6 +1,7 @@ require "ostruct" require "optparse" require "rake/file_list" +require "method_source" module Rails class TestRunner @@ -53,6 +54,14 @@ module Rails run_tests end + def find_method + return if @line.blank? + method = test_methods.find do |test_method, start_line, end_line| + (start_line..end_line).include?(@line.to_i) + end + method.first if method + end + private def run_tests test_files.to_a.each do |file| @@ -66,6 +75,10 @@ module Rails def test_files if @options[:pattern] pattern = NAMED_PATTERNS[@options[:pattern]] + unless pattern + filename, @line = @options[:pattern].split(':') + return [filename] if filename + end else pattern = "test/**/*_test.rb" end @@ -79,5 +92,19 @@ module Rails def filter_tests_by_name ARGV.push("-n", @options[:name]) end + + def test_methods + methods_map = [] + suites = Minitest::Runnable.runnables.shuffle + suites.each do |suite_class| + suite_class.runnable_methods.each do |test_method| + method = suite_class.instance_method(test_method) + start_line = method.source_location.last + end_line = method.source.split("\n").size + start_line - 1 + methods_map << [test_method, start_line, end_line] + end + end + methods_map + end end end -- cgit v1.2.3 From 5d934caabbf339dccba2837d686ce9ac73a1eeb0 Mon Sep 17 00:00:00 2001 From: Arthur Neves Date: Fri, 23 Jan 2015 16:19:58 -0500 Subject: If a nem given, use that name to filter the test --- railties/lib/rails/test_unit/runner.rb | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) (limited to 'railties/lib/rails') diff --git a/railties/lib/rails/test_unit/runner.rb b/railties/lib/rails/test_unit/runner.rb index 6f9fdab68a..da3d553e53 100644 --- a/railties/lib/rails/test_unit/runner.rb +++ b/railties/lib/rails/test_unit/runner.rb @@ -49,12 +49,12 @@ module Rails def run enable_backtrace if @options[:backtrace] - filter_tests_by_name if @options[:name] run_tests end def find_method + return @options[:name] if @options[:name] return if @line.blank? method = test_methods.find do |test_method, start_line, end_line| (start_line..end_line).include?(@line.to_i) @@ -89,10 +89,6 @@ module Rails ENV["BACKTRACE"] = "1" end - def filter_tests_by_name - ARGV.push("-n", @options[:name]) - end - def test_methods methods_map = [] suites = Minitest::Runnable.runnables.shuffle -- cgit v1.2.3 From cf7ba1fbbd9801b4514b12b2be1242b15c27b550 Mon Sep 17 00:00:00 2001 From: Arthur Neves Date: Fri, 23 Jan 2015 16:25:38 -0500 Subject: no need to escape the method in a regex wrap, minitest does that for us --- railties/lib/rails/commands/test.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'railties/lib/rails') diff --git a/railties/lib/rails/commands/test.rb b/railties/lib/rails/commands/test.rb index 5c997336a0..cd1192eb6e 100644 --- a/railties/lib/rails/commands/test.rb +++ b/railties/lib/rails/commands/test.rb @@ -10,7 +10,7 @@ $runner = Rails::TestRunner.new(options) def Minitest.plugin_rails_init(options) self.reporter << Rails::TestUnitReporter.new(options[:io], options) if method = $runner.find_method - options[:filter] = "/^(#{method})$/" + options[:filter] = method end end Minitest.extensions << 'rails' -- cgit v1.2.3 From d1a5460e3eb939d8de780df9a6530b02fe6d5614 Mon Sep 17 00:00:00 2001 From: Arthur Neves Date: Fri, 23 Jan 2015 20:35:13 -0500 Subject: Better file:line parsing and filter method by file too --- railties/lib/rails/test_unit/runner.rb | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) (limited to 'railties/lib/rails') diff --git a/railties/lib/rails/test_unit/runner.rb b/railties/lib/rails/test_unit/runner.rb index da3d553e53..c76bd58d22 100644 --- a/railties/lib/rails/test_unit/runner.rb +++ b/railties/lib/rails/test_unit/runner.rb @@ -38,7 +38,16 @@ module Rails end opt_parser.order!(args) - options[:pattern] = ARGV.shift + + if arg = args.shift + if NAMED_PATTERNS.key?(arg) + options[:pattern] = arg + else + options[:filename], options[:line] = arg.split(':') + options[:filename] = File.expand_path options[:filename] + options[:line] &&= options[:line].to_i + end + end options end end @@ -55,11 +64,12 @@ module Rails def find_method return @options[:name] if @options[:name] - return if @line.blank? - method = test_methods.find do |test_method, start_line, end_line| - (start_line..end_line).include?(@line.to_i) + return unless @options[:line] + method = test_methods.find do |location, test_method, start_line, end_line| + location == @options[:filename] && + (start_line..end_line).include?(@options[:line].to_i) end - method.first if method + method[1] if method end private @@ -73,12 +83,9 @@ module Rails "models" => "test/models/**/*_test.rb" } def test_files + return [@options[:filename]] if @options[:filename] if @options[:pattern] pattern = NAMED_PATTERNS[@options[:pattern]] - unless pattern - filename, @line = @options[:pattern].split(':') - return [filename] if filename - end else pattern = "test/**/*_test.rb" end @@ -95,9 +102,10 @@ module Rails suites.each do |suite_class| suite_class.runnable_methods.each do |test_method| method = suite_class.instance_method(test_method) - start_line = method.source_location.last + location = method.source_location + start_line = location.last end_line = method.source.split("\n").size + start_line - 1 - methods_map << [test_method, start_line, end_line] + methods_map << [location.first, test_method, start_line, end_line] end end methods_map -- cgit v1.2.3 From 5ae2c7060f6a1bf23fd598eb92ff5417ac589dcc Mon Sep 17 00:00:00 2001 From: Arthur Neves Date: Fri, 23 Jan 2015 20:40:33 -0500 Subject: Dont display Failed test if suite passed --- railties/lib/rails/test_unit/reporter.rb | 1 + 1 file changed, 1 insertion(+) (limited to 'railties/lib/rails') diff --git a/railties/lib/rails/test_unit/reporter.rb b/railties/lib/rails/test_unit/reporter.rb index bd4d455589..6d33fae911 100644 --- a/railties/lib/rails/test_unit/reporter.rb +++ b/railties/lib/rails/test_unit/reporter.rb @@ -3,6 +3,7 @@ require "minitest" module Rails class TestUnitReporter < Minitest::StatisticsReporter def report + return if passed? io.puts io.puts "Failed test:" io.puts -- cgit v1.2.3 From 739cfd5d90aff0d8eba3d3937df63afaa6ad106f Mon Sep 17 00:00:00 2001 From: Arthur Neves Date: Fri, 23 Jan 2015 20:58:48 -0500 Subject: Move some logic to runner --- railties/lib/rails/commands/test.rb | 14 +------------- railties/lib/rails/test_unit/runner.rb | 10 ++++++++++ 2 files changed, 11 insertions(+), 13 deletions(-) (limited to 'railties/lib/rails') diff --git a/railties/lib/rails/commands/test.rb b/railties/lib/rails/commands/test.rb index cd1192eb6e..1efd010dc7 100644 --- a/railties/lib/rails/commands/test.rb +++ b/railties/lib/rails/commands/test.rb @@ -1,19 +1,7 @@ ENV["RAILS_ENV"] = "test" require "rails/test_unit/runner" -require "rails/test_unit/reporter" options = Rails::TestRunner::Options.parse(ARGV) $: << File.expand_path("../../test", APP_PATH) -$runner = Rails::TestRunner.new(options) - -def Minitest.plugin_rails_init(options) - self.reporter << Rails::TestUnitReporter.new(options[:io], options) - if method = $runner.find_method - options[:filter] = method - end -end -Minitest.extensions << 'rails' - -# Config Rails backtrace in tests. -$runner.run +Rails::TestRunner.new(options).run diff --git a/railties/lib/rails/test_unit/runner.rb b/railties/lib/rails/test_unit/runner.rb index c76bd58d22..31ce94a503 100644 --- a/railties/lib/rails/test_unit/runner.rb +++ b/railties/lib/rails/test_unit/runner.rb @@ -2,6 +2,7 @@ require "ostruct" require "optparse" require "rake/file_list" require "method_source" +require "rails/test_unit/reporter" module Rails class TestRunner @@ -59,6 +60,15 @@ module Rails def run enable_backtrace if @options[:backtrace] + $rails_test_runner = self + def Minitest.plugin_rails_init(options) + self.reporter << Rails::TestUnitReporter.new(options[:io], options) + if method = $rails_test_runner.find_method + options[:filter] = method + end + end + Minitest.extensions << 'rails' + run_tests end -- cgit v1.2.3 From a71bcdb4f3ab25594955277fa7a7c49c84cd0925 Mon Sep 17 00:00:00 2001 From: Arthur Neves Date: Fri, 23 Jan 2015 21:26:58 -0500 Subject: Move minitest plugin Minitest Rails plugin should be loaded on test_help, so we report errors even when not running from the runner. Also fix the backtrace --- railties/lib/rails/test_help.rb | 7 +------ railties/lib/rails/test_unit/minitest_plugin.rb | 15 +++++++++++++++ railties/lib/rails/test_unit/runner.rb | 19 ++++--------------- 3 files changed, 20 insertions(+), 21 deletions(-) create mode 100644 railties/lib/rails/test_unit/minitest_plugin.rb (limited to 'railties/lib/rails') diff --git a/railties/lib/rails/test_help.rb b/railties/lib/rails/test_help.rb index 8953e5fd48..d52bb46728 100644 --- a/railties/lib/rails/test_help.rb +++ b/railties/lib/rails/test_help.rb @@ -2,6 +2,7 @@ # so fixtures aren't loaded into that environment abort("Abort testing: Your Rails environment is running in production mode!") if Rails.env.production? +require "rails/test_unit/minitest_plugin" require 'active_support/testing/autorun' require 'active_support/test_case' require 'action_controller' @@ -9,12 +10,6 @@ require 'action_controller/test_case' require 'action_dispatch/testing/integration' require 'rails/generators/test_case' -# Config Rails backtrace in tests. -require 'rails/backtrace_cleaner' -if ENV["BACKTRACE"].nil? - Minitest.backtrace_filter = Rails.backtrace_cleaner -end - if defined?(ActiveRecord::Base) ActiveRecord::Migration.maintain_test_schema! diff --git a/railties/lib/rails/test_unit/minitest_plugin.rb b/railties/lib/rails/test_unit/minitest_plugin.rb new file mode 100644 index 0000000000..149aad372c --- /dev/null +++ b/railties/lib/rails/test_unit/minitest_plugin.rb @@ -0,0 +1,15 @@ +require "minitest" +require "rails/test_unit/reporter" + +def Minitest.plugin_rails_init(options) + self.reporter << Rails::TestUnitReporter.new(options[:io], options) + if $rails_test_runner && (method = $rails_test_runner.find_method) + options[:filter] = method + end + + if ENV["BACKTRACE"].nil? && !($rails_test_runner && $rails_test_runner.show_backtrace?) + Minitest.backtrace_filter = Rails.backtrace_cleaner + end +end +Minitest.extensions << 'rails' + diff --git a/railties/lib/rails/test_unit/runner.rb b/railties/lib/rails/test_unit/runner.rb index 31ce94a503..c777091b57 100644 --- a/railties/lib/rails/test_unit/runner.rb +++ b/railties/lib/rails/test_unit/runner.rb @@ -2,7 +2,6 @@ require "ostruct" require "optparse" require "rake/file_list" require "method_source" -require "rails/test_unit/reporter" module Rails class TestRunner @@ -58,17 +57,7 @@ module Rails end def run - enable_backtrace if @options[:backtrace] - $rails_test_runner = self - def Minitest.plugin_rails_init(options) - self.reporter << Rails::TestUnitReporter.new(options[:io], options) - if method = $rails_test_runner.find_method - options[:filter] = method - end - end - Minitest.extensions << 'rails' - run_tests end @@ -82,6 +71,10 @@ module Rails method[1] if method end + def show_backtrace? + @options[:backtrace] + end + private def run_tests test_files.to_a.each do |file| @@ -102,10 +95,6 @@ module Rails Rake::FileList[pattern] end - def enable_backtrace - ENV["BACKTRACE"] = "1" - end - def test_methods methods_map = [] suites = Minitest::Runnable.runnables.shuffle -- cgit v1.2.3 From ccee7eea8e5a8a34256467dd1096ac549dd17362 Mon Sep 17 00:00:00 2001 From: Arthur Neves Date: Fri, 23 Jan 2015 22:57:52 -0500 Subject: Show the right file when test raises --- railties/lib/rails/test_unit/reporter.rb | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) (limited to 'railties/lib/rails') diff --git a/railties/lib/rails/test_unit/reporter.rb b/railties/lib/rails/test_unit/reporter.rb index 6d33fae911..e82c4e653c 100644 --- a/railties/lib/rails/test_unit/reporter.rb +++ b/railties/lib/rails/test_unit/reporter.rb @@ -14,10 +14,9 @@ module Rails filtered_results = results.dup filtered_results.reject!(&:skipped?) unless options[:verbose] filtered_results.map do |result| - result.failures.map { |failure| - "bin/rails test #{failure.location}\n" - }.join "\n" - end.join + location, line = result.method(result.name).source_location + "bin/rails test #{location}:#{line}" + end.join "\n" end end end -- cgit v1.2.3 From 7e9775bdb03f2f6648fe58958645fd8e31b5c79b Mon Sep 17 00:00:00 2001 From: Yves Senn Date: Sat, 24 Jan 2015 11:48:17 +0100 Subject: pluralize rerun snippet heading. --- railties/lib/rails/test_unit/reporter.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'railties/lib/rails') diff --git a/railties/lib/rails/test_unit/reporter.rb b/railties/lib/rails/test_unit/reporter.rb index e82c4e653c..0efba7664a 100644 --- a/railties/lib/rails/test_unit/reporter.rb +++ b/railties/lib/rails/test_unit/reporter.rb @@ -5,7 +5,7 @@ module Rails def report return if passed? io.puts - io.puts "Failed test:" + io.puts "Failed tests:" io.puts io.puts aggregated_results end -- cgit v1.2.3 From fb2dc2adcad11cea8573536621d9a0841be7901b Mon Sep 17 00:00:00 2001 From: Yves Senn Date: Sat, 24 Jan 2015 11:56:14 +0100 Subject: document running a test by line number. --- railties/lib/rails/test_unit/runner.rb | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'railties/lib/rails') diff --git a/railties/lib/rails/test_unit/runner.rb b/railties/lib/rails/test_unit/runner.rb index c777091b57..d84f1cf9af 100644 --- a/railties/lib/rails/test_unit/runner.rb +++ b/railties/lib/rails/test_unit/runner.rb @@ -14,6 +14,13 @@ module Rails opts.separator "" opts.separator "Filter options:" + opts.separator "" + opts.separator <<-DESC + You can run a single test by appending the line number to filename: + + bin/rails test test/models/user_test.rb:27 + + DESC opts.on("-n", "--name [NAME]", "Only run tests matching NAME") do |name| -- cgit v1.2.3 From 613bdd9d4c6b024cbc26186f1ad443faa6cdffb1 Mon Sep 17 00:00:00 2001 From: Yves Senn Date: Sat, 24 Jan 2015 12:10:46 +0100 Subject: get rid of NAMED_PATTERNS in favor of running a whole directory --- railties/lib/rails/test_unit/runner.rb | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) (limited to 'railties/lib/rails') diff --git a/railties/lib/rails/test_unit/runner.rb b/railties/lib/rails/test_unit/runner.rb index d84f1cf9af..67c86e0dd0 100644 --- a/railties/lib/rails/test_unit/runner.rb +++ b/railties/lib/rails/test_unit/runner.rb @@ -10,7 +10,7 @@ module Rails options = { backtrace: false, name: nil } opt_parser = ::OptionParser.new do |opts| - opts.banner = "Usage: bin/rails test [options]" + opts.banner = "Usage: bin/rails test [options] [file or directory]" opts.separator "" opts.separator "Filter options:" @@ -47,8 +47,8 @@ module Rails opt_parser.order!(args) if arg = args.shift - if NAMED_PATTERNS.key?(arg) - options[:pattern] = arg + if Dir.exists?(arg) + options[:pattern] = "#{arg}/**/*_test.rb" else options[:filename], options[:line] = arg.split(':') options[:filename] = File.expand_path options[:filename] @@ -89,13 +89,10 @@ module Rails end end - NAMED_PATTERNS = { - "models" => "test/models/**/*_test.rb" - } def test_files return [@options[:filename]] if @options[:filename] if @options[:pattern] - pattern = NAMED_PATTERNS[@options[:pattern]] + pattern = @options[:pattern] else pattern = "test/**/*_test.rb" end -- cgit v1.2.3 From f4ea8dda1b150d8abc2f41a214e15ae81ed5f9b5 Mon Sep 17 00:00:00 2001 From: Yves Senn Date: Thu, 29 Jan 2015 11:34:10 +0100 Subject: move argument parsing into the `Runner`. --- railties/lib/rails/commands/test.rb | 4 +--- railties/lib/rails/test_unit/runner.rb | 5 +++++ 2 files changed, 6 insertions(+), 3 deletions(-) (limited to 'railties/lib/rails') diff --git a/railties/lib/rails/commands/test.rb b/railties/lib/rails/commands/test.rb index 1efd010dc7..ada21e7d3f 100644 --- a/railties/lib/rails/commands/test.rb +++ b/railties/lib/rails/commands/test.rb @@ -1,7 +1,5 @@ ENV["RAILS_ENV"] = "test" require "rails/test_unit/runner" -options = Rails::TestRunner::Options.parse(ARGV) $: << File.expand_path("../../test", APP_PATH) - -Rails::TestRunner.new(options).run +Rails::TestRunner.run(ARGV) diff --git a/railties/lib/rails/test_unit/runner.rb b/railties/lib/rails/test_unit/runner.rb index 67c86e0dd0..c5bf97bfc9 100644 --- a/railties/lib/rails/test_unit/runner.rb +++ b/railties/lib/rails/test_unit/runner.rb @@ -63,6 +63,11 @@ module Rails @options = options end + def self.run(arguments) + options = Rails::TestRunner::Options.parse(arguments) + Rails::TestRunner.new(options).run + end + def run $rails_test_runner = self run_tests -- cgit v1.2.3 From 090c83672f333d6a4d87752ca1f980bfb6be6849 Mon Sep 17 00:00:00 2001 From: Yves Senn Date: Thu, 29 Jan 2015 15:03:08 +0100 Subject: `-e` / `--environment` for the test runner. --- railties/lib/rails/commands/test.rb | 2 +- railties/lib/rails/test_unit/runner.rb | 8 +++++++- 2 files changed, 8 insertions(+), 2 deletions(-) (limited to 'railties/lib/rails') diff --git a/railties/lib/rails/commands/test.rb b/railties/lib/rails/commands/test.rb index ada21e7d3f..598e224a6f 100644 --- a/railties/lib/rails/commands/test.rb +++ b/railties/lib/rails/commands/test.rb @@ -1,5 +1,5 @@ -ENV["RAILS_ENV"] = "test" require "rails/test_unit/runner" $: << File.expand_path("../../test", APP_PATH) + Rails::TestRunner.run(ARGV) diff --git a/railties/lib/rails/test_unit/runner.rb b/railties/lib/rails/test_unit/runner.rb index c5bf97bfc9..baf33b4468 100644 --- a/railties/lib/rails/test_unit/runner.rb +++ b/railties/lib/rails/test_unit/runner.rb @@ -7,11 +7,16 @@ module Rails class TestRunner class Options def self.parse(args) - options = { backtrace: false, name: nil } + options = { backtrace: false, name: nil, environment: "test" } opt_parser = ::OptionParser.new do |opts| opts.banner = "Usage: bin/rails test [options] [file or directory]" + opts.separator "" + opts.on("-e", "--environment [ENV]", + "run tests in the ENV environment") do |env| + options[:environment] = env.strip + end opts.separator "" opts.separator "Filter options:" opts.separator "" @@ -70,6 +75,7 @@ module Rails def run $rails_test_runner = self + ENV["RAILS_ENV"] = @options[:environment] run_tests end -- cgit v1.2.3 From b58c0914f4d85faa39f22eb3408970ac8a176913 Mon Sep 17 00:00:00 2001 From: Arthur Neves Date: Thu, 29 Jan 2015 10:00:43 -0500 Subject: Run multiple files on runner --- railties/lib/rails/test_unit/runner.rb | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) (limited to 'railties/lib/rails') diff --git a/railties/lib/rails/test_unit/runner.rb b/railties/lib/rails/test_unit/runner.rb index baf33b4468..982f77d0e3 100644 --- a/railties/lib/rails/test_unit/runner.rb +++ b/railties/lib/rails/test_unit/runner.rb @@ -51,9 +51,10 @@ module Rails opt_parser.order!(args) - if arg = args.shift + options[:patterns] = [] + while arg = args.shift if Dir.exists?(arg) - options[:pattern] = "#{arg}/**/*_test.rb" + options[:patterns] << "#{arg}/**/*_test.rb" else options[:filename], options[:line] = arg.split(':') options[:filename] = File.expand_path options[:filename] @@ -102,8 +103,8 @@ module Rails def test_files return [@options[:filename]] if @options[:filename] - if @options[:pattern] - pattern = @options[:pattern] + if @options[:patterns] + pattern = @options[:patterns] else pattern = "test/**/*_test.rb" end -- cgit v1.2.3 From 3cc783b6bf20da27079038e7c52ce214f1042df3 Mon Sep 17 00:00:00 2001 From: Arthur Neves Date: Thu, 29 Jan 2015 10:07:32 -0500 Subject: Add tests for runner#test_files method --- railties/lib/rails/test_unit/runner.rb | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'railties/lib/rails') diff --git a/railties/lib/rails/test_unit/runner.rb b/railties/lib/rails/test_unit/runner.rb index 982f77d0e3..a970039b93 100644 --- a/railties/lib/rails/test_unit/runner.rb +++ b/railties/lib/rails/test_unit/runner.rb @@ -94,13 +94,6 @@ module Rails @options[:backtrace] end - private - def run_tests - test_files.to_a.each do |file| - require File.expand_path file - end - end - def test_files return [@options[:filename]] if @options[:filename] if @options[:patterns] @@ -111,6 +104,13 @@ module Rails Rake::FileList[pattern] end + private + def run_tests + test_files.to_a.each do |file| + require File.expand_path file + end + end + def test_methods methods_map = [] suites = Minitest::Runnable.runnables.shuffle -- cgit v1.2.3 From de94929d70d94fd8bb20791bf544ee483db8ddc1 Mon Sep 17 00:00:00 2001 From: Arthur Neves Date: Thu, 29 Jan 2015 10:39:43 -0500 Subject: Fix relative dir call on test runner --- railties/lib/rails/test_unit/runner.rb | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) (limited to 'railties/lib/rails') diff --git a/railties/lib/rails/test_unit/runner.rb b/railties/lib/rails/test_unit/runner.rb index a970039b93..6f9c3690f5 100644 --- a/railties/lib/rails/test_unit/runner.rb +++ b/railties/lib/rails/test_unit/runner.rb @@ -53,12 +53,17 @@ module Rails options[:patterns] = [] while arg = args.shift - if Dir.exists?(arg) - options[:patterns] << "#{arg}/**/*_test.rb" - else - options[:filename], options[:line] = arg.split(':') + if (file_and_line = arg.split(':')).size > 1 + options[:filename], options[:line] = file_and_line options[:filename] = File.expand_path options[:filename] options[:line] &&= options[:line].to_i + else + arg = arg.gsub(':', '') + if Dir.exists?("test/#{arg}") + options[:patterns] << File.expand_path("test/#{arg}/**/*_test.rb") + elsif File.file?(arg) + options[:patterns] << File.expand_path(arg) + end end end options @@ -96,7 +101,7 @@ module Rails def test_files return [@options[:filename]] if @options[:filename] - if @options[:patterns] + if @options[:patterns] && @options[:patterns].count > 0 pattern = @options[:patterns] else pattern = "test/**/*_test.rb" -- cgit v1.2.3 From f78708c180868d54e9b86e5d406b69e3de50f503 Mon Sep 17 00:00:00 2001 From: Yves Senn Date: Fri, 30 Jan 2015 14:54:41 +0100 Subject: use `bin/rails t` runner in `test_runner_test.rb`. --- railties/lib/rails/test_unit/runner.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'railties/lib/rails') diff --git a/railties/lib/rails/test_unit/runner.rb b/railties/lib/rails/test_unit/runner.rb index 6f9c3690f5..1672e4b09b 100644 --- a/railties/lib/rails/test_unit/runner.rb +++ b/railties/lib/rails/test_unit/runner.rb @@ -59,8 +59,8 @@ module Rails options[:line] &&= options[:line].to_i else arg = arg.gsub(':', '') - if Dir.exists?("test/#{arg}") - options[:patterns] << File.expand_path("test/#{arg}/**/*_test.rb") + if Dir.exists?("#{arg}") + options[:patterns] << File.expand_path("#{arg}/**/*_test.rb") elsif File.file?(arg) options[:patterns] << File.expand_path(arg) end -- cgit v1.2.3 From ac5ead59ae488e937c6a87662e14fba98b8a1e7e Mon Sep 17 00:00:00 2001 From: Yves Senn Date: Fri, 30 Jan 2015 15:58:55 +0100 Subject: `-p`, `--pattern` to run tests using a pattern. --- railties/lib/rails/test_unit/runner.rb | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'railties/lib/rails') diff --git a/railties/lib/rails/test_unit/runner.rb b/railties/lib/rails/test_unit/runner.rb index 1672e4b09b..5a1b98d534 100644 --- a/railties/lib/rails/test_unit/runner.rb +++ b/railties/lib/rails/test_unit/runner.rb @@ -31,6 +31,10 @@ module Rails "Only run tests matching NAME") do |name| options[:name] = name end + opts.on("-p", "--pattern [PATTERN]", + "Only run tests matching PATTERN") do |pattern| + options[:name] = "/#{pattern}/" + end opts.separator "" opts.separator "Output options:" -- cgit v1.2.3 From 6ccbeb458a5698db6242d7bd326ef9d9bd23c7b6 Mon Sep 17 00:00:00 2001 From: Yves Senn Date: Fri, 30 Jan 2015 16:19:37 +0100 Subject: move `ENV["BACKTRACE"]` support into the TestRunner. --- railties/lib/rails/test_unit/minitest_plugin.rb | 3 +-- railties/lib/rails/test_unit/runner.rb | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) (limited to 'railties/lib/rails') diff --git a/railties/lib/rails/test_unit/minitest_plugin.rb b/railties/lib/rails/test_unit/minitest_plugin.rb index 149aad372c..70ce9d3360 100644 --- a/railties/lib/rails/test_unit/minitest_plugin.rb +++ b/railties/lib/rails/test_unit/minitest_plugin.rb @@ -7,9 +7,8 @@ def Minitest.plugin_rails_init(options) options[:filter] = method end - if ENV["BACKTRACE"].nil? && !($rails_test_runner && $rails_test_runner.show_backtrace?) + if !($rails_test_runner && $rails_test_runner.show_backtrace?) Minitest.backtrace_filter = Rails.backtrace_cleaner end end Minitest.extensions << 'rails' - diff --git a/railties/lib/rails/test_unit/runner.rb b/railties/lib/rails/test_unit/runner.rb index 5a1b98d534..aec4707947 100644 --- a/railties/lib/rails/test_unit/runner.rb +++ b/railties/lib/rails/test_unit/runner.rb @@ -7,7 +7,7 @@ module Rails class TestRunner class Options def self.parse(args) - options = { backtrace: false, name: nil, environment: "test" } + options = { backtrace: !ENV["BACKTRACE"].nil?, name: nil, environment: "test" } opt_parser = ::OptionParser.new do |opts| opts.banner = "Usage: bin/rails test [options] [file or directory]" -- cgit v1.2.3 From f10c7e184939064be9e4995551efc3ebc3a8b883 Mon Sep 17 00:00:00 2001 From: Arthur Neves Date: Tue, 3 Feb 2015 21:54:50 -0500 Subject: Fix reporter test and verbose mode --- railties/lib/rails/test_unit/reporter.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'railties/lib/rails') diff --git a/railties/lib/rails/test_unit/reporter.rb b/railties/lib/rails/test_unit/reporter.rb index 0efba7664a..64e99626eb 100644 --- a/railties/lib/rails/test_unit/reporter.rb +++ b/railties/lib/rails/test_unit/reporter.rb @@ -3,7 +3,7 @@ require "minitest" module Rails class TestUnitReporter < Minitest::StatisticsReporter def report - return if passed? + return if results.empty? io.puts io.puts "Failed tests:" io.puts -- cgit v1.2.3 From 8017e6af31caa58a58787274ff0ca01397219e49 Mon Sep 17 00:00:00 2001 From: Arthur Neves Date: Wed, 18 Feb 2015 10:17:31 -0500 Subject: Use Rails::TestRunner on rake test --- railties/lib/rails/test_unit/testing.rake | 33 ++++++++++++++++--------------- 1 file changed, 17 insertions(+), 16 deletions(-) (limited to 'railties/lib/rails') diff --git a/railties/lib/rails/test_unit/testing.rake b/railties/lib/rails/test_unit/testing.rake index d836c0d6d6..f3a43a12db 100644 --- a/railties/lib/rails/test_unit/testing.rake +++ b/railties/lib/rails/test_unit/testing.rake @@ -1,11 +1,12 @@ -require 'rake/testtask' -require 'rails/test_unit/sub_test_task' +require "rails/test_unit/runner" task default: :test desc "Runs all tests in test folder" task :test do - Rails::TestTask.test_creator(Rake.application.top_level_tasks).invoke_rake_task + $: << "test" + ARGV.shift if ARGV[0] == "test" + Rails::TestRunner.run(ARGV) end namespace :test do @@ -14,30 +15,30 @@ namespace :test do # If used with Active Record, this task runs before the database schema is synchronized. end - Rails::TestTask.new(:run) do |t| - t.pattern = "test/**/*_test.rb" - end + task :run => %w[test] desc "Run tests quickly, but also reset db" task :db => %w[db:test:prepare test] - Rails::TestTask.new(single: "test:prepare") - ["models", "helpers", "controllers", "mailers", "integration", "jobs"].each do |name| - Rails::TestTask.new(name => "test:prepare") do |t| - t.pattern = "test/#{name}/**/*_test.rb" + task name => "test:prepare" do + $: << "test" + Rails::TestRunner.run(["test/#{name}"]) end end - Rails::TestTask.new(generators: "test:prepare") do |t| - t.pattern = "test/lib/generators/**/*_test.rb" + task :generators => "test:prepare" do + $: << "test" + Rails::TestRunner.run(["test/lib/generators"]) end - Rails::TestTask.new(units: "test:prepare") do |t| - t.pattern = 'test/{models,helpers,unit}/**/*_test.rb' + task :units => "test:prepare" do + $: << "test" + Rails::TestRunner.run(["test/models", "test/helpers", "test/unit"]) end - Rails::TestTask.new(functionals: "test:prepare") do |t| - t.pattern = 'test/{controllers,mailers,functional}/**/*_test.rb' + task :functionals => "test:prepare" do + $: << "test" + Rails::TestRunner.run(["test/controllers", "test/mailers", "test/functional"]) end end -- cgit v1.2.3