summaryrefslogtreecommitdiff
path: root/test/rubygems/test_gem_commands_setup_command.rb
blob: 5cf94a1dc990a8c097ab2fd71929c5d59d933019 (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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
# frozen_string_literal: true

require_relative 'helper'
require 'rubygems/commands/setup_command'

class TestGemCommandsSetupCommand < Gem::TestCase
  bundler_gemspec = File.expand_path("../../../bundler/lib/bundler/version.rb", __FILE__)
  if File.exist?(bundler_gemspec)
    BUNDLER_VERS = File.read(bundler_gemspec).match(/VERSION = "(#{Gem::Version::VERSION_PATTERN})"/)[1]
  else
    BUNDLER_VERS = "2.0.1".freeze
  end

  def setup
    super

    @cmd = Gem::Commands::SetupCommand.new

    filelist = %w[
      bin/gem
      lib/rubygems.rb
      lib/rubygems/requirement.rb
      lib/rubygems/ssl_certs/rubygems.org/foo.pem
      bundler/exe/bundle
      bundler/exe/bundler
      bundler/lib/bundler.rb
      bundler/lib/bundler/b.rb
      bundler/bin/bundler/man/bundle-b.1
      bundler/lib/bundler/man/bundle-b.1.ronn
      bundler/lib/bundler/man/gemfile.5
      bundler/lib/bundler/man/gemfile.5.ronn
      bundler/lib/bundler/templates/.circleci/config.yml
      bundler/lib/bundler/templates/.travis.yml
    ]

    create_dummy_files(filelist)

    gemspec = Gem::Specification.new
    gemspec.author = "Us"
    gemspec.name = "bundler"
    gemspec.version = BUNDLER_VERS
    gemspec.bindir = "exe"
    gemspec.executables = ["bundle", "bundler"]

    File.open 'bundler/bundler.gemspec', 'w' do |io|
      io.puts gemspec.to_ruby
    end

    File.open(File.join(Gem.default_specifications_dir, "bundler-1.15.4.gemspec"), 'w') do |io|
      gemspec.version = "1.15.4"
      io.puts gemspec.to_ruby
    end

    spec_fetcher do |fetcher|
      fetcher.download "bundler", "1.15.4"

      fetcher.gem "bundler", BUNDLER_VERS

      fetcher.gem "bundler-audit", "1.0.0"
    end
  end

  def test_execute_regenerate_binstubs
    gem_bin_path = gem_install 'a'
    write_file gem_bin_path do |io|
      io.puts 'I changed it!'
    end

    @cmd.options[:document] = []
    @cmd.execute

    assert_match %r{\A#!}, File.read(gem_bin_path)
  end

  def test_execute_no_regenerate_binstubs
    gem_bin_path = gem_install 'a'
    write_file gem_bin_path do |io|
      io.puts 'I changed it!'
    end

    @cmd.options[:document] = []
    @cmd.options[:regenerate_binstubs] = false
    @cmd.execute

    assert_equal "I changed it!\n", File.read(gem_bin_path)
  end

  def test_execute_regenerate_plugins
    gem_plugin_path = gem_install_with_plugin 'a'
    write_file gem_plugin_path do |io|
      io.puts 'I changed it!'
    end

    @cmd.options[:document] = []
    @cmd.execute

    assert_match %r{\Arequire}, File.read(gem_plugin_path)
  end

  def test_execute_no_regenerate_plugins
    gem_plugin_path = gem_install_with_plugin 'a'
    write_file gem_plugin_path do |io|
      io.puts 'I changed it!'
    end

    @cmd.options[:document] = []
    @cmd.options[:regenerate_plugins] = false
    @cmd.execute

    assert_equal "I changed it!\n", File.read(gem_plugin_path)
  end

  def test_execute_regenerate_plugins_creates_plugins_dir_if_not_there
    gem_plugin_path = gem_install_with_plugin 'a'

    # Simulate gem installed with an older rubygems without a plugins layout
    FileUtils.rm_rf Gem.plugindir

    @cmd.options[:document] = []
    @cmd.execute

    assert_match %r{\Arequire}, File.read(gem_plugin_path)
  end

  def test_execute_informs_about_installed_executables
    @cmd.options[:document] = []

    use_ui @ui do
      @cmd.execute
    end

    out = @ui.output.split "\n"

    exec_line = out.shift until exec_line == "RubyGems installed the following executables:"
    assert_equal "\t#{default_gem_bin_path}", out.shift
    assert_equal "\t#{default_bundle_bin_path}", out.shift
    assert_equal "\t#{default_bundler_bin_path}", out.shift
  end

  def test_env_shebang_flag
    gem_bin_path = gem_install 'a'
    write_file gem_bin_path do |io|
      io.puts 'I changed it!'
    end

    @cmd.options[:document] = []
    @cmd.options[:env_shebang] = true
    @cmd.execute

    ruby_exec = sprintf Gem.default_exec_format, 'ruby'

    bin_env = win_platform? ? "" : %w[/usr/bin/env /bin/env].find {|f| File.executable?(f) } + " "
    assert_match %r{\A#!\s*#{bin_env}#{ruby_exec}}, File.read(default_gem_bin_path)
    assert_match %r{\A#!\s*#{bin_env}#{ruby_exec}}, File.read(default_bundle_bin_path)
    assert_match %r{\A#!\s*#{bin_env}#{ruby_exec}}, File.read(default_bundler_bin_path)
    assert_match %r{\A#!\s*#{bin_env}#{ruby_exec}}, File.read(gem_bin_path)
  end

  def test_destdir_flag_does_not_try_to_write_to_the_default_gem_home
    FileUtils.chmod "-w", File.join(@gemhome, "plugins")

    destdir = File.join(@tempdir, 'foo')

    @cmd.options[:destdir] = destdir
    @cmd.execute

    spec = Gem::Specification.load("bundler/bundler.gemspec")

    spec.executables.each do |e|
      assert_path_exist File.join destdir, @gemhome.gsub(/^[a-zA-Z]:/, ''), 'gems', spec.full_name, spec.bindir, e
    end
  end

  def test_files_in
    assert_equal %w[rubygems.rb rubygems/requirement.rb rubygems/ssl_certs/rubygems.org/foo.pem],
                 @cmd.files_in('lib').sort
  end

  def test_install_lib
    @cmd.extend FileUtils

    Dir.mktmpdir 'lib' do |dir|
      @cmd.install_lib dir

      assert_path_exist File.join(dir, 'rubygems.rb')
      assert_path_exist File.join(dir, 'rubygems/ssl_certs/rubygems.org/foo.pem')

      assert_path_exist File.join(dir, 'bundler.rb')
      assert_path_exist File.join(dir, 'bundler/b.rb')

      assert_path_exist File.join(dir, 'bundler/templates/.circleci/config.yml')
      assert_path_exist File.join(dir, 'bundler/templates/.travis.yml')
    end
  end

  def test_install_default_bundler_gem
    @cmd.extend FileUtils

    bin_dir = File.join(@gemhome, 'bin')
    @cmd.install_default_bundler_gem bin_dir

    bundler_spec = Gem::Specification.load("bundler/bundler.gemspec")
    default_spec_path = File.join(Gem.default_specifications_dir, "#{bundler_spec.full_name}.gemspec")
    spec = Gem::Specification.load(default_spec_path)

    spec.executables.each do |e|
      if Gem.win_platform?
        assert_path_exist File.join(bin_dir, "#{e}.bat")
      end

      assert_path_exist File.join bin_dir, e
    end

    default_dir = Gem.default_specifications_dir

    # expect to remove other versions of bundler gemspecs on default specification directory.
    assert_path_not_exist File.join(default_dir, "bundler-1.15.4.gemspec")
    assert_path_exist File.join(default_dir, "bundler-#{BUNDLER_VERS}.gemspec")

    # expect to not remove bundler-* gemspecs.
    assert_path_exist File.join(Gem.dir, "specifications", "bundler-audit-1.0.0.gemspec")

    # expect to remove normal gem that was same version. because it's promoted default gems.
    assert_path_not_exist File.join(Gem.dir, "specifications", "bundler-#{BUNDLER_VERS}.gemspec")

    assert_path_exist "#{Gem.dir}/gems/bundler-#{BUNDLER_VERS}"
    assert_path_exist "#{Gem.dir}/gems/bundler-1.15.4"
    assert_path_exist "#{Gem.dir}/gems/bundler-audit-1.0.0"
  end

  def test_install_default_bundler_gem_with_force_flag
    @cmd.extend FileUtils

    bin_dir = File.join(@gemhome, 'bin')
    bundle_bin = File.join(bin_dir, 'bundle')

    write_file bundle_bin do |f|
      f.puts '#!/usr/bin/ruby'
      f.puts ''
      f.puts 'echo "hello"'
    end

    @cmd.options[:force] = true

    @cmd.install_default_bundler_gem bin_dir

    bundler_spec = Gem::Specification.load("bundler/bundler.gemspec")
    default_spec_path = File.join(Gem.default_specifications_dir, "#{bundler_spec.full_name}.gemspec")
    spec = Gem::Specification.load(default_spec_path)

    spec.executables.each do |e|
      if Gem.win_platform?
        assert_path_exist File.join(bin_dir, "#{e}.bat")
      end

      assert_path_exist File.join bin_dir, e
    end
  end

  def test_install_default_bundler_gem_with_destdir_flag
    @cmd.extend FileUtils

    FileUtils.chmod "-w", @gemhome

    destdir = File.join(@tempdir, 'foo')
    bin_dir = File.join(destdir, 'bin')

    @cmd.options[:destdir] = destdir

    @cmd.install_default_bundler_gem bin_dir

    spec = Gem::Specification.load("bundler/bundler.gemspec")

    spec.executables.each do |e|
      assert_path_exist File.join destdir, @gemhome.gsub(/^[a-zA-Z]:/, ''), 'gems', spec.full_name, spec.bindir, e
    end
  end

  def test_install_default_bundler_gem_with_destdir_and_prefix_flags
    @cmd.extend FileUtils

    destdir = File.join(@tempdir, 'foo')
    bin_dir = File.join(destdir, 'bin')

    @cmd.options[:destdir] = destdir
    @cmd.options[:prefix] = "/"

    @cmd.install_default_bundler_gem bin_dir

    spec = Gem::Specification.load("bundler/bundler.gemspec")

    spec.executables.each do |e|
      assert_path_exist File.join destdir, 'gems', spec.full_name, spec.bindir, e
    end
  end

  def test_remove_old_lib_files
    lib                   = RbConfig::CONFIG["sitelibdir"]
    lib_rubygems          = File.join lib, 'rubygems'
    lib_bundler           = File.join lib, 'bundler'
    lib_rubygems_defaults = File.join lib_rubygems, 'defaults'
    lib_bundler_templates = File.join lib_bundler, 'templates'

    securerandom_rb = File.join lib, 'securerandom.rb'

    engine_defaults_rb = File.join lib_rubygems_defaults, 'jruby.rb'
    os_defaults_rb     = File.join lib_rubygems_defaults, 'operating_system.rb'

    old_gauntlet_rubygems_rb = File.join lib, 'gauntlet_rubygems.rb'

    old_builder_rb     = File.join lib_rubygems, 'builder.rb'
    old_format_rb      = File.join lib_rubygems, 'format.rb'
    old_bundler_c_rb   = File.join lib_bundler,  'c.rb'
    old_bundler_ci     = File.join lib_bundler_templates, '.lecacy_ci', 'config.yml'

    files_that_go   = [old_gauntlet_rubygems_rb, old_builder_rb, old_format_rb, old_bundler_c_rb, old_bundler_ci]
    files_that_stay = [securerandom_rb, engine_defaults_rb, os_defaults_rb]

    create_dummy_files(files_that_go + files_that_stay)

    @cmd.remove_old_lib_files lib

    files_that_go.each {|file| assert_path_not_exist(file) unless file == old_bundler_ci }

    files_that_stay.each {|file| assert_path_exist file }
  end

  def test_remove_old_man_files
    man = File.join RbConfig::CONFIG['mandir'], 'man'

    ruby_1             = File.join man, 'man1', 'ruby.1'
    bundle_b_1         = File.join man, 'man1', 'bundle-b.1'
    bundle_b_1_ronn    = File.join man, 'man1', 'bundle-b.1.ronn'
    bundle_b_1_txt     = File.join man, 'man1', 'bundle-b.1.txt'
    gemfile_5          = File.join man, 'man5', 'gemfile.5'
    gemfile_5_ronn     = File.join man, 'man5', 'gemfile.5.ronn'
    gemfile_5_txt      = File.join man, 'man5', 'gemfile.5.txt'

    files_that_go   = [bundle_b_1, bundle_b_1_txt, bundle_b_1_ronn, gemfile_5, gemfile_5_txt, gemfile_5_ronn]
    files_that_stay = [ruby_1]

    create_dummy_files(files_that_go + files_that_stay)

    @cmd.remove_old_man_files man

    files_that_go.each {|file| assert_path_not_exist file }

    files_that_stay.each {|file| assert_path_exist file }
  end

  def test_show_release_notes
    @default_external = @ui.outs.external_encoding
    @ui.outs.set_encoding Encoding::US_ASCII

    @cmd.options[:previous_version] = Gem::Version.new '2.0.2'

    File.open 'CHANGELOG.md', 'w' do |io|
      io.puts <<-HISTORY_TXT
# #{Gem::VERSION} / 2013-03-26

## Bug fixes:
  * Fixed release note display for LANG=C when installing rubygems
  * π is tasty

# 2.0.2 / 2013-03-06

## Bug fixes:
  * Other bugs fixed

# 2.0.1 / 2013-03-05

## Bug fixes:
  * Yet more bugs fixed
      HISTORY_TXT
    end

    use_ui @ui do
      @cmd.show_release_notes
    end

    expected = <<-EXPECTED
# #{Gem::VERSION} / 2013-03-26

## Bug fixes:
  * Fixed release note display for LANG=C when installing rubygems
  * π is tasty

    EXPECTED

    output = @ui.output
    output.force_encoding Encoding::UTF_8

    assert_equal expected, output
  ensure
    @ui.outs.set_encoding @default_external if @default_external
  end

  private

  def create_dummy_files(list)
    list.each do |file|
      FileUtils.mkdir_p File.dirname(file)

      File.open file, 'w' do |io|
        io.puts "# #{File.basename(file)}"
      end
    end
  end

  def gem_install(name)
    gem = util_spec name do |s|
      s.executables = [name]
      s.files = %W[bin/#{name}]
    end
    write_file File.join @tempdir, 'bin', name do |f|
      f.puts '#!/usr/bin/ruby'
    end
    install_gem gem
    File.join @gemhome, 'bin', name
  end

  def gem_install_with_plugin(name)
    gem = util_spec name do |s|
      s.files = %W[lib/rubygems_plugin.rb]
    end
    write_file File.join @tempdir, 'lib', 'rubygems_plugin.rb' do |f|
      f.puts "require '#{gem.plugins.first}'"
    end
    install_gem gem

    File.join Gem.plugindir, "#{name}_plugin.rb"
  end

  def default_gem_bin_path
    File.join RbConfig::CONFIG['bindir'], 'gem'
  end

  def default_bundle_bin_path
    File.join RbConfig::CONFIG['bindir'], 'bundle'
  end

  def default_bundler_bin_path
    File.join RbConfig::CONFIG['bindir'], 'bundler'
  end
end unless Gem.java_platform?