From c5532e06d9ef8b75f7443019aa1f34a3b697efdc Mon Sep 17 00:00:00 2001
From: Yves Senn <yves.senn@gmail.com>
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 +++++++++++++++++++++++++++
 railties/test/test_unit/runner_test.rb        | 21 +++++++
 5 files changed, 121 insertions(+), 2 deletions(-)
 create mode 100644 railties/lib/rails/commands/test.rb
 create mode 100644 railties/lib/rails/test_unit/runner.rb
 create mode 100644 railties/test/test_unit/runner_test.rb

(limited to 'railties')

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
diff --git a/railties/test/test_unit/runner_test.rb b/railties/test/test_unit/runner_test.rb
new file mode 100644
index 0000000000..cfc1d897b9
--- /dev/null
+++ b/railties/test/test_unit/runner_test.rb
@@ -0,0 +1,21 @@
+require 'abstract_unit'
+require 'rails/test_unit/runner'
+
+class TestUnitTestRunnerTest < ActiveSupport::TestCase
+  setup do
+    @options = Rails::TestRunner::Options
+  end
+
+  test "shows the filtered backtrace by default" do
+    options = @options.parse([])
+    assert_not options[:backtrace]
+  end
+
+  test "has --backtrace (-b) option to show the full backtrace" do
+    options = @options.parse(["-b"])
+    assert options[:backtrace]
+
+    options = @options.parse(["--backtrace"])
+    assert options[:backtrace]
+  end
+end
-- 
cgit v1.2.3


From ff0a7611cf912ce7ae32966cc42da5dbdff3a0bc Mon Sep 17 00:00:00 2001
From: Arthur Neves <arthurnn@gmail.com>
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')

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 <arthurnn@gmail.com>
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')

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 <arthurnn@gmail.com>
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')

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 <arthurnn@gmail.com>
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 +++++++++++++++++++-----------
 railties/test/test_unit/runner_test.rb | 26 ++++++++++++++++++++++++++
 2 files changed, 45 insertions(+), 11 deletions(-)

(limited to 'railties')

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
diff --git a/railties/test/test_unit/runner_test.rb b/railties/test/test_unit/runner_test.rb
index cfc1d897b9..fa906d4080 100644
--- a/railties/test/test_unit/runner_test.rb
+++ b/railties/test/test_unit/runner_test.rb
@@ -18,4 +18,30 @@ class TestUnitTestRunnerTest < ActiveSupport::TestCase
     options = @options.parse(["--backtrace"])
     assert options[:backtrace]
   end
+
+  test "parse the filename and line" do
+    options = @options.parse(["foobar.rb:20"])
+    assert_equal File.expand_path("foobar.rb"), options[:filename]
+    assert_equal 20, options[:line]
+
+    options = @options.parse(["foobar.rb:"])
+    assert_equal File.expand_path("foobar.rb"), options[:filename]
+    assert_nil options[:line]
+
+    options = @options.parse(["foobar.rb"])
+    assert_equal File.expand_path("foobar.rb"), options[:filename]
+    assert_nil options[:line]
+  end
+
+  test "find_method on same file" do
+    options = @options.parse(["#{__FILE__}:#{__LINE__}"])
+    runner = Rails::TestRunner.new(options)
+    assert_equal "test_find_method_on_same_file", runner.find_method
+  end
+
+  test "find_method on a different file" do
+    options = @options.parse(["foobar.rb:#{__LINE__}"])
+    runner = Rails::TestRunner.new(options)
+    assert_nil runner.find_method
+  end
 end
-- 
cgit v1.2.3


From 5ae2c7060f6a1bf23fd598eb92ff5417ac589dcc Mon Sep 17 00:00:00 2001
From: Arthur Neves <arthurnn@gmail.com>
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')

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 <arthurnn@gmail.com>
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')

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 <arthurnn@gmail.com>
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')

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 <arthurnn@gmail.com>
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 +++----
 railties/test/application/test_test.rb   | 1 +
 2 files changed, 4 insertions(+), 4 deletions(-)

(limited to 'railties')

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
diff --git a/railties/test/application/test_test.rb b/railties/test/application/test_test.rb
index c7132837b1..58b1bdbe86 100644
--- a/railties/test/application/test_test.rb
+++ b/railties/test/application/test_test.rb
@@ -65,6 +65,7 @@ module ApplicationTests
 
       output = run_test_file('unit/failing_test.rb', env: { "BACKTRACE" => "1" })
       assert_match %r{/app/test/unit/failing_test\.rb}, output
+      assert_match %r{/app/test/unit/failing_test\.rb:4}, output
     end
 
     test "ruby schema migrations" do
-- 
cgit v1.2.3


From ff79441d49a52a2669e147cd8264a2dc6e452b80 Mon Sep 17 00:00:00 2001
From: Yves Senn <yves.senn@gmail.com>
Date: Sat, 24 Jan 2015 11:41:33 +0100
Subject: add tests for our minitest reporter.

---
 railties/test/test_unit/reporter_test.rb | 76 ++++++++++++++++++++++++++++++++
 1 file changed, 76 insertions(+)
 create mode 100644 railties/test/test_unit/reporter_test.rb

(limited to 'railties')

diff --git a/railties/test/test_unit/reporter_test.rb b/railties/test/test_unit/reporter_test.rb
new file mode 100644
index 0000000000..587a0f878b
--- /dev/null
+++ b/railties/test/test_unit/reporter_test.rb
@@ -0,0 +1,76 @@
+require 'abstract_unit'
+require 'rails/test_unit/reporter'
+
+class TestUnitReporterTest < ActiveSupport::TestCase
+  class ExampleTest < Minitest::Test
+    def woot; end
+  end
+
+  setup do
+    @output = StringIO.new
+    @reporter = Rails::TestUnitReporter.new @output
+  end
+
+  test "prints rerun snippet to run a single failed test" do
+    @reporter.results << failed_test
+    @reporter.report
+
+    assert_match %r{^bin/rails test .*test/test_unit/reporter_test.rb:6$}, @output.string
+    assert_rerun_snippet_count 1
+  end
+
+  test "prints rerun snippet for every failed test" do
+    @reporter.results << failed_test
+    @reporter.results << failed_test
+    @reporter.results << failed_test
+    @reporter.report
+
+    assert_rerun_snippet_count 3
+  end
+
+  test "does not print snippet for successful and skipped tests" do
+    skip "confirm the expected behavior with Arthur"
+    @reporter.results << passing_test
+    @reporter.results << skipped_test
+    @reporter.report
+    assert_rerun_snippet_count 0
+  end
+
+  test "prints rerun snippet for skipped tests if run in verbose mode" do
+    skip "confirm the expected behavior with Arthur"
+    verbose = Rails::TestUnitReporter.new @output, verbose: true
+    verbose.results << skipped_test
+    verbose.report
+
+    assert_rerun_snippet_count 1
+  end
+
+  private
+  def assert_rerun_snippet_count(snippet_count)
+    assert_equal snippet_count, @output.string.scan(%r{^bin/rails test }).size
+  end
+
+  def failed_test
+    ft = ExampleTest.new(:woot)
+    ft.failures << begin
+                     raise Minitest::Assertion, "boo"
+                   rescue Minitest::Assertion => e
+                     e
+                   end
+    ft
+  end
+
+  def passing_test
+    ExampleTest.new(:woot)
+  end
+
+  def skipped_test
+    st = ExampleTest.new(:woot)
+    st.failures << begin
+                     raise Minitest::Skip
+                   rescue Minitest::Assertion => e
+                     e
+                   end
+    st
+  end
+end
-- 
cgit v1.2.3


From 7e9775bdb03f2f6648fe58958645fd8e31b5c79b Mon Sep 17 00:00:00 2001
From: Yves Senn <yves.senn@gmail.com>
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')

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 <yves.senn@gmail.com>
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')

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 <yves.senn@gmail.com>
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 ++++-------
 railties/test/test_unit/runner_test.rb |  8 ++++++++
 2 files changed, 12 insertions(+), 7 deletions(-)

(limited to 'railties')

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
diff --git a/railties/test/test_unit/runner_test.rb b/railties/test/test_unit/runner_test.rb
index fa906d4080..be226e3aa5 100644
--- a/railties/test/test_unit/runner_test.rb
+++ b/railties/test/test_unit/runner_test.rb
@@ -44,4 +44,12 @@ class TestUnitTestRunnerTest < ActiveSupport::TestCase
     runner = Rails::TestRunner.new(options)
     assert_nil runner.find_method
   end
+
+  test "run all tests in a directory" do
+    options = @options.parse([__dir__])
+
+    assert_equal "#{__dir__}/**/*_test.rb", options[:pattern]
+    assert_nil options[:filename]
+    assert_nil options[:line]
+  end
 end
-- 
cgit v1.2.3


From f4ea8dda1b150d8abc2f41a214e15ae81ed5f9b5 Mon Sep 17 00:00:00 2001
From: Yves Senn <yves.senn@gmail.com>
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')

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 <yves.senn@gmail.com>
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 +++++++-
 railties/test/test_unit/runner_test.rb | 14 ++++++++++++++
 3 files changed, 22 insertions(+), 2 deletions(-)

(limited to 'railties')

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
 
diff --git a/railties/test/test_unit/runner_test.rb b/railties/test/test_unit/runner_test.rb
index be226e3aa5..cd0c66a5f7 100644
--- a/railties/test/test_unit/runner_test.rb
+++ b/railties/test/test_unit/runner_test.rb
@@ -19,6 +19,16 @@ class TestUnitTestRunnerTest < ActiveSupport::TestCase
     assert options[:backtrace]
   end
 
+  test "tests run in the test environment by default" do
+    options = @options.parse([])
+    assert_equal "test", options[:environment]
+  end
+
+  test "can run in a specific environment" do
+    options = @options.parse(["-e development"])
+    assert_equal "development", options[:environment]
+  end
+
   test "parse the filename and line" do
     options = @options.parse(["foobar.rb:20"])
     assert_equal File.expand_path("foobar.rb"), options[:filename]
@@ -52,4 +62,8 @@ class TestUnitTestRunnerTest < ActiveSupport::TestCase
     assert_nil options[:filename]
     assert_nil options[:line]
   end
+
+  test "run multiple files" do
+    skip "needs implementation"
+  end
 end
-- 
cgit v1.2.3


From b58c0914f4d85faa39f22eb3408970ac8a176913 Mon Sep 17 00:00:00 2001
From: Arthur Neves <arthurnn@gmail.com>
Date: Thu, 29 Jan 2015 10:00:43 -0500
Subject: Run multiple files on runner

---
 railties/lib/rails/test_unit/runner.rb |  9 +++++----
 railties/test/test_unit/runner_test.rb | 18 ++++++++++++++++--
 2 files changed, 21 insertions(+), 6 deletions(-)

(limited to 'railties')

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
diff --git a/railties/test/test_unit/runner_test.rb b/railties/test/test_unit/runner_test.rb
index cd0c66a5f7..9690893a0e 100644
--- a/railties/test/test_unit/runner_test.rb
+++ b/railties/test/test_unit/runner_test.rb
@@ -58,12 +58,26 @@ class TestUnitTestRunnerTest < ActiveSupport::TestCase
   test "run all tests in a directory" do
     options = @options.parse([__dir__])
 
-    assert_equal "#{__dir__}/**/*_test.rb", options[:pattern]
+    assert_equal ["#{__dir__}/**/*_test.rb"], options[:patterns]
     assert_nil options[:filename]
     assert_nil options[:line]
   end
 
   test "run multiple files" do
-    skip "needs implementation"
+    application_dir = File.expand_path("#{__dir__}/../application")
+    options = @options.parse([__dir__, application_dir])
+
+    assert_equal ["#{__dir__}/**/*_test.rb", "#{application_dir}/**/*_test.rb"], options[:patterns]
+    assert_nil options[:filename]
+    assert_nil options[:line]
+  end
+
+  test "run multiple files and run one file by line" do
+    line = __LINE__
+    options = @options.parse([__dir__, "#{__FILE__}:#{line}"])
+
+    assert_equal ["#{__dir__}/**/*_test.rb"], options[:patterns]
+    assert_equal __FILE__, options[:filename]
+    assert_equal line, options[:line]
   end
 end
-- 
cgit v1.2.3


From 3cc783b6bf20da27079038e7c52ce214f1042df3 Mon Sep 17 00:00:00 2001
From: Arthur Neves <arthurnn@gmail.com>
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 +++++++-------
 railties/test/test_unit/runner_test.rb | 14 ++++++++++++++
 2 files changed, 21 insertions(+), 7 deletions(-)

(limited to 'railties')

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
diff --git a/railties/test/test_unit/runner_test.rb b/railties/test/test_unit/runner_test.rb
index 9690893a0e..94f338344d 100644
--- a/railties/test/test_unit/runner_test.rb
+++ b/railties/test/test_unit/runner_test.rb
@@ -70,6 +70,9 @@ class TestUnitTestRunnerTest < ActiveSupport::TestCase
     assert_equal ["#{__dir__}/**/*_test.rb", "#{application_dir}/**/*_test.rb"], options[:patterns]
     assert_nil options[:filename]
     assert_nil options[:line]
+
+    runner = Rails::TestRunner.new(options)
+    assert runner.test_files.size > 0
   end
 
   test "run multiple files and run one file by line" do
@@ -79,5 +82,16 @@ class TestUnitTestRunnerTest < ActiveSupport::TestCase
     assert_equal ["#{__dir__}/**/*_test.rb"], options[:patterns]
     assert_equal __FILE__, options[:filename]
     assert_equal line, options[:line]
+
+    runner = Rails::TestRunner.new(options)
+    assert_equal [__FILE__], runner.test_files, 'Only returns the file that running by line'
+  end
+
+  test "running multiple files passing line number" do
+    line = __LINE__
+    options = @options.parse(["foobar.rb:8", "#{__FILE__}:#{line}"])
+
+    assert_equal __FILE__, options[:filename], 'Returns the last file'
+    assert_equal line, options[:line]
   end
 end
-- 
cgit v1.2.3


From de94929d70d94fd8bb20791bf544ee483db8ddc1 Mon Sep 17 00:00:00 2001
From: Arthur Neves <arthurnn@gmail.com>
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 ++++++++++-----
 railties/test/test_unit/runner_test.rb | 24 ++++++++++++++----------
 2 files changed, 24 insertions(+), 15 deletions(-)

(limited to 'railties')

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"
diff --git a/railties/test/test_unit/runner_test.rb b/railties/test/test_unit/runner_test.rb
index 94f338344d..e8623bef67 100644
--- a/railties/test/test_unit/runner_test.rb
+++ b/railties/test/test_unit/runner_test.rb
@@ -30,16 +30,18 @@ class TestUnitTestRunnerTest < ActiveSupport::TestCase
   end
 
   test "parse the filename and line" do
-    options = @options.parse(["foobar.rb:20"])
-    assert_equal File.expand_path("foobar.rb"), options[:filename]
+    file = "test/test_unit/runner_test.rb"
+    absolute_file = __FILE__
+    options = @options.parse(["#{file}:20"])
+    assert_equal absolute_file, options[:filename]
     assert_equal 20, options[:line]
 
-    options = @options.parse(["foobar.rb:"])
-    assert_equal File.expand_path("foobar.rb"), options[:filename]
+    options = @options.parse(["#{file}:"])
+    assert_equal [absolute_file], options[:patterns]
     assert_nil options[:line]
 
-    options = @options.parse(["foobar.rb"])
-    assert_equal File.expand_path("foobar.rb"), options[:filename]
+    options = @options.parse([file])
+    assert_equal [absolute_file], options[:patterns]
     assert_nil options[:line]
   end
 
@@ -56,16 +58,18 @@ class TestUnitTestRunnerTest < ActiveSupport::TestCase
   end
 
   test "run all tests in a directory" do
-    options = @options.parse([__dir__])
+    dir = Pathname.new(__dir__).basename.to_s
+    options = @options.parse([dir])
 
     assert_equal ["#{__dir__}/**/*_test.rb"], options[:patterns]
     assert_nil options[:filename]
     assert_nil options[:line]
   end
 
-  test "run multiple files" do
+  test "run multiple folders" do
     application_dir = File.expand_path("#{__dir__}/../application")
-    options = @options.parse([__dir__, application_dir])
+
+    options = @options.parse([Pathname.new(__dir__).basename.to_s, Pathname.new(application_dir).basename.to_s])
 
     assert_equal ["#{__dir__}/**/*_test.rb", "#{application_dir}/**/*_test.rb"], options[:patterns]
     assert_nil options[:filename]
@@ -77,7 +81,7 @@ class TestUnitTestRunnerTest < ActiveSupport::TestCase
 
   test "run multiple files and run one file by line" do
     line = __LINE__
-    options = @options.parse([__dir__, "#{__FILE__}:#{line}"])
+    options = @options.parse([Pathname.new(__dir__).basename.to_s, "#{__FILE__}:#{line}"])
 
     assert_equal ["#{__dir__}/**/*_test.rb"], options[:patterns]
     assert_equal __FILE__, options[:filename]
-- 
cgit v1.2.3


From f78708c180868d54e9b86e5d406b69e3de50f503 Mon Sep 17 00:00:00 2001
From: Yves Senn <yves.senn@gmail.com>
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 +--
 railties/test/application/test_runner_test.rb | 42 ++++++++++++---------------
 railties/test/test_unit/runner_test.rb        |  7 ++---
 3 files changed, 23 insertions(+), 30 deletions(-)

(limited to 'railties')

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
diff --git a/railties/test/application/test_runner_test.rb b/railties/test/application/test_runner_test.rb
index a12f3cfc24..e273a5467f 100644
--- a/railties/test/application/test_runner_test.rb
+++ b/railties/test/application/test_runner_test.rb
@@ -7,7 +7,6 @@ module ApplicationTests
 
     def setup
       build_app
-      ENV['RAILS_ENV'] = nil
       create_schema
     end
 
@@ -55,7 +54,7 @@ module ApplicationTests
       create_test_file :models, 'foo'
       create_test_file :models, 'bar'
       create_test_file :controllers, 'foobar_controller'
-      run_test_models_command.tap do |output|
+      run_test_command("test/models").tap do |output|
         assert_match "FooTest", output
         assert_match "BarTest", output
         assert_match "2 runs, 2 assertions, 0 failures", output
@@ -66,7 +65,7 @@ module ApplicationTests
       create_test_file :helpers, 'foo_helper'
       create_test_file :helpers, 'bar_helper'
       create_test_file :controllers, 'foobar_controller'
-      run_test_helpers_command.tap do |output|
+      run_test_command("test/helpers").tap do |output|
         assert_match "FooHelperTest", output
         assert_match "BarHelperTest", output
         assert_match "2 runs, 2 assertions, 0 failures", output
@@ -74,6 +73,7 @@ module ApplicationTests
     end
 
     def test_run_units
+      skip "we no longer have the concept of unit tests. Just different directories..."
       create_test_file :models, 'foo'
       create_test_file :helpers, 'bar_helper'
       create_test_file :unit, 'baz_unit'
@@ -90,7 +90,7 @@ module ApplicationTests
       create_test_file :controllers, 'foo_controller'
       create_test_file :controllers, 'bar_controller'
       create_test_file :models, 'foo'
-      run_test_controllers_command.tap do |output|
+      run_test_command("test/controllers").tap do |output|
         assert_match "FooControllerTest", output
         assert_match "BarControllerTest", output
         assert_match "2 runs, 2 assertions, 0 failures", output
@@ -101,7 +101,7 @@ module ApplicationTests
       create_test_file :mailers, 'foo_mailer'
       create_test_file :mailers, 'bar_mailer'
       create_test_file :models, 'foo'
-      run_test_mailers_command.tap do |output|
+      run_test_command("test/mailers").tap do |output|
         assert_match "FooMailerTest", output
         assert_match "BarMailerTest", output
         assert_match "2 runs, 2 assertions, 0 failures", output
@@ -112,7 +112,7 @@ module ApplicationTests
       create_test_file :jobs, 'foo_job'
       create_test_file :jobs, 'bar_job'
       create_test_file :models, 'foo'
-      run_test_jobs_command.tap do |output|
+      run_test_command("test/jobs").tap do |output|
         assert_match "FooJobTest", output
         assert_match "BarJobTest", output
         assert_match "2 runs, 2 assertions, 0 failures", output
@@ -120,6 +120,7 @@ module ApplicationTests
     end
 
     def test_run_functionals
+      skip "we no longer have the concept of functional tests. Just different directories..."
       create_test_file :mailers, 'foo_mailer'
       create_test_file :controllers, 'bar_controller'
       create_test_file :functional, 'baz_functional'
@@ -135,7 +136,7 @@ module ApplicationTests
     def test_run_integration
       create_test_file :integration, 'foo_integration'
       create_test_file :models, 'foo'
-      run_test_integration_command.tap do |output|
+      run_test_command("test/integration").tap do |output|
         assert_match "FooIntegration", output
         assert_match "1 runs, 1 assertions, 0 failures", output
       end
@@ -165,13 +166,14 @@ module ApplicationTests
         end
       RUBY
 
-      run_test_command('test/unit/chu_2_koi_test.rb test_rikka').tap do |output|
+      run_test_command('-n test_rikka test/unit/chu_2_koi_test.rb').tap do |output|
         assert_match "Rikka", output
         assert_no_match "Sanae", output
       end
     end
 
     def test_run_matched_test
+      skip "bin/rails test does not yet support running tests by pattern"
       app_file 'test/unit/chu_2_koi_test.rb', <<-RUBY
         require 'test_helper'
 
@@ -186,7 +188,7 @@ module ApplicationTests
         end
       RUBY
 
-      run_test_command('test/unit/chu_2_koi_test.rb /rikka/').tap do |output|
+      run_test_command('-n rikka test/unit/chu_2_koi_test.rb').tap do |output|
         assert_match "Rikka", output
         assert_no_match "Sanae", output
       end
@@ -194,18 +196,18 @@ module ApplicationTests
 
     def test_load_fixtures_when_running_test_suites
       create_model_with_fixture
-      suites = [:models, :helpers, [:units, :unit], :controllers, :mailers,
-        [:functionals, :functional], :integration]
+      suites = [:models, :helpers, :controllers, :mailers, :integration]
 
       suites.each do |suite, directory|
         directory ||= suite
         create_fixture_test directory
-        assert_match "3 users", run_task(["test:#{suite}"])
+        assert_match "3 users", run_test_command("test/#{suite}")
         Dir.chdir(app_path) { FileUtils.rm_f "test/#{directory}" }
       end
     end
 
     def test_run_with_model
+      skip "These feel a bit odd. Not sure we should keep supporting them."
       create_model_with_fixture
       create_fixture_test 'models', 'user'
       assert_match "3 users", run_task(["test models/user"])
@@ -213,6 +215,7 @@ module ApplicationTests
     end
 
     def test_run_different_environment_using_env_var
+      skip "no longer possible. Running tests in a different environment should be explicit"
       app_file 'test/unit/env_test.rb', <<-RUBY
         require 'test_helper'
 
@@ -227,7 +230,7 @@ module ApplicationTests
       assert_match "development", run_test_command('test/unit/env_test.rb')
     end
 
-    def test_run_different_environment_using_e_tag
+    def test_run_different_environment
       env = "development"
       app_file 'test/unit/env_test.rb', <<-RUBY
         require 'test_helper'
@@ -239,7 +242,7 @@ module ApplicationTests
         end
       RUBY
 
-      assert_match env, run_test_command("test/unit/env_test.rb RAILS_ENV=#{env}")
+      assert_match env, run_test_command("-e #{env} test/unit/env_test.rb")
     end
 
     def test_generated_scaffold_works_with_rails_test
@@ -248,17 +251,8 @@ module ApplicationTests
     end
 
     private
-      def run_task(tasks)
-        Dir.chdir(app_path) { `bundle exec rake #{tasks.join ' '}` }
-      end
-
       def run_test_command(arguments = 'test/unit/test_test.rb')
-        run_task ['test', arguments]
-      end
-      %w{ mailers models helpers units controllers functionals integration jobs }.each do |type|
-        define_method("run_test_#{type}_command") do
-          run_task ["test:#{type}"]
-        end
+        Dir.chdir(app_path) { `bin/rails t #{arguments}` }
       end
 
       def create_model_with_fixture
diff --git a/railties/test/test_unit/runner_test.rb b/railties/test/test_unit/runner_test.rb
index e8623bef67..fa5442c15c 100644
--- a/railties/test/test_unit/runner_test.rb
+++ b/railties/test/test_unit/runner_test.rb
@@ -58,8 +58,7 @@ class TestUnitTestRunnerTest < ActiveSupport::TestCase
   end
 
   test "run all tests in a directory" do
-    dir = Pathname.new(__dir__).basename.to_s
-    options = @options.parse([dir])
+    options = @options.parse([__dir__])
 
     assert_equal ["#{__dir__}/**/*_test.rb"], options[:patterns]
     assert_nil options[:filename]
@@ -69,7 +68,7 @@ class TestUnitTestRunnerTest < ActiveSupport::TestCase
   test "run multiple folders" do
     application_dir = File.expand_path("#{__dir__}/../application")
 
-    options = @options.parse([Pathname.new(__dir__).basename.to_s, Pathname.new(application_dir).basename.to_s])
+    options = @options.parse([__dir__, application_dir])
 
     assert_equal ["#{__dir__}/**/*_test.rb", "#{application_dir}/**/*_test.rb"], options[:patterns]
     assert_nil options[:filename]
@@ -81,7 +80,7 @@ class TestUnitTestRunnerTest < ActiveSupport::TestCase
 
   test "run multiple files and run one file by line" do
     line = __LINE__
-    options = @options.parse([Pathname.new(__dir__).basename.to_s, "#{__FILE__}:#{line}"])
+    options = @options.parse([__dir__, "#{__FILE__}:#{line}"])
 
     assert_equal ["#{__dir__}/**/*_test.rb"], options[:patterns]
     assert_equal __FILE__, options[:filename]
-- 
cgit v1.2.3


From ac5ead59ae488e937c6a87662e14fba98b8a1e7e Mon Sep 17 00:00:00 2001
From: Yves Senn <yves.senn@gmail.com>
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 ++++
 railties/test/application/test_runner_test.rb | 3 +--
 2 files changed, 5 insertions(+), 2 deletions(-)

(limited to 'railties')

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:"
diff --git a/railties/test/application/test_runner_test.rb b/railties/test/application/test_runner_test.rb
index e273a5467f..c122b315c0 100644
--- a/railties/test/application/test_runner_test.rb
+++ b/railties/test/application/test_runner_test.rb
@@ -173,7 +173,6 @@ module ApplicationTests
     end
 
     def test_run_matched_test
-      skip "bin/rails test does not yet support running tests by pattern"
       app_file 'test/unit/chu_2_koi_test.rb', <<-RUBY
         require 'test_helper'
 
@@ -188,7 +187,7 @@ module ApplicationTests
         end
       RUBY
 
-      run_test_command('-n rikka test/unit/chu_2_koi_test.rb').tap do |output|
+      run_test_command('-p rikka test/unit/chu_2_koi_test.rb').tap do |output|
         assert_match "Rikka", output
         assert_no_match "Sanae", output
       end
-- 
cgit v1.2.3


From 6ccbeb458a5698db6242d7bd326ef9d9bd23c7b6 Mon Sep 17 00:00:00 2001
From: Yves Senn <yves.senn@gmail.com>
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 +-
 railties/test/test_unit/runner_test.rb          | 10 ++++++++++
 3 files changed, 12 insertions(+), 3 deletions(-)

(limited to 'railties')

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]"
diff --git a/railties/test/test_unit/runner_test.rb b/railties/test/test_unit/runner_test.rb
index fa5442c15c..c040c71635 100644
--- a/railties/test/test_unit/runner_test.rb
+++ b/railties/test/test_unit/runner_test.rb
@@ -1,7 +1,10 @@
 require 'abstract_unit'
+require 'env_helpers'
 require 'rails/test_unit/runner'
 
 class TestUnitTestRunnerTest < ActiveSupport::TestCase
+  include EnvHelpers
+
   setup do
     @options = Rails::TestRunner::Options
   end
@@ -19,6 +22,13 @@ class TestUnitTestRunnerTest < ActiveSupport::TestCase
     assert options[:backtrace]
   end
 
+  test "show full backtrace using BACKTRACE environment variable" do
+    switch_env "BACKTRACE", "true" do
+      options = @options.parse([])
+      assert options[:backtrace]
+    end
+  end
+
   test "tests run in the test environment by default" do
     options = @options.parse([])
     assert_equal "test", options[:environment]
-- 
cgit v1.2.3


From f10c7e184939064be9e4995551efc3ebc3a8b883 Mon Sep 17 00:00:00 2001
From: Arthur Neves <arthurnn@gmail.com>
Date: Tue, 3 Feb 2015 21:54:50 -0500
Subject: Fix reporter test and verbose mode

---
 railties/lib/rails/test_unit/reporter.rb |  2 +-
 railties/test/test_unit/reporter_test.rb | 16 +++++++---------
 2 files changed, 8 insertions(+), 10 deletions(-)

(limited to 'railties')

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
diff --git a/railties/test/test_unit/reporter_test.rb b/railties/test/test_unit/reporter_test.rb
index 587a0f878b..77883612f5 100644
--- a/railties/test/test_unit/reporter_test.rb
+++ b/railties/test/test_unit/reporter_test.rb
@@ -12,7 +12,7 @@ class TestUnitReporterTest < ActiveSupport::TestCase
   end
 
   test "prints rerun snippet to run a single failed test" do
-    @reporter.results << failed_test
+    @reporter.record(failed_test)
     @reporter.report
 
     assert_match %r{^bin/rails test .*test/test_unit/reporter_test.rb:6$}, @output.string
@@ -20,26 +20,24 @@ class TestUnitReporterTest < ActiveSupport::TestCase
   end
 
   test "prints rerun snippet for every failed test" do
-    @reporter.results << failed_test
-    @reporter.results << failed_test
-    @reporter.results << failed_test
+    @reporter.record(failed_test)
+    @reporter.record(failed_test)
+    @reporter.record(failed_test)
     @reporter.report
 
     assert_rerun_snippet_count 3
   end
 
   test "does not print snippet for successful and skipped tests" do
-    skip "confirm the expected behavior with Arthur"
-    @reporter.results << passing_test
-    @reporter.results << skipped_test
+    @reporter.record(passing_test)
+    @reporter.record(skipped_test)
     @reporter.report
     assert_rerun_snippet_count 0
   end
 
   test "prints rerun snippet for skipped tests if run in verbose mode" do
-    skip "confirm the expected behavior with Arthur"
     verbose = Rails::TestUnitReporter.new @output, verbose: true
-    verbose.results << skipped_test
+    verbose.record(skipped_test)
     verbose.report
 
     assert_rerun_snippet_count 1
-- 
cgit v1.2.3


From 8017e6af31caa58a58787274ff0ca01397219e49 Mon Sep 17 00:00:00 2001
From: Arthur Neves <arthurnn@gmail.com>
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')

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


From d9bbafb2e79bbaee93648b7ee5138a0dbd7e8277 Mon Sep 17 00:00:00 2001
From: Yves Senn <yves.senn@gmail.com>
Date: Thu, 5 Mar 2015 15:19:09 +0100
Subject: run `test_test.rb` using the `bin/rails test`.

---
 railties/test/application/test_test.rb | 18 +-----------------
 1 file changed, 1 insertion(+), 17 deletions(-)

(limited to 'railties')

diff --git a/railties/test/application/test_test.rb b/railties/test/application/test_test.rb
index 58b1bdbe86..61652e5052 100644
--- a/railties/test/application/test_test.rb
+++ b/railties/test/application/test_test.rb
@@ -301,23 +301,7 @@ Expected: ["id", "name"]
       end
 
       def run_test_file(name, options = {})
-        ruby '-Itest', "#{app_path}/test/#{name}", options.deep_merge(env: {"RAILS_ENV" => "test"})
-      end
-
-      def ruby(*args)
-        options = args.extract_options!
-        env = options.fetch(:env, {})
-        env["RUBYLIB"] = $:.join(':')
-
-        Dir.chdir(app_path) do
-          `#{env_string(env)} #{Gem.ruby} #{args.join(' ')} 2>&1`
-        end
-      end
-
-      def env_string(variables)
-        variables.map do |key, value|
-          "#{key}='#{value}'"
-        end.join " "
+        Dir.chdir(app_path) { `bin/rails test "#{app_path}/test/#{name}" 2>&1` }
       end
   end
 end
-- 
cgit v1.2.3