Skip to content

Commit

Permalink
Fix GemIndex not correctly finding all specifications.
Browse files Browse the repository at this point in the history
The wrong API call was being used to search for all specifications.
RubyGems 2.x expects Gem::Specification.each to be called to iterate
over all specs. find_all_* does not accept ''.

Fixes #1058
  • Loading branch information
lsegal committed Jan 13, 2017
1 parent ea6ed94 commit cec5629
Show file tree
Hide file tree
Showing 10 changed files with 24 additions and 11 deletions.
2 changes: 1 addition & 1 deletion lib/yard/cli/diff.rb
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ def load_gem_data(gemfile)
# Next check installed RubyGems
gemfile_without_ext = gemfile.sub(/\.gem$/, '')
log.info "Searching for installed gem #{gemfile_without_ext}"
YARD::GemIndex.find_all_by_name('').find do |spec|
YARD::GemIndex.each.find do |spec|
next unless spec.full_name == gemfile_without_ext
yardoc = Registry.yardoc_file_for_gem(spec.name, "= #{spec.version}")
if yardoc
Expand Down
2 changes: 1 addition & 1 deletion lib/yard/cli/gems.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def optparse(*args)
if !args.empty? && @gems.empty?
log.error "No specified gems could be found for command"
elsif @gems.empty?
@gems += YARD::GemIndex.find_all_by_name('') if @gems.empty?
@gems += YARD::GemIndex.all if @gems.empty?
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/yard/cli/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ def create_library_version_if_yardopts_exist(library, dir)

def add_gems
require 'rubygems'
YARD::GemIndex.find_all_by_name('').each do |spec|
YARD::GemIndex.each do |spec|
libraries[spec.name] ||= []
libraries[spec.name] |= [YARD::Server::LibraryVersion.new(spec.name, spec.version.to_s, nil, :gem)]
end
Expand Down
2 changes: 1 addition & 1 deletion lib/yard/cli/yri.rb
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ def load_cache
def add_gem_paths
require 'rubygems'
gem_paths = []
YARD::GemIndex.find_all_by_name('').each do |spec|
YARD::GemIndex.each do |spec|
yfile = Registry.yardoc_file_for_gem(spec.name)
next if yfile.nil?

Expand Down
2 changes: 1 addition & 1 deletion lib/yard/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ def self.load_gem_plugins
return true unless options[:load_plugins]
require 'rubygems'
result = true
YARD::GemIndex.find_all_by_name('').each do |gem|
YARD::GemIndex.each do |gem|
begin
next true unless gem.name =~ YARD_PLUGIN_PREFIX
load_plugin(gem.name)
Expand Down
12 changes: 12 additions & 0 deletions lib/yard/gem_index.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,17 @@ def find_all_by_name(*args)
Gem.source_index.find_name(*args)
end
end

def each(&block)
if defined?(Gem::Specification) && Gem::Specification.respond_to?(:each)
Gem::Specification.each(&block)
else
Gem.source_index.find_name('').each(&block)
end
end

def all
each.to_a
end
end
end
2 changes: 1 addition & 1 deletion spec/cli/diff_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ def baz; BAR end
expect(File).to receive(:directory?).with('gem2-1.0.0').and_return(false)
spec1 = double(:spec)
spec2 = double(:spec)
expect(YARD::GemIndex).to receive(:find_all_by_name).at_least(1).times.and_return([spec1, spec2])
allow(YARD::GemIndex).to receive(:each) {|&b| [spec1, spec2].each(&b) }
allow(spec1).to receive(:full_name).and_return('gem1-1.0.0')
allow(spec1).to receive(:name).and_return('gem1')
allow(spec1).to receive(:version).and_return('1.0.0')
Expand Down
4 changes: 2 additions & 2 deletions spec/cli/gems_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def build_specs(*specs)
describe "#run" do
it "builds all gem indexes if no gem is specified" do
build_specs(@gem1, @gem2)
expect(YARD::GemIndex).to receive(:find_all_by_name).with('').and_return([@gem1, @gem2])
expect(YARD::GemIndex).to receive(:each) {|&b| [@gem1, @gem2].each(&b) }
CLI::Gems.run
end

Expand Down Expand Up @@ -74,7 +74,7 @@ def build_specs(*specs)
it "accepts --rebuild" do
@rebuild = true
build_specs(@gem1)
expect(YARD::GemIndex).to receive(:find_all_by_name).with('').and_return([@gem1])
expect(YARD::GemIndex).to receive(:each) {|&b| [@gem1].each(&b) }
CLI::Gems.run('--rebuild')
end
end
Expand Down
3 changes: 2 additions & 1 deletion spec/cli/server_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -251,8 +251,9 @@ def mock_file(filename, content = nil)
gem2 = double(:gem2, :name => 'gem2', :version => '1.0.0', :full_gem_path => '/path/to/bar')
specs = {'gem1' => gem1, 'gem2' => gem2}
allow(YARD::GemIndex).to receive(:find_all_by_name) do |k, _ver|
k == '' ? specs.values : specs.grep(k).map {|name| specs[name] }
specs.grep(k).map {|name| specs[name] }
end
allow(YARD::GemIndex).to receive(:each) {|&b| specs.values.each(&b) }
run '-g'
run '--gems'
end
Expand Down
4 changes: 2 additions & 2 deletions spec/config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@
expect(v).to receive(:name).at_least(1).times.and_return(k)
end

expect(YARD::GemIndex).to receive(:find_all_by_name).with('').and_return(plugins.values)
expect(YARD::GemIndex).to receive(:each) {|&b| plugins.values.each(&b) }
expect(YARD::Config).to receive(:load_plugin).with('yard_plugin').and_return(false)
expect(YARD::Config).to receive(:load_plugin).with('yard-plugin').and_return(true)
expect(YARD::Config.load_plugins).to be true
Expand All @@ -162,7 +162,7 @@
expect(v).to receive(:name).at_least(1).times.and_return(k)
end

expect(YARD::GemIndex).to receive(:find_all_by_name).with('').and_return(plugins.values)
expect(YARD::GemIndex).to receive(:each) {|&b| plugins.values.each(&b) }
expect(YARD::Config).to receive(:load_plugin).with('yard-plugin').and_raise(Gem::LoadError)
expect(log).to receive(:error).with(/Error loading plugin 'yard-plugin'/)
expect(YARD::Config.load_plugins).to be false
Expand Down

0 comments on commit cec5629

Please sign in to comment.