From 2e0e5bb1e4ee5836572fffe6e3f16eefd56b4a25 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Fri, 5 Apr 2013 17:06:36 -0700 Subject: rake test app/model/foo.rb and rake test models/foo works --- railties/lib/rails/test_unit/sub_test_task.rb | 26 +++++++++++++++++++++++--- railties/test/test_info_test.rb | 24 ++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 3 deletions(-) (limited to 'railties') diff --git a/railties/lib/rails/test_unit/sub_test_task.rb b/railties/lib/rails/test_unit/sub_test_task.rb index ac2bde2071..a463380e2d 100644 --- a/railties/lib/rails/test_unit/sub_test_task.rb +++ b/railties/lib/rails/test_unit/sub_test_task.rb @@ -8,11 +8,21 @@ module Rails end def files - @tasks.find_all { |t| File.file?(t) && !File.directory?(t) } + @tasks.map { |task| + [task, translate(task)].find { |file| test_file?(file) } + }.compact + end + + def translate(file) + if file =~ /^app\/(.*)$/ + "test/#{$1.sub(/\.rb$/, '')}_test.rb" + else + "test/#{file}_test.rb" + end end def tasks - @tasks - files - opt_names + @tasks - test_file_tasks - opt_names end def opts @@ -24,8 +34,18 @@ module Rails private + def test_file_tasks + @tasks.find_all { |task| + [task, translate(task)].any? { |file| test_file?(file) } + } + end + + def test_file?(file) + file =~ /^test/ && File.file?(file) && !File.directory?(file) + end + def opt_names - (@tasks - files).reject { |t| task_defined? t } + (@tasks - test_file_tasks).reject { |t| task_defined? t } end def task_defined?(task) diff --git a/railties/test/test_info_test.rb b/railties/test/test_info_test.rb index 2f78dcff1b..d5463c11de 100644 --- a/railties/test/test_info_test.rb +++ b/railties/test/test_info_test.rb @@ -24,6 +24,30 @@ module Rails assert_equal ['test'], info.tasks end + def test_with_model_shorthand + info = new_test_info ['test', 'models/foo', '/foo/'] + + def info.test_file?(file) + file == "test/models/foo_test.rb" || super + end + + assert_equal ['test/models/foo_test.rb'], info.files + assert_equal '-n /foo/', info.opts + assert_equal ['test'], info.tasks + end + + def test_with_model_path + info = new_test_info ['test', 'app/models/foo.rb', '/foo/'] + + def info.test_file?(file) + file == "test/models/foo_test.rb" || super + end + + assert_equal ['test/models/foo_test.rb'], info.files + assert_equal '-n /foo/', info.opts + assert_equal ['test'], info.tasks + end + def new_test_info(tasks) Class.new(TestTask::TestInfo) { def task_defined?(task) -- cgit v1.2.3