summaryrefslogtreecommitdiff
path: root/spec/commands/config_spec.rb
blob: a9adde3711902fdc1c8aa9d6b3489aa9bed53fde (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
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
require "spec_helper"

describe ".bundle/config" do
  before :each do
    gemfile <<-G
      source "file://#{gem_repo1}"
      gem "rack", "1.0.0"
    G
  end

  describe "BUNDLE_APP_CONFIG" do
    it "can be moved with an environment variable" do
      ENV["BUNDLE_APP_CONFIG"] = tmp("foo/bar").to_s
      bundle "config path vendor/bundle"
      bundle "install"

      # See CLI::Install#run.
      with_config(:disable_shared_gems => "1") do
        expect(bundled_app(".bundle")).not_to exist
        expect(tmp("foo/bar/config")).to exist
        should_be_installed "rack 1.0.0"
      end
    end

    it "can provide a relative path with the environment variable" do
      FileUtils.mkdir_p bundled_app("omg")
      Dir.chdir bundled_app("omg")

      ENV["BUNDLE_APP_CONFIG"] = "../foo"
      bundle "config path vendor/bundle"
      bundle "install"

      # See CLI::Install#run.
      with_config(:disable_shared_gems => "1") do
        expect(bundled_app(".bundle")).not_to exist
        expect(bundled_app("../foo/config")).to exist
        should_be_installed "rack 1.0.0"
      end
    end

    it "removes environment.rb from BUNDLE_APP_CONFIG's path" do
      FileUtils.mkdir_p(tmp("foo/bar"))
      ENV["BUNDLE_APP_CONFIG"] = tmp("foo/bar").to_s
      bundle "install"
      FileUtils.touch tmp("foo/bar/environment.rb")
      should_be_installed "rack 1.0.0"
      expect(tmp("foo/bar/environment.rb")).not_to exist
    end
  end

  describe "global" do
    before(:each) { bundle :install }

    it "is the default" do
      bundle "config foo global"
      run "puts Bundler.settings[:foo]"
      expect(out).to eq("global")
    end

    it "can also be set explicitly" do
      bundle "config --global foo global"
      run "puts Bundler.settings[:foo]"
      expect(out).to eq("global")
    end

    it "has lower precedence than local" do
      bundle "config --local  foo local"

      bundle "config --global foo global"
      expect(out).to match(/Your application has set foo to "local"/)

      run "puts Bundler.settings[:foo]"
      expect(out).to eq("local")
    end

    it "has lower precedence than env" do
      begin
        ENV["BUNDLE_FOO"] = "env"

        bundle "config --global foo global"
        expect(out).to match(/You have a bundler environment variable for foo set to "env"/)

        run "puts Bundler.settings[:foo]"
        expect(out).to eq("env")
      ensure
        ENV.delete("BUNDLE_FOO")
      end
    end

    it "can be deleted" do
      bundle "config --global foo global"
      bundle "config --delete foo"

      run "puts Bundler.settings[:foo] == nil"
      expect(out).to eq("true")
    end

    it "warns when overriding" do
      bundle "config --global foo previous"
      bundle "config --global foo global"
      expect(out).to match(/You are replacing the current global value of foo/)

      run "puts Bundler.settings[:foo]"
      expect(out).to eq("global")
    end

    it "does not warn when using the same value twice" do
      bundle "config --global foo value"
      bundle "config --global foo value"
      expect(out).not_to match(/You are replacing the current global value of foo/)

      run "puts Bundler.settings[:foo]"
      expect(out).to eq("value")
    end

    it "expands the path at time of setting" do
      bundle "config --global local.foo .."
      run "puts Bundler.settings['local.foo']"
      expect(out).to eq(File.expand_path(Dir.pwd + "/.."))
    end
  end

  describe "local" do
    before(:each) { bundle :install }

    it "can also be set explicitly" do
      bundle "config --local foo local"
      run "puts Bundler.settings[:foo]"
      expect(out).to eq("local")
    end

    it "has higher precedence than env" do
      begin
        ENV["BUNDLE_FOO"] = "env"
        bundle "config --local foo local"

        run "puts Bundler.settings[:foo]"
        expect(out).to eq("local")
      ensure
        ENV.delete("BUNDLE_FOO")
      end
    end

    it "can be deleted" do
      bundle "config --local foo local"
      bundle "config --delete foo"

      run "puts Bundler.settings[:foo] == nil"
      expect(out).to eq("true")
    end

    it "warns when overriding" do
      bundle "config --local foo previous"
      bundle "config --local foo local"
      expect(out).to match(/You are replacing the current local value of foo/)

      run "puts Bundler.settings[:foo]"
      expect(out).to eq("local")
    end

    it "expands the path at time of setting" do
      bundle "config --local local.foo .."
      run "puts Bundler.settings['local.foo']"
      expect(out).to eq(File.expand_path(Dir.pwd + "/.."))
    end
  end

  describe "env" do
    before(:each) { bundle :install }

    it "can set boolean properties via the environment" do
      ENV["BUNDLE_FROZEN"] = "true"

      run "if Bundler.settings[:frozen]; puts 'true' else puts 'false' end"
      expect(out).to eq("true")
    end

    it "can set negative boolean properties via the environment" do
      run "if Bundler.settings[:frozen]; puts 'true' else puts 'false' end"
      expect(out).to eq("false")

      ENV["BUNDLE_FROZEN"] = "false"

      run "if Bundler.settings[:frozen]; puts 'true' else puts 'false' end"
      expect(out).to eq("false")

      ENV["BUNDLE_FROZEN"] = "0"

      run "if Bundler.settings[:frozen]; puts 'true' else puts 'false' end"
      expect(out).to eq("false")

      ENV["BUNDLE_FROZEN"] = ""

      run "if Bundler.settings[:frozen]; puts 'true' else puts 'false' end"
      expect(out).to eq("false")
    end

    it "can set properties with periods via the environment" do
      ENV["BUNDLE_FOO__BAR"] = "baz"

      run "puts Bundler.settings['foo.bar']"
      expect(out).to eq("baz")
    end
  end

  describe "gem mirrors" do
    before(:each) { bundle :install }

    it "configures mirrors using keys with `mirror.`" do
      bundle "config --local mirror.http://gems.example.org http://gem-mirror.example.org"
      run(<<-E)
Bundler.settings.gem_mirrors.each do |k, v|
  puts "\#{k} => \#{v}"
end
E
      expect(out).to eq("http://gems.example.org/ => http://gem-mirror.example.org/")
    end
  end

  describe "quoting" do
    before(:each) { gemfile "# no gems" }
    let(:long_string) do
      "--with-xml2-include=/usr/pkg/include/libxml2 --with-xml2-lib=/usr/pkg/lib " \
      "--with-xslt-dir=/usr/pkg"
    end

    it "saves quotes" do
      bundle "config foo something\\'"
      run "puts Bundler.settings[:foo]"
      expect(out).to eq("something'")
    end

    it "doesn't return quotes around values", :ruby => "1.9" do
      bundle "config foo '1'"
      run "puts Bundler.settings.send(:global_config_file).read"
      expect(out).to include("'1'")
      run "puts Bundler.settings[:foo]"
      expect(out).to eq("1")
    end

    it "doesn't duplicate quotes around values", :if => (RUBY_VERSION >= "2.1") do
      bundled_app(".bundle").mkpath
      File.open(bundled_app(".bundle/config"), "w") do |f|
        f.write 'BUNDLE_FOO: "$BUILD_DIR"'
      end

      bundle "config bar baz"
      run "puts Bundler.settings.send(:local_config_file).read"

      # Starting in Ruby 2.1, YAML automatically adds double quotes
      # around some values, including $ and newlines.
      expect(out).to include('BUNDLE_FOO: "$BUILD_DIR"')
    end

    it "doesn't duplicate quotes around long wrapped values" do
      bundle "config foo #{long_string}"

      run "puts Bundler.settings[:foo]"
      expect(out).to eq(long_string)

      bundle "config bar baz"

      run "puts Bundler.settings[:foo]"
      expect(out).to eq(long_string)
    end
  end

  describe "very long lines" do
    before(:each) { bundle :install }

    let(:long_string) do
      "--with-xml2-include=/usr/pkg/include/libxml2 --with-xml2-lib=/usr/pkg/lib " \
      "--with-xslt-dir=/usr/pkg"
    end

    let(:long_string_without_special_characters) do
      "here is quite a long string that will wrap to a second line but will not be " \
      "surrounded by quotes"
    end

    it "doesn't wrap values" do
      bundle "config foo #{long_string}"
      run "puts Bundler.settings[:foo]"
      expect(out).to match(long_string)
    end

    it "can read wrapped unquoted values" do
      bundle "config foo #{long_string_without_special_characters}"
      run "puts Bundler.settings[:foo]"
      expect(out).to match(long_string_without_special_characters)
    end
  end

  describe "conflicting path settings" do
    before(:each) { bundle :install }

    describe "setting `path` when `path.system` is already set" do
      it "should print a warning and remove the `path.system` setting" do
        bundle "config path.system true"
        # Note that if we use `default_bundle_path` in place of
        # `bundled_app(".bundle")` below, we add two `Bundler.ruby_scope`s to
        # `Bundler.settings.path`, making it
        # `bundler/tmp/bundled_app/.bundle/ruby/1.9.1/ruby/1.9.1` instead of
        # `bundler/tmp/bundled_app/.bundle/ruby/1.9.1`.
        # FIXME: Maybe we should ensure that installation paths cannot have
        # two `Bundler.ruby_scope`s in them in `Settings#path`.
        bundle "config path #{bundled_app(".bundle")}"

        expect(out).to include("`path.system` is already configured")

        expect(Bundler.settings["path.system"]).to be_nil
      end

      it "should persist `path`" do
        bundle "config path.system true"
        bundle "config path #{bundled_app(".bundle")}"

        expect(Bundler.settings[:path]).to eq(bundled_app(".bundle").to_s)
      end
    end

    describe "setting `path.system` when `path` is already set" do
      it "should print a warning and remove the `path` setting" do
        # Here, the path does not matter, since the option gets overwritten
        # when we set `path.system`. We could choose 'any/path/'.
        bundle "config path #{default_bundle_path}"
        bundle "config path.system true"

        expect(out).to include("`path` is already configured")
        expect(Bundler.settings[:path]).to be_nil
      end

      it "should persist `path.system`" do
        bundle "config path #{default_bundle_path}"
        bundle "config path.system true"

        expect(Bundler.settings["path.system"]).to eq("true")
      end
    end
  end
end

describe "setting gemfile via config" do
  context "when only the non-default Gemfile exists" do
    it "persists the gemfile location to .bundle/config" do
      File.open(bundled_app("NotGemfile"), "w") do |f|
        f.write <<-G
          source "file://#{gem_repo1}"
          gem 'rack'
        G
      end

      bundle "config --local gemfile #{bundled_app("NotGemfile")}"
      expect(File.exist?(".bundle/config")).to eq(true)

      bundle "config"
      expect(out).to include("NotGemfile")
    end
  end
end

describe "setting a global install path" do
  before(:each) do
    gemfile <<-G
      source "file://#{gem_repo1}"
      gem "rack", "1.0.0"
    G
  end

  context "with the --global flag" do
    it "should concat `ruby_scope` to the install path" do
      bundle "config --global path my/global/path"
      bundle "install"

      expect(bundled_app("my/global/path/#{Bundler.ruby_scope}")).to exist
      should_be_installed "rack 1.0.0"
    end
  end

  context "with the BUNDLE_PATH env variable" do
    it "should concat `ruby_scope` to the install path" do
      ENV["BUNDLE_PATH"] = "another/global/dir"
      bundle "install"

      expect(bundled_app("another/global/dir/#{Bundler.ruby_scope}")).to exist
      should_be_installed "rack 1.0.0"
    end
  end
end

describe "setting `with` and `without` options" do
  context "when `with` has already been set" do
    context "and `without` is set to conflict" do
      it "prints a message and unsets the already set setting" do
        bundle "config with foo"
        bundle "config without foo"

        expect(out).to include("already set `with foo` globally, so it will be unset.")
        expect(Bundler.settings.with).to eq([])
        expect(Bundler.settings.without).to eq([:foo]) # There's a conflict, so without should be set
      end
    end
  end

  context "when `without` has already been set" do
    context "and `with` is set to conflict" do
      it "prints a message and unsets the already set setting" do
        bundle "config without foo"
        bundle "config with foo"

        expect(out).to include("already set `without foo` globally, so it will be unset.")
        expect(Bundler.settings.without).to eq([])
        expect(Bundler.settings.with).to eq([:foo])
      end
    end
  end

  context "with scopes" do
    context "when `with` has been set locally" do
      before(:each) { bundle "config --local with foo" }

      context "and `without` is set locally to conflict" do
        it "prints a message and unsets the already set setting" do
          bundle "config --local without foo"

          expect(out).to include("already set `with foo` locally, so it will be unset.")
          expect(Bundler.settings.with).to eq([])
          expect(Bundler.settings.without).to eq([:foo])
        end
      end

      context "and `without` is set globally to conflict" do
        it "does not print a message and does not unset the previously set setting" do
          bundle "config --global without foo"

          expect(out).not_to include("already set `with foo` locally, so it will be unset.")
          expect(Bundler.settings.with).to eq([:foo])
          expect(Bundler.settings.without).to eq([:foo])
        end
      end
    end

    context "when `with` has been set globally" do
      before(:each) { bundle "config --global with foo" }

      context "and `without` is set locally to conflict" do
        it "does not print a message and does not unset the previously set setting" do
          bundle "config --local without foo"

          expect(out).not_to include("already set `with foo` globally, so it will be unset.")
          expect(Bundler.settings.with).to eq([:foo])
          expect(Bundler.settings.without).to eq([:foo])
        end
      end

      context "and `without` is set globally to conflict" do
        it "prints a message and unsets the already set setting" do
          bundle "config without foo --global"

          expect(out).to include("already set `with foo` globally, so it will be unset.")
          expect(Bundler.settings.with).to eq([])
          expect(Bundler.settings.without).to eq([:foo])
        end
      end
    end

    context "when `without` has been set locally" do
      before(:each) { bundle "config --local without foo" }

      context "and `with` is set locally to conflict" do
        it "prints a message and unsets the already set setting" do
          bundle "config --local with foo"

          expect(out).to include("already set `without foo` locally, so it will be unset.")
          expect(Bundler.settings.without).to eq([])
          expect(Bundler.settings.with).to eq([:foo])
        end
      end

      context "and `with` is set globally to conflict" do
        it "does not print a message and does not unset the previously set setting" do
          bundle "config --global with foo"

          expect(out).not_to include("already set `without foo` locally, so it will be unset.")
          expect(Bundler.settings.without).to eq([:foo])
          expect(Bundler.settings.with).to eq([:foo])
        end
      end
    end

    context "when `without` has been set globally" do
      before(:each) { bundle "config --global without foo" }

      context "and `with` is set locally to conflict" do
        it "does not print a message and does not unset the previously set setting" do
          bundle "config --local with foo"

          expect(out).not_to include("already set `without foo` globally, so it will be unset.")
          expect(Bundler.settings.without).to eq([:foo])
          expect(Bundler.settings.with).to eq([:foo])
        end
      end

      context "and `with` is set globally to conflict" do
        it "prints a message and unsets the already set setting" do
          bundle "config --global with foo"

          expect(out).to include("already set `without foo` globally, so it will be unset.")
          expect(Bundler.settings.without).to eq([])
          expect(Bundler.settings.with).to eq([:foo])
        end
      end
    end
  end

  context "when more than one group is given" do
    before(:each) { bundle "config --local with foo bar" }

    context "and there is one conflicting group" do
      it "prints a message and unsets the already set setting" do
        bundle "config --local without bar baz qux"

        expect(out).to include("already set `with bar` locally, so it will be unset.")
        expect(Bundler.settings.with).to eq([:foo])
        expect(Bundler.settings.without).to eq([:bar, :baz, :qux])
      end
    end

    context "and there are two conflicting groups" do
      it "prints a message and unsets the already set setting" do
        bundle "config --local without foo bar baz qux"

        expect(out).to include("already set `with foo bar` locally, so it will be unset.")
        expect(Bundler.settings.with).to eq([])
        expect(Bundler.settings.without).to eq([:foo, :bar, :baz, :qux])
      end
    end
  end

  context "when the scope flag placement is varied" do
    it "should not affect the command" do
      bundle "config with --local foo"
      bundle "config without foo --global"

      expect(Bundler.settings.with).to eq([:foo])
      expect(Bundler.settings.without).to eq([:foo])
      expect(out).not_to include("already set")
    end

    it "should not affect the command" do
      bundle "config with foo --local"
      bundle "config without --local foo"

      expect(Bundler.settings.with).to eq([])
      expect(Bundler.settings.without).to eq([:foo])
      expect(out).to include("already set")
    end

    it "should not affect the command" do
      bundle "config with --local foo"
      bundle "config --global without foo"

      expect(Bundler.settings.with).to eq([:foo])
      expect(Bundler.settings.without).to eq([:foo])
      expect(out).not_to include("already set")
    end

    it "should not affect the command" do
      bundle "config with foo --local"
      bundle "config --local without foo"

      expect(Bundler.settings.with).to eq([])
      expect(Bundler.settings.without).to eq([:foo])
      expect(out).to include("already set")
    end

    context "and there are multiple options" do
      it "should not affect the command" do
        bundle "config with --local foo bar"
        bundle "config without bar --local baz qux"

        expect(out).to include("already set `with bar` locally, so it will be unset.")
        expect(Bundler.settings.with).to eq([:foo])
        expect(Bundler.settings.without).to eq([:bar, :baz, :qux])
      end

      it "should not affect the command" do
        bundle "config with foo --local bar"
        bundle "config  without foo bar baz qux --local"

        expect(out).to include("already set `with foo bar` locally, so it will be unset.")
        expect(Bundler.settings.with).to eq([])
        expect(Bundler.settings.without).to eq([:foo, :bar, :baz, :qux])
      end
    end
  end

  context "when fed arguments in separate commands" do
    it "overwrites Bundler.settings" do
      bundle "config without foo"
      bundle "config without bar"
      bundle "config with foo"

      expect(Bundler.settings.without).to eq([:bar])
      expect(Bundler.settings.with).to eq([:foo])
      expect(out).not_to include("already set")
    end
  end
end