summaryrefslogtreecommitdiff
path: root/test/rubygems/test_gem_bundler_version_finder.rb
blob: b5ef6293abfdb2bc39f69686724229bc0a6a8b81 (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
# frozen_string_literal: true

require_relative "helper"
require "rubygems/bundler_version_finder"
require "tempfile"

class TestGemBundlerVersionFinder < Gem::TestCase
  def setup
    @argv = ARGV.dup
    @dollar_0 = $0
    super

    without_any_upwards_gemfiles
  end

  def teardown
    ARGV.replace @argv
    $0 = @dollar_0

    super
  end

  def bvf
    Gem::BundlerVersionFinder
  end

  def test_bundler_version_defaults_to_nil
    assert_nil bvf.bundler_version
  end

  def test_bundler_version_with_env_var
    ENV["BUNDLER_VERSION"] = "1.1.1.1"
    assert_equal v("1.1.1.1"), bvf.bundler_version
  end

  def test_bundler_version_with_empty_env_var
    ENV["BUNDLER_VERSION"] = ""
    assert_nil bvf.bundler_version
  end

  def test_bundler_version_with_bundle_update_bundler
    ARGV.replace %w[update --bundler]
    assert_nil bvf.bundler_version
    $0 = "/foo/bar/bundle"
    assert_nil bvf.bundler_version
    ARGV.replace %w[update --bundler=1.1.1.1 gem_name]
    assert_equal v("1.1.1.1"), bvf.bundler_version
    ARGV.replace %w[update --bundler 1.1.1.1 gem_name]
    assert_equal v("1.1.1.1"), bvf.bundler_version
    ARGV.replace %w[update --bundler\ 1.1.1.1 gem_name]
    assert_equal v("1.1.1.1"), bvf.bundler_version
    ARGV.replace %w[update --bundler\ 1.1.1.2 --bundler --bundler 1.1.1.1 gem_name]
    assert_equal v("1.1.1.1"), bvf.bundler_version
    $0 = "/foo/bar/bundler"
    assert_equal v("1.1.1.1"), bvf.bundler_version
    $0 = "other"
    assert_nil bvf.bundler_version
  end

  def test_bundler_version_with_bundle_config
    config_content = <<~CONFIG
      BUNDLE_VERSION: "system"
    CONFIG

    Tempfile.create("bundle_config") do |f|
      f.write(config_content)
      f.flush

      bvf.stub(:bundler_global_config_file, f.path) do
        assert_nil bvf.bundler_version
      end
    end
  end

  def test_bundler_version_with_bundle_config_single_quoted
    config_with_single_quoted_version = <<~CONFIG
      BUNDLE_VERSION: 'system'
    CONFIG

    Tempfile.create("bundle_config") do |f|
      f.write(config_with_single_quoted_version)
      f.flush

      bvf.stub(:bundler_global_config_file, f.path) do
        assert_nil bvf.bundler_version
      end
    end
  end

  def test_bundler_version_with_bundle_config_version
    ENV["BUNDLER_VERSION"] = "1.1.1.1"

    config_content = <<~CONFIG
      BUNDLE_VERSION: "1.2.3"
    CONFIG

    Tempfile.create("bundle_config") do |f|
      f.write(config_content)
      f.flush

      bvf.stub(:bundler_global_config_file, f.path) do
        assert_equal v("1.1.1.1"), bvf.bundler_version
      end
    end
  end

  def test_bundler_version_with_bundle_version_env_system
    ENV["BUNDLE_VERSION"] = "system"

    bvf.stub(:lockfile_contents, "\n\nBUNDLED WITH\n   1.1.1.1\n") do
      assert_nil bvf.bundler_version
    end
  end

  def test_bundler_version_with_bundle_version_env_overrides_config
    ENV["BUNDLE_VERSION"] = "2.3.4"

    config_content = <<~CONFIG
      BUNDLE_VERSION: "1.2.3"
    CONFIG

    Tempfile.create("bundle_config") do |f|
      f.write(config_content)
      f.flush

      bvf.stub(:bundler_global_config_file, f.path) do
        assert_equal v("2.3.4"), bvf.bundler_version
      end
    end
  end

  def test_bundler_version_with_empty_bundle_version_env
    ENV["BUNDLE_VERSION"] = ""

    config_content = <<~CONFIG
      BUNDLE_VERSION: "1.2.3"
    CONFIG

    Tempfile.create("bundle_config") do |f|
      f.write(config_content)
      f.flush

      bvf.stub(:bundler_global_config_file, f.path) do
        assert_equal v("1.2.3"), bvf.bundler_version
      end
    end
  end

  def test_bundler_version_with_bundle_version_env_lockfile
    ENV["BUNDLE_VERSION"] = "lockfile"

    bvf.stub(:lockfile_contents, "\n\nBUNDLED WITH\n   1.1.1.1\n") do
      assert_equal v("1.1.1.1"), bvf.bundler_version
    end
  end

  def test_bundler_version_with_bundle_config_version_lockfile
    config_content = <<~CONFIG
      BUNDLE_VERSION: "lockfile"
    CONFIG

    Tempfile.create("bundle_config") do |f|
      f.write(config_content)
      f.flush

      bvf.stub(:bundler_global_config_file, f.path) do
        bvf.stub(:lockfile_contents, "\n\nBUNDLED WITH\n   1.1.1.1\n") do
          assert_equal v("1.1.1.1"), bvf.bundler_version
        end
      end
    end
  end

  def test_bundler_version_with_bundle_config_non_existent_file
    bvf.stub(:bundler_global_config_file, "/non/existent/path") do
      assert_nil bvf.bundler_version
    end
  end

  def test_bundler_version_set_on_local_config
    config_content = <<~CONFIG
      BUNDLE_VERSION: "1.2.3"
    CONFIG

    Tempfile.create("bundle_config") do |f|
      f.write(config_content)
      f.flush

      bvf.stub(:bundler_local_config_file, f.path) do
        assert_equal v("1.2.3"), bvf.bundler_version
      end
    end
  end

  def test_bundler_version_with_bundle_config_without_version
    config_without_version = <<~CONFIG
      BUNDLE_JOBS: "8"
      BUNDLE_GEM__TEST: "minitest"
    CONFIG

    Tempfile.create("bundle_config") do |f|
      f.write(config_without_version)
      f.flush

      bvf.stub(:bundler_global_config_file, f.path) do
        assert_nil bvf.bundler_version
      end
    end
  end

  def test_bundler_version_with_lockfile
    bvf.stub(:lockfile_contents, "") do
      assert_nil bvf.bundler_version
    end
    bvf.stub(:lockfile_contents, "\n\nBUNDLED WITH\n   1.1.1.1\n") do
      assert_equal v("1.1.1.1"), bvf.bundler_version
    end
    bvf.stub(:lockfile_contents, "\n\nBUNDLED WITH\n   fjdkslfjdkslfjsldk\n") do
      assert_nil bvf.bundler_version
    end
  end

  def test_bundler_version
    assert_nil bvf.bundler_version
    bvf.stub(:lockfile_contents, "\n\nBUNDLED WITH\n   1.1.1.1\n") do
      assert_equal "1.1.1.1", bvf.bundler_version.to_s

      $0 = "bundle"
      ARGV.replace %w[update --bundler]
      assert_nil bvf.bundler_version

      ARGV.replace %w[update --bundler=1.1.1.2]
      assert_equal "1.1.1.2",  bvf.bundler_version.to_s

      ENV["BUNDLER_VERSION"] = "1.1.1.3"
      assert_equal "1.1.1.3", bvf.bundler_version.to_s
    end
  end

  def test_deleted_directory
    pend "Cannot perform this test on windows" if Gem.win_platform?

    require "tmpdir"

    orig_dir = Dir.pwd

    begin
      Dir.mktmpdir("some_dir") do |dir|
        Dir.chdir(dir)
      end
    ensure
      Dir.chdir(orig_dir)
    end

    assert_nil bvf.bundler_version
  end

  def test_prioritize
    versions = %w[1 1.0 1.0.1.1 2 2.a 2.0 2.1.1 3 3.a 3.0 3.1.1]
    specs = versions.map {|v| util_spec("bundler", v) }

    assert_equal %w[1 1.0 1.0.1.1 2 2.a 2.0 2.1.1 3 3.a 3.0 3.1.1], util_prioritize_specs(specs)

    bvf.stub(:bundler_version, v("2.1.1.1")) do
      assert_equal %w[1 1.0 1.0.1.1 2 2.a 2.0 2.1.1 3 3.a 3.0 3.1.1], util_prioritize_specs(specs)
    end
    bvf.stub(:bundler_version, v("1.1.1.1")) do
      assert_equal %w[1 1.0 1.0.1.1 2 2.a 2.0 2.1.1 3 3.a 3.0 3.1.1], util_prioritize_specs(specs)
    end
    bvf.stub(:bundler_version, v("1")) do
      assert_equal %w[1 1.0 1.0.1.1 2 2.a 2.0 2.1.1 3 3.a 3.0 3.1.1], util_prioritize_specs(specs)
    end
    bvf.stub(:bundler_version, v("2.a")) do
      assert_equal %w[2.a 1 1.0 1.0.1.1 2 2.0 2.1.1 3 3.a 3.0 3.1.1], util_prioritize_specs(specs)
    end
    bvf.stub(:bundler_version, v("3")) do
      assert_equal %w[3 1 1.0 1.0.1.1 2 2.a 2.0 2.1.1 3.a 3.0 3.1.1], util_prioritize_specs(specs)
    end
  end

  def util_prioritize_specs(specs)
    specs = specs.dup
    bvf.prioritize!(specs)
    specs.map(&:version).map(&:to_s)
  end
end