Skip to content

Commit 6cf6e16

Browse files
committed
Dump plain objects as RDoc::Options
So that the generated `.rdoc_options` file is loadable.
1 parent 8166b84 commit 6cf6e16

File tree

4 files changed

+59
-33
lines changed

4 files changed

+59
-33
lines changed

lib/rdoc/options.rb

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ class RDoc::Options
106106
generator_options
107107
generators
108108
op_dir
109+
page_dir
109110
option_parser
110111
pipe
111112
rdoc_include
@@ -434,6 +435,7 @@ def override map # :nodoc:
434435
@main_page = map['main_page'] if map.has_key?('main_page')
435436
@markup = map['markup'] if map.has_key?('markup')
436437
@op_dir = map['op_dir'] if map.has_key?('op_dir')
438+
@page_dir = map['page_dir'] if map.has_key?('page_dir')
437439
@show_hash = map['show_hash'] if map.has_key?('show_hash')
438440
@tab_width = map['tab_width'] if map.has_key?('tab_width')
439441
@template_dir = map['template_dir'] if map.has_key?('template_dir')
@@ -513,19 +515,22 @@ def default_title=(string)
513515
##
514516
# For dumping YAML
515517

516-
def encode_with coder # :nodoc:
518+
def to_yaml(*options) # :nodoc:
517519
encoding = @encoding ? @encoding.name : nil
518520

519-
coder.add 'encoding', encoding
520-
coder.add 'static_path', sanitize_path(@static_path)
521-
coder.add 'rdoc_include', sanitize_path(@rdoc_include)
521+
yaml = {}
522+
yaml['encoding'] = encoding
523+
yaml['static_path'] = sanitize_path(@static_path)
524+
yaml['rdoc_include'] = sanitize_path(@rdoc_include)
525+
yaml['page_dir'] = (sanitize_path([@page_dir]).first if @page_dir)
522526

523527
ivars = instance_variables.map { |ivar| ivar.to_s[1..-1] }
524528
ivars -= SPECIAL
525529

526530
ivars.sort.each do |ivar|
527-
coder.add ivar, instance_variable_get("@#{ivar}")
531+
yaml[ivar] = instance_variable_get("@#{ivar}")
528532
end
533+
yaml.to_yaml
529534
end
530535

531536
##
@@ -548,6 +553,11 @@ def exclude
548553
# #template.
549554

550555
def finish
556+
if @write_options then
557+
write_options
558+
exit
559+
end
560+
551561
@op_dir ||= 'doc'
552562

553563
@rdoc_include << "." if @rdoc_include.empty?
@@ -585,14 +595,14 @@ def finish
585595
def finish_page_dir
586596
return unless @page_dir
587597

588-
@files << @page_dir.to_s
598+
@files << @page_dir
589599

590-
page_dir = nil
600+
page_dir = Pathname(@page_dir)
591601
begin
592-
page_dir = @page_dir.expand_path.relative_path_from @root
602+
page_dir = page_dir.expand_path.relative_path_from @root
593603
rescue ArgumentError
594604
# On Windows, sometimes crosses different drive letters.
595-
page_dir = @page_dir.expand_path
605+
page_dir = page_dir.expand_path
596606
end
597607

598608
@page_dir = page_dir
@@ -847,7 +857,7 @@ def parse argv
847857
"such files at your project root.",
848858
"NOTE: Do not use the same file name in",
849859
"the page dir and the root of your project") do |page_dir|
850-
@page_dir = Pathname(page_dir)
860+
@page_dir = page_dir
851861
end
852862

853863
opt.separator nil
@@ -1159,13 +1169,6 @@ def parse argv
11591169

11601170
@files = argv.dup
11611171

1162-
finish
1163-
1164-
if @write_options then
1165-
write_options
1166-
exit
1167-
end
1168-
11691172
self
11701173
end
11711174

@@ -1278,7 +1281,7 @@ def write_options
12781281
File.open '.rdoc_options', 'w' do |io|
12791282
io.set_encoding Encoding::UTF_8
12801283

1281-
YAML.dump self, io
1284+
io.print to_yaml
12821285
end
12831286
end
12841287

lib/rdoc/rdoc.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -440,11 +440,11 @@ def document options
440440

441441
if RDoc::Options === options then
442442
@options = options
443-
@options.finish
444443
else
445444
@options = RDoc::Options.load_options
446445
@options.parse options
447446
end
447+
@options.finish
448448

449449
if @options.pipe then
450450
handle_pipe

test/rdoc/test_rdoc_options.rb

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,8 @@ def test_dry_run_default
5555
refute @options.dry_run
5656
end
5757

58-
def test_encode_with
59-
coder = {}
60-
class << coder; alias add []=; end
61-
62-
@options.encode_with coder
58+
def test_to_yaml
59+
coder = YAML.load(@options.to_yaml)
6360

6461
encoding = 'UTF-8'
6562

@@ -89,10 +86,9 @@ class << coder; alias add []=; end
8986
assert_equal expected, coder
9087
end
9188

92-
def test_encode_with_trim_paths
89+
def test_to_yaml_trim_paths
9390
subdir = nil
94-
coder = {}
95-
class << coder; alias add []=; end
91+
coder = nil
9692

9793
temp_dir do |dir|
9894
FileUtils.mkdir 'project'
@@ -113,7 +109,7 @@ class << coder; alias add []=; end
113109
--include /
114110
]
115111

116-
@options.encode_with coder
112+
coder = YAML.load(@options.to_yaml)
117113
end
118114
end
119115

@@ -145,7 +141,9 @@ def test_init_with_encoding
145141

146142
@options.encoding = Encoding::IBM437
147143

148-
options = YAML.safe_load(YAML.dump(@options), permitted_classes: [RDoc::Options, Symbol])
144+
options = @options.to_yaml
145+
options = YAML.safe_load(options, permitted_classes: [Symbol])
146+
options = RDoc::Options.new(options)
149147

150148
assert_equal Encoding::IBM437, options.encoding
151149
end
@@ -154,14 +152,15 @@ def test_init_with_trim_paths
154152
RDoc.load_yaml
155153

156154
yaml = <<-YAML
157-
--- !ruby/object:RDoc::Options
155+
---
158156
static_path:
159157
- /etc
160158
rdoc_include:
161159
- /etc
162160
YAML
163161

164-
options = YAML.safe_load(yaml, permitted_classes: [RDoc::Options, Symbol])
162+
options = YAML.safe_load(yaml, permitted_classes: [Symbol])
163+
options = RDoc::Options.new(options)
165164

166165
assert_empty options.rdoc_include
167166
assert_empty options.static_path
@@ -243,6 +242,7 @@ def test_parse_dash_p_files
243242

244243
def test_parse_default
245244
@options.parse []
245+
@options.finish
246246

247247
assert_equal RDoc::Generator::Darkfish, @options.generator
248248
assert_equal 'darkfish', @options.template
@@ -502,6 +502,7 @@ def test_parse_page_dir
502502

503503
out, err = capture_output do
504504
@options.parse %W[--page-dir #{Dir.tmpdir}]
505+
@options.finish
505506
end
506507

507508
assert_empty out
@@ -530,6 +531,7 @@ def test_parse_page_dir_root
530531

531532
out, err = capture_output do
532533
@options.parse %W[--page-dir #{abs_page_dir} --root #{abs_root}]
534+
@options.finish
533535
end
534536

535537
assert_empty out
@@ -558,6 +560,8 @@ def test_parse_root
558560
assert_empty err
559561

560562
assert_equal Pathname(Dir.tmpdir), @options.root
563+
564+
@options.finish
561565
assert_includes @options.rdoc_include, @options.root.to_s
562566
end
563567

@@ -602,6 +606,7 @@ def test_parse_template_nonexistent
602606
assert_empty out
603607
assert_equal "could not find template NONEXISTENT\n", err
604608

609+
@options.finish
605610
assert_equal 'darkfish', @options.template
606611
assert_match %r%rdoc/generator/template/darkfish$%, @options.template_dir
607612
end
@@ -668,6 +673,7 @@ def test_parse_write_options
668673
Dir.chdir tmpdir do
669674
e = assert_raise SystemExit do
670675
@options.parse %w[--write-options]
676+
@options.finish
671677
end
672678

673679
assert_equal 0, e.status
@@ -764,7 +770,9 @@ def test_write_options
764770

765771
assert File.exist? '.rdoc_options'
766772

767-
assert_equal @options, YAML.safe_load(File.read('.rdoc_options'), permitted_classes: [RDoc::Options, Symbol])
773+
options = File.read('.rdoc_options')
774+
options = YAML.safe_load(options, permitted_classes: [Symbol])
775+
assert_equal @options, RDoc::Options.new(options)
768776
end
769777
end
770778

@@ -834,12 +842,20 @@ def test_load_options_empty_file
834842
def test_load_options_partial_override
835843
temp_dir do
836844
File.open '.rdoc_options', 'w' do |io|
837-
io.write "markup: Markdown"
845+
io.puts "markup: Markdown"
846+
io.puts "encoding: iso-8859-1"
847+
io.puts "static_path: [static]"
848+
io.puts "rdoc_include: [.]"
849+
io.puts "page_dir: pages"
838850
end
839851

840852
options = RDoc::Options.load_options
841853

842854
assert_equal 'Markdown', options.markup
855+
assert_equal Encoding::ISO_8859_1, options.encoding
856+
assert_equal ["static"], options.static_path
857+
assert_equal ["."], options.rdoc_include
858+
assert_equal "pages", options.page_dir
843859
end
844860
end
845861

@@ -850,4 +866,10 @@ def test_load_options_no_file
850866
assert_kind_of RDoc::Options, options
851867
end
852868
end
869+
870+
class DummyCoder < Hash
871+
alias add :[]=
872+
def tag=(tag)
873+
end
874+
end
853875
end

test/rdoc/test_rdoc_rdoc.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,7 @@ def test_parse_file_include_root
254254
top_level = nil
255255
temp_dir do |dir|
256256
@rdoc.options.parse %W[--root #{test_path}]
257+
@rdoc.options.finish
257258

258259
File.open 'include.txt', 'w' do |io|
259260
io.puts ':include: test.txt'

0 commit comments

Comments
 (0)