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
|
require File.join(File.expand_path(File.dirname(__FILE__)), 'gemutilities')
require 'rubygems/spec_fetcher'
class TestGemSpecFetcher < RubyGemTestCase
def setup
super
@uri = URI.parse @gem_repo
util_setup_fake_fetcher
@source_index.add_spec @pl1
@specs = @source_index.gems.sort.map do |name, spec|
[spec.name, spec.version, spec.original_platform]
end.sort
@fetcher.data["#{@gem_repo}specs.#{Gem.marshal_version}.gz"] =
util_gzip(Marshal.dump(@specs))
@latest_specs = @source_index.latest_specs.sort.map do |spec|
[spec.name, spec.version, spec.original_platform]
end
@fetcher.data["#{@gem_repo}latest_specs.#{Gem.marshal_version}.gz"] =
util_gzip(Marshal.dump(@latest_specs))
@sf = Gem::SpecFetcher.new
end
def test_fetch_all
@fetcher.data["#{@gem_repo}#{Gem::MARSHAL_SPEC_DIR}#{@a1.full_name}.gemspec.rz"] =
util_zip(Marshal.dump(@a1))
@fetcher.data["#{@gem_repo}#{Gem::MARSHAL_SPEC_DIR}#{@a2.full_name}.gemspec.rz"] =
util_zip(Marshal.dump(@a2))
dep = Gem::Dependency.new 'a', 1
specs_and_sources = @sf.fetch dep, true
spec_names = specs_and_sources.map do |spec, source_uri|
[spec.full_name, source_uri]
end
expected = [[@a1.full_name, @gem_repo], [@a2.full_name, @gem_repo]]
assert_equal expected, spec_names
assert_same specs_and_sources.first.last, specs_and_sources.last.last
end
def test_fetch_latest
@fetcher.data["#{@gem_repo}#{Gem::MARSHAL_SPEC_DIR}#{@a1.full_name}.gemspec.rz"] =
util_zip(Marshal.dump(@a1))
@fetcher.data["#{@gem_repo}#{Gem::MARSHAL_SPEC_DIR}#{@a2.full_name}.gemspec.rz"] =
util_zip(Marshal.dump(@a2))
dep = Gem::Dependency.new 'a', 1
specs_and_sources = @sf.fetch dep
spec_names = specs_and_sources.map do |spec, source_uri|
[spec.full_name, source_uri]
end
assert_equal [[@a2.full_name, @gem_repo]], spec_names
end
def test_fetch_legacy_repo
@fetcher.data.delete "#{@gem_repo}specs.#{Gem.marshal_version}.gz"
@fetcher.data["#{@gem_repo}yaml"] = ''
util_setup_source_info_cache @a1, @a2
dep = Gem::Dependency.new 'a', 1
specs = nil
use_ui @ui do
specs = @sf.fetch dep, true
end
expected = <<-EOF
WARNING: RubyGems 1.2+ index not found for:
\thttp://gems.example.com/
RubyGems will revert to legacy indexes degrading performance.
EOF
assert_equal expected, @ui.error
specs = specs.map { |spec, source_uri| [spec.full_name, source_uri] }
expected = [
[@a1.full_name, @gem_repo],
[@a2.full_name, @gem_repo],
]
assert_equal expected, specs
end
def test_fetch_platform
util_set_arch 'i386-linux'
@fetcher.data["#{@gem_repo}#{Gem::MARSHAL_SPEC_DIR}#{@pl1.original_name}.gemspec.rz"] =
util_zip(Marshal.dump(@pl1))
dep = Gem::Dependency.new 'pl', 1
specs_and_sources = @sf.fetch dep
spec_names = specs_and_sources.map do |spec, source_uri|
[spec.full_name, source_uri]
end
assert_equal [[@pl1.full_name, @gem_repo]], spec_names
end
def test_fetch_spec
spec_uri = "#{@gem_repo}#{Gem::MARSHAL_SPEC_DIR}#{@a1.full_name}.gemspec"
@fetcher.data["#{spec_uri}.rz"] = util_zip(Marshal.dump(@a1))
spec = @sf.fetch_spec ['a', Gem::Version.new(1), 'ruby'], @uri
assert_equal @a1.full_name, spec.full_name
cache_dir = @sf.cache_dir URI.parse(spec_uri)
cache_file = File.join cache_dir, "#{@a1.full_name}.gemspec"
assert File.exist?(cache_file)
end
def test_fetch_spec_cached
spec_uri = "#{@gem_repo}/#{Gem::MARSHAL_SPEC_DIR}#{@a1.full_name}.gemspec"
@fetcher.data["#{spec_uri}.rz"] = nil
cache_dir = @sf.cache_dir URI.parse(spec_uri)
FileUtils.mkdir_p cache_dir
cache_file = File.join cache_dir, "#{@a1.full_name}.gemspec"
open cache_file, 'wb' do |io|
Marshal.dump @a1, io
end
spec = @sf.fetch_spec ['a', Gem::Version.new(1), 'ruby'], @uri
assert_equal @a1.full_name, spec.full_name
end
def test_fetch_spec_platform
@fetcher.data["#{@gem_repo}#{Gem::MARSHAL_SPEC_DIR}#{@pl1.original_name}.gemspec.rz"] =
util_zip(Marshal.dump(@pl1))
spec = @sf.fetch_spec ['pl', Gem::Version.new(1), 'i386-linux'], @uri
assert_equal @pl1.full_name, spec.full_name
end
def test_fetch_spec_platform_ruby
@fetcher.data["#{@gem_repo}#{Gem::MARSHAL_SPEC_DIR}#{@a1.full_name}.gemspec.rz"] =
util_zip(Marshal.dump(@a1))
spec = @sf.fetch_spec ['a', Gem::Version.new(1), nil], @uri
assert_equal @a1.full_name, spec.full_name
spec = @sf.fetch_spec ['a', Gem::Version.new(1), ''], @uri
assert_equal @a1.full_name, spec.full_name
end
def test_find_matching_all
dep = Gem::Dependency.new 'a', 1
specs = @sf.find_matching dep, true
expected = [
[['a', Gem::Version.new(1), Gem::Platform::RUBY], @gem_repo],
[['a', Gem::Version.new(2), Gem::Platform::RUBY], @gem_repo],
]
assert_equal expected, specs
end
def test_find_matching_latest
dep = Gem::Dependency.new 'a', 1
specs = @sf.find_matching dep
expected = [
[['a', Gem::Version.new(2), Gem::Platform::RUBY], @gem_repo],
]
assert_equal expected, specs
end
def test_find_matching_platform
util_set_arch 'i386-linux'
dep = Gem::Dependency.new 'pl', 1
specs = @sf.find_matching dep
expected = [
[['pl', Gem::Version.new(1), 'i386-linux'], @gem_repo],
]
assert_equal expected, specs
util_set_arch 'i386-freebsd6'
dep = Gem::Dependency.new 'pl', 1
specs = @sf.find_matching dep
assert_equal [], specs
end
def test_find_all_platforms
util_set_arch 'i386-freebsd6'
dep = Gem::Dependency.new 'pl', 1
specs = @sf.find_matching dep, false, false
expected = [
[['pl', Gem::Version.new(1), 'i386-linux'], @gem_repo],
]
assert_equal expected, specs
end
def test_list
specs = @sf.list
assert_equal [@uri], specs.keys
assert_equal @latest_specs, specs[@uri].sort
end
def test_list_all
specs = @sf.list true
assert_equal [@uri], specs.keys
assert_equal @specs, specs[@uri].sort
end
def test_list_cache
specs = @sf.list
assert !specs[@uri].empty?
@fetcher.data["#{@gem_repo}/latest_specs.#{Gem.marshal_version}.gz"] = nil
cached_specs = @sf.list
assert_equal specs, cached_specs
end
def test_list_cache_all
specs = @sf.list true
assert !specs[@uri].empty?
@fetcher.data["#{@gem_repo}/specs.#{Gem.marshal_version}.gz"] = nil
cached_specs = @sf.list true
assert_equal specs, cached_specs
end
def test_list_latest_all
specs = @sf.list false
assert_equal [@latest_specs], specs.values
specs = @sf.list true
assert_equal [@specs], specs.values, 'specs file not loaded'
end
def test_load_specs
specs = @sf.load_specs @uri, 'specs'
expected = [
['a', Gem::Version.new(1), Gem::Platform::RUBY],
['a', Gem::Version.new(2), Gem::Platform::RUBY],
['a_evil', Gem::Version.new(9), Gem::Platform::RUBY],
['c', Gem::Version.new('1.2'), Gem::Platform::RUBY],
['pl', Gem::Version.new(1), 'i386-linux'],
]
assert_equal expected, specs
cache_dir = File.join Gem.user_home, '.gem', 'specs', 'gems.example.com%80'
assert File.exist?(cache_dir), "#{cache_dir} does not exist"
cache_file = File.join cache_dir, "specs.#{Gem.marshal_version}"
assert File.exist?(cache_file)
end
def test_load_specs_cached
@fetcher.data["#{@gem_repo}latest_specs.#{Gem.marshal_version}.gz"] = nil
@fetcher.data["#{@gem_repo}latest_specs.#{Gem.marshal_version}"] =
' ' * Marshal.dump(@latest_specs).length
cache_dir = File.join Gem.user_home, '.gem', 'specs', 'gems.example.com%80'
FileUtils.mkdir_p cache_dir
cache_file = File.join cache_dir, "latest_specs.#{Gem.marshal_version}"
open cache_file, 'wb' do |io|
Marshal.dump @latest_specs, io
end
latest_specs = @sf.load_specs @uri, 'latest_specs'
assert_equal @latest_specs, latest_specs
end
end
|