summaryrefslogtreecommitdiff
path: root/spec/other/trampoline_spec.rb
blob: 69bfa9eae115a4419585ff1e3e4c01c9db58954d (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
# frozen_string_literal: true
require "spec_helper"

describe "bundler version trampolining" do
  before do
    ENV["BUNDLE_DISABLE_POSTIT"] = nil
    FileUtils.rm_rf(system_gem_path)
    FileUtils.cp_r(base_system_gems, system_gem_path)
  end

  context "version guessing" do
    shared_examples_for "guesses" do |version|
      it "guesses the correct bundler version" do
        bundle! "--version", :in_process_exec => false
        expect(out).to eq("Bundler version #{version}")

        if bundled_app("Gemfile").file?
          bundle! "exec ruby -e 'puts Bundler::VERSION'", :in_process_exec => false
          expect(out).to eq(version)
        end
      end
    end

    context "with a lockfile" do
      before do
        install_gemfile ""
        lockfile lockfile.sub(Bundler::VERSION, "1.12.0")
      end

      include_examples "guesses", "1.12.0"
    end

    context "with BUNDLER_VERSION" do
      before do
        ENV["BUNDLER_VERSION"] = "1.12.0"
      end

      context "with a lockfile" do
        before { install_gemfile "" }
        include_examples "guesses", "1.12.0"
      end

      context "without a lockfile" do
        include_examples "guesses", "1.12.0"
      end
    end

    context "with no hints" do
      include_examples "guesses", Bundler::VERSION
    end

    context "with a gemfile and no lockfile" do
      before do
        gemfile ""
      end

      include_examples "guesses", Bundler::VERSION
    end
  end

  context "installing missing bundler versions", :realworld => true do
    before do
      ENV["BUNDLER_VERSION"] = "1.12.3"
      if Bundler::RubygemsIntegration.provides?("< 2.6.4")
        # necessary since we intall with 2.6.4 but the specs can run against
        # older versions that match againt the "gem" invocation
        %w(bundle bundler).each do |exe|
          system_gem_path.join("bin", exe).open("a") do |f|
            f << %(\ngem "bundler", ">= 0.a"\n)
          end
        end
      end
    end

    it "guesses & installs the correct bundler version" do
      expect(system_gem_path.join("gems", "bundler-1.12.3")).not_to exist
      bundle! "--version", :in_process_exec => false
      expect(out).to eq("Bundler version 1.12.3")
      expect(system_gem_path.join("gems", "bundler-1.12.3")).to exist
    end

    it "fails gracefully when installing the bundler fails" do
      ENV["BUNDLER_VERSION"] = "9999"
      bundle "--version", :expect_err => true, :in_process_exec => false
      expect(err).to start_with(<<-E.strip)
Installing the inferred bundler version (= 9999) failed.
If you'd like to update to the current bundler version (#{Bundler::VERSION}) in this project, run `bundle update --bundler`.
The error was:
      E
    end
  end

  context "bundle update --bundler" do
    before do
      simulate_bundler_version("1.11.1") do
        install_gemfile ""
      end
    end

    it "updates to the specified version" do
      # HACK: since no released bundler version actually supports this feature!
      bundle "update --bundler=1.12.0", :expect_err => true, :in_process_exec => false
      expect(out).to include("Unknown switches '--bundler=1.12.0'")
    end

    it "updates to the specified (running) version" do
      # HACK: since no released bundler version actually supports this feature!
      bundle! "update --bundler=#{Bundler::VERSION}", :in_process_exec => false
      bundle! "--version", :in_process_exec => false
      expect(out).to eq("Bundler version #{Bundler::VERSION}")
    end

    it "updates to the running version" do
      # HACK: since no released bundler version actually supports this feature!
      bundle! "update --bundler", :in_process_exec => false
      bundle! "--version", :in_process_exec => false
      expect(out).to eq("Bundler version #{Bundler::VERSION}")
    end
  end

  context "-rbundler/setup" do
    before do
      simulate_bundler_version("1.12.0") do
        install_gemfile ""
      end
    end

    it "uses the locked version" do
      ruby! <<-R, :in_process_exec => false
        require "bundler/setup"
        puts Bundler::VERSION
      R
      expect(err).to be_empty
      expect(out).to include("1.12.0")
    end
  end

  context "warnings" do
    before do
      simulate_bundler_version("1.12.0") do
        install_gemfile ""
      end
    end

    it "warns user if Bundler is outdated and is < 1.13.0.rc.1" do
      ENV["BUNDLER_VERSION"] = "1.12.0"
      bundle! "install"
      expect(out).to include(<<-WARN.strip)
You're running Bundler #{Bundler::VERSION} but this project uses #{ENV["BUNDLER_VERSION"]}. To update, run `bundle update --bundler`.
      WARN
    end
  end
end