diff --git a/lib/rake/testtask.rb b/lib/rake/testtask.rb index 6979b6b4c..bd0515862 100644 --- a/lib/rake/testtask.rb +++ b/lib/rake/testtask.rb @@ -110,9 +110,12 @@ def define task @name => Array(deps) do effective_verbose = @verbose || FileUtilsExt.verbose_flag == true FileUtilsExt.verbose(effective_verbose) do + # The global verbose flag (e.g. set by --trace or -n) controls + # whether rake echoes the command, but it must not inject "-v" + # into the test runner's ARGV. Only the task's own @verbose does. args = "#{ruby_opts_string} #{run_code} " + - "#{file_list_string} #{option_list(verbose: effective_verbose)}" + "#{file_list_string} #{option_list(verbose: @verbose)}" ruby args do |ok, status| if !ok && status.respond_to?(:signaled?) && status.signaled? raise SignalException.new(status.termsig) diff --git a/test/test_rake_test_task.rb b/test/test_rake_test_task.rb index 3514cfedd..2a171447b 100644 --- a/test/test_rake_test_task.rb +++ b/test/test_rake_test_task.rb @@ -242,6 +242,21 @@ def test_option_list_verbose_keyword_overrides assert_equal "-v", tt.option_list(verbose: true) end + def test_run_omits_v_for_global_verbose_only + # --trace and -n set the global verbose flag via Rake.verbose(true), but + # that must not leak "-v" into the test runner's ARGV. (issue #732) + Rake::FileUtilsExt.verbose_flag = true + tt = Rake::TestTask.new { |t| t.verbose = false } + refute_includes captured_test_command(tt).split, "-v" + ensure + Rake::FileUtilsExt.verbose_flag = false + end + + def test_run_injects_v_for_task_verbose + tt = Rake::TestTask.new { |t| t.verbose = true } + assert_includes captured_test_command(tt).split, "-v" + end + def test_task_order_only_prerequisites_key t = task "a" => "b", order_only: ["c"] b, c = task("b"), task("c") @@ -249,4 +264,18 @@ def test_task_order_only_prerequisites_key assert_equal ["c"], t.order_only_prerequisites assert_equal [b, c], t.prerequisite_tasks end + + private + + # Run the test task's action with the +ruby+ invocation stubbed out and + # return the command string that would have been executed. + def captured_test_command(test_task) + command = nil + test_task.define_singleton_method(:ruby) do |*args, **_opts, &_block| + command = args.first + nil + end + Rake::Task[test_task.name].execute + command + end end