Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make the result of tty? obtainable with flexible stdout #87

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

koic
Copy link

@koic koic commented Feb 2, 2025

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.

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.

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 ruby#70.

This PR replaces `STDOUT` with `$stdout` throughout, based on the assumption
that `$stdout` is sufficient for use with optparse.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

1 participant