Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 262cf6f

Browse files
koicnobu
authored andcommitted
Make the result of tty? obtainable with flexible stdout
In mock testing for stdout, `StringIO.new` is sometimes used to redirect the output. In such cases, the assignment is done with `$stdout = StringIO.new`, not the constant `STDOUT`. e.g., https://github.com/rubocop/rubocop/blob/v1.71.1/lib/rubocop/rspec/shared_contexts.rb#L154-L164 After assigning `StringIO.new`, `$stdout.tty?` returns `false`, allowing the standard output destination to be switched during test execution. ```ruby STDOUT.tty? # => true StringIO.new.tty? # => false ``` However, since `STDOUT.tty?` returns `true`, a failure occurred in environments where the environment variables `RUBY_PAGER` or `PAGER` are set. e.g., rubocop/rubocop#13784 To address this, `STDOUT` has been updated to `$stdout` so that the result of `tty?` can be flexibly overridden. A potential concern is that `$stdout`, unlike `STDOUT`, does not always represent the standard output at the time the Ruby process started. However, no concrete examples of issues related to this have been identified. `STDOUT.tty?` is the logic of optparse introduced in #70. This PR replaces `STDOUT` with `$stdout` throughout, based on the assumption that `$stdout` is sufficient for use with optparse.
1 parent e1957d7 commit 262cf6f

File tree

2 files changed

+5
-6
lines changed

2 files changed

+5
-6
lines changed

lib/optparse.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1056,7 +1056,7 @@ def compsys(to, name = File.basename($0)) # :nodoc:
10561056
end
10571057

10581058
def help_exit
1059-
if STDOUT.tty? && (pager = ENV.values_at(*%w[RUBY_PAGER PAGER]).find {|e| e && !e.empty?})
1059+
if $stdout.tty? && (pager = ENV.values_at(*%w[RUBY_PAGER PAGER]).find {|e| e && !e.empty?})
10601060
less = ENV["LESS"]
10611061
args = [{"LESS" => "#{!less || less.empty? ? '-' : less}Fe"}, pager, "w"]
10621062
print = proc do |f|
@@ -1065,7 +1065,7 @@ def help_exit
10651065
# pager terminated
10661066
end
10671067
if Process.respond_to?(:fork) and false
1068-
IO.popen("-") {|f| f ? Process.exec(*args, in: f) : print.call(STDOUT)}
1068+
IO.popen("-") {|f| f ? Process.exec(*args, in: f) : print.call($stdout)}
10691069
# unreachable
10701070
end
10711071
IO.popen(*args, &print)
@@ -1107,7 +1107,7 @@ def help_exit
11071107
#
11081108
Officious['*-completion-zsh'] = proc do |parser|
11091109
Switch::OptionalArgument.new do |arg|
1110-
parser.compsys(STDOUT, arg)
1110+
parser.compsys($stdout, arg)
11111111
exit
11121112
end
11131113
end

test/optparse/test_optparse.rb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -184,10 +184,9 @@ def test_help_pager
184184
File.open(File.join(dir, "options.rb"), "w") do |f|
185185
f.puts "#{<<~"begin;"}\n#{<<~'end;'}"
186186
begin;
187-
stdout = STDOUT.dup
187+
stdout = $stdout.dup
188188
def stdout.tty?; true; end
189-
Object.__send__(:remove_const, :STDOUT)
190-
STDOUT = stdout
189+
$stdout = stdout
191190
ARGV.options do |opt|
192191
end;
193192
100.times {|i| f.puts " opt.on('--opt-#{i}') {}"}

0 commit comments

Comments
 (0)