1require 'rubygems/test_case'
2require 'rubygems/command_manager'
3
4class TestGemCommandManager < Gem::TestCase
5
6  def setup
7    super
8
9    @command_manager = Gem::CommandManager.new
10  end
11
12  def test_find_command
13    command = @command_manager.find_command 'install'
14
15    assert_kind_of Gem::Commands::InstallCommand, command
16
17    command = @command_manager.find_command 'ins'
18
19    assert_kind_of Gem::Commands::InstallCommand, command
20  end
21
22  def test_find_command_ambiguous
23    e = assert_raises Gem::CommandLineError do
24      @command_manager.find_command 'u'
25    end
26
27    assert_equal 'Ambiguous command u matches [uninstall, unpack, update]',
28                 e.message
29  end
30
31  def test_find_command_ambiguous_exact
32    ins_command = Class.new
33    Gem::Commands.send :const_set, :InsCommand, ins_command
34
35    @command_manager.register_command :ins
36
37    command = @command_manager.find_command 'ins'
38
39    assert_kind_of ins_command, command
40  ensure
41    Gem::Commands.send :remove_const, :InsCommand
42  end
43
44  def test_find_command_unknown
45    e = assert_raises Gem::CommandLineError do
46      @command_manager.find_command 'xyz'
47    end
48
49    assert_equal 'Unknown command xyz', e.message
50  end
51
52  def test_run_interrupt
53    old_load_path = $:.dup
54    $: << File.expand_path("test/rubygems", @@project_dir)
55    Gem.load_env_plugins
56
57    @command_manager.register_command :interrupt
58
59    use_ui @ui do
60      assert_raises Gem::MockGemUi::TermError do
61        @command_manager.run 'interrupt'
62      end
63      assert_equal '', ui.output
64      assert_equal "ERROR:  Interrupted\n", ui.error
65    end
66  ensure
67    $:.replace old_load_path
68    Gem::CommandManager.reset
69  end
70
71  def test_run_crash_command
72    old_load_path = $:.dup
73    $: << File.expand_path("test/rubygems", @@project_dir)
74
75    @command_manager.register_command :crash
76    use_ui @ui do
77      assert_raises Gem::MockGemUi::TermError do
78        @command_manager.run 'crash'
79      end
80      assert_equal '', ui.output
81      err = ui.error.split("\n").first
82      assert_equal "ERROR:  Loading command: crash (RuntimeError)", err
83    end
84  ensure
85    $:.replace old_load_path
86    @command_manager.unregister_command :crash
87  end
88
89  def test_process_args_bad_arg
90    use_ui @ui do
91      assert_raises Gem::MockGemUi::TermError do
92        @command_manager.process_args("--bad-arg")
93      end
94    end
95
96    assert_match(/invalid option: --bad-arg/i, @ui.error)
97  end
98
99  # HACK move to install command test
100  def test_process_args_install
101    #capture all install options
102    use_ui @ui do
103      check_options = nil
104      @command_manager['install'].when_invoked do |options|
105        check_options = options
106        true
107      end
108
109      #check defaults
110      @command_manager.process_args("install")
111      assert_equal %w[ri], check_options[:document].sort
112      assert_equal false, check_options[:force]
113      assert_equal :both, check_options[:domain]
114      assert_equal true, check_options[:wrappers]
115      assert_equal Gem::Requirement.default, check_options[:version]
116      assert_equal nil, check_options[:install_dir]
117      assert_equal nil, check_options[:bin_dir]
118
119      #check settings
120      check_options = nil
121      @command_manager.process_args(
122        "install --force --local --rdoc --install-dir . --version 3.0 --no-wrapper --bindir . ")
123      assert_equal %w[rdoc ri], check_options[:document].sort
124      assert_equal true, check_options[:force]
125      assert_equal :local, check_options[:domain]
126      assert_equal false, check_options[:wrappers]
127      assert_equal Gem::Requirement.new('3.0'), check_options[:version]
128      assert_equal Dir.pwd, check_options[:install_dir]
129      assert_equal Dir.pwd, check_options[:bin_dir]
130
131      #check remote domain
132      check_options = nil
133      @command_manager.process_args("install --remote")
134      assert_equal :remote, check_options[:domain]
135
136      #check both domain
137      check_options = nil
138      @command_manager.process_args("install --both")
139      assert_equal :both, check_options[:domain]
140
141      #check both domain
142      check_options = nil
143      @command_manager.process_args("install --both")
144      assert_equal :both, check_options[:domain]
145    end
146  end
147
148  # HACK move to uninstall command test
149  def test_process_args_uninstall
150    #capture all uninstall options
151    check_options = nil
152    @command_manager['uninstall'].when_invoked do |options|
153      check_options = options
154      true
155    end
156
157    #check defaults
158    @command_manager.process_args("uninstall")
159    assert_equal Gem::Requirement.default, check_options[:version]
160
161    #check settings
162    check_options = nil
163    @command_manager.process_args("uninstall foobar --version 3.0")
164    assert_equal "foobar", check_options[:args].first
165    assert_equal Gem::Requirement.new('3.0'), check_options[:version]
166  end
167
168  # HACK move to check command test
169  def test_process_args_check
170    #capture all check options
171    check_options = nil
172    @command_manager['check'].when_invoked do |options|
173      check_options = options
174      true
175    end
176
177    #check defaults
178    @command_manager.process_args("check")
179    assert_equal true, check_options[:alien]
180
181    #check settings
182    check_options = nil
183    @command_manager.process_args("check foobar --alien")
184    assert_equal true, check_options[:alien]
185  end
186
187  # HACK move to build command test
188  def test_process_args_build
189    #capture all build options
190    check_options = nil
191    @command_manager['build'].when_invoked do |options|
192      check_options = options
193      true
194    end
195
196    #check defaults
197    @command_manager.process_args("build")
198    #NOTE: Currently no defaults
199
200    #check settings
201    check_options = nil
202    @command_manager.process_args("build foobar.rb")
203    assert_equal 'foobar.rb', check_options[:args].first
204  end
205
206  # HACK move to query command test
207  def test_process_args_query
208    #capture all query options
209    check_options = nil
210    @command_manager['query'].when_invoked do |options|
211      check_options = options
212      true
213    end
214
215    #check defaults
216    @command_manager.process_args("query")
217    assert_equal(//, check_options[:name])
218    assert_equal :local, check_options[:domain]
219    assert_equal false, check_options[:details]
220
221    #check settings
222    check_options = nil
223    @command_manager.process_args("query --name foobar --local --details")
224    assert_equal(/foobar/i, check_options[:name])
225    assert_equal :local, check_options[:domain]
226    assert_equal true, check_options[:details]
227
228    #remote domain
229    check_options = nil
230    @command_manager.process_args("query --remote")
231    assert_equal :remote, check_options[:domain]
232
233    #both (local/remote) domains
234    check_options = nil
235    @command_manager.process_args("query --both")
236    assert_equal :both, check_options[:domain]
237  end
238
239  # HACK move to update command test
240  def test_process_args_update
241    #capture all update options
242    check_options = nil
243    @command_manager['update'].when_invoked do |options|
244      check_options = options
245      true
246    end
247
248    #check defaults
249    @command_manager.process_args("update")
250    assert_includes check_options[:document], 'rdoc'
251
252    #check settings
253    check_options = nil
254    @command_manager.process_args("update --force --rdoc --install-dir .")
255    assert_includes check_options[:document], 'ri'
256    assert_equal true, check_options[:force]
257    assert_equal Dir.pwd, check_options[:install_dir]
258  end
259
260end
261
262