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

require_relative "helper"
require "rubygems/spec_fetcher"

class TestGemSpecFetcher < Gem::TestCase
  def tuple(*args)
    Gem::NameTuple.new(*args)
  end

  def setup
    super

    @uri = URI.parse @gem_repo
    @source = Gem::Source.new(@uri)

    @sf = Gem::SpecFetcher.new
  end

  def test_initialize
    fetcher = Gem::SpecFetcher.new

    assert_same Gem.sources, fetcher.sources
  end

  def test_initialize_source
    alternate = "http://alternate.example"
    fetcher = Gem::SpecFetcher.new alternate

    refute_same Gem.sources, fetcher.sources

    assert_equal alternate, fetcher.sources
  end

  def test_initialize_nonexistent_home_dir
    FileUtils.rmdir Gem.user_home

    assert Gem::SpecFetcher.new
  end

  def test_initialize_unwritable_home_dir
    pend "chmod not supported" if Gem.win_platform?

    FileUtils.chmod 0o000, Gem.user_home

    begin
      assert Gem::SpecFetcher.new
    ensure
      FileUtils.chmod 0o755, Gem.user_home
    end
  end

  def test_spec_for_dependency_all
    spec_fetcher do |fetcher|
      fetcher.spec "a", 1
      fetcher.spec "a", "2.a"
      fetcher.spec "a", 2
      fetcher.spec "a", "3.a"
    end

    dep = Gem::Dependency.new "a", ">= 1"

    specs_and_sources, _ = @sf.spec_for_dependency dep

    spec_names = specs_and_sources.map do |spec, source_uri|
      [spec.full_name, source_uri]
    end

    expected = [["a-1", @source], ["a-2", @source]]

    assert_equal expected, spec_names

    assert_same specs_and_sources.first.last, specs_and_sources.last.last
  end

  def test_spec_for_dependency_latest
    spec_fetcher do |fetcher|
      fetcher.spec "a", 1
      fetcher.spec "a", 2
      fetcher.spec "a", "3.a"
    end

    dep = Gem::Dependency.new "a"
    specs_and_sources, _ = @sf.spec_for_dependency dep

    spec_names = specs_and_sources.map do |spec, source_uri|
      [spec.full_name, source_uri]
    end

    assert_equal [["a-2", Gem::Source.new(@gem_repo)]],
                 spec_names
  end

  def test_spec_for_dependency_prerelease
    spec_fetcher do |fetcher|
      fetcher.spec "a", "1.a"
      fetcher.spec "a", 1
    end

    specs_and_sources, _ = @sf.spec_for_dependency dep("a", "1.a")

    spec_names = specs_and_sources.map do |spec, source_uri|
      [spec.full_name, source_uri]
    end

    assert_equal [["a-1.a", Gem::Source.new(@gem_repo)]], spec_names
  end

  def test_spec_for_dependency_platform
    util_set_arch "i386-linux"

    spec_fetcher(&:legacy_platform)

    dep = Gem::Dependency.new "pl", 1
    specs_and_sources, _ = @sf.spec_for_dependency dep

    spec_names = specs_and_sources.map do |spec, source_uri|
      [spec.full_name, source_uri]
    end

    assert_equal [["pl-1-x86-linux", Gem::Source.new(@gem_repo)]],
                 spec_names
  end

  def test_spec_for_dependency_mismatched_platform
    util_set_arch "hrpa-989"

    spec_fetcher(&:legacy_platform)

    dep = Gem::Dependency.new "pl", 1
    specs_and_sources, errors = @sf.spec_for_dependency dep

    assert_equal 0, specs_and_sources.size
    assert_equal 1, errors.size
    pmm = errors.first

    assert_equal "i386-linux", pmm.platforms.first
    assert_equal "Found pl (1), but was for platform i386-linux", pmm.wordy
  end

  def test_spec_for_dependency_bad_fetch_spec
    src = Gem::Source.new(@gem_repo)
    def src.fetch_spec(name)
      raise Gem::RemoteFetcher::FetchError.new("bad news from the internet", @uri)
    end

    Gem.sources.replace [src]

    spec_fetcher do |fetcher|
      fetcher.spec "a", 1
      fetcher.spec "a", "2.a"
      fetcher.spec "a", 2
      fetcher.spec "a", "3.a"
    end

    dep = Gem::Dependency.new "a", ">= 1"

    specs_and_sources, errors = @sf.spec_for_dependency dep

    assert_equal [], specs_and_sources
    sfp = errors.first

    assert_kind_of Gem::SourceFetchProblem, sfp
    assert_equal src, sfp.source
    assert_equal "bad news from the internet (#{@gem_repo})", sfp.error.message
  end

  def test_suggest_gems_from_name_latest
    spec_fetcher do|fetcher|
      fetcher.spec "example", 1
      fetcher.spec "other-example", 1
      fetcher.spec "examp", 1
    end

    suggestions = @sf.suggest_gems_from_name("examplw", :latest, 1)
    assert_equal ["example"], suggestions

    suggestions = @sf.suggest_gems_from_name("other")
    assert_equal ["other-example"], suggestions

    suggestions = @sf.suggest_gems_from_name("exam")
    assert suggestions.any? { ["examp"] }
    assert suggestions.any? { ["example"] }
    assert suggestions.any? { ["other-example"] }
  end

  def test_suggest_gems_from_name_prerelease
    spec_fetcher do|fetcher|
      fetcher.spec "example", "1.a"
      fetcher.spec "other-example", 1
    end

    suggestions = @sf.suggest_gems_from_name("examplw")
    assert_equal ["example"], suggestions
  end

  def test_available_specs_latest
    spec_fetcher do |fetcher|
      fetcher.spec "a", 1
      fetcher.spec "a", 2
      fetcher.spec "a", "3.a"
      fetcher.legacy_platform
    end

    specs, _ = @sf.available_specs(:latest)

    assert_equal [@source], specs.keys

    expected = Gem::NameTuple.from_list \
      [["a",      v(2),     Gem::Platform::RUBY],
       ["pl",     v(1),     "i386-linux"]]

    assert_equal expected, specs[@source]
  end

  def test_available_specs_released
    spec_fetcher do |fetcher|
      fetcher.spec "a", 1
      fetcher.legacy_platform
    end

    specs, _ = @sf.available_specs(:released)

    assert_equal [@source], specs.keys

    expected = Gem::NameTuple.from_list \
      [["a",      v(1),     Gem::Platform::RUBY],
       ["pl",     v(1),     "i386-linux"]]

    assert_equal expected, specs[@source]
  end

  def test_available_specs_complete
    spec_fetcher do |fetcher|
      fetcher.spec "a", 1
      fetcher.spec "a", "2.a"
      fetcher.spec "b", 2
      fetcher.legacy_platform
    end

    specs, _ = @sf.available_specs(:complete)

    assert_equal [@source], specs.keys

    expected = Gem::NameTuple.from_list \
      [["a",      v(1),     Gem::Platform::RUBY],
       ["a",      v("2.a"), Gem::Platform::RUBY],
       ["b",      v(2),     Gem::Platform::RUBY],
       ["pl",     v(1),     "i386-linux"]]

    assert_equal expected, specs[@source]
  end

  def test_available_specs_complete_handles_no_prerelease
    spec_fetcher do |fetcher|
      fetcher.spec "a", 1
      fetcher.spec "a", "2.a"
      fetcher.spec "b", 2
      fetcher.legacy_platform
    end

    v = Gem.marshal_version
    @fetcher.data.delete "#{@gem_repo}prerelease_specs.#{v}.gz"

    specs, _ = @sf.available_specs(:complete)

    assert_equal [@source], specs.keys

    expected = Gem::NameTuple.from_list \
      [["a",      v(1), Gem::Platform::RUBY],
       ["b",      v(2), Gem::Platform::RUBY],
       ["pl",     v(1), "i386-linux"]]

    assert_equal expected, specs[@source]
  end

  def test_available_specs_cache
    spec_fetcher do |fetcher|
      fetcher.spec "a", 1
    end

    specs, _ = @sf.available_specs(:latest)

    refute specs[@source].empty?

    @fetcher.data["#{@gem_repo}/latest_specs.#{Gem.marshal_version}.gz"] = nil

    cached_specs, _ = @sf.available_specs(:latest)

    assert_equal specs, cached_specs
  end

  def test_available_specs_cache_released
    spec_fetcher do |fetcher|
      fetcher.spec "a", 1
      fetcher.spec "a", "2.a"
      fetcher.spec "b", 2
      fetcher.legacy_platform
    end

    specs, _ = @sf.available_specs(:released)

    refute specs[@source].empty?

    @fetcher.data["#{@gem_repo}/specs.#{Gem.marshal_version}.gz"] = nil

    cached_specs, _ = @sf.available_specs(:released)

    assert_equal specs, cached_specs
  end

  def test_available_specs_prerelease
    spec_fetcher do |fetcher|
      fetcher.spec "a", 1
      fetcher.spec "a", "2.a"
    end

    specs, _ = @sf.available_specs(:prerelease)

    expected = Gem::NameTuple.from_list \
      [["a", v("2.a"), Gem::Platform::RUBY]]

    assert_equal expected, specs[@source]
  end

  def test_available_specs_with_bad_source
    Gem.sources.replace ["http://not-there.nothing"]

    specs, errors = @sf.available_specs(:latest)

    assert_equal({}, specs)
    assert_kind_of Gem::SourceFetchProblem, errors.first
  end
end