summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVít Ondruch <vondruch@redhat.com>2023-11-28 15:38:51 +0100
committerHiroshi SHIBATA <hsbt@ruby-lang.org>2023-12-07 13:55:31 +0900
commitaabf2ce181053d37cbf1d6db55ff533055c96d83 (patch)
treeae260297625121ed531e2384bd95bf14340c4d1c
parentd89280e8bf6496aa83326b5f9c293724bd1cc1e9 (diff)
[rubygems/rubygems] Use globals variables for standard input/output
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. https://github.com/rubygems/rubygems/commit/a0a6fc1b76
-rw-r--r--lib/rubygems/user_interaction.rb8
-rw-r--r--test/rubygems/test_gem_console_ui.rb19
2 files changed, 23 insertions, 4 deletions
diff --git a/lib/rubygems/user_interaction.rb b/lib/rubygems/user_interaction.rb
index 1cee1555a0..0172c4ee89 100644
--- a/lib/rubygems/user_interaction.rb
+++ b/lib/rubygems/user_interaction.rb
@@ -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
@@ -591,8 +591,8 @@ class Gem::StreamUI
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
##
@@ -600,7 +600,7 @@ class Gem::ConsoleUI < Gem::StreamUI
# stdin, output to stdout and warnings or errors to stderr.
def initialize
- super STDIN, STDOUT, STDERR, true
+ super $stdin, $stdout, $stderr, true
end
end
diff --git a/test/rubygems/test_gem_console_ui.rb b/test/rubygems/test_gem_console_ui.rb
new file mode 100644
index 0000000000..b8a619625f
--- /dev/null
+++ b/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