Skip to content

Commit 5886caa

Browse files
hsbtclaude
andcommitted
Preserve ENV["TESTOPTS"] when verbose is enabled
Rake 13.4.0 made `FileUtilsExt.verbose(true)` set `ENV["TESTOPTS"] = "-v"` unconditionally so that `rake -v test` would propagate verbosity to the test runner. The unconditional assignment also overwrote values set by other tools such as `ci_reporter_minitest`, breaking any workflow that relied on `TESTOPTS` being populated before `TestTask` ran. Move the `-v` injection out of `FileUtilsExt.verbose` and into `Rake::TestTask#option_list`. The task block now computes an effective verbose flag from `@verbose` or the global `FileUtilsExt.verbose_flag`, passes it to `option_list(verbose:)`, and the option list appends `-v` to the existing `TESTOPTS` only when it is not already present. This keeps the PR #394 behavior for `rake -v` and `t.verbose = true` while no longer clobbering pre-set `TESTOPTS` values. Fixes #722. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 92193ac commit 5886caa

File tree

4 files changed

+45
-10
lines changed

4 files changed

+45
-10
lines changed

lib/rake/file_utils_ext.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def #{name}(*args, **options, &block)
5353
def verbose(value=nil)
5454
oldvalue = FileUtilsExt.verbose_flag
5555
FileUtilsExt.verbose_flag = value unless value.nil?
56-
ENV["TESTOPTS"] = "-v" if value
56+
5757
if block_given?
5858
begin
5959
yield

lib/rake/testtask.rb

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,11 @@ def initialize(name=:test)
108108
def define
109109
desc @description
110110
task @name => Array(deps) do
111-
FileUtilsExt.verbose(@verbose) do
111+
effective_verbose = @verbose || FileUtilsExt.verbose_flag == true
112+
FileUtilsExt.verbose(effective_verbose) do
112113
args =
113114
"#{ruby_opts_string} #{run_code} " +
114-
"#{file_list_string} #{option_list}"
115+
"#{file_list_string} #{option_list(verbose: effective_verbose)}"
115116
ruby args do |ok, status|
116117
if !ok && status.respond_to?(:signaled?) && status.signaled?
117118
raise SignalException.new(status.termsig)
@@ -133,13 +134,17 @@ def define
133134
self
134135
end
135136

136-
def option_list # :nodoc:
137-
(ENV["TESTOPTS"] ||
137+
def option_list(verbose: @verbose) # :nodoc:
138+
opts = ENV["TESTOPTS"] ||
138139
ENV["TESTOPT"] ||
139140
ENV["TEST_OPTS"] ||
140141
ENV["TEST_OPT"] ||
141142
@options ||
142-
"")
143+
""
144+
if verbose && !opts.split.include?("-v")
145+
opts = opts.empty? ? "-v" : "#{opts} -v"
146+
end
147+
opts
143148
end
144149

145150
def ruby_opts_string # :nodoc:

test/test_rake_file_utils.rb

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ def teardown
1212
FileUtils::LN_SUPPORTED[0] = true
1313
RakeFileUtils.verbose_flag = Rake::FileUtilsExt::DEFAULT
1414
ENV["RAKE_TEST_SH"] = @rake_test_sh
15-
ENV["TESTOPTS"] = nil
1615

1716
super
1817
end
@@ -107,12 +106,9 @@ def test_safe_ln_fails_on_script_error
107106
def test_verbose
108107
verbose true
109108
assert_equal true, verbose
110-
assert_equal "-v", ENV["TESTOPTS"]
111109

112-
ENV["TESTOPTS"] = nil
113110
verbose false
114111
assert_equal false, verbose
115-
assert_equal nil, ENV["TESTOPTS"]
116112

117113
verbose(true) {
118114
assert_equal true, verbose

test/test_rake_test_task.rb

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,40 @@ def test_task_order_only_prerequisites
191191
assert_equal [b, c], t.prerequisite_tasks
192192
end
193193

194+
def test_option_list_verbose_without_testopts
195+
tt = Rake::TestTask.new { |t| t.verbose = true }
196+
assert_equal "-v", tt.option_list
197+
end
198+
199+
def test_option_list_verbose_with_testopts
200+
ENV["TESTOPTS"] = "--ci-reporter"
201+
tt = Rake::TestTask.new { |t| t.verbose = true }
202+
assert_equal "--ci-reporter -v", tt.option_list
203+
ensure
204+
ENV.delete "TESTOPTS"
205+
end
206+
207+
def test_option_list_not_verbose_with_testopts
208+
ENV["TESTOPTS"] = "--ci-reporter"
209+
tt = Rake::TestTask.new { |t| t.verbose = false }
210+
assert_equal "--ci-reporter", tt.option_list
211+
ensure
212+
ENV.delete "TESTOPTS"
213+
end
214+
215+
def test_option_list_skips_duplicate_v
216+
ENV["TESTOPTS"] = "-v --ci-reporter"
217+
tt = Rake::TestTask.new { |t| t.verbose = true }
218+
assert_equal "-v --ci-reporter", tt.option_list
219+
ensure
220+
ENV.delete "TESTOPTS"
221+
end
222+
223+
def test_option_list_verbose_keyword_overrides
224+
tt = Rake::TestTask.new { |t| t.verbose = false }
225+
assert_equal "-v", tt.option_list(verbose: true)
226+
end
227+
194228
def test_task_order_only_prerequisites_key
195229
t = task "a" => "b", order_only: ["c"]
196230
b, c = task("b"), task("c")

0 commit comments

Comments
 (0)