2016-02-01 15:43:26 +03:00
|
|
|
# frozen_string_literal: true
|
2011-01-29 02:46:47 +03:00
|
|
|
require 'rubygems/test_case'
|
2007-11-10 10:48:56 +03:00
|
|
|
require 'rubygems/user_interaction'
|
2011-01-19 03:08:49 +03:00
|
|
|
require 'timeout'
|
2007-11-10 10:48:56 +03:00
|
|
|
|
2011-01-29 02:46:47 +03:00
|
|
|
class TestGemStreamUI < Gem::TestCase
|
2019-02-14 15:59:03 +03:00
|
|
|
|
2018-12-04 14:45:05 +03:00
|
|
|
SHORT_TIMEOUT = (defined?(RubyVM::MJIT) && RubyVM::MJIT.enabled?) ? 1.0 : 0.1 # increase timeout with MJIT for --jit-wait testing
|
2007-11-10 10:48:56 +03:00
|
|
|
|
|
|
|
module IsTty
|
|
|
|
attr_accessor :tty
|
|
|
|
|
|
|
|
def tty?
|
|
|
|
@tty = true unless defined? @tty
|
|
|
|
return @tty
|
|
|
|
end
|
|
|
|
|
|
|
|
alias_method :isatty, :tty?
|
2010-05-09 21:26:40 +04:00
|
|
|
|
|
|
|
def noecho
|
|
|
|
yield self
|
|
|
|
end
|
2007-11-10 10:48:56 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
def setup
|
|
|
|
super
|
|
|
|
|
|
|
|
@cfg = Gem.configuration
|
|
|
|
|
|
|
|
@in = StringIO.new
|
|
|
|
@out = StringIO.new
|
|
|
|
@err = StringIO.new
|
|
|
|
|
|
|
|
@in.extend IsTty
|
2011-01-19 03:08:49 +03:00
|
|
|
@out.extend IsTty
|
2007-11-10 10:48:56 +03:00
|
|
|
|
2011-03-10 01:32:29 +03:00
|
|
|
@sui = Gem::StreamUI.new @in, @out, @err, true
|
2007-11-10 10:48:56 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_ask
|
2019-05-26 09:23:21 +03:00
|
|
|
Timeout.timeout(5) do
|
2007-11-10 10:48:56 +03:00
|
|
|
expected_answer = "Arthur, King of the Britons"
|
|
|
|
@in.string = "#{expected_answer}\n"
|
|
|
|
actual_answer = @sui.ask("What is your name?")
|
|
|
|
assert_equal expected_answer, actual_answer
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_ask_no_tty
|
|
|
|
@in.tty = false
|
|
|
|
|
2018-12-04 14:45:05 +03:00
|
|
|
Timeout.timeout(SHORT_TIMEOUT) do
|
2007-11-10 10:48:56 +03:00
|
|
|
answer = @sui.ask("what is your favorite color?")
|
2018-05-30 16:01:35 +03:00
|
|
|
assert_nil answer
|
2007-11-10 10:48:56 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-02-22 05:52:35 +03:00
|
|
|
def test_ask_for_password
|
2019-05-29 07:01:53 +03:00
|
|
|
Timeout.timeout(5) do
|
2010-02-22 05:52:35 +03:00
|
|
|
expected_answer = "Arthur, King of the Britons"
|
|
|
|
@in.string = "#{expected_answer}\n"
|
|
|
|
actual_answer = @sui.ask_for_password("What is your name?")
|
|
|
|
assert_equal expected_answer, actual_answer
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_ask_for_password_no_tty
|
|
|
|
@in.tty = false
|
|
|
|
|
2018-12-04 14:45:05 +03:00
|
|
|
Timeout.timeout(SHORT_TIMEOUT) do
|
2010-02-22 05:52:35 +03:00
|
|
|
answer = @sui.ask_for_password("what is the airspeed velocity of an unladen swallow?")
|
2018-05-30 16:01:35 +03:00
|
|
|
assert_nil answer
|
2010-02-22 05:52:35 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2007-11-10 10:48:56 +03:00
|
|
|
def test_ask_yes_no_no_tty_with_default
|
|
|
|
@in.tty = false
|
|
|
|
|
2018-12-04 14:45:05 +03:00
|
|
|
Timeout.timeout(SHORT_TIMEOUT) do
|
2007-11-10 10:48:56 +03:00
|
|
|
answer = @sui.ask_yes_no("do coconuts migrate?", false)
|
|
|
|
assert_equal false, answer
|
|
|
|
|
|
|
|
answer = @sui.ask_yes_no("do coconuts migrate?", true)
|
|
|
|
assert_equal true, answer
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_ask_yes_no_no_tty_without_default
|
|
|
|
@in.tty = false
|
|
|
|
|
2018-12-04 14:45:05 +03:00
|
|
|
Timeout.timeout(SHORT_TIMEOUT) do
|
2008-09-25 14:13:50 +04:00
|
|
|
assert_raises(Gem::OperationNotSupportedError) do
|
2007-11-10 10:48:56 +03:00
|
|
|
@sui.ask_yes_no("do coconuts migrate?")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_choose_from_list
|
|
|
|
@in.puts "1"
|
|
|
|
@in.rewind
|
|
|
|
|
|
|
|
result = @sui.choose_from_list 'which one?', %w[foo bar]
|
|
|
|
|
|
|
|
assert_equal ['foo', 0], result
|
|
|
|
assert_equal "which one?\n 1. foo\n 2. bar\n> ", @out.string
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_choose_from_list_EOF
|
|
|
|
result = @sui.choose_from_list 'which one?', %w[foo bar]
|
|
|
|
|
|
|
|
assert_equal [nil, nil], result
|
|
|
|
assert_equal "which one?\n 1. foo\n 2. bar\n> ", @out.string
|
|
|
|
end
|
|
|
|
|
2011-01-19 03:08:49 +03:00
|
|
|
def test_progress_reporter_silent_nil
|
2007-11-10 10:48:56 +03:00
|
|
|
@cfg.verbose = nil
|
|
|
|
reporter = @sui.progress_reporter 10, 'hi'
|
|
|
|
assert_kind_of Gem::StreamUI::SilentProgressReporter, reporter
|
|
|
|
end
|
|
|
|
|
2011-01-19 03:08:49 +03:00
|
|
|
def test_progress_reporter_silent_false
|
2007-11-10 10:48:56 +03:00
|
|
|
@cfg.verbose = false
|
|
|
|
reporter = @sui.progress_reporter 10, 'hi'
|
|
|
|
assert_kind_of Gem::StreamUI::SilentProgressReporter, reporter
|
|
|
|
assert_equal "", @out.string
|
|
|
|
end
|
|
|
|
|
2011-01-19 03:08:49 +03:00
|
|
|
def test_progress_reporter_simple
|
2007-11-10 10:48:56 +03:00
|
|
|
@cfg.verbose = true
|
|
|
|
reporter = @sui.progress_reporter 10, 'hi'
|
|
|
|
assert_kind_of Gem::StreamUI::SimpleProgressReporter, reporter
|
|
|
|
assert_equal "hi\n", @out.string
|
|
|
|
end
|
|
|
|
|
2011-01-19 03:08:49 +03:00
|
|
|
def test_progress_reporter_verbose
|
2007-11-10 10:48:56 +03:00
|
|
|
@cfg.verbose = 0
|
|
|
|
reporter = @sui.progress_reporter 10, 'hi'
|
|
|
|
assert_kind_of Gem::StreamUI::VerboseProgressReporter, reporter
|
|
|
|
assert_equal "hi\n", @out.string
|
|
|
|
end
|
|
|
|
|
2011-01-19 03:08:49 +03:00
|
|
|
def test_download_reporter_silent_nil
|
|
|
|
@cfg.verbose = nil
|
|
|
|
reporter = @sui.download_reporter
|
|
|
|
reporter.fetch 'a.gem', 1024
|
|
|
|
assert_kind_of Gem::StreamUI::SilentDownloadReporter, reporter
|
|
|
|
assert_equal "", @out.string
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_download_reporter_silent_false
|
|
|
|
@cfg.verbose = false
|
|
|
|
reporter = @sui.download_reporter
|
|
|
|
reporter.fetch 'a.gem', 1024
|
|
|
|
assert_kind_of Gem::StreamUI::SilentDownloadReporter, reporter
|
|
|
|
assert_equal "", @out.string
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_download_reporter_anything
|
|
|
|
@cfg.verbose = 0
|
|
|
|
reporter = @sui.download_reporter
|
2018-09-18 11:37:18 +03:00
|
|
|
assert_kind_of Gem::StreamUI::ThreadedDownloadReporter, reporter
|
2011-01-19 03:08:49 +03:00
|
|
|
end
|
|
|
|
|
2018-09-18 11:37:18 +03:00
|
|
|
def test_threaded_download_reporter
|
2011-01-19 03:08:49 +03:00
|
|
|
@cfg.verbose = true
|
|
|
|
reporter = @sui.download_reporter
|
|
|
|
reporter.fetch 'a.gem', 1024
|
2018-09-18 11:37:18 +03:00
|
|
|
assert_equal "Fetching a.gem\n", @out.string
|
2011-01-19 03:08:49 +03:00
|
|
|
end
|
2007-11-10 10:48:56 +03:00
|
|
|
|
2011-01-19 03:08:49 +03:00
|
|
|
def test_verbose_download_reporter_progress
|
|
|
|
@cfg.verbose = true
|
|
|
|
reporter = @sui.download_reporter
|
|
|
|
reporter.fetch 'a.gem', 1024
|
|
|
|
reporter.update 512
|
2018-09-18 11:37:18 +03:00
|
|
|
assert_equal "Fetching a.gem\n", @out.string
|
2011-01-19 03:08:49 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_verbose_download_reporter_progress_once
|
|
|
|
@cfg.verbose = true
|
|
|
|
reporter = @sui.download_reporter
|
|
|
|
reporter.fetch 'a.gem', 1024
|
|
|
|
reporter.update 510
|
|
|
|
reporter.update 512
|
2018-09-18 11:37:18 +03:00
|
|
|
assert_equal "Fetching a.gem\n", @out.string
|
2011-01-19 03:08:49 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_verbose_download_reporter_progress_complete
|
|
|
|
@cfg.verbose = true
|
|
|
|
reporter = @sui.download_reporter
|
|
|
|
reporter.fetch 'a.gem', 1024
|
|
|
|
reporter.update 510
|
|
|
|
reporter.done
|
2018-09-18 11:37:18 +03:00
|
|
|
assert_equal "Fetching a.gem\n", @out.string
|
2011-01-19 03:08:49 +03:00
|
|
|
end
|
|
|
|
|
2011-01-29 02:46:47 +03:00
|
|
|
def test_verbose_download_reporter_progress_nil_length
|
|
|
|
@cfg.verbose = true
|
|
|
|
reporter = @sui.download_reporter
|
|
|
|
reporter.fetch 'a.gem', nil
|
|
|
|
reporter.update 1024
|
|
|
|
reporter.done
|
2018-09-18 11:37:18 +03:00
|
|
|
assert_equal "Fetching a.gem\n", @out.string
|
2011-01-29 02:46:47 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_verbose_download_reporter_progress_zero_length
|
|
|
|
@cfg.verbose = true
|
|
|
|
reporter = @sui.download_reporter
|
|
|
|
reporter.fetch 'a.gem', 0
|
|
|
|
reporter.update 1024
|
|
|
|
reporter.done
|
2018-09-18 11:37:18 +03:00
|
|
|
assert_equal "Fetching a.gem\n", @out.string
|
2011-01-29 02:46:47 +03:00
|
|
|
end
|
|
|
|
|
2011-01-19 03:08:49 +03:00
|
|
|
def test_verbose_download_reporter_no_tty
|
|
|
|
@out.tty = false
|
|
|
|
|
|
|
|
@cfg.verbose = true
|
|
|
|
reporter = @sui.download_reporter
|
|
|
|
reporter.fetch 'a.gem', 1024
|
|
|
|
assert_equal "", @out.string
|
|
|
|
end
|
2019-02-14 15:59:03 +03:00
|
|
|
|
2011-01-19 03:08:49 +03:00
|
|
|
end
|