Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The line filter parsing added to ActiveSupport::TestCase is only half the story to enable line filtering. The other half, of adding the patterns to the options, is done in the Minitest plugin that Railties has. Thus it makes more sense to have the filter in Railties with the other half and all the line filtering tests. Move the filter and extend Active Support in an initializer, so that when users or `rails/all.rb` require `rails/test_unit/railtie` we can still filter by line.
- Loading branch information
Showing
4 changed files
with
69 additions
and
64 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: 0 additions & 54 deletions
54
activesupport/lib/active_support/testing/composite_filter.rb
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 | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,63 @@ | |||
require 'method_source' | |||
|
|||
module Rails | |||
module LineFiltering # :nodoc: | |||
def run(reporter, options = {}) | |||
if options[:patterns] && options[:patterns].any? { |p| p =~ /:\d+/ } | |||
options[:filter] = \ | |||
CompositeFilter.new(self, options[:filter], options[:patterns]) | |||
end | |||
|
|||
super | |||
end | |||
end | |||
|
|||
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 | |||
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 |
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