summaryrefslogtreecommitdiff
path: root/spec/bundler/commands/check_spec.rb
blob: b4996977c1850f618e94979aaf3ab52ec3283200 (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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
# frozen_string_literal: true

RSpec.describe "bundle check" do
  it "returns success when the Gemfile is satisfied" do
    install_gemfile <<-G
      source "#{file_uri_for(gem_repo1)}"
      gem "rails"
    G

    bundle :check
    expect(out).to include("The Gemfile's dependencies are satisfied")
  end

  it "works with the --gemfile flag when not in the directory" do
    install_gemfile <<-G
      source "#{file_uri_for(gem_repo1)}"
      gem "rails"
    G

    bundle "check --gemfile bundled_app/Gemfile", :dir => tmp
    expect(out).to include("The Gemfile's dependencies are satisfied")
  end

  it "creates a Gemfile.lock by default if one does not exist" do
    install_gemfile <<-G
      source "#{file_uri_for(gem_repo1)}"
      gem "rails"
    G

    FileUtils.rm(bundled_app_lock)

    bundle "check"

    expect(bundled_app_lock).to exist
  end

  it "does not create a Gemfile.lock if --dry-run was passed" do
    install_gemfile <<-G
      source "#{file_uri_for(gem_repo1)}"
      gem "rails"
    G

    FileUtils.rm(bundled_app_lock)

    bundle "check --dry-run"

    expect(bundled_app_lock).not_to exist
  end

  it "prints a generic error if the missing gems are unresolvable" do
    system_gems ["rails-2.3.2"]

    gemfile <<-G
      source "#{file_uri_for(gem_repo1)}"
      gem "rails"
    G

    bundle :check, :raise_on_error => false
    expect(err).to include("Bundler can't satisfy your Gemfile's dependencies.")
  end

  it "prints a generic error if a Gemfile.lock does not exist and a toplevel dependency does not exist" do
    gemfile <<-G
      source "#{file_uri_for(gem_repo1)}"
      gem "rails"
    G

    bundle :check, :raise_on_error => false
    expect(exitstatus).to be > 0
    expect(err).to include("Bundler can't satisfy your Gemfile's dependencies.")
  end

  it "prints a generic message if you changed your lockfile" do
    build_repo2 do
      build_gem "rails_pinned_to_old_activesupport" do |s|
        s.add_dependency "activesupport", "= 1.2.3"
      end
    end

    install_gemfile <<-G
      source "#{file_uri_for(gem_repo2)}"
      gem 'rails'
    G

    gemfile <<-G
      source "#{file_uri_for(gem_repo2)}"
      gem "rails"
      gem "rails_pinned_to_old_activesupport"
    G

    bundle :check, :raise_on_error => false
    expect(err).to include("Bundler can't satisfy your Gemfile's dependencies.")
  end

  it "remembers --without option from install", :bundler => "< 3" do
    gemfile <<-G
      source "#{file_uri_for(gem_repo1)}"
      group :foo do
        gem "rack"
      end
    G

    bundle "install --without foo"
    bundle "check"
    expect(out).to include("The Gemfile's dependencies are satisfied")
  end

  it "uses the without setting" do
    bundle "config set without foo"
    install_gemfile <<-G
      source "#{file_uri_for(gem_repo1)}"
      group :foo do
        gem "rack"
      end
    G

    bundle "check"
    expect(out).to include("The Gemfile's dependencies are satisfied")
  end

  it "ensures that gems are actually installed and not just cached" do
    gemfile <<-G
      source "#{file_uri_for(gem_repo1)}"
      gem "rack", :group => :foo
    G

    bundle "config set --local without foo"
    bundle :install

    gemfile <<-G
      source "#{file_uri_for(gem_repo1)}"
      gem "rack"
    G

    bundle "check", :raise_on_error => false
    expect(err).to include("* rack (1.0.0)")
    expect(exitstatus).to eq(1)
  end

  it "ensures that gems are actually installed and not just cached in applications' cache" do
    gemfile <<-G
      source "#{file_uri_for(gem_repo1)}"
      gem "rack"
    G

    bundle "config set --local path vendor/bundle"
    bundle :cache

    gem_command "uninstall rack", :env => { "GEM_HOME" => vendored_gems.to_s }

    bundle "check", :raise_on_error => false
    expect(err).to include("* rack (1.0.0)")
    expect(exitstatus).to eq(1)
  end

  it "ignores missing gems restricted to other platforms" do
    gemfile <<-G
      source "#{file_uri_for(gem_repo1)}"
      gem "rack"
      platforms :#{not_local_tag} do
        gem "activesupport"
      end
    G

    system_gems "rack-1.0.0", :path => default_bundle_path

    lockfile <<-G
      GEM
        remote: #{file_uri_for(gem_repo1)}/
        specs:
          activesupport (2.3.5)
          rack (1.0.0)

      PLATFORMS
        #{local}
        #{not_local}

      DEPENDENCIES
        rack
        activesupport
    G

    bundle :check
    expect(out).to include("The Gemfile's dependencies are satisfied")
  end

  it "works with env conditionals" do
    gemfile <<-G
      source "#{file_uri_for(gem_repo1)}"
      gem "rack"
      env :NOT_GOING_TO_BE_SET do
        gem "activesupport"
      end
    G

    system_gems "rack-1.0.0", :path => default_bundle_path

    lockfile <<-G
      GEM
        remote: #{file_uri_for(gem_repo1)}/
        specs:
          activesupport (2.3.5)
          rack (1.0.0)

      PLATFORMS
        #{local}
        #{not_local}

      DEPENDENCIES
        rack
        activesupport
    G

    bundle :check
    expect(out).to include("The Gemfile's dependencies are satisfied")
  end

  it "outputs an error when the default Gemfile is not found" do
    bundle :check, :raise_on_error => false
    expect(exitstatus).to eq(10)
    expect(err).to include("Could not locate Gemfile")
  end

  it "does not output fatal error message" do
    bundle :check, :raise_on_error => false
    expect(exitstatus).to eq(10)
    expect(err).not_to include("Unfortunately, a fatal error has occurred. ")
  end

  it "fails when there's no lock file and frozen is set" do
    install_gemfile <<-G
      source "#{file_uri_for(gem_repo1)}"
      gem "foo"
    G

    bundle "config set --local deployment true"
    bundle "install"
    FileUtils.rm(bundled_app_lock)

    bundle :check, :raise_on_error => false
    expect(last_command).to be_failure
  end

  context "--path", :bundler => "< 3" do
    context "after installing gems in the proper directory" do
      before do
        gemfile <<-G
          source "#{file_uri_for(gem_repo1)}"
          gem "rails"
        G
        bundle "install --path vendor/bundle"

        FileUtils.rm_rf(bundled_app(".bundle"))
      end

      it "returns success" do
        bundle "check --path vendor/bundle"
        expect(out).to include("The Gemfile's dependencies are satisfied")
      end

      it "should write to .bundle/config" do
        bundle "check --path vendor/bundle"
        bundle "check"
      end
    end

    context "after installing gems on a different directory" do
      before do
        install_gemfile <<-G
          source "#{file_uri_for(gem_repo1)}"
          gem "rails"
        G

        bundle "check --path vendor/bundle", :raise_on_error => false
      end

      it "returns false" do
        expect(exitstatus).to eq(1)
        expect(err).to match(/The following gems are missing/)
      end
    end
  end

  describe "when locked" do
    before :each do
      system_gems "rack-1.0.0"
      install_gemfile <<-G
        source "#{file_uri_for(gem_repo1)}"
        gem "rack", "1.0"
      G
    end

    it "returns success when the Gemfile is satisfied" do
      bundle :install
      bundle :check
      expect(out).to include("The Gemfile's dependencies are satisfied")
    end

    it "shows what is missing with the current Gemfile if it is not satisfied" do
      simulate_new_machine
      bundle :check, :raise_on_error => false
      expect(err).to match(/The following gems are missing/)
      expect(err).to include("* rack (1.0")
    end
  end

  describe "when locked with multiple dependents with different requirements" do
    before :each do
      build_repo4 do
        build_gem "depends_on_rack" do |s|
          s.add_dependency "rack", ">= 1.0"
        end
        build_gem "also_depends_on_rack" do |s|
          s.add_dependency "rack", "~> 1.0"
        end
        build_gem "rack"
      end

      gemfile <<-G
        source "#{file_uri_for(gem_repo4)}"
        gem "depends_on_rack"
        gem "also_depends_on_rack"
      G

      bundle "lock"
    end

    it "shows what is missing with the current Gemfile without duplications" do
      bundle :check, :raise_on_error => false
      expect(err).to match(/The following gems are missing/)
      expect(err).to include("* rack (1.0").once
    end
  end

  describe "when locked under multiple platforms" do
    before :each do
      build_repo4 do
        build_gem "rack"
      end

      gemfile <<-G
        source "#{file_uri_for(gem_repo4)}"
        gem "rack"
      G

      lockfile <<-L
        GEM
          remote: #{file_uri_for(gem_repo4)}/
          specs:
            rack (1.0)

        PLATFORMS
          ruby
          #{specific_local_platform}

        DEPENDENCIES
          rack

        BUNDLED WITH
           #{Bundler::VERSION}
      L
    end

    it "shows what is missing with the current Gemfile without duplications" do
      bundle :check, :raise_on_error => false
      expect(err).to match(/The following gems are missing/)
      expect(err).to include("* rack (1.0").once
    end
  end

  describe "when using only scoped rubygems sources" do
    before do
      gemfile <<~G
        source "#{file_uri_for(gem_repo2)}"
        source "#{file_uri_for(gem_repo1)}" do
          gem "rack"
        end
      G
    end

    it "returns success when the Gemfile is satisfied" do
      system_gems "rack-1.0.0", :path => default_bundle_path
      bundle :check
      expect(out).to include("The Gemfile's dependencies are satisfied")
    end
  end

  describe "when using only scoped rubygems sources with indirect dependencies" do
    before do
      build_repo4 do
        build_gem "depends_on_rack" do |s|
          s.add_dependency "rack"
        end

        build_gem "rack"
      end

      gemfile <<~G
        source "#{file_uri_for(gem_repo1)}"
        source "#{file_uri_for(gem_repo4)}" do
          gem "depends_on_rack"
        end
      G
    end

    it "returns success when the Gemfile is satisfied and generates a correct lockfile" do
      system_gems "depends_on_rack-1.0", "rack-1.0", :gem_repo => gem_repo4, :path => default_bundle_path
      bundle :check
      expect(out).to include("The Gemfile's dependencies are satisfied")
      expect(lockfile).to eq <<~L
        GEM
          remote: #{file_uri_for(gem_repo1)}/
          specs:

        GEM
          remote: #{file_uri_for(gem_repo4)}/
          specs:
            depends_on_rack (1.0)
              rack
            rack (1.0)

        PLATFORMS
          #{lockfile_platforms}

        DEPENDENCIES
          depends_on_rack!

        BUNDLED WITH
           #{Bundler::VERSION}
      L
    end
  end

  context "with gemspec directive and scoped sources" do
    before do
      build_repo4 do
        build_gem "awesome_print"
      end

      build_repo2 do
        build_gem "dex-dispatch-engine"
      end

      build_lib("bundle-check-issue", :path => tmp.join("bundle-check-issue")) do |s|
        s.write "Gemfile", <<-G
          source "https://localgemserver.test"

          gemspec

          source "https://localgemserver.test/extra" do
            gem "dex-dispatch-engine"
          end
        G

        s.add_dependency "awesome_print"
      end

      bundle "install", :artifice => "compact_index_extra", :env => { "BUNDLER_SPEC_GEM_REPO" => gem_repo4.to_s }, :dir => tmp.join("bundle-check-issue")
    end

    it "does not corrupt lockfile when changing version" do
      version_file = tmp.join("bundle-check-issue/bundle-check-issue.gemspec")
      File.write(version_file, File.read(version_file).gsub(/s\.version = .+/, "s.version = '9999'"))

      bundle "check --verbose", :dir => tmp.join("bundle-check-issue")

      expect(File.read(tmp.join("bundle-check-issue/Gemfile.lock"))).to eq <<~L
        PATH
          remote: .
          specs:
            bundle-check-issue (9999)
              awesome_print

        GEM
          remote: https://localgemserver.test/
          specs:
            awesome_print (1.0)

        GEM
          remote: https://localgemserver.test/extra/
          specs:
            dex-dispatch-engine (1.0)

        PLATFORMS
          #{lockfile_platforms}

        DEPENDENCIES
          bundle-check-issue!
          dex-dispatch-engine!

        BUNDLED WITH
           #{Bundler::VERSION}
      L
    end
  end

  describe "BUNDLED WITH" do
    def lock_with(bundler_version = nil)
      lock = <<~L
        GEM
          remote: #{file_uri_for(gem_repo1)}/
          specs:
            rack (1.0.0)

        PLATFORMS
          #{lockfile_platforms}

        DEPENDENCIES
          rack
      L

      if bundler_version
        lock += "\nBUNDLED WITH\n   #{bundler_version}\n"
      end

      lock
    end

    before do
      bundle "config set --local path vendor/bundle"

      install_gemfile <<-G
        source "#{file_uri_for(gem_repo1)}"
        gem "rack"
      G
    end

    context "is not present" do
      it "does not change the lock" do
        lockfile lock_with(nil)
        bundle :check
        expect(lockfile).to eq lock_with(nil)
      end
    end

    context "is newer" do
      it "does not change the lock and does not warn" do
        lockfile lock_with(Bundler::VERSION.succ)
        bundle :check
        expect(err).to be_empty
        expect(lockfile).to eq lock_with(Bundler::VERSION.succ)
      end
    end

    context "is older" do
      it "does not change the lock" do
        system_gems "bundler-1.18.0"
        lockfile lock_with("1.18.0")
        bundle :check
        expect(lockfile).to eq lock_with("1.18.0")
      end
    end
  end
end