Skip to content

Commit

Permalink
[rubygems/rubygems] Use globals variables for standard input/output
Browse files Browse the repository at this point in the history
Replace use of `STDIN`, `STDOUT` and `STDERR` constants by their
`$stdin`, `$stdout` and `$stderr` global variable equivalents.

This enables easier testing via standard means, such as `assert_output`
for minitest or `expect { print 'foo' }.to output.to_stdout` for RSpec
test suites.

rubygems/rubygems@a0a6fc1b76
  • Loading branch information
voxik authored and hsbt committed Dec 7, 2023
1 parent d89280e commit aabf2ce
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
8 changes: 4 additions & 4 deletions lib/rubygems/user_interaction.rb
Expand Up @@ -193,7 +193,7 @@ class Gem::StreamUI
# then special operations (like asking for passwords) will use the TTY
# commands to disable character echo.

def initialize(in_stream, out_stream, err_stream=STDERR, usetty=true)
def initialize(in_stream, out_stream, err_stream=$stderr, usetty=true)
@ins = in_stream
@outs = out_stream
@errs = err_stream
Expand Down Expand Up @@ -591,16 +591,16 @@ def locked_puts(message)
end

##
# Subclass of StreamUI that instantiates the user interaction using STDIN,
# STDOUT, and STDERR.
# Subclass of StreamUI that instantiates the user interaction using $stdin,
# $stdout, and $stderr.

class Gem::ConsoleUI < Gem::StreamUI
##
# The Console UI has no arguments as it defaults to reading input from
# stdin, output to stdout and warnings or errors to stderr.

def initialize
super STDIN, STDOUT, STDERR, true
super $stdin, $stdout, $stderr, true
end
end

Expand Down
19 changes: 19 additions & 0 deletions test/rubygems/test_gem_console_ui.rb
@@ -0,0 +1,19 @@
# frozen_string_literal: true

require_relative "helper"
require "rubygems/user_interaction"

class TestGemConsoleUI < Gem::TestCase
def test_output_can_be_captured_by_test_unit
output = capture_output do
ui = Gem::ConsoleUI.new

ui.alert_error "test error"
ui.alert_warning "test warning"
ui.alert "test alert"
end

assert_equal "INFO: test alert\n", output.first
assert_equal "ERROR: test error\n" + "WARNING: test warning\n", output.last
end
end

0 comments on commit aabf2ce

Please sign in to comment.