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

require_relative "helper"

class TestGemResolverInstallerSet < Gem::TestCase
  def test_add_always_install
    spec_fetcher do |fetcher|
      fetcher.download "a", 1
      fetcher.download "a", 2
    end

    util_gem "a", 1

    set = Gem::Resolver::InstallerSet.new :both

    set.add_always_install dep("a")

    assert_equal %w[a-2], set.always_install.map(&:full_name)

    e = assert_raise Gem::UnsatisfiableDependencyError do
      set.add_always_install dep("b")
    end

    assert_equal dep("b"), e.dependency.dependency
  end

  def test_add_always_install_errors
    @stub_fetcher = Gem::FakeFetcher.new
    Gem::RemoteFetcher.fetcher = @stub_fetcher

    set = Gem::Resolver::InstallerSet.new :both

    e = assert_raise Gem::UnsatisfiableDependencyError do
      set.add_always_install dep "a"
    end

    refute_empty e.errors
  end

  def test_add_always_install_platform
    spec_fetcher do |fetcher|
      fetcher.download "a", 1
      fetcher.download "a", 2 do |s|
        s.platform = Gem::Platform.new "x86-freebsd-9"
      end
    end

    set = Gem::Resolver::InstallerSet.new :both

    set.add_always_install dep("a")

    assert_equal %w[a-1], set.always_install.map(&:full_name)
  end

  def test_add_always_install_platform_if_gem_platforms_modified_by_platform_flag
    freebsd = Gem::Platform.new "x86-freebsd-9"

    spec_fetcher do |fetcher|
      fetcher.download "a", 1
      fetcher.download "a", 1 do |s|
        s.platform = freebsd
      end
    end

    # equivalent to --platform=x86-freebsd-9
    Gem.platforms << freebsd
    set = Gem::Resolver::InstallerSet.new :both

    set.add_always_install dep("a")

    assert_equal %w[a-1-x86-freebsd-9], set.always_install.map(&:full_name)
  end

  def test_add_always_install_index_spec_platform
    _, a_1_local_gem = util_gem "a", 1 do |s|
      s.platform = Gem::Platform.local
    end

    FileUtils.mv a_1_local_gem, @tempdir

    set = Gem::Resolver::InstallerSet.new :both
    set.add_always_install dep("a")

    assert_equal [Gem::Platform.local], set.always_install.map(&:platform)
  end

  def test_add_always_install_prerelease
    spec_fetcher do |fetcher|
      fetcher.gem "a", 1
      fetcher.gem "a", "3.a"
    end

    set = Gem::Resolver::InstallerSet.new :both

    set.add_always_install dep("a")

    assert_equal %w[a-1], set.always_install.map(&:full_name)
  end

  def test_add_always_install_prerelease_github_problem
    spec_fetcher do |fetcher|
      fetcher.gem "a", 1
    end

    # Github has an issue in which it will generate a misleading prerelease output in its RubyGems server API and
    # returns a 0 version for the gem while it doesn't exist.
    @fetcher.data["#{@gem_repo}prerelease_specs.#{Gem.marshal_version}.gz"] = util_gzip(Marshal.dump([
      Gem::NameTuple.new("a", Gem::Version.new(0), "ruby"),
    ]))

    set = Gem::Resolver::InstallerSet.new :both

    set.add_always_install dep("a")

    assert_equal %w[a-1], set.always_install.map(&:full_name)
  end

  def test_add_always_install_prerelease_only
    spec_fetcher do |fetcher|
      fetcher.gem "a", "3.a"
    end

    set = Gem::Resolver::InstallerSet.new :both

    assert_raise Gem::UnsatisfiableDependencyError do
      set.add_always_install dep("a")
    end
  end

  def test_add_local
    a_1, a_1_gem = util_gem "a", 1

    a_1_source = Gem::Source::SpecificFile.new a_1_gem

    set = Gem::Resolver::InstallerSet.new :both

    set.add_local File.basename(a_1_gem), a_1, a_1_source

    assert set.local? File.basename(a_1_gem)

    FileUtils.rm a_1_gem
    util_clear_gems

    req = Gem::Resolver::DependencyRequest.new dep("a"), nil

    assert_equal %w[a-1], set.find_all(req).map(&:full_name)
  end

  def test_consider_local_eh
    set = Gem::Resolver::InstallerSet.new :remote

    refute set.consider_local?

    set = Gem::Resolver::InstallerSet.new :both

    assert set.consider_local?

    set = Gem::Resolver::InstallerSet.new :local

    assert set.consider_local?
  end

  def test_consider_remote_eh
    set = Gem::Resolver::InstallerSet.new :remote

    assert set.consider_remote?

    set = Gem::Resolver::InstallerSet.new :both

    assert set.consider_remote?

    set = Gem::Resolver::InstallerSet.new :local

    refute set.consider_remote?
  end

  def test_errors
    set = Gem::Resolver::InstallerSet.new :both

    set.instance_variable_get(:@errors) << :a

    req = Gem::Resolver::DependencyRequest.new dep("a"), nil

    set.find_all req

    assert_equal [:a, set.remote_set.errors.first], set.errors
  end

  def test_find_all_always_install
    spec_fetcher do |fetcher|
      fetcher.download "a", 2
    end

    util_gem "a", 1

    set = Gem::Resolver::InstallerSet.new :both

    set.add_always_install dep "a"

    req = Gem::Resolver::DependencyRequest.new dep("a"), nil

    assert_equal %w[a-2], set.find_all(req).map(&:full_name)
  end

  def test_find_all_prerelease
    spec_fetcher do |fetcher|
      fetcher.download "a", "1"
      fetcher.download "a", "1.a"
    end

    set = Gem::Resolver::InstallerSet.new :both

    req = Gem::Resolver::DependencyRequest.new dep("a"), nil

    assert_equal %w[a-1], set.find_all(req).map(&:full_name)

    req = Gem::Resolver::DependencyRequest.new dep("a", ">= 0.a"), nil

    assert_equal %w[a-1 a-1.a],
                 set.find_all(req).map(&:full_name).sort
  end

  def test_find_all_prerelease_dependencies_with_add_local
    activesupport_7_1_0_alpha = util_spec "activesupport", "7.1.0.alpha"

    install_gem activesupport_7_1_0_alpha

    set = Gem::Resolver::InstallerSet.new :both

    req = Gem::Resolver::DependencyRequest.new dep("activesupport", ">= 4.2.0"), nil

    assert_equal %w[activesupport-7.1.0.alpha], set.find_all(req).map(&:full_name)
  end

  def test_load_spec
    specs = spec_fetcher do |fetcher|
      fetcher.spec "a", 2
      fetcher.spec "a", 2 do |s|
        s.platform = Gem::Platform.local
      end
    end

    source = Gem::Source.new @gem_repo
    version = v 2

    set = Gem::Resolver::InstallerSet.new :remote

    spec = set.load_spec "a", version, Gem::Platform.local, source

    assert_equal specs["a-2-#{Gem::Platform.local}"].full_name, spec.full_name
  end

  def test_prefetch
    set = Gem::Resolver::InstallerSet.new :remote
    def (set.remote_set).prefetch(_)
      raise "called"
    end
    assert_raise(RuntimeError) { set.prefetch(nil) }

    set = Gem::Resolver::InstallerSet.new :local
    def (set.remote_set).prefetch(_)
      raise "called"
    end
    assert_nil set.prefetch(nil)
  end

  def test_prerelease_equals
    set = Gem::Resolver::InstallerSet.new :remote

    refute set.prerelease
    refute set.remote_set.prerelease

    set.prerelease = true

    assert set.prerelease
    assert set.remote_set.prerelease
  end

  def test_remote_equals_both
    set = Gem::Resolver::InstallerSet.new :both
    set.remote = true

    assert set.consider_local?
    assert set.consider_remote?

    set = Gem::Resolver::InstallerSet.new :both
    set.remote = false

    assert set.consider_local?
    refute set.consider_remote?
  end

  def test_remote_equals_local
    set = Gem::Resolver::InstallerSet.new :local
    set.remote = true

    assert set.consider_local?
    assert set.consider_remote?

    set = Gem::Resolver::InstallerSet.new :local
    set.remote = false

    assert set.consider_local?
    refute set.consider_remote?
  end

  def test_remote_equals_remote
    set = Gem::Resolver::InstallerSet.new :remote
    set.remote = true

    refute set.consider_local?
    assert set.consider_remote?

    set = Gem::Resolver::InstallerSet.new :remote
    set.remote = false

    refute set.consider_local?
    refute set.consider_remote?
  end
end