Geminstaller C0 Coverage Information - RCov

spec/fixture/rubygems_dist/rubygems-trunk/lib/rubygems/user_interaction.rb

Name Total Lines Lines of Code Total Coverage Code Coverage
spec/fixture/rubygems_dist/rubygems-trunk/lib/rubygems/user_interaction.rb 435 221
68.05%
45.25%

Key

Code reported as executed by Ruby looks like this...and this: this line is also marked as covered.Lines considered as run by rcov, but not reported by Ruby, look like this,and this: these lines were inferred by rcov (using simple heuristics).Finally, here's a line marked as not executed.

Coverage Details

1 #--
2 # Copyright 2006 by Chad Fowler, Rich Kilmer, Jim Weirich and others.
3 # All rights reserved.
4 # See LICENSE.txt for permissions.
5 #++
6 
7 ##
8 # Module that defines the default UserInteraction.  Any class including this
9 # module will have access to the +ui+ method that returns the default UI.
10 
11 module Gem::DefaultUserInteraction
12 
13   ##
14   # The default UI is a class variable of the singleton class for this
15   # module.
16 
17   @ui = nil
18 
19   ##
20   # Return the default UI.
21 
22   def self.ui
23     @ui ||= Gem::ConsoleUI.new
24   end
25 
26   ##
27   # Set the default UI.  If the default UI is never explicitly set, a simple
28   # console based UserInteraction will be used automatically.
29 
30   def self.ui=(new_ui)
31     @ui = new_ui
32   end
33 
34   ##
35   # Use +new_ui+ for the duration of +block+.
36 
37   def self.use_ui(new_ui)
38     old_ui = @ui
39     @ui = new_ui
40     yield
41   ensure
42     @ui = old_ui
43   end
44 
45   ##
46   # See DefaultUserInteraction::ui
47 
48   def ui
49     Gem::DefaultUserInteraction.ui
50   end
51 
52   ##
53   # See DefaultUserInteraction::ui=
54 
55   def ui=(new_ui)
56     Gem::DefaultUserInteraction.ui = new_ui
57   end
58 
59   ##
60   # See DefaultUserInteraction::use_ui
61 
62   def use_ui(new_ui, &block)
63     Gem::DefaultUserInteraction.use_ui(new_ui, &block)
64   end
65 
66 end
67 
68 ##
69 # Make the default UI accessable without the "ui." prefix.  Classes
70 # including this module may use the interaction methods on the default UI
71 # directly.  Classes may also reference the ui and ui= methods.
72 #
73 # Example:
74 #
75 #   class X
76 #     include Gem::UserInteraction
77 #
78 #     def get_answer
79 #       n = ask("What is the meaning of life?")
80 #     end
81 #   end
82 
83 module Gem::UserInteraction
84 
85   include Gem::DefaultUserInteraction
86 
87   ##
88   # :method: alert
89 
90   ##
91   # :method: alert_error
92 
93   ##
94   # :method: alert_warning
95 
96   ##
97   # :method: ask
98 
99   ##
100   # :method: ask_yes_no
101 
102   ##
103   # :method: choose_from_list
104 
105   ##
106   # :method: say
107 
108   ##
109   # :method: terminate_interaction
110 
111   [:alert,
112    :alert_error,
113    :alert_warning,
114    :ask,
115    :ask_for_password,
116    :ask_yes_no,
117    :choose_from_list,
118    :say,
119    :terminate_interaction ].each do |methname|
120     class_eval %{
121       def #{methname}(*args)
122         ui.#{methname}(*args)
123       end
124     }, __FILE__, __LINE__
125   end
126 end
127 
128 ##
129 # Gem::StreamUI implements a simple stream based user interface.
130 
131 class Gem::StreamUI
132 
133   attr_reader :ins, :outs, :errs
134 
135   def initialize(in_stream, out_stream, err_stream=STDERR)
136     @ins = in_stream
137     @outs = out_stream
138     @errs = err_stream
139   end
140 
141   ##
142   # Choose from a list of options.  +question+ is a prompt displayed above
143   # the list.  +list+ is a list of option strings.  Returns the pair
144   # [option_name, option_index].
145 
146   def choose_from_list(question, list)
147     @outs.puts question
148 
149     list.each_with_index do |item, index|
150       @outs.puts " #{index+1}. #{item}"
151     end
152 
153     @outs.print "> "
154     @outs.flush
155 
156     result = @ins.gets
157 
158     return nil, nil unless result
159 
160     result = result.strip.to_i - 1
161     return list[result], result
162   end
163 
164   ##
165   # Ask a question.  Returns a true for yes, false for no.  If not connected
166   # to a tty, raises an exception if default is nil, otherwise returns
167   # default.
168 
169   def ask_yes_no(question, default=nil)
170     unless @ins.tty? then
171       if default.nil? then
172         raise Gem::OperationNotSupportedError,
173               "Not connected to a tty and no default specified"
174       else
175         return default
176       end
177     end
178 
179     qstr = case default
180            when nil
181              'yn'
182            when true
183              'Yn'
184            else
185              'yN'
186            end
187 
188     result = nil
189 
190     while result.nil?
191       result = ask("#{question} [#{qstr}]")
192       result = case result
193       when /^[Yy].*/
194         true
195       when /^[Nn].*/
196         false
197       when /^$/
198         default
199       else
200         nil
201       end
202     end
203 
204     return result
205   end
206 
207   ##
208   # Ask a question.  Returns an answer if connected to a tty, nil otherwise.
209 
210   def ask(question)
211     return nil if not @ins.tty?
212 
213     @outs.print(question + "  ")
214     @outs.flush
215 
216     result = @ins.gets
217     result.chomp! if result
218     result
219   end
220 
221   ##
222   # Ask for a password. Does not echo response to terminal.
223 
224   def ask_for_password(question)
225     return nil if not @ins.tty?
226 
227     @outs.print(question + "  ")
228     @outs.flush
229 
230     Gem.win_platform? ? ask_for_password_on_windows : ask_for_password_on_unix
231   end
232 
233   ##
234   # Asks for a password that works on windows. Ripped from the Heroku gem.
235 
236   def ask_for_password_on_windows
237     require "Win32API"
238     char = nil
239     password = ''
240 
241     while char = Win32API.new("crtdll", "_getch", [ ], "L").Call do
242       break if char == 10 || char == 13 # received carriage return or newline
243       if char == 127 || char == 8 # backspace and delete
244         password.slice!(-1, 1)
245       else
246         password << char.chr
247       end
248     end
249 
250     puts
251     password
252   end
253 
254   ##
255   # Asks for a password that works on unix
256 
257   def ask_for_password_on_unix
258     system "stty -echo"
259     password = @ins.gets
260     password.chomp! if password
261     system "stty echo"
262     password
263   end
264 
265   ##
266   # Display a statement.
267 
268   def say(statement="")
269     @outs.puts statement
270   end
271 
272   ##
273   # Display an informational alert.  Will ask +question+ if it is not nil.
274 
275   def alert(statement, question=nil)
276     @outs.puts "INFO:  #{statement}"
277     ask(question) if question
278   end
279 
280   ##
281   # Display a warning in a location expected to get error messages.  Will
282   # ask +question+ if it is not nil.
283 
284   def alert_warning(statement, question=nil)
285     @errs.puts "WARNING:  #{statement}"
286     ask(question) if question
287   end
288 
289   ##
290   # Display an error message in a location expected to get error messages.
291   # Will ask +question+ if it is not nil.
292 
293   def alert_error(statement, question=nil)
294     @errs.puts "ERROR:  #{statement}"
295     ask(question) if question
296   end
297 
298   ##
299   # Display a debug message on the same location as error messages.
300 
301   def debug(statement)
302     @errs.puts statement
303   end
304 
305   ##
306   # Terminate the application with exit code +status+, running any exit
307   # handlers that might have been defined.
308 
309   def terminate_interaction(status = 0)
310     raise Gem::SystemExitException, status
311   end
312 
313   ##
314   # Return a progress reporter object chosen from the current verbosity.
315 
316   def progress_reporter(*args)
317     case Gem.configuration.verbose
318     when nil, false
319       SilentProgressReporter.new(@outs, *args)
320     when true
321       SimpleProgressReporter.new(@outs, *args)
322     else
323       VerboseProgressReporter.new(@outs, *args)
324     end
325   end
326 
327   ##
328   # An absolutely silent progress reporter.
329 
330   class SilentProgressReporter
331     attr_reader :count
332 
333     def initialize(out_stream, size, initial_message, terminal_message = nil)
334     end
335 
336     def updated(message)
337     end
338 
339     def done
340     end
341   end
342 
343   ##
344   # A basic dotted progress reporter.
345 
346   class SimpleProgressReporter
347 
348     include Gem::DefaultUserInteraction
349 
350     attr_reader :count
351 
352     def initialize(out_stream, size, initial_message,
353                    terminal_message = "complete")
354       @out = out_stream
355       @total = size
356       @count = 0
357       @terminal_message = terminal_message
358 
359       @out.puts initial_message
360     end
361 
362     ##
363     # Prints out a dot and ignores +message+.
364 
365     def updated(message)
366       @count += 1
367       @out.print "."
368       @out.flush
369     end
370 
371     ##
372     # Prints out the terminal message.
373 
374     def done
375       @out.puts "\n#{@terminal_message}"
376     end
377 
378   end
379 
380   ##
381   # A progress reporter that prints out messages about the current progress.
382 
383   class VerboseProgressReporter
384 
385     include Gem::DefaultUserInteraction
386 
387     attr_reader :count
388 
389     def initialize(out_stream, size, initial_message,
390                    terminal_message = 'complete')
391       @out = out_stream
392       @total = size
393       @count = 0
394       @terminal_message = terminal_message
395 
396       @out.puts initial_message
397     end
398 
399     ##
400     # Prints out the position relative to the total and the +message+.
401 
402     def updated(message)
403       @count += 1
404       @out.puts "#{@count}/#{@total}: #{message}"
405     end
406 
407     ##
408     # Prints out the terminal message.
409 
410     def done
411       @out.puts @terminal_message
412     end
413   end
414 
415 end
416 
417 ##
418 # Subclass of StreamUI that instantiates the user interaction using STDIN,
419 # STDOUT, and STDERR.
420 
421 class Gem::ConsoleUI < Gem::StreamUI
422   def initialize
423     super STDIN, STDOUT, STDERR
424   end
425 end
426 
427 ##
428 # SilentUI is a UI choice that is absolutely silent.
429 
430 class Gem::SilentUI
431   def method_missing(sym, *args, &block)
432     self
433   end
434 end
435 

Generated on Mon May 10 23:40:29 -0700 2010 with rcov 0.9.8