Skip to content

Commit

Permalink
Merge remote branch 'erikh/windows-silentui-hack'
Browse files Browse the repository at this point in the history
  • Loading branch information
luislavena committed Mar 1, 2011
2 parents a2a8823 + adff87c commit c2d6cd5
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 9 deletions.
2 changes: 1 addition & 1 deletion lib/rubygems/mock_gem_ui.rb
Expand Up @@ -31,7 +31,7 @@ def initialize(input = "")
outs.extend TTY outs.extend TTY
errs.extend TTY errs.extend TTY


super ins, outs, errs super ins, outs, errs, true


@terminated = false @terminated = false
end end
Expand Down
35 changes: 28 additions & 7 deletions lib/rubygems/user_interaction.rb
Expand Up @@ -132,10 +132,19 @@ class Gem::StreamUI


attr_reader :ins, :outs, :errs attr_reader :ins, :outs, :errs


def initialize(in_stream, out_stream, err_stream=STDERR) def initialize(in_stream, out_stream, err_stream=STDERR, usetty=true)
@ins = in_stream @ins = in_stream
@outs = out_stream @outs = out_stream
@errs = err_stream @errs = err_stream
@usetty = usetty
end

def tty?
if RUBY_PLATFORM =~ /mingw|mswin/
@usetty
else
@usetty && @ins.tty?
end
end end


## ##
Expand Down Expand Up @@ -167,7 +176,7 @@ def choose_from_list(question, list)
# default. # default.


def ask_yes_no(question, default=nil) def ask_yes_no(question, default=nil)
unless @ins.tty? then unless tty? then
if default.nil? then if default.nil? then
raise Gem::OperationNotSupportedError, raise Gem::OperationNotSupportedError,
"Not connected to a tty and no default specified" "Not connected to a tty and no default specified"
Expand Down Expand Up @@ -203,7 +212,7 @@ def ask_yes_no(question, default=nil)
# Ask a question. Returns an answer if connected to a tty, nil otherwise. # Ask a question. Returns an answer if connected to a tty, nil otherwise.


def ask(question) def ask(question)
return nil if not @ins.tty? return nil if not tty?


@outs.print(question + " ") @outs.print(question + " ")
@outs.flush @outs.flush
Expand All @@ -218,7 +227,7 @@ def ask(question)
# Ask for a password. Does not echo response to terminal. # Ask for a password. Does not echo response to terminal.


def ask_for_password(question) def ask_for_password(question)
return nil if not @ins.tty? return nil if not tty?


require 'io/console' require 'io/console'


Expand All @@ -234,7 +243,7 @@ def ask_for_password(question)
# Ask for a password. Does not echo response to terminal. # Ask for a password. Does not echo response to terminal.


def ask_for_password(question) def ask_for_password(question)
return nil if not @ins.tty? return nil if not tty?


@outs.print(question + " ") @outs.print(question + " ")
@outs.flush @outs.flush
Expand All @@ -246,6 +255,8 @@ def ask_for_password(question)
# Asks for a password that works on windows. Ripped from the Heroku gem. # Asks for a password that works on windows. Ripped from the Heroku gem.


def ask_for_password_on_windows def ask_for_password_on_windows
return nil if not tty?

require "Win32API" require "Win32API"
char = nil char = nil
password = '' password = ''
Expand All @@ -267,6 +278,8 @@ def ask_for_password_on_windows
# Asks for a password that works on unix # Asks for a password that works on unix


def ask_for_password_on_unix def ask_for_password_on_unix
return nil if not tty?

system "stty -echo" system "stty -echo"
password = @ins.gets password = @ins.gets
password.chomp! if password password.chomp! if password
Expand Down Expand Up @@ -327,6 +340,10 @@ def terminate_interaction(status = 0)
# Return a progress reporter object chosen from the current verbosity. # Return a progress reporter object chosen from the current verbosity.


def progress_reporter(*args) def progress_reporter(*args)
if self.kind_of?(Gem::SilentUI)
return SilentProgressReporter.new(@outs, *args)
end

case Gem.configuration.verbose case Gem.configuration.verbose
when nil, false when nil, false
SilentProgressReporter.new(@outs, *args) SilentProgressReporter.new(@outs, *args)
Expand Down Expand Up @@ -429,6 +446,10 @@ def done
# Return a download reporter object chosen from the current verbosity # Return a download reporter object chosen from the current verbosity


def download_reporter(*args) def download_reporter(*args)
if self.kind_of?(Gem::SilentUI)
return SilentDownloadReporter.new(@outs, *args)
end

case Gem.configuration.verbose case Gem.configuration.verbose
when nil, false when nil, false
SilentDownloadReporter.new(@outs, *args) SilentDownloadReporter.new(@outs, *args)
Expand Down Expand Up @@ -512,7 +533,7 @@ def update_display(show_progress = true, new_line = false)


class Gem::ConsoleUI < Gem::StreamUI class Gem::ConsoleUI < Gem::StreamUI
def initialize def initialize
super STDIN, STDOUT, STDERR super STDIN, STDOUT, STDERR, true
end end
end end


Expand All @@ -531,7 +552,7 @@ def initialize
writer = File.open('nul', 'w') writer = File.open('nul', 'w')
end end


super reader, writer, writer super reader, writer, writer, false
end end


def download_reporter(*args) def download_reporter(*args)
Expand Down
12 changes: 11 additions & 1 deletion test/rubygems/test_gem_stream_ui.rb
Expand Up @@ -31,10 +31,12 @@ def setup
@in.extend IsTty @in.extend IsTty
@out.extend IsTty @out.extend IsTty


@sui = Gem::StreamUI.new @in, @out, @err @sui = Gem::StreamUI.new @in, @out, @err, true
end end


def test_ask def test_ask
skip 'TTY detection broken on windows' if Gem.win_platform?

timeout(1) do timeout(1) do
expected_answer = "Arthur, King of the Britons" expected_answer = "Arthur, King of the Britons"
@in.string = "#{expected_answer}\n" @in.string = "#{expected_answer}\n"
Expand All @@ -44,6 +46,8 @@ def test_ask
end end


def test_ask_no_tty def test_ask_no_tty
skip 'TTY handling is broken on windows' if Gem.win_platform?

@in.tty = false @in.tty = false


timeout(0.1) do timeout(0.1) do
Expand All @@ -64,6 +68,8 @@ def test_ask_for_password
end end


def test_ask_for_password_no_tty def test_ask_for_password_no_tty
skip 'TTY handling is broken on windows' if Gem.win_platform?

@in.tty = false @in.tty = false


timeout(0.1) do timeout(0.1) do
Expand All @@ -73,6 +79,8 @@ def test_ask_for_password_no_tty
end end


def test_ask_yes_no_no_tty_with_default def test_ask_yes_no_no_tty_with_default
skip 'TTY handling is broken on windows' if Gem.win_platform?

@in.tty = false @in.tty = false


timeout(0.1) do timeout(0.1) do
Expand All @@ -85,6 +93,8 @@ def test_ask_yes_no_no_tty_with_default
end end


def test_ask_yes_no_no_tty_without_default def test_ask_yes_no_no_tty_without_default
skip 'TTY handling is broken on windows' if Gem.win_platform?

@in.tty = false @in.tty = false


timeout(0.1) do timeout(0.1) do
Expand Down

0 comments on commit c2d6cd5

Please sign in to comment.