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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
|
require 'rdoc/test_case'
class TestRDocServlet < RDoc::TestCase
def setup
super
@orig_gem_path = Gem.path
@tempdir = File.join Dir.tmpdir, "test_rdoc_servlet_#{$$}"
Gem.use_paths @tempdir
Gem.ensure_gem_subdirectories @tempdir
@spec = Gem::Specification.new 'spec', '1.0'
@spec.loaded_from = @spec.spec_file
Gem::Specification.reset
Gem::Specification.all = [@spec]
@server = {}
def @server.mount(*) end
@stores = {}
@cache = Hash.new { |hash, store| hash[store] = {} }
@extra_dirs = [File.join(@tempdir, 'extra1'), File.join(@tempdir, 'extra2')]
@s = RDoc::Servlet.new @server, @stores, @cache, nil, @extra_dirs
@req = WEBrick::HTTPRequest.new :Logger => nil
@res = WEBrick::HTTPResponse.new :HTTPVersion => '1.0'
def @req.path= path
instance_variable_set :@path, path
end
@req.instance_variable_set :@header, Hash.new { |h, k| h[k] = [] }
@base = File.join @tempdir, 'base'
@system_dir = File.join @tempdir, 'base', 'system'
@home_dir = File.join @tempdir, 'home'
@gem_doc_dir = File.join @tempdir, 'doc'
@orig_base = RDoc::RI::Paths::BASE
RDoc::RI::Paths::BASE.replace @base
@orig_ri_path_homedir = RDoc::RI::Paths::HOMEDIR
RDoc::RI::Paths::HOMEDIR.replace @home_dir
RDoc::RI::Paths.instance_variable_set \
:@gemdirs, %w[/nonexistent/gems/example-1.0/ri]
end
def teardown
super
Gem.use_paths(*@orig_gem_path)
Gem::Specification.reset
FileUtils.rm_rf @tempdir
RDoc::RI::Paths::BASE.replace @orig_base
RDoc::RI::Paths::HOMEDIR.replace @orig_ri_path_homedir
RDoc::RI::Paths.instance_variable_set :@gemdirs, nil
end
def test_asset
temp_dir do
now = Time.now
open 'rdoc.css', 'w' do |io| io.write 'h1 { color: red }' end
File.utime now, now, 'rdoc.css'
@s.asset_dirs[:darkfish] = '.'
@req.path = 'rdoc.css'
@s.asset :darkfish, @req, @res
assert_equal 'h1 { color: red }', @res.body
assert_equal 'text/css', @res.content_type
assert_equal now.httpdate, @res['last-modified']
end
end
def test_do_GET
touch_system_cache_path
@req.path = '/ruby/Missing.html'
@s.do_GET @req, @res
assert_equal 404, @res.status
end
def test_do_GET_asset_darkfish
temp_dir do
FileUtils.touch 'rdoc.css'
@s.asset_dirs[:darkfish] = '.'
@req.path = '/rdoc.css'
@s.do_GET @req, @res
assert_equal 'text/css', @res.content_type
end
end
def test_do_GET_asset_json_index
temp_dir do
FileUtils.mkdir 'js'
FileUtils.touch 'js/navigation.js'
@s.asset_dirs[:json_index] = '.'
@req.path = '/js/navigation.js'
@s.do_GET @req, @res
assert_equal 'application/javascript', @res.content_type
end
end
def test_do_GET_error
touch_system_cache_path
def @req.path() raise 'no' end
@s.do_GET @req, @res
assert_equal 500, @res.status
end
def test_do_GET_mount_path
@s = RDoc::Servlet.new @server, @stores, @cache, '/mount/path'
temp_dir do
FileUtils.touch 'rdoc.css'
@s.asset_dirs[:darkfish] = '.'
@req.path = '/mount/path/rdoc.css'
@s.do_GET @req, @res
assert_equal 'text/css', @res.content_type
end
end
def do_GET_not_found
touch_system_cache_path
@req.path = "/#{@spec.full_name}"
@s.do_GET @req, @res
assert_equal 404, @res.status
end
def test_do_GET_not_modified
touch_system_cache_path
@req.header['if-modified-since'] = [(Time.now + 10).httpdate]
@req.path = '/ruby/Missing.html'
assert_raises WEBrick::HTTPStatus::NotModified do
@s.do_GET @req, @res
end
end
def test_do_GET_root
touch_system_cache_path
@req.path = '/'
@s.do_GET @req, @res
assert_equal 'text/html', @res.content_type
assert_match %r%<title>Local RDoc Documentation</title>%, @res.body
end
def test_do_GET_root_search
touch_system_cache_path
@req.path = '/js/search_index.js'
@s.do_GET @req, @res
assert_equal 'application/javascript', @res.content_type
end
def test_documentation_page_class
store = RDoc::Store.new
generator = @s.generator_for store
file = store.add_file 'file.rb'
klass = file.add_class RDoc::NormalClass, 'Klass'
klass.add_class RDoc::NormalClass, 'Sub'
@s.documentation_page store, generator, 'Klass::Sub.html', @req, @res
assert_match %r%<title>class Klass::Sub - </title>%, @res.body
assert_match %r%<body id="top" role="document" class="class">%, @res.body
end
def test_documentation_page_not_found
store = RDoc::Store.new
generator = @s.generator_for store
@req.path = '/ruby/Missing.html'
@s.documentation_page store, generator, 'Missing.html', @req, @res
assert_equal 404, @res.status
end
def test_documentation_page_page
store = RDoc::Store.new
generator = @s.generator_for store
readme = store.add_file 'README.rdoc'
readme.parser = RDoc::Parser::Simple
@s.documentation_page store, generator, 'README_rdoc.html', @req, @res
assert_match %r%<title>README - </title>%, @res.body
assert_match %r%<body [^>]+ class="file">%, @res.body
end
def test_documentation_source
store, path = @s.documentation_source '/ruby/Object.html'
assert_equal @system_dir, store.path
assert_equal 'Object.html', path
end
def test_documentation_source_cached
cached_store = RDoc::Store.new
@stores['ruby'] = cached_store
store, path = @s.documentation_source '/ruby/Object.html'
assert_same cached_store, store
assert_equal 'Object.html', path
end
def test_error
e = RuntimeError.new 'foo'
e.set_backtrace caller
@s.error e, @req, @res
assert_equal 'text/html', @res.content_type
assert_equal 500, @res.status
assert_match %r%<title>Error%, @res.body
end
def test_generator_for
store = RDoc::Store.new
store.main = 'MAIN_PAGE.rdoc'
store.title = 'Title'
generator = @s.generator_for store
refute generator.file_output
assert_equal '..', generator.asset_rel_path
assert_equal 'MAIN_PAGE.rdoc', @s.options.main_page
assert_equal 'Title', @s.options.title
assert_kind_of RDoc::RDoc, store.rdoc
assert_same generator, store.rdoc.generator
end
def test_if_modified_since
skip 'File.utime on directory not supported' if Gem.win_platform?
temp_dir do
now = Time.now
File.utime now, now, '.'
@s.if_modified_since @req, @res, '.'
assert_equal now.to_i, Time.parse(@res['last-modified']).to_i
end
end
def test_if_modified_since_not_modified
skip 'File.utime on directory not supported' if Gem.win_platform?
temp_dir do
now = Time.now
File.utime now, now, '.'
@req.header['if-modified-since'] = [(now + 10).httpdate]
assert_raises WEBrick::HTTPStatus::NotModified do
@s.if_modified_since @req, @res, '.'
end
assert_equal now.to_i, Time.parse(@res['last-modified']).to_i
end
end
def test_installed_docs
touch_system_cache_path
touch_extra_cache_path
expected = [
['My Extra Documentation', 'extra-1/', true, :extra,
@extra_dirs[0]],
['Extra Documentation', 'extra-2/', false, :extra,
@extra_dirs[1]],
['Ruby Documentation', 'ruby/', true, :system,
@system_dir],
['Site Documentation', 'site/', false, :site,
File.join(@base, 'site')],
['Home Documentation', 'home/', false, :home,
RDoc::RI::Paths::HOMEDIR],
['spec-1.0', 'spec-1.0/', false, :gem,
File.join(@spec.doc_dir, 'ri')],
]
assert_equal expected, @s.installed_docs
end
def test_not_found
generator = @s.generator_for RDoc::Store.new
@req.path = '/ruby/Missing.html'
@s.not_found generator, @req, @res
assert_equal 404, @res.status
assert_match %r%<title>Not Found</title>%, @res.body
assert_match %r%<kbd>/ruby/Missing\.html</kbd>%, @res.body
end
def test_not_found_message
generator = @s.generator_for RDoc::Store.new
@req.path = '/ruby/Missing.html'
@s.not_found generator, @req, @res, 'woo, this is a message'
assert_equal 404, @res.status
assert_match %r%<title>Not Found</title>%, @res.body
assert_match %r%woo, this is a message%, @res.body
end
def test_ri_paths
paths = @s.ri_paths
expected = [
[@extra_dirs[0], :extra],
[@extra_dirs[1], :extra],
[@system_dir, :system],
[File.join(@base, 'site'), :site],
[RDoc::RI::Paths::HOMEDIR, :home],
[File.join(@spec.doc_dir, 'ri'), :gem],
]
assert_equal expected, paths.to_a
end
def test_root
@s.root @req, @res
assert_equal 'text/html', @res.content_type
assert_match %r%<title>Local RDoc Documentation</title>%, @res.body
end
def test_root_search
touch_system_cache_path
touch_extra_cache_path
@s.root_search @req, @res
assert_equal 'application/javascript', @res.content_type
@res.body =~ /\{.*\}/
index = JSON.parse $&
expected = {
'index' => {
'searchIndex' => %w[
My\ Extra\ Documentation
Ruby\ Documentation
],
'longSearchIndex' => %w[
My\ Extra\ Documentation
Ruby\ Documentation
],
'info' => [
['My Extra Documentation', '', @extra_dirs[0], '',
'My Extra Documentation'],
['Ruby Documentation', '', 'ruby', '',
'Documentation for the Ruby standard library'],
],
}
}
assert_equal expected, index
end
def test_show_documentation_index
touch_system_cache_path
@req.path = '/ruby'
@s.show_documentation @req, @res
assert_equal 'text/html', @res.content_type
assert_match %r%<title>Standard Library Documentation%, @res.body
end
def test_show_documentation_table_of_contents
touch_system_cache_path
@req.path = '/ruby/table_of_contents.html'
@s.show_documentation @req, @res
assert_equal 'text/html', @res.content_type
assert_match %r%<title>Table of Contents - Standard Library Documentation%,
@res.body
end
def test_show_documentation_page
touch_system_cache_path
@req.path = '/ruby/Missing.html'
@s.show_documentation @req, @res
assert_equal 404, @res.status
end
def test_show_documentation_search_index
touch_system_cache_path
@req.path = '/ruby/js/search_index.js'
@s.show_documentation @req, @res
assert_equal 'application/javascript', @res.content_type
assert_match %r%\Avar search_data =%, @res.body
end
def test_store_for_gem
ri_dir = File.join @gem_doc_dir, 'spec-1.0', 'ri'
FileUtils.mkdir_p ri_dir
FileUtils.touch File.join ri_dir, 'cache.ri'
store = @s.store_for 'spec-1.0'
assert_equal File.join(@gem_doc_dir, 'spec-1.0', 'ri'), store.path
assert_equal :gem, store.type
end
def test_store_for_home
store = @s.store_for 'home'
assert_equal @home_dir, store.path
assert_equal :home, store.type
end
def test_store_for_missing_documentation
FileUtils.mkdir_p(File.join @gem_doc_dir, 'spec-1.0', 'ri')
e = assert_raises WEBrick::HTTPStatus::NotFound do
@s.store_for 'spec-1.0'
end
assert_equal 'Could not find documentation for "spec-1.0". Please run `gem rdoc --ri gem_name`',
e.message
end
def test_store_for_missing_gem
e = assert_raises WEBrick::HTTPStatus::NotFound do
@s.store_for 'missing'
end
assert_equal 'Could not find gem "missing". Are you sure you installed it?',
e.message
end
def test_store_for_ruby
store = @s.store_for 'ruby'
assert_equal @system_dir, store.path
assert_equal :system, store.type
end
def test_store_for_site
store = @s.store_for 'site'
assert_equal File.join(@base, 'site'), store.path
assert_equal :site, store.type
end
def test_store_for_extra
store = @s.store_for 'extra-1'
assert_equal @extra_dirs.first, store.path
assert_equal :extra, store.type
end
def touch_system_cache_path
store = RDoc::Store.new @system_dir
store.title = 'Standard Library Documentation'
FileUtils.mkdir_p File.dirname store.cache_path
store.save
end
def touch_extra_cache_path
store = RDoc::Store.new @extra_dirs.first
store.title = 'My Extra Documentation'
FileUtils.mkdir_p File.dirname store.cache_path
store.save
end
end
|