1require 'rubygems/test_case'
2require 'rubygems/user_interaction'
3require 'timeout'
4
5class TestGemStreamUI < Gem::TestCase
6
7  module IsTty
8    attr_accessor :tty
9
10    def tty?
11      @tty = true unless defined? @tty
12      return @tty
13    end
14
15    alias_method :isatty, :tty?
16
17    def noecho
18      yield self
19    end
20  end
21
22  def setup
23    super
24
25    @cfg = Gem.configuration
26
27    @in = StringIO.new
28    @out = StringIO.new
29    @err = StringIO.new
30
31    @in.extend IsTty
32    @out.extend IsTty
33
34    @sui = Gem::StreamUI.new @in, @out, @err, true
35  end
36
37  def test_ask
38    skip 'TTY detection broken on windows' if
39      Gem.win_platform? unless RUBY_VERSION > '1.9.2'
40
41    timeout(1) do
42      expected_answer = "Arthur, King of the Britons"
43      @in.string = "#{expected_answer}\n"
44      actual_answer = @sui.ask("What is your name?")
45      assert_equal expected_answer, actual_answer
46    end
47  end
48
49  def test_ask_no_tty
50    skip 'TTY detection broken on windows' if
51      Gem.win_platform? unless RUBY_VERSION > '1.9.2'
52
53    @in.tty = false
54
55    timeout(0.1) do
56      answer = @sui.ask("what is your favorite color?")
57      assert_equal nil, answer
58    end
59  end
60
61  def test_ask_for_password
62    skip 'Always uses $stdin on windows' if
63      Gem.win_platform? unless RUBY_VERSION > '1.9.2'
64
65    timeout(1) do
66      expected_answer = "Arthur, King of the Britons"
67      @in.string = "#{expected_answer}\n"
68      actual_answer = @sui.ask_for_password("What is your name?")
69      assert_equal expected_answer, actual_answer
70    end
71  end
72
73  def test_ask_for_password_no_tty
74    skip 'TTY handling is broken on windows' if
75      Gem.win_platform? unless RUBY_VERSION > '1.9.2'
76
77    @in.tty = false
78
79    timeout(0.1) do
80      answer = @sui.ask_for_password("what is the airspeed velocity of an unladen swallow?")
81      assert_equal nil, answer
82    end
83  end
84
85  def test_ask_yes_no_no_tty_with_default
86    skip 'TTY handling is broken on windows' if
87      Gem.win_platform? unless RUBY_VERSION > '1.9.2'
88
89    @in.tty = false
90
91    timeout(0.1) do
92      answer = @sui.ask_yes_no("do coconuts migrate?", false)
93      assert_equal false, answer
94
95      answer = @sui.ask_yes_no("do coconuts migrate?", true)
96      assert_equal true, answer
97    end
98  end
99
100  def test_ask_yes_no_no_tty_without_default
101    skip 'TTY handling is broken on windows' if
102      Gem.win_platform? unless RUBY_VERSION > '1.9.2'
103
104    @in.tty = false
105
106    timeout(0.1) do
107      assert_raises(Gem::OperationNotSupportedError) do
108        @sui.ask_yes_no("do coconuts migrate?")
109      end
110    end
111  end
112
113  def test_choose_from_list
114    @in.puts "1"
115    @in.rewind
116
117    result = @sui.choose_from_list 'which one?', %w[foo bar]
118
119    assert_equal ['foo', 0], result
120    assert_equal "which one?\n 1. foo\n 2. bar\n> ", @out.string
121  end
122
123  def test_choose_from_list_EOF
124    result = @sui.choose_from_list 'which one?', %w[foo bar]
125
126    assert_equal [nil, nil], result
127    assert_equal "which one?\n 1. foo\n 2. bar\n> ", @out.string
128  end
129
130  def test_progress_reporter_silent_nil
131    @cfg.verbose = nil
132    reporter = @sui.progress_reporter 10, 'hi'
133    assert_kind_of Gem::StreamUI::SilentProgressReporter, reporter
134  end
135
136  def test_progress_reporter_silent_false
137    @cfg.verbose = false
138    reporter = @sui.progress_reporter 10, 'hi'
139    assert_kind_of Gem::StreamUI::SilentProgressReporter, reporter
140    assert_equal "", @out.string
141  end
142
143  def test_progress_reporter_simple
144    @cfg.verbose = true
145    reporter = @sui.progress_reporter 10, 'hi'
146    assert_kind_of Gem::StreamUI::SimpleProgressReporter, reporter
147    assert_equal "hi\n", @out.string
148  end
149
150  def test_progress_reporter_verbose
151    @cfg.verbose = 0
152    reporter = @sui.progress_reporter 10, 'hi'
153    assert_kind_of Gem::StreamUI::VerboseProgressReporter, reporter
154    assert_equal "hi\n", @out.string
155  end
156
157  def test_download_reporter_silent_nil
158    @cfg.verbose = nil
159    reporter = @sui.download_reporter
160    reporter.fetch 'a.gem', 1024
161    assert_kind_of Gem::StreamUI::SilentDownloadReporter, reporter
162    assert_equal "", @out.string
163  end
164
165  def test_download_reporter_silent_false
166    @cfg.verbose = false
167    reporter = @sui.download_reporter
168    reporter.fetch 'a.gem', 1024
169    assert_kind_of Gem::StreamUI::SilentDownloadReporter, reporter
170    assert_equal "", @out.string
171  end
172
173  def test_download_reporter_anything
174    @cfg.verbose = 0
175    reporter = @sui.download_reporter
176    assert_kind_of Gem::StreamUI::VerboseDownloadReporter, reporter
177  end
178
179  def test_verbose_download_reporter
180    @cfg.verbose = true
181    reporter = @sui.download_reporter
182    reporter.fetch 'a.gem', 1024
183    assert_equal "Fetching: a.gem", @out.string
184  end
185
186  def test_verbose_download_reporter_progress
187    @cfg.verbose = true
188    reporter = @sui.download_reporter
189    reporter.fetch 'a.gem', 1024
190    reporter.update 512
191    assert_equal "Fetching: a.gem\rFetching: a.gem ( 50%)", @out.string
192  end
193
194  def test_verbose_download_reporter_progress_once
195    @cfg.verbose = true
196    reporter = @sui.download_reporter
197    reporter.fetch 'a.gem', 1024
198    reporter.update 510
199    reporter.update 512
200    assert_equal "Fetching: a.gem\rFetching: a.gem ( 50%)", @out.string
201  end
202
203  def test_verbose_download_reporter_progress_complete
204    @cfg.verbose = true
205    reporter = @sui.download_reporter
206    reporter.fetch 'a.gem', 1024
207    reporter.update 510
208    reporter.done
209    assert_equal "Fetching: a.gem\rFetching: a.gem ( 50%)\rFetching: a.gem (100%)\n", @out.string
210  end
211
212  def test_verbose_download_reporter_progress_nil_length
213    @cfg.verbose = true
214    reporter = @sui.download_reporter
215    reporter.fetch 'a.gem', nil
216    reporter.update 1024
217    reporter.done
218    assert_equal "Fetching: a.gem\rFetching: a.gem (1024B)\rFetching: a.gem (1024B)\n", @out.string
219  end
220
221  def test_verbose_download_reporter_progress_zero_length
222    @cfg.verbose = true
223    reporter = @sui.download_reporter
224    reporter.fetch 'a.gem', 0
225    reporter.update 1024
226    reporter.done
227    assert_equal "Fetching: a.gem\rFetching: a.gem (1024B)\rFetching: a.gem (1024B)\n", @out.string
228  end
229
230  def test_verbose_download_reporter_no_tty
231    @out.tty = false
232
233    @cfg.verbose = true
234    reporter = @sui.download_reporter
235    reporter.fetch 'a.gem', 1024
236    assert_equal "", @out.string
237  end
238end
239