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

Add existence check for readme #1367

Merged
merged 1 commit into from
May 6, 2021
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/yard/cli/yardoc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -295,9 +295,10 @@ def parse_arguments(*args)
self.files = Parser::SourceParser::DEFAULT_PATH_GLOB if files.empty?
files.delete_if {|x| x =~ /\A\s*\Z/ } # remove empty ones
readme = Dir.glob('README{,*[^~]}').
select {|f| extra_file_valid?(f)}.
sort_by {|r| [r.count('.'), r.index('.'), r] }.first
readme ||= Dir.glob(files.first).first if options.onefile && !files.empty?
options.readme ||= CodeObjects::ExtraFileObject.new(readme) if readme
options.readme ||= CodeObjects::ExtraFileObject.new(readme) if readme && extra_file_valid?(readme)
options.files.unshift(options.readme).uniq! if options.readme

Tags::Library.visible_tags -= hidden_tags
Expand Down
26 changes: 25 additions & 1 deletion spec/cli/yardoc_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,12 @@ def foo; end
end

describe "Extra file arguments" do
def expect_extra_files_valid(obj, validity_hash = {})
validity_hash.each do |filename, valid|
expect(obj).to receive(:extra_file_valid?).with(filename).and_return(valid)
end
end

it "accepts extra files if specified after '-' with source files" do
expect(Dir).to receive(:glob).with('README{,*[^~]}').and_return([])
expect(File).to receive(:file?).with('extra_file1').and_return(true)
Expand Down Expand Up @@ -642,6 +648,7 @@ def foo; end
it "uses first file as readme if no readme is specified when using --one-file" do
expect(Dir).to receive(:glob).with('README{,*[^~]}').and_return []
expect(Dir).to receive(:glob).with('lib/*.rb').and_return(['lib/foo.rb'])
expect_extra_files_valid(@yardoc, 'lib/foo.rb' => true)
expect(File).to receive(:read).with('lib/foo.rb').and_return('')
@yardoc.parse_arguments(*%w(--one-file lib/*.rb))
expect(@yardoc.options.readme).to eq CodeObjects::ExtraFileObject.new('lib/foo.rb', '')
Expand All @@ -653,22 +660,28 @@ def foo; end
expect(@yardoc.options.readme).to be_nil
end

it "uses readme it exists when using --one-file" do
it "uses readme if it exists when using --one-file" do
expect(Dir).to receive(:glob).with('README{,*[^~]}').and_return ['README']
expect_extra_files_valid(@yardoc, 'README' => true)
expect_extra_files_valid(@yardoc, 'README' => true)
expect(File).to receive(:read).with('README').and_return('')
@yardoc.parse_arguments(*%w(--one-file lib/*.rb))
expect(@yardoc.options.readme).to eq CodeObjects::ExtraFileObject.new('README', '')
end

it "selects readme with no file extension over readme with file extension" do
expect(Dir).to receive(:glob).with('README{,*[^~]}').and_return ['README.md', 'README-DEV', 'README']
expect_extra_files_valid(@yardoc, 'README.md' => true, 'README-DEV' => true, 'README' => true)
expect_extra_files_valid(@yardoc, 'README' => true)
expect(File).to receive(:read).with('README').and_return('')
@yardoc.parse_arguments
expect(@yardoc.options.readme).to eq CodeObjects::ExtraFileObject.new('README', '')
end

it "selects readme with no suffix over readme with hyphenated suffix" do
expect(Dir).to receive(:glob).with('README{,*[^~]}').and_return ['README-fr.md', 'README.long-extension', 'README-de.md']
expect_extra_files_valid(@yardoc, 'README-fr.md' => true, 'README.long-extension'=> true, 'README-de.md' => true)
expect_extra_files_valid(@yardoc, 'README.long-extension'=> true)
expect(File).to receive(:read).with('README.long-extension').and_return('')
@yardoc.parse_arguments
expect(@yardoc.options.readme).to eq CodeObjects::ExtraFileObject.new('README.long-extension', '')
Expand All @@ -683,11 +696,22 @@ def foo; end

it "selects first readme from lexically sorted list" do
expect(Dir).to receive(:glob).with('README{,*[^~]}').and_return ['README-fr.md', 'README-de.md']
expect_extra_files_valid(@yardoc, 'README-fr.md'=> true, 'README-de.md' => true)
expect_extra_files_valid(@yardoc, 'README-de.md' => true)
expect(File).to receive(:read).with('README-de.md').and_return('')
@yardoc.parse_arguments
expect(@yardoc.options.readme).to eq CodeObjects::ExtraFileObject.new('README-de.md', '')
end

it "selects readme that exists over a readme that does not" do
expect(Dir).to receive(:glob).with('README{,*[^~]}').and_return ['README.fr.md', 'README.md', 'README.de.md']
expect_extra_files_valid(@yardoc, 'README.fr.md'=> true, 'README.md' => false, 'README.de.md' => false)
expect_extra_files_valid(@yardoc, 'README.fr.md'=> true)
expect(File).to receive(:read).with('README.fr.md').and_return('')
@yardoc.parse_arguments
expect(@yardoc.options.readme).to eq CodeObjects::ExtraFileObject.new('README.fr.md', '')
end

it "does not allow US-ASCII charset when using --one-file" do
ienc = Encoding.default_internal
eenc = Encoding.default_external
Expand Down