From d1a5460e3eb939d8de780df9a6530b02fe6d5614 Mon Sep 17 00:00:00 2001 From: Arthur Neves Date: Fri, 23 Jan 2015 20:35:13 -0500 Subject: [PATCH] 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(-) 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