Skip to content

Commit

Permalink
Merge pull request #832 from aycabta/move-rdoc-rdoc-load_options-to-r…
Browse files Browse the repository at this point in the history
…doc-options-load_options

Move RDoc::RDoc#load_options to RDoc::RDoc.load_options
  • Loading branch information
aycabta authored Sep 1, 2021
2 parents 89a59f2 + ac85e01 commit 5437418
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 89 deletions.
29 changes: 29 additions & 0 deletions lib/rdoc/options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1282,4 +1282,33 @@ def write_options
end
end

##
# Loads options from .rdoc_options if the file exists, otherwise creates a
# new RDoc::Options instance.

def self.load_options
options_file = File.expand_path '.rdoc_options'
return RDoc::Options.new unless File.exist? options_file

RDoc.load_yaml

begin
options = YAML.safe_load File.read('.rdoc_options'), permitted_classes: [RDoc::Options, Symbol]
rescue Psych::SyntaxError
raise RDoc::Error, "#{options_file} is not a valid rdoc options file"
end

return RDoc::Options.new unless options # Allow empty file.

raise RDoc::Error, "#{options_file} is not a valid rdoc options file" unless
RDoc::Options === options or Hash === options

if Hash === options
# Override the default values with the contents of YAML file.
options = RDoc::Options.new options
end

options
end

end
33 changes: 2 additions & 31 deletions lib/rdoc/rdoc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
# is:
#
# rdoc = RDoc::RDoc.new
# options = rdoc.load_options # returns an RDoc::Options instance
# options = RDoc::Options.load_options # returns an RDoc::Options instance
# # set extra options
# rdoc.document options
#
Expand Down Expand Up @@ -151,35 +151,6 @@ def install_siginfo_handler
end
end

##
# Loads options from .rdoc_options if the file exists, otherwise creates a
# new RDoc::Options instance.

def load_options
options_file = File.expand_path '.rdoc_options'
return RDoc::Options.new unless File.exist? options_file

RDoc.load_yaml

begin
options = YAML.safe_load File.read('.rdoc_options'), permitted_classes: [RDoc::Options, Symbol]
rescue Psych::SyntaxError
raise RDoc::Error, "#{options_file} is not a valid rdoc options file"
end

return RDoc::Options.new unless options # Allow empty file.

raise RDoc::Error, "#{options_file} is not a valid rdoc options file" unless
RDoc::Options === options or Hash === options

if Hash === options
# Override the default values with the contents of YAML file.
options = RDoc::Options.new options
end

options
end

##
# Create an output dir if it doesn't exist. If it does exist, but doesn't
# contain the flag file <tt>created.rid</tt> then we refuse to use it, as
Expand Down Expand Up @@ -471,7 +442,7 @@ def document options
@options = options
@options.finish
else
@options = load_options
@options = RDoc::Options.load_options
@options.parse options
end

Expand Down
58 changes: 58 additions & 0 deletions test/rdoc/test_rdoc_options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -777,4 +777,62 @@ def test_visibility
@options.visibility = :all
assert_equal :private, @options.visibility
end

def test_load_options
temp_dir do
options = RDoc::Options.new
options.markup = 'tomdoc'
options.write_options

options = RDoc::Options.load_options

assert_equal 'tomdoc', options.markup
end
end

def test_load_options_invalid
temp_dir do
File.open '.rdoc_options', 'w' do |io|
io.write "a: !ruby.yaml.org,2002:str |\nfoo"
end

e = assert_raise RDoc::Error do
RDoc::Options.load_options
end

options_file = File.expand_path '.rdoc_options'
assert_equal "#{options_file} is not a valid rdoc options file", e.message
end
end

def test_load_options_empty_file
temp_dir do
File.open '.rdoc_options', 'w' do |io|
end

options = RDoc::Options.load_options

assert_equal 'rdoc', options.markup
end
end

def test_load_options_partial_override
temp_dir do
File.open '.rdoc_options', 'w' do |io|
io.write "markup: Markdown"
end

options = RDoc::Options.load_options

assert_equal 'Markdown', options.markup
end
end

def load_options_no_file
temp_dir do
options = RDoc::Options.load_options

assert_kind_of RDoc::Options, options
end
end
end
58 changes: 0 additions & 58 deletions test/rdoc/test_rdoc_rdoc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -106,64 +106,6 @@ def test_handle_pipe_rd
$stdin = STDIN
end

def test_load_options
temp_dir do
options = RDoc::Options.new
options.markup = 'tomdoc'
options.write_options

options = @rdoc.load_options

assert_equal 'tomdoc', options.markup
end
end

def test_load_options_invalid
temp_dir do
File.open '.rdoc_options', 'w' do |io|
io.write "a: !ruby.yaml.org,2002:str |\nfoo"
end

e = assert_raise RDoc::Error do
@rdoc.load_options
end

options_file = File.expand_path '.rdoc_options'
assert_equal "#{options_file} is not a valid rdoc options file", e.message
end
end

def test_load_options_empty_file
temp_dir do
File.open '.rdoc_options', 'w' do |io|
end

options = @rdoc.load_options

assert_equal 'rdoc', options.markup
end
end

def test_load_options_partial_override
temp_dir do
File.open '.rdoc_options', 'w' do |io|
io.write "markup: Markdown"
end

options = @rdoc.load_options

assert_equal 'Markdown', options.markup
end
end

def load_options_no_file
temp_dir do
options = @rdoc.load_options

assert_kind_of RDoc::Options, options
end
end

def test_normalized_file_list
test_path = File.expand_path(__FILE__)
files = temp_dir do |dir|
Expand Down

0 comments on commit 5437418

Please sign in to comment.