Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make .rdoc_options file plain ruby objects only #839

Merged
merged 1 commit into from
Feb 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
39 changes: 21 additions & 18 deletions lib/rdoc/options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ class RDoc::Options
generator_options
generators
op_dir
page_dir
option_parser
pipe
rdoc_include
Expand Down Expand Up @@ -434,6 +435,7 @@ def override map # :nodoc:
@main_page = map['main_page'] if map.has_key?('main_page')
@markup = map['markup'] if map.has_key?('markup')
@op_dir = map['op_dir'] if map.has_key?('op_dir')
@page_dir = map['page_dir'] if map.has_key?('page_dir')
@show_hash = map['show_hash'] if map.has_key?('show_hash')
@tab_width = map['tab_width'] if map.has_key?('tab_width')
@template_dir = map['template_dir'] if map.has_key?('template_dir')
Expand Down Expand Up @@ -513,19 +515,22 @@ def default_title=(string)
##
# For dumping YAML

def encode_with coder # :nodoc:
def to_yaml(*options) # :nodoc:
encoding = @encoding ? @encoding.name : nil

coder.add 'encoding', encoding
coder.add 'static_path', sanitize_path(@static_path)
coder.add 'rdoc_include', sanitize_path(@rdoc_include)
yaml = {}
yaml['encoding'] = encoding
yaml['static_path'] = sanitize_path(@static_path)
yaml['rdoc_include'] = sanitize_path(@rdoc_include)
yaml['page_dir'] = (sanitize_path([@page_dir]).first if @page_dir)

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

ivars.sort.each do |ivar|
coder.add ivar, instance_variable_get("@#{ivar}")
yaml[ivar] = instance_variable_get("@#{ivar}")
end
yaml.to_yaml
end

##
Expand All @@ -548,6 +553,11 @@ def exclude
# #template.

def finish
if @write_options then
write_options
exit
end

@op_dir ||= 'doc'

@rdoc_include << "." if @rdoc_include.empty?
Expand Down Expand Up @@ -585,14 +595,14 @@ def finish
def finish_page_dir
return unless @page_dir

@files << @page_dir.to_s
@files << @page_dir

page_dir = nil
page_dir = Pathname(@page_dir)
begin
page_dir = @page_dir.expand_path.relative_path_from @root
page_dir = page_dir.expand_path.relative_path_from @root
rescue ArgumentError
# On Windows, sometimes crosses different drive letters.
page_dir = @page_dir.expand_path
page_dir = page_dir.expand_path
end

@page_dir = page_dir
Expand Down Expand Up @@ -847,7 +857,7 @@ def parse argv
"such files at your project root.",
"NOTE: Do not use the same file name in",
"the page dir and the root of your project") do |page_dir|
@page_dir = Pathname(page_dir)
@page_dir = page_dir
end

opt.separator nil
Expand Down Expand Up @@ -1159,13 +1169,6 @@ def parse argv

@files = argv.dup

finish

if @write_options then
write_options
exit
end

self
end

Expand Down Expand Up @@ -1278,7 +1281,7 @@ def write_options
File.open '.rdoc_options', 'w' do |io|
io.set_encoding Encoding::UTF_8

YAML.dump self, io
io.print to_yaml
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/rdoc/rdoc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -440,11 +440,11 @@ def document options

if RDoc::Options === options then
@options = options
@options.finish
else
@options = RDoc::Options.load_options
@options.parse options
end
@options.finish

if @options.pipe then
handle_pipe
Expand Down
50 changes: 36 additions & 14 deletions test/rdoc/test_rdoc_options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,8 @@ def test_dry_run_default
refute @options.dry_run
end

def test_encode_with
coder = {}
class << coder; alias add []=; end

@options.encode_with coder
def test_to_yaml
coder = YAML.load(@options.to_yaml)

encoding = 'UTF-8'

Expand Down Expand Up @@ -89,10 +86,9 @@ class << coder; alias add []=; end
assert_equal expected, coder
end

def test_encode_with_trim_paths
def test_to_yaml_trim_paths
subdir = nil
coder = {}
class << coder; alias add []=; end
coder = nil

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

@options.encode_with coder
coder = YAML.load(@options.to_yaml)
end
end

Expand Down Expand Up @@ -145,7 +141,9 @@ def test_init_with_encoding

@options.encoding = Encoding::IBM437

options = YAML.safe_load(YAML.dump(@options), permitted_classes: [RDoc::Options, Symbol])
options = @options.to_yaml
options = YAML.safe_load(options, permitted_classes: [Symbol])
options = RDoc::Options.new(options)

assert_equal Encoding::IBM437, options.encoding
end
Expand All @@ -154,14 +152,15 @@ def test_init_with_trim_paths
RDoc.load_yaml

yaml = <<-YAML
--- !ruby/object:RDoc::Options
---
static_path:
- /etc
rdoc_include:
- /etc
YAML

options = YAML.safe_load(yaml, permitted_classes: [RDoc::Options, Symbol])
options = YAML.safe_load(yaml, permitted_classes: [Symbol])
options = RDoc::Options.new(options)

assert_empty options.rdoc_include
assert_empty options.static_path
Expand Down Expand Up @@ -243,6 +242,7 @@ def test_parse_dash_p_files

def test_parse_default
@options.parse []
@options.finish

assert_equal RDoc::Generator::Darkfish, @options.generator
assert_equal 'darkfish', @options.template
Expand Down Expand Up @@ -502,6 +502,7 @@ def test_parse_page_dir

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

assert_empty out
Expand Down Expand Up @@ -530,6 +531,7 @@ def test_parse_page_dir_root

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

assert_empty out
Expand Down Expand Up @@ -558,6 +560,8 @@ def test_parse_root
assert_empty err

assert_equal Pathname(Dir.tmpdir), @options.root

@options.finish
assert_includes @options.rdoc_include, @options.root.to_s
end

Expand Down Expand Up @@ -602,6 +606,7 @@ def test_parse_template_nonexistent
assert_empty out
assert_equal "could not find template NONEXISTENT\n", err

@options.finish
assert_equal 'darkfish', @options.template
assert_match %r%rdoc/generator/template/darkfish$%, @options.template_dir
end
Expand Down Expand Up @@ -668,6 +673,7 @@ def test_parse_write_options
Dir.chdir tmpdir do
e = assert_raise SystemExit do
@options.parse %w[--write-options]
@options.finish
end

assert_equal 0, e.status
Expand Down Expand Up @@ -764,7 +770,9 @@ def test_write_options

assert File.exist? '.rdoc_options'

assert_equal @options, YAML.safe_load(File.read('.rdoc_options'), permitted_classes: [RDoc::Options, Symbol])
options = File.read('.rdoc_options')
options = YAML.safe_load(options, permitted_classes: [Symbol])
assert_equal @options, RDoc::Options.new(options)
end
end

Expand Down Expand Up @@ -834,12 +842,20 @@ def test_load_options_empty_file
def test_load_options_partial_override
temp_dir do
File.open '.rdoc_options', 'w' do |io|
io.write "markup: Markdown"
io.puts "markup: Markdown"
io.puts "encoding: iso-8859-1"
io.puts "static_path: [static]"
io.puts "rdoc_include: [.]"
io.puts "page_dir: pages"
end

options = RDoc::Options.load_options

assert_equal 'Markdown', options.markup
assert_equal Encoding::ISO_8859_1, options.encoding
assert_equal ["static"], options.static_path
assert_equal ["."], options.rdoc_include
assert_equal "pages", options.page_dir
end
end

Expand All @@ -850,4 +866,10 @@ def test_load_options_no_file
assert_kind_of RDoc::Options, options
end
end

class DummyCoder < Hash
alias add :[]=
def tag=(tag)
end
end
end
1 change: 1 addition & 0 deletions test/rdoc/test_rdoc_rdoc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ def test_parse_file_include_root
top_level = nil
temp_dir do |dir|
@rdoc.options.parse %W[--root #{test_path}]
@rdoc.options.finish

File.open 'include.txt', 'w' do |io|
io.puts ':include: test.txt'
Expand Down