summaryrefslogtreecommitdiff
path: root/spec/bundler/runtime/self_management_spec.rb
blob: a0b2d83d0e583cc31632752a21359b677840a4c1 (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
# frozen_string_literal: true

RSpec.describe "Self management" do
  describe "auto switching" do
    let(:previous_minor) do
      "2.3.0"
    end

    let(:current_version) do
      "2.4.0"
    end

    before do
      build_repo4 do
        build_bundler previous_minor

        build_bundler current_version

        build_gem "myrack", "1.0.0"
      end

      gemfile <<-G
        source "https://gem.repo4"

        gem "myrack"
      G
    end

    it "installs locked version when using system path and uses it" do
      lockfile_bundled_with(previous_minor)

      bundle "config set --local path.system true"
      bundle "install", preserve_ruby_flags: true
      expect(out).to include("Bundler #{Bundler::VERSION} is running, but your lockfile was generated with #{previous_minor}. Installing Bundler #{previous_minor} and restarting using that version.")

      # It uninstalls the older system bundler
      bundle "clean --force", artifice: nil
      expect(out).to eq("Removing bundler (#{Bundler::VERSION})")

      # App now uses locked version
      bundle "-v", artifice: nil
      expect(out).to end_with(previous_minor[0] == "2" ? "Bundler version #{previous_minor}" : previous_minor)

      # ruby-core test setup has always "lib" in $LOAD_PATH so `require "bundler/setup"` always activate the local version rather than using RubyGems gem activation stuff
      unless ruby_core?
        # App now uses locked version, even when not using the CLI directly
        file = bundled_app("bin/bundle_version.rb")
        create_file file, <<-RUBY
          #!#{Gem.ruby}
          require 'bundler/setup'
          puts Bundler::VERSION
        RUBY
        file.chmod(0o777)
        cmd = Gem.win_platform? ? "#{Gem.ruby} bin/bundle_version.rb" : "bin/bundle_version.rb"
        sys_exec cmd, artifice: nil
        expect(out).to eq(previous_minor)
      end

      # Subsequent installs use the locked version without reinstalling
      bundle "install --verbose", artifice: nil
      expect(out).to include("Using bundler #{previous_minor}")
      expect(out).not_to include("Bundler #{Bundler::VERSION} is running, but your lockfile was generated with #{previous_minor}. Installing Bundler #{previous_minor} and restarting using that version.")
    end

    it "installs locked version when using local path and uses it" do
      lockfile_bundled_with(previous_minor)

      bundle "config set --local path vendor/bundle"
      bundle "install", preserve_ruby_flags: true
      expect(out).to include("Bundler #{Bundler::VERSION} is running, but your lockfile was generated with #{previous_minor}. Installing Bundler #{previous_minor} and restarting using that version.")
      expect(vendored_gems("gems/bundler-#{previous_minor}")).to exist

      # It does not uninstall the locked bundler
      bundle "clean"
      expect(out).to be_empty

      # App now uses locked version
      bundle "-v"
      expect(out).to end_with(previous_minor[0] == "2" ? "Bundler version #{previous_minor}" : previous_minor)

      # ruby-core test setup has always "lib" in $LOAD_PATH so `require "bundler/setup"` always activate the local version rather than using RubyGems gem activation stuff
      unless ruby_core?
        # App now uses locked version, even when not using the CLI directly
        file = bundled_app("bin/bundle_version.rb")
        create_file file, <<-RUBY
          #!#{Gem.ruby}
          require 'bundler/setup'
          puts Bundler::VERSION
        RUBY
        file.chmod(0o777)
        cmd = Gem.win_platform? ? "#{Gem.ruby} bin/bundle_version.rb" : "bin/bundle_version.rb"
        sys_exec cmd, artifice: nil
        expect(out).to eq(previous_minor)
      end

      # Subsequent installs use the locked version without reinstalling
      bundle "install --verbose"
      expect(out).to include("Using bundler #{previous_minor}")
      expect(out).not_to include("Bundler #{Bundler::VERSION} is running, but your lockfile was generated with #{previous_minor}. Installing Bundler #{previous_minor} and restarting using that version.")
    end

    it "installs locked version when using deployment option and uses it" do
      lockfile_bundled_with(previous_minor)

      bundle "config set --local deployment true"
      bundle "install", preserve_ruby_flags: true
      expect(out).to include("Bundler #{Bundler::VERSION} is running, but your lockfile was generated with #{previous_minor}. Installing Bundler #{previous_minor} and restarting using that version.")
      expect(vendored_gems("gems/bundler-#{previous_minor}")).to exist

      # It does not uninstall the locked bundler
      bundle "clean"
      expect(out).to be_empty

      # App now uses locked version
      bundle "-v"
      expect(out).to end_with(previous_minor[0] == "2" ? "Bundler version #{previous_minor}" : previous_minor)

      # Subsequent installs use the locked version without reinstalling
      bundle "install --verbose"
      expect(out).to include("Using bundler #{previous_minor}")
      expect(out).not_to include("Bundler #{Bundler::VERSION} is running, but your lockfile was generated with #{previous_minor}. Installing Bundler #{previous_minor} and restarting using that version.")
    end

    it "does not try to install a development version" do
      lockfile_bundled_with("#{previous_minor}.dev")

      bundle "install --verbose"
      expect(out).not_to match(/restarting using that version/)

      bundle "-v"
      expect(out).to eq(Bundler::VERSION[0] == "2" ? "Bundler version #{Bundler::VERSION}" : Bundler::VERSION)
    end

    it "does not try to install when --local is passed" do
      lockfile_bundled_with(previous_minor)
      system_gems "myrack-1.0.0", path: default_bundle_path

      bundle "install --local"
      expect(out).not_to match(/Installing Bundler/)

      bundle "-v"
      expect(out).to eq(Bundler::VERSION[0] == "2" ? "Bundler version #{Bundler::VERSION}" : Bundler::VERSION)
    end

    it "shows a discrete message if locked bundler does not exist" do
      missing_minor = "#{Bundler::VERSION[0]}.999.999"

      lockfile_bundled_with(missing_minor)

      bundle "install"
      expect(err).to eq("Your lockfile is locked to a version of bundler (#{missing_minor}) that doesn't exist at https://rubygems.org/. Going on using #{Bundler::VERSION}")

      bundle "-v"
      expect(out).to eq(Bundler::VERSION[0] == "2" ? "Bundler version #{Bundler::VERSION}" : Bundler::VERSION)
    end

    it "installs BUNDLE_VERSION version when using bundle config version x.y.z" do
      lockfile_bundled_with(current_version)

      bundle "config set --local version #{previous_minor}"
      bundle "install", preserve_ruby_flags: true
      expect(out).to include("Bundler #{Bundler::VERSION} is running, but your configuration was #{previous_minor}. Installing Bundler #{previous_minor} and restarting using that version.")

      bundle "-v"
      expect(out).to eq(previous_minor[0] == "2" ? "Bundler version #{previous_minor}" : previous_minor)
    end

    it "does not try to install when using bundle config version global" do
      lockfile_bundled_with(previous_minor)

      bundle "config set version system"
      bundle "install"
      expect(out).not_to match(/restarting using that version/)

      bundle "-v"
      expect(out).to eq(Bundler::VERSION[0] == "2" ? "Bundler version #{Bundler::VERSION}" : Bundler::VERSION)
    end

    it "does not try to install when using bundle config version <dev-version>" do
      lockfile_bundled_with(previous_minor)

      bundle "config set version #{previous_minor}.dev"
      bundle "install"
      expect(out).not_to match(/restarting using that version/)

      bundle "-v"
      expect(out).to eq(Bundler::VERSION[0] == "2" ? "Bundler version #{Bundler::VERSION}" : Bundler::VERSION)
    end

    it "ignores malformed lockfile version" do
      lockfile_bundled_with("2.3.")

      bundle "install --verbose"
      expect(out).to include("Using bundler #{Bundler::VERSION}")
    end

    it "uses the right original script when re-execing, if `$0` has been changed to something that's not a script", :ruby_repo do
      bundle "config path vendor/bundle"

      system_gems "bundler-9.9.9", path: vendored_gems

      test = bundled_app("test.rb")

      create_file test, <<~RUBY
        $0 = "this is the program name"
        require "bundler/setup"
      RUBY

      lockfile_bundled_with("9.9.9")

      sys_exec "#{Gem.ruby} #{test}", artifice: nil, raise_on_error: false
      expect(err).to include("Could not find myrack-1.0.0")
      expect(err).not_to include("this is the program name")
    end

    it "uses modified $0 when re-execing, if `$0` has been changed to a script", :ruby_repo do
      bundle "config path vendor/bundle"

      system_gems "bundler-9.9.9", path: vendored_gems

      runner = bundled_app("runner.rb")

      create_file runner, <<~RUBY
        $0 = ARGV.shift
        load $0
      RUBY

      script = bundled_app("script.rb")
      create_file script, <<~RUBY
        require "bundler/setup"
      RUBY

      lockfile_bundled_with("9.9.9")

      sys_exec "#{Gem.ruby} #{runner} #{script}", artifice: nil, raise_on_error: false
      expect(err).to include("Could not find myrack-1.0.0")
    end

    private

    def lockfile_bundled_with(version)
      lockfile <<~L
        GEM
          remote: https://gem.repo4/
          specs:
            myrack (1.0.0)

        PLATFORMS
          #{lockfile_platforms}

        DEPENDENCIES
          myrack

        BUNDLED WITH
           #{version}
      L
    end
  end
end