[ruby/rdoc] Dump plain objects as RDoc::Options
So that the generated `.rdoc_options` file is loadable. https://github.com/ruby/rdoc/commit/6cf6e1647b
This commit is contained in:
parent
11f3882173
commit
3b3fb73d61
@ -106,6 +106,7 @@ class RDoc::Options
|
|||||||
generator_options
|
generator_options
|
||||||
generators
|
generators
|
||||||
op_dir
|
op_dir
|
||||||
|
page_dir
|
||||||
option_parser
|
option_parser
|
||||||
pipe
|
pipe
|
||||||
rdoc_include
|
rdoc_include
|
||||||
@ -434,6 +435,7 @@ class RDoc::Options
|
|||||||
@main_page = map['main_page'] if map.has_key?('main_page')
|
@main_page = map['main_page'] if map.has_key?('main_page')
|
||||||
@markup = map['markup'] if map.has_key?('markup')
|
@markup = map['markup'] if map.has_key?('markup')
|
||||||
@op_dir = map['op_dir'] if map.has_key?('op_dir')
|
@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')
|
@show_hash = map['show_hash'] if map.has_key?('show_hash')
|
||||||
@tab_width = map['tab_width'] if map.has_key?('tab_width')
|
@tab_width = map['tab_width'] if map.has_key?('tab_width')
|
||||||
@template_dir = map['template_dir'] if map.has_key?('template_dir')
|
@template_dir = map['template_dir'] if map.has_key?('template_dir')
|
||||||
@ -513,19 +515,22 @@ class RDoc::Options
|
|||||||
##
|
##
|
||||||
# For dumping YAML
|
# For dumping YAML
|
||||||
|
|
||||||
def encode_with coder # :nodoc:
|
def to_yaml(*options) # :nodoc:
|
||||||
encoding = @encoding ? @encoding.name : nil
|
encoding = @encoding ? @encoding.name : nil
|
||||||
|
|
||||||
coder.add 'encoding', encoding
|
yaml = {}
|
||||||
coder.add 'static_path', sanitize_path(@static_path)
|
yaml['encoding'] = encoding
|
||||||
coder.add 'rdoc_include', sanitize_path(@rdoc_include)
|
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 = instance_variables.map { |ivar| ivar.to_s[1..-1] }
|
||||||
ivars -= SPECIAL
|
ivars -= SPECIAL
|
||||||
|
|
||||||
ivars.sort.each do |ivar|
|
ivars.sort.each do |ivar|
|
||||||
coder.add ivar, instance_variable_get("@#{ivar}")
|
yaml[ivar] = instance_variable_get("@#{ivar}")
|
||||||
end
|
end
|
||||||
|
yaml.to_yaml
|
||||||
end
|
end
|
||||||
|
|
||||||
##
|
##
|
||||||
@ -548,6 +553,11 @@ class RDoc::Options
|
|||||||
# #template.
|
# #template.
|
||||||
|
|
||||||
def finish
|
def finish
|
||||||
|
if @write_options then
|
||||||
|
write_options
|
||||||
|
exit
|
||||||
|
end
|
||||||
|
|
||||||
@op_dir ||= 'doc'
|
@op_dir ||= 'doc'
|
||||||
|
|
||||||
@rdoc_include << "." if @rdoc_include.empty?
|
@rdoc_include << "." if @rdoc_include.empty?
|
||||||
@ -585,14 +595,14 @@ class RDoc::Options
|
|||||||
def finish_page_dir
|
def finish_page_dir
|
||||||
return unless @page_dir
|
return unless @page_dir
|
||||||
|
|
||||||
@files << @page_dir.to_s
|
@files << @page_dir
|
||||||
|
|
||||||
page_dir = nil
|
page_dir = Pathname(@page_dir)
|
||||||
begin
|
begin
|
||||||
page_dir = @page_dir.expand_path.relative_path_from @root
|
page_dir = page_dir.expand_path.relative_path_from @root
|
||||||
rescue ArgumentError
|
rescue ArgumentError
|
||||||
# On Windows, sometimes crosses different drive letters.
|
# On Windows, sometimes crosses different drive letters.
|
||||||
page_dir = @page_dir.expand_path
|
page_dir = page_dir.expand_path
|
||||||
end
|
end
|
||||||
|
|
||||||
@page_dir = page_dir
|
@page_dir = page_dir
|
||||||
@ -847,7 +857,7 @@ Usage: #{opt.program_name} [options] [names...]
|
|||||||
"such files at your project root.",
|
"such files at your project root.",
|
||||||
"NOTE: Do not use the same file name in",
|
"NOTE: Do not use the same file name in",
|
||||||
"the page dir and the root of your project") do |page_dir|
|
"the page dir and the root of your project") do |page_dir|
|
||||||
@page_dir = Pathname(page_dir)
|
@page_dir = page_dir
|
||||||
end
|
end
|
||||||
|
|
||||||
opt.separator nil
|
opt.separator nil
|
||||||
@ -1159,13 +1169,6 @@ Usage: #{opt.program_name} [options] [names...]
|
|||||||
|
|
||||||
@files = argv.dup
|
@files = argv.dup
|
||||||
|
|
||||||
finish
|
|
||||||
|
|
||||||
if @write_options then
|
|
||||||
write_options
|
|
||||||
exit
|
|
||||||
end
|
|
||||||
|
|
||||||
self
|
self
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -1278,7 +1281,7 @@ Usage: #{opt.program_name} [options] [names...]
|
|||||||
File.open '.rdoc_options', 'w' do |io|
|
File.open '.rdoc_options', 'w' do |io|
|
||||||
io.set_encoding Encoding::UTF_8
|
io.set_encoding Encoding::UTF_8
|
||||||
|
|
||||||
YAML.dump self, io
|
io.print to_yaml
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -440,11 +440,11 @@ The internal error was:
|
|||||||
|
|
||||||
if RDoc::Options === options then
|
if RDoc::Options === options then
|
||||||
@options = options
|
@options = options
|
||||||
@options.finish
|
|
||||||
else
|
else
|
||||||
@options = RDoc::Options.load_options
|
@options = RDoc::Options.load_options
|
||||||
@options.parse options
|
@options.parse options
|
||||||
end
|
end
|
||||||
|
@options.finish
|
||||||
|
|
||||||
if @options.pipe then
|
if @options.pipe then
|
||||||
handle_pipe
|
handle_pipe
|
||||||
|
@ -55,11 +55,8 @@ class TestRDocOptions < RDoc::TestCase
|
|||||||
refute @options.dry_run
|
refute @options.dry_run
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_encode_with
|
def test_to_yaml
|
||||||
coder = {}
|
coder = YAML.load(@options.to_yaml)
|
||||||
class << coder; alias add []=; end
|
|
||||||
|
|
||||||
@options.encode_with coder
|
|
||||||
|
|
||||||
encoding = 'UTF-8'
|
encoding = 'UTF-8'
|
||||||
|
|
||||||
@ -89,10 +86,9 @@ class TestRDocOptions < RDoc::TestCase
|
|||||||
assert_equal expected, coder
|
assert_equal expected, coder
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_encode_with_trim_paths
|
def test_to_yaml_trim_paths
|
||||||
subdir = nil
|
subdir = nil
|
||||||
coder = {}
|
coder = nil
|
||||||
class << coder; alias add []=; end
|
|
||||||
|
|
||||||
temp_dir do |dir|
|
temp_dir do |dir|
|
||||||
FileUtils.mkdir 'project'
|
FileUtils.mkdir 'project'
|
||||||
@ -113,7 +109,7 @@ class TestRDocOptions < RDoc::TestCase
|
|||||||
--include /
|
--include /
|
||||||
]
|
]
|
||||||
|
|
||||||
@options.encode_with coder
|
coder = YAML.load(@options.to_yaml)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -145,7 +141,9 @@ class TestRDocOptions < RDoc::TestCase
|
|||||||
|
|
||||||
@options.encoding = Encoding::IBM437
|
@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
|
assert_equal Encoding::IBM437, options.encoding
|
||||||
end
|
end
|
||||||
@ -154,14 +152,15 @@ class TestRDocOptions < RDoc::TestCase
|
|||||||
RDoc.load_yaml
|
RDoc.load_yaml
|
||||||
|
|
||||||
yaml = <<-YAML
|
yaml = <<-YAML
|
||||||
--- !ruby/object:RDoc::Options
|
---
|
||||||
static_path:
|
static_path:
|
||||||
- /etc
|
- /etc
|
||||||
rdoc_include:
|
rdoc_include:
|
||||||
- /etc
|
- /etc
|
||||||
YAML
|
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.rdoc_include
|
||||||
assert_empty options.static_path
|
assert_empty options.static_path
|
||||||
@ -243,6 +242,7 @@ rdoc_include:
|
|||||||
|
|
||||||
def test_parse_default
|
def test_parse_default
|
||||||
@options.parse []
|
@options.parse []
|
||||||
|
@options.finish
|
||||||
|
|
||||||
assert_equal RDoc::Generator::Darkfish, @options.generator
|
assert_equal RDoc::Generator::Darkfish, @options.generator
|
||||||
assert_equal 'darkfish', @options.template
|
assert_equal 'darkfish', @options.template
|
||||||
@ -502,6 +502,7 @@ rdoc_include:
|
|||||||
|
|
||||||
out, err = capture_output do
|
out, err = capture_output do
|
||||||
@options.parse %W[--page-dir #{Dir.tmpdir}]
|
@options.parse %W[--page-dir #{Dir.tmpdir}]
|
||||||
|
@options.finish
|
||||||
end
|
end
|
||||||
|
|
||||||
assert_empty out
|
assert_empty out
|
||||||
@ -530,6 +531,7 @@ rdoc_include:
|
|||||||
|
|
||||||
out, err = capture_output do
|
out, err = capture_output do
|
||||||
@options.parse %W[--page-dir #{abs_page_dir} --root #{abs_root}]
|
@options.parse %W[--page-dir #{abs_page_dir} --root #{abs_root}]
|
||||||
|
@options.finish
|
||||||
end
|
end
|
||||||
|
|
||||||
assert_empty out
|
assert_empty out
|
||||||
@ -558,6 +560,8 @@ rdoc_include:
|
|||||||
assert_empty err
|
assert_empty err
|
||||||
|
|
||||||
assert_equal Pathname(Dir.tmpdir), @options.root
|
assert_equal Pathname(Dir.tmpdir), @options.root
|
||||||
|
|
||||||
|
@options.finish
|
||||||
assert_includes @options.rdoc_include, @options.root.to_s
|
assert_includes @options.rdoc_include, @options.root.to_s
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -602,6 +606,7 @@ rdoc_include:
|
|||||||
assert_empty out
|
assert_empty out
|
||||||
assert_equal "could not find template NONEXISTENT\n", err
|
assert_equal "could not find template NONEXISTENT\n", err
|
||||||
|
|
||||||
|
@options.finish
|
||||||
assert_equal 'darkfish', @options.template
|
assert_equal 'darkfish', @options.template
|
||||||
assert_match %r%rdoc/generator/template/darkfish$%, @options.template_dir
|
assert_match %r%rdoc/generator/template/darkfish$%, @options.template_dir
|
||||||
end
|
end
|
||||||
@ -668,6 +673,7 @@ rdoc_include:
|
|||||||
Dir.chdir tmpdir do
|
Dir.chdir tmpdir do
|
||||||
e = assert_raise SystemExit do
|
e = assert_raise SystemExit do
|
||||||
@options.parse %w[--write-options]
|
@options.parse %w[--write-options]
|
||||||
|
@options.finish
|
||||||
end
|
end
|
||||||
|
|
||||||
assert_equal 0, e.status
|
assert_equal 0, e.status
|
||||||
@ -764,7 +770,9 @@ rdoc_include:
|
|||||||
|
|
||||||
assert File.exist? '.rdoc_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
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -834,12 +842,20 @@ rdoc_include:
|
|||||||
def test_load_options_partial_override
|
def test_load_options_partial_override
|
||||||
temp_dir do
|
temp_dir do
|
||||||
File.open '.rdoc_options', 'w' do |io|
|
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
|
end
|
||||||
|
|
||||||
options = RDoc::Options.load_options
|
options = RDoc::Options.load_options
|
||||||
|
|
||||||
assert_equal 'Markdown', options.markup
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -850,4 +866,10 @@ rdoc_include:
|
|||||||
assert_kind_of RDoc::Options, options
|
assert_kind_of RDoc::Options, options
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
class DummyCoder < Hash
|
||||||
|
alias add :[]=
|
||||||
|
def tag=(tag)
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
@ -254,6 +254,7 @@ class TestRDocRDoc < RDoc::TestCase
|
|||||||
top_level = nil
|
top_level = nil
|
||||||
temp_dir do |dir|
|
temp_dir do |dir|
|
||||||
@rdoc.options.parse %W[--root #{test_path}]
|
@rdoc.options.parse %W[--root #{test_path}]
|
||||||
|
@rdoc.options.finish
|
||||||
|
|
||||||
File.open 'include.txt', 'w' do |io|
|
File.open 'include.txt', 'w' do |io|
|
||||||
io.puts ':include: test.txt'
|
io.puts ':include: test.txt'
|
||||||
|
Loading…
x
Reference in New Issue
Block a user