Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve test runner's Minitest integration.
This also adds free mix and matching of directories, files and lines filters. Like so: bin/rails test models/post_test.rb test/integration models/person_test.rb:26 You can also mix in a traditional Minitest filter: bin/rails test test/integration -n /check_it_out/
- Loading branch information
Showing
12 changed files
with
287 additions
and
293 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
activesupport/lib/active_support/testing/composite_filter.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
require 'method_source' | ||
|
||
module ActiveSupport | ||
module Testing | ||
class CompositeFilter # :nodoc: | ||
def initialize(runnable, filter, patterns) | ||
@runnable = runnable | ||
@filters = [ derive_regexp(filter), *derive_line_filters(patterns) ].compact | ||
end | ||
|
||
def ===(method) | ||
@filters.any? { |filter| filter === method } | ||
end | ||
|
||
private | ||
def derive_regexp(filter) | ||
filter =~ %r%/(.*)/% ? Regexp.new($1) : filter | ||
end | ||
|
||
def derive_line_filters(patterns) | ||
patterns.map do |file_and_line| | ||
file, line = file_and_line.split(':') | ||
Filter.new(@runnable, file, line) if file | ||
end | ||
end | ||
|
||
class Filter # :nodoc: | ||
def initialize(runnable, file, line) | ||
@runnable, @file = runnable, File.expand_path(file) | ||
@line = line.to_i if line | ||
end | ||
|
||
def ===(method) | ||
return unless @runnable.method_defined?(method) | ||
|
||
if @line | ||
test_file, test_range = definition_for(@runnable.instance_method(method)) | ||
test_file == @file && test_range.include?(@line) | ||
else | ||
@runnable.instance_method(method).source_location.first == @file | ||
end | ||
end | ||
|
||
private | ||
def definition_for(method) | ||
file, start_line = method.source_location | ||
end_line = method.source.count("\n") + start_line - 1 | ||
|
||
return file, start_line..end_line | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
require "rails/test_unit/runner" | ||
require "rails/test_unit/minitest_plugin" | ||
|
||
$: << File.expand_path("../../test", APP_PATH) | ||
|
||
Rails::TestRunner.run(ARGV) | ||
exit Minitest.run(ARGV) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,51 @@ | ||
require "minitest" | ||
require "active_support/core_ext/module/attribute_accessors" | ||
require "rails/test_unit/reporter" | ||
require "rails/test_unit/test_requirer" | ||
|
||
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 | ||
module Minitest | ||
def self.plugin_rails_options(opts, options) | ||
opts.separator "" | ||
opts.separator "Usage: bin/rails test [options] [files or directories]" | ||
opts.separator "You can run a single test by appending a line number to a filename:" | ||
opts.separator "" | ||
opts.separator " bin/rails test test/models/user_test.rb:27" | ||
opts.separator "" | ||
opts.separator "You can run multiple files and directories at the same time:" | ||
opts.separator "" | ||
opts.separator " bin/rails test test/controllers test/integration/login_test.rb" | ||
opts.separator "" | ||
|
||
opts.separator "Rails options:" | ||
opts.on("-e", "--environment [ENV]", | ||
"Run tests in the ENV environment") do |env| | ||
options[:environment] = env.strip | ||
end | ||
|
||
opts.on("-b", "--backtrace", | ||
"Show the complete backtrace") do | ||
options[:full_backtrace] = true | ||
end | ||
|
||
options[:patterns] = opts.order! | ||
end | ||
|
||
if !($rails_test_runner && $rails_test_runner.show_backtrace?) | ||
Minitest.backtrace_filter = Rails.backtrace_cleaner | ||
def self.plugin_rails_init(options) | ||
self.run_with_rails_extension = true | ||
|
||
ENV["RAILS_ENV"] = options[:environment] || "test" | ||
|
||
Rails::TestRequirer.require_files options[:patterns] unless run_with_autorun | ||
|
||
unless options[:full_backtrace] || ENV["BACKTRACE"] | ||
# Plugin can be run without Rails being loaded, check before filtering. | ||
Minitest.backtrace_filter = Rails.backtrace_cleaner if Rails.respond_to?(:backtrace_cleaner) | ||
end | ||
|
||
self.reporter << Rails::TestUnitReporter.new(options[:io], options) | ||
end | ||
|
||
mattr_accessor(:run_with_autorun) { false } | ||
mattr_accessor(:run_with_rails_extension) { false } | ||
end | ||
|
||
Minitest.extensions << 'rails' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
require 'active_support/core_ext/object/blank' | ||
require 'rake/file_list' | ||
|
||
module Rails | ||
class TestRequirer # :nodoc: | ||
class << self | ||
def require_files(patterns) | ||
patterns = expand_patterns(patterns) | ||
|
||
Rake::FileList[patterns.compact.presence || 'test/**/*_test.rb'].to_a.each do |file| | ||
require File.expand_path(file) | ||
end | ||
end | ||
|
||
private | ||
def expand_patterns(patterns) | ||
patterns.map do |arg| | ||
arg = arg.gsub(/:(\d+)?$/, '') | ||
if Dir.exist?(arg) | ||
"#{arg}/**/*_test.rb" | ||
elsif File.file?(arg) | ||
arg | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.