summaryrefslogtreecommitdiff
path: root/test/rubygems/test_gem_command_manager.rb
blob: 8108dc08705816ec3209a928daaeb16c7434dc6c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
# frozen_string_literal: true
require_relative "helper"
require "rubygems/command_manager"

class TestGemCommandManager < Gem::TestCase
  PROJECT_DIR = File.expand_path("../..", __dir__).tap(&Gem::UNTAINT)

  def setup
    super

    @command_manager = Gem::CommandManager.new
  end

  def test_find_command
    command = @command_manager.find_command "install"

    assert_kind_of Gem::Commands::InstallCommand, command

    command = @command_manager.find_command "ins"

    assert_kind_of Gem::Commands::InstallCommand, command
  end

  def test_find_command_ambiguous
    e = assert_raise Gem::CommandLineError do
      @command_manager.find_command "u"
    end

    assert_equal "Ambiguous command u matches [uninstall, unpack, update]",
                 e.message
  end

  def test_find_alias_command
    command = @command_manager.find_command "i"

    assert_kind_of Gem::Commands::InstallCommand, command
  end

  def test_find_login_alias_command
    command = @command_manager.find_command "login"

    assert_kind_of Gem::Commands::SigninCommand, command
  end

  def test_find_logout_alias_comamnd
    command = @command_manager.find_command "logout"

    assert_kind_of Gem::Commands::SignoutCommand, command
  end

  def test_find_command_ambiguous_exact
    ins_command = Class.new
    Gem::Commands.send :const_set, :InsCommand, ins_command

    @command_manager.register_command :ins

    command = @command_manager.find_command "ins"

    assert_kind_of ins_command, command
  ensure
    Gem::Commands.send :remove_const, :InsCommand
  end

  def test_find_command_unknown
    e = assert_raise Gem::UnknownCommandError do
      @command_manager.find_command "xyz"
    end

    assert_equal "Unknown command xyz", e.message
  end

  def test_find_command_unknown_suggestions
    e = assert_raise Gem::UnknownCommandError do
      @command_manager.find_command "pish"
    end

    message = "Unknown command pish".dup

    if defined?(DidYouMean::SPELL_CHECKERS) && defined?(DidYouMean::Correctable)
      message << "\nDid you mean?  \"push\""
    end

    if e.respond_to?(:detailed_message)
      actual_message = e.detailed_message(highlight: false).sub(/\A(.*?)(?: \(.+?\))/) { $1 }
    else
      actual_message = e.message
    end

    assert_equal message, actual_message
  end

  def test_run_interrupt
    old_load_path = $:.dup
    $: << File.expand_path("test/rubygems", PROJECT_DIR)
    Gem.load_env_plugins

    @command_manager.register_command :interrupt

    use_ui @ui do
      assert_raise Gem::MockGemUi::TermError do
        @command_manager.run %w[interrupt]
      end
      assert_equal "", ui.output
      assert_equal "ERROR:  Interrupted\n", ui.error
    end
  ensure
    $:.replace old_load_path
    Gem::CommandManager.reset
  end

  def test_run_crash_command
    old_load_path = $:.dup
    $: << File.expand_path("test/rubygems", PROJECT_DIR)

    @command_manager.register_command :crash
    use_ui @ui do
      assert_raise Gem::MockGemUi::TermError do
        @command_manager.run %w[crash]
      end
      assert_equal "", ui.output
      err = ui.error.split("\n").first
      assert_equal "ERROR:  Loading command: crash (RuntimeError)", err
    end
  ensure
    $:.replace old_load_path
    @command_manager.unregister_command :crash
  end

  def test_process_args_with_c_flag
    custom_start_point = File.join @tempdir, "nice_folder"
    FileUtils.mkdir_p custom_start_point

    execution_path = nil
    use_ui @ui do
      @command_manager[:install].when_invoked do
        execution_path = Dir.pwd
        true
      end
      @command_manager.process_args %W[-C #{custom_start_point} install net-scp-4.0.0.gem --local]
    end

    assert_equal custom_start_point, execution_path
  end

  def test_process_args_with_c_flag_without_path
    use_ui @ui do
      assert_raise Gem::MockGemUi::TermError do
        @command_manager.process_args %w[-C install net-scp-4.0.0.gem --local]
      end
    end

    assert_match(/install isn't a directory./i, @ui.error)
  end

  def test_process_args_with_c_flag_path_not_found
    custom_start_point = File.join @tempdir, "nice_folder"
    FileUtils.mkdir_p custom_start_point
    custom_start_point.tr!("_", "-")

    use_ui @ui do
      assert_raise Gem::MockGemUi::TermError do
        @command_manager.process_args %W[-C #{custom_start_point} install net-scp-4.0.0.gem --local]
      end
    end

    assert_match(/#{custom_start_point} isn't a directory./i, @ui.error)
  end

  def test_process_args_bad_arg
    use_ui @ui do
      assert_raise Gem::MockGemUi::TermError do
        @command_manager.process_args %w[--bad-arg]
      end
    end

    assert_match(/invalid option: --bad-arg/i, @ui.error)
  end

  # HACK move to install command test
  def test_process_args_install
    #capture all install options
    use_ui @ui do
      check_options = nil
      @command_manager["install"].when_invoked do |options|
        check_options = options
        true
      end

      #check defaults
      @command_manager.process_args %w[install]
      assert_equal %w[ri], check_options[:document].sort
      assert_equal false, check_options[:force]
      assert_equal :both, check_options[:domain]
      assert_equal true, check_options[:wrappers]
      assert_equal Gem::Requirement.default, check_options[:version]
      assert_nil   check_options[:install_dir]
      assert_nil   check_options[:bin_dir]

      #check settings
      check_options = nil
      @command_manager.process_args %w[
        install --force --local --document=ri,rdoc --install-dir .
        --version 3.0 --no-wrapper --bindir .
      ]
      assert_equal %w[rdoc ri], check_options[:document].sort
      assert_equal true, check_options[:force]
      assert_equal :local, check_options[:domain]
      assert_equal false, check_options[:wrappers]
      assert_equal Gem::Requirement.new("3.0"), check_options[:version]
      assert_equal Dir.pwd, check_options[:install_dir]
      assert_equal Dir.pwd, check_options[:bin_dir]

      #check remote domain
      check_options = nil
      @command_manager.process_args %w[install --remote]
      assert_equal :remote, check_options[:domain]

      #check both domain
      check_options = nil
      @command_manager.process_args %w[install --both]
      assert_equal :both, check_options[:domain]

      #check both domain
      check_options = nil
      @command_manager.process_args %w[install --both]
      assert_equal :both, check_options[:domain]
    end
  end

  # HACK move to uninstall command test
  def test_process_args_uninstall
    #capture all uninstall options
    check_options = nil
    @command_manager["uninstall"].when_invoked do |options|
      check_options = options
      true
    end

    #check defaults
    @command_manager.process_args %w[uninstall]
    assert_equal Gem::Requirement.default, check_options[:version]

    #check settings
    check_options = nil
    @command_manager.process_args %w[uninstall foobar --version 3.0]
    assert_equal "foobar", check_options[:args].first
    assert_equal Gem::Requirement.new("3.0"), check_options[:version]
  end

  # HACK move to check command test
  def test_process_args_check
    #capture all check options
    check_options = nil
    @command_manager["check"].when_invoked do |options|
      check_options = options
      true
    end

    #check defaults
    @command_manager.process_args %w[check]
    assert_equal true, check_options[:alien]

    #check settings
    check_options = nil
    @command_manager.process_args %w[check foobar --alien]
    assert_equal true, check_options[:alien]
  end

  # HACK move to build command test
  def test_process_args_build
    #capture all build options
    check_options = nil
    @command_manager["build"].when_invoked do |options|
      check_options = options
      true
    end

    #check defaults
    @command_manager.process_args %w[build]
    #NOTE: Currently no defaults

    #check settings
    check_options = nil
    @command_manager.process_args %w[build foobar.rb]
    assert_equal "foobar.rb", check_options[:args].first
  end

  # HACK move to query command test
  def test_process_args_query
    #capture all query options
    check_options = nil
    @command_manager["query"].when_invoked do |options|
      check_options = options
      true
    end

    #check defaults
    Gem::Deprecate.skip_during do
      @command_manager.process_args %w[query]
    end
    assert_nil(check_options[:name])
    assert_equal :local, check_options[:domain]
    assert_equal false, check_options[:details]

    #check settings
    check_options = nil
    Gem::Deprecate.skip_during do
      @command_manager.process_args %w[query --name foobar --local --details]
    end
    assert_equal(/foobar/i, check_options[:name])
    assert_equal :local, check_options[:domain]
    assert_equal true, check_options[:details]

    #remote domain
    check_options = nil
    Gem::Deprecate.skip_during do
      @command_manager.process_args %w[query --remote]
    end
    assert_equal :remote, check_options[:domain]

    #both (local/remote) domains
    check_options = nil
    Gem::Deprecate.skip_during do
      @command_manager.process_args %w[query --both]
    end
    assert_equal :both, check_options[:domain]
  end

  # HACK move to update command test
  def test_process_args_update
    #capture all update options
    check_options = nil
    @command_manager["update"].when_invoked do |options|
      check_options = options
      true
    end

    #check defaults
    @command_manager.process_args %w[update]
    assert_includes check_options[:document], "ri"

    #check settings
    check_options = nil
    @command_manager.process_args %w[update --force --document=ri --install-dir .]
    assert_includes check_options[:document], "ri"
    assert_equal true, check_options[:force]
    assert_equal Dir.pwd, check_options[:install_dir]
  end

  def test_deprecated_command
    require "rubygems/command"
    foo_command = Class.new(Gem::Command) do
      extend Gem::Deprecate

      rubygems_deprecate_command

      def execute
        say "pew pew!"
      end
    end

    Gem::Commands.send(:const_set, :FooCommand, foo_command)
    @command_manager.register_command(:foo, foo_command.new("foo"))

    use_ui @ui do
      @command_manager.process_args(%w[foo])
    end

    assert_equal "pew pew!\n", @ui.output
    assert_match(/WARNING:  foo command is deprecated. It will be removed in Rubygems [0-9]+/, @ui.error)
  ensure
    Gem::Commands.send(:remove_const, :FooCommand)
  end

  def test_deprecated_command_with_version
    require "rubygems/command"
    foo_command = Class.new(Gem::Command) do
      extend Gem::Deprecate

      rubygems_deprecate_command("9.9.9")

      def execute
        say "pew pew!"
      end
    end

    Gem::Commands.send(:const_set, :FooCommand, foo_command)
    @command_manager.register_command(:foo, foo_command.new("foo"))

    use_ui @ui do
      @command_manager.process_args(%w[foo])
    end

    assert_equal "pew pew!\n", @ui.output
    assert_match(/WARNING:  foo command is deprecated. It will be removed in Rubygems 9.9.9/, @ui.error)
  ensure
    Gem::Commands.send(:remove_const, :FooCommand)
  end
end