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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
|
#!/usr/bin/env ruby
#--
# Copyright 2006 by Chad Fowler, Rich Kilmer, Jim Weirich and others.
# All rights reserved.
# See LICENSE.txt for permissions.
#++
require 'test/unit'
require File.join(File.expand_path(File.dirname(__FILE__)), 'gemutilities')
require 'rubygems/source_info_cache'
class Gem::SourceIndex
public :gems
end
class TestGemSourceInfoCache < RubyGemTestCase
def setup
@original_sources = Gem.sources
super
util_setup_fake_fetcher
@sic = Gem::SourceInfoCache.new
@sic.instance_variable_set :@fetcher, @fetcher
@si_new = Gem::SourceIndex.new
@sice_new = Gem::SourceInfoCacheEntry.new @si_new, 0
prep_cache_files @sic
@sic.reset_cache_data
end
def teardown
super
Gem.sources.replace @original_sources
Gem::SourceInfoCache.instance_variable_set :@cache, nil
end
def test_self_cache_refreshes
Gem.configuration.update_sources = true #true by default
si = Gem::SourceIndex.new
si.add_spec @a1
@fetcher.data["#{@gem_repo}Marshal.#{@marshal_version}"] = si.dump
Gem.sources.replace %W[#{@gem_repo}]
use_ui @ui do
assert_not_nil Gem::SourceInfoCache.cache
assert_kind_of Gem::SourceInfoCache, Gem::SourceInfoCache.cache
assert_equal Gem::SourceInfoCache.cache.object_id,
Gem::SourceInfoCache.cache.object_id
end
assert_match %r|Bulk updating|, @ui.output
end
def test_self_cache_skips_refresh_based_on_configuration
Gem.configuration.update_sources = false
si = Gem::SourceIndex.new
si.add_spec @a1
@fetcher.data["#{@gem_repo}Marshal.#{@marshal_version}"] = si.dump
Gem.sources.replace %w[#{@gem_repo}]
use_ui @ui do
assert_not_nil Gem::SourceInfoCache.cache
assert_kind_of Gem::SourceInfoCache, Gem::SourceInfoCache.cache
assert_equal Gem::SourceInfoCache.cache.object_id,
Gem::SourceInfoCache.cache.object_id
assert_no_match %r|Bulk updating|, @ui.output
end
end
def test_self_cache_data
si = Gem::SourceIndex.new
si.add_spec @a1
@fetcher.data["#{@gem_repo}Marshal.#{@marshal_version}"] = si.dump
Gem::SourceInfoCache.instance_variable_set :@cache, nil
sice = Gem::SourceInfoCacheEntry.new si, 0
use_ui @ui do
gems = Gem::SourceInfoCache.cache_data[@gem_repo].source_index.gems
gem_names = gems.map { |_, spec| spec.full_name }
assert_equal si.gems.map { |_,spec| spec.full_name }, gem_names
end
end
def test_cache_data
assert_equal [[@gem_repo, @usr_sice]], @sic.cache_data.to_a.sort
end
def test_cache_data_dirty
def @sic.dirty() @dirty; end
assert_equal false, @sic.dirty, 'clean on init'
@sic.cache_data
assert_equal false, @sic.dirty, 'clean on fetch'
@sic.update
@sic.cache_data
assert_equal true, @sic.dirty, 'still dirty'
end
def test_cache_data_irreparable
@fetcher.data["#{@gem_repo}Marshal.#{@marshal_version}"] = @source_index.dump
data = { @gem_repo => { 'totally' => 'borked' } }
cache_files = [
@sic.system_cache_file,
@sic.latest_system_cache_file,
@sic.user_cache_file,
@sic.latest_user_cache_file
]
cache_files.each do |fn|
FileUtils.mkdir_p File.dirname(fn)
open(fn, "wb") { |f| f.write Marshal.dump(data) }
end
@sic.instance_eval { @cache_data = nil }
fetched = use_ui @ui do @sic.cache_data end
fetched_si = fetched["#{@gem_repo}"].source_index
assert_equal @source_index.index_signature, fetched_si.index_signature
end
def test_cache_data_none_readable
FileUtils.chmod 0222, @sic.system_cache_file
FileUtils.chmod 0222, @sic.latest_system_cache_file
FileUtils.chmod 0222, @sic.user_cache_file
FileUtils.chmod 0222, @sic.latest_user_cache_file
return if (File.stat(@sic.system_cache_file).mode & 0222) != 0222
return if (File.stat(@sic.user_cache_file).mode & 0222) != 0222
# HACK for systems that don't support chmod
assert_equal({}, @sic.cache_data)
end
def test_cache_data_none_writable
FileUtils.chmod 0444, @sic.system_cache_file
FileUtils.chmod 0444, @sic.user_cache_file
e = assert_raise RuntimeError do
@sic.cache_data
end
assert_equal 'unable to locate a writable cache file', e.message
end
def test_cache_data_nonexistent
FileUtils.rm @sic.system_cache_file
FileUtils.rm @sic.latest_system_cache_file
FileUtils.rm @sic.user_cache_file
FileUtils.rm @sic.latest_user_cache_file
# TODO test verbose output
assert_equal [], @sic.cache_data.to_a.sort
end
def test_cache_data_repair
data = {
@gem_repo => {
'cache' => Gem::SourceIndex.new,
'size' => 0,
}
}
[@sic.system_cache_file, @sic.user_cache_file].each do |fn|
FileUtils.mkdir_p File.dirname(fn)
open(fn, "wb") { |f| f.write Marshal.dump(data) }
end
@sic.instance_eval { @cache_data = nil }
expected = {
@gem_repo =>
Gem::SourceInfoCacheEntry.new(Gem::SourceIndex.new, 0)
}
assert_equal expected, @sic.cache_data
end
def test_cache_data_user_fallback
FileUtils.chmod 0444, @sic.system_cache_file
assert_equal [[@gem_repo, @usr_sice]], @sic.cache_data.to_a.sort
end
def test_cache_file
assert_equal @gemcache, @sic.cache_file
end
def test_cache_file_user_fallback
FileUtils.chmod 0444, @sic.system_cache_file
assert_equal @usrcache, @sic.cache_file
end
def test_cache_file_none_writable
FileUtils.chmod 0444, @sic.system_cache_file
FileUtils.chmod 0444, @sic.user_cache_file
e = assert_raise RuntimeError do
@sic.cache_file
end
assert_equal 'unable to locate a writable cache file', e.message
end
def test_flush
@sic.cache_data[@gem_repo] = @sice_new
@sic.update
@sic.flush
assert_equal [[@gem_repo, @sice_new]],
read_cache(@sic.system_cache_file).to_a.sort
end
def test_latest_cache_data
util_make_gems
sice = Gem::SourceInfoCacheEntry.new @source_index, 0
@sic.set_cache_data @gem_repo => sice
latest = @sic.latest_cache_data
gems = latest[@gem_repo].source_index.search('a').map { |s| s.full_name }
assert_equal %w[a-2 a_evil-9], gems
end
def test_latest_cache_file
latest_cache_file = File.join File.dirname(@gemcache),
"latest_#{File.basename @gemcache}"
assert_equal latest_cache_file, @sic.latest_cache_file
end
def test_latest_system_cache_file
assert_equal File.join(Gem.dir, "latest_source_cache"),
@sic.latest_system_cache_file
end
def test_latest_user_cache_file
assert_equal @latest_usrcache, @sic.latest_user_cache_file
end
def test_read_system_cache
assert_equal [[@gem_repo, @sys_sice]], @sic.cache_data.to_a.sort
end
def test_read_user_cache
FileUtils.chmod 0444, @sic.user_cache_file
FileUtils.chmod 0444, @sic.latest_user_cache_file
@si = Gem::SourceIndex.new
@si.add_specs @a1, @a2
@sice = Gem::SourceInfoCacheEntry.new @si, 0
@sic.set_cache_data({ @gem_repo => @sice })
@sic.update
@sic.write_cache
@sic.reset_cache_data
user_cache_data = @sic.cache_data.to_a.sort
assert_equal 1, user_cache_data.length
user_cache_data = user_cache_data.first
assert_equal @gem_repo, user_cache_data.first
gems = user_cache_data.last.source_index.map { |_,spec| spec.full_name }
assert_equal [@a2.full_name], gems
end
def test_search
si = Gem::SourceIndex.new
si.add_spec @a1
cache_data = { @gem_repo => Gem::SourceInfoCacheEntry.new(si, nil) }
@sic.instance_variable_set :@cache_data, cache_data
assert_equal [@a1], @sic.search(//)
end
def test_search_all
util_make_gems
sice = Gem::SourceInfoCacheEntry.new @source_index, 0
@sic.set_cache_data @gem_repo => sice
@sic.update
@sic.instance_variable_set :@only_latest, false
@sic.write_cache
@sic.reset_cache_data
gem_names = @sic.search(//, false, true).map { |spec| spec.full_name }
assert_equal %w[a-1 a-2 a_evil-9 c-1.2], gem_names
end
def test_search_dependency
si = Gem::SourceIndex.new
si.add_spec @a1
cache_data = { @gem_repo => Gem::SourceInfoCacheEntry.new(si, nil) }
@sic.instance_variable_set :@cache_data, cache_data
dep = Gem::Dependency.new @a1.name, @a1.version
assert_equal [@a1], @sic.search(dep)
end
def test_search_no_matches
si = Gem::SourceIndex.new
si.add_spec @a1
cache_data = { @gem_repo => Gem::SourceInfoCacheEntry.new(si, nil) }
@sic.instance_variable_set :@cache_data, cache_data
assert_equal [], @sic.search(/nonexistent/)
end
def test_search_no_matches_in_source
si = Gem::SourceIndex.new
si.add_spec @a1
cache_data = { @gem_repo => Gem::SourceInfoCacheEntry.new(si, nil) }
@sic.instance_variable_set :@cache_data, cache_data
Gem.sources.replace %w[more-gems.example.com]
assert_equal [], @sic.search(/nonexistent/)
end
def test_search_with_source
si = Gem::SourceIndex.new
si.add_spec @a1
cache_data = { @gem_repo => Gem::SourceInfoCacheEntry.new(si, nil) }
@sic.instance_variable_set :@cache_data, cache_data
assert_equal [[@a1, @gem_repo]],
@sic.search_with_source(//)
end
def test_system_cache_file
assert_equal File.join(Gem.dir, "source_cache"), @sic.system_cache_file
end
def test_user_cache_file
assert_equal @usrcache, @sic.user_cache_file
end
def test_write_cache
@sic.cache_data[@gem_repo] = @sice_new
@sic.write_cache
assert_equal [[@gem_repo, @sice_new]],
read_cache(@sic.system_cache_file).to_a.sort
assert_equal [[@gem_repo, @usr_sice]],
read_cache(@sic.user_cache_file).to_a.sort
end
def test_write_cache_user
FileUtils.chmod 0444, @sic.system_cache_file
@sic.set_cache_data({@gem_repo => @sice_new})
@sic.update
@sic.write_cache
@sic.instance_variable_set :@only_latest, false
assert File.exist?(@sic.user_cache_file), 'user_cache_file'
assert File.exist?(@sic.latest_user_cache_file),
'latest_user_cache_file exists'
assert_equal [[@gem_repo, @sys_sice]],
read_cache(@sic.system_cache_file).to_a.sort
assert_equal [[@gem_repo, @sice_new]],
read_cache(@sic.user_cache_file).to_a.sort
end
def test_write_cache_user_from_scratch
FileUtils.rm_rf @sic.user_cache_file
FileUtils.rm_rf @sic.latest_user_cache_file
FileUtils.chmod 0444, @sic.system_cache_file
FileUtils.chmod 0444, @sic.latest_system_cache_file
@si = Gem::SourceIndex.new
@si.add_specs @a1, @a2
@sice = Gem::SourceInfoCacheEntry.new @si, 0
@sic.set_cache_data({ @gem_repo => @sice })
@sic.update
@sic.write_cache
assert File.exist?(@sic.user_cache_file), 'system_cache_file'
assert File.exist?(@sic.latest_user_cache_file),
'latest_system_cache_file'
user_cache_data = read_cache(@sic.user_cache_file).to_a.sort
assert_equal 1, user_cache_data.length, 'user_cache_data length'
user_cache_data = user_cache_data.first
assert_equal @gem_repo, user_cache_data.first
gems = user_cache_data.last.source_index.map { |_,spec| spec.full_name }
assert_equal [@a1.full_name, @a2.full_name], gems.sort
user_cache_data = read_cache(@sic.latest_user_cache_file).to_a.sort
assert_equal 1, user_cache_data.length
user_cache_data = user_cache_data.first
assert_equal @gem_repo, user_cache_data.first
gems = user_cache_data.last.source_index.map { |_,spec| spec.full_name }
assert_equal [@a2.full_name], gems
end
def test_write_cache_user_no_directory
FileUtils.rm_rf File.dirname(@sic.user_cache_file)
FileUtils.chmod 0444, @sic.system_cache_file
@sic.set_cache_data({ @gem_repo => @sice_new })
@sic.update
@sic.write_cache
assert_equal [[@gem_repo, @sys_sice]],
read_cache(@sic.system_cache_file).to_a.sort
assert_equal [[@gem_repo, @sys_sice]],
read_cache(@sic.user_cache_file).to_a.sort
assert_equal [[@gem_repo, @sice_new]],
read_cache(@sic.latest_user_cache_file).to_a.sort
end
def test_write_cache_user_only_latest
FileUtils.chmod 0444, @sic.system_cache_file
@sic.set_cache_data({@gem_repo => @sice_new})
@sic.update
@sic.write_cache
assert File.exist?(@sic.user_cache_file), 'user_cache_file'
assert File.exist?(@sic.latest_user_cache_file),
'latest_user_cache_file exists'
assert_equal [[@gem_repo, @sys_sice]],
read_cache(@sic.system_cache_file).to_a.sort
assert_equal [[@gem_repo, @sice_new]],
read_cache(@sic.user_cache_file).to_a.sort
end
end
|