Skip to content

Commit

Permalink
workarround the version check for pre released plugins as looks like …
Browse files Browse the repository at this point in the history
…rubygems is not activating them by default

Add a pre release gem test by using a mock to reproduce the behaviour
reaised by Gem::Specification.find_by_name when dealing with pre release
gems.

Fixes elastic#3476
  • Loading branch information
Pere Urbon-Bayes authored and jordansissel committed Jul 2, 2015
1 parent a81f024 commit bd75161
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
16 changes: 14 additions & 2 deletions lib/logstash/util/plugin_version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,14 @@ def initialize(*options)

def self.find_version!(name)
begin
specification = Gem::Specification.find_by_name(name)
new(specification.version)
spec = Gem::Specification.find_by_name(name)
if spec.nil?
# Checking for nil? is a workaround for situations where find_by_name
# is not able to find the real spec, as for example with pre releases
# of plugins
spec = Gem::Specification.find_all_by_name(name).first
end
new(spec.version)
rescue Gem::LoadError
# Rescuing the LoadError and raise a Logstash specific error.
# Likely we can't find the gem in the current GEM_PATH
Expand All @@ -39,5 +45,11 @@ def self.find_plugin_version!(type, name)
def <=>(other)
version <=> other.version
end

private

def self.build_from_spec(spec)
new(spec.version)
end
end
end
19 changes: 17 additions & 2 deletions spec/util/plugin_version_spec.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,33 @@
require "spec_helper"
require "logstash/util/plugin_version"

describe LogStash::Util::PluginVersion do
describe "LogStash::Util::PluginVersion" do

subject { LogStash::Util::PluginVersion }

context "#find_version!" do

let(:gem) { "bundler" }

it 'raises an PluginNoVersionError if we cant find the plugin in the gem path' do
dummy_name ='this-character-doesnt-exist-in-the-marvel-universe'
expect { subject.find_version!(dummy_name) }.to raise_error(LogStash::PluginNoVersionError)
end

it 'returns the version of the gem' do
expect { subject.find_version!('bundler') }.not_to raise_error
expect { subject.find_version!(gem) }.not_to raise_error
end

context "with a pre release gem" do

it 'return the version of the gem' do
# Gem::Specification.find_by_name return nil if the gem is not activated, as for
# example the pre release ones.
expect(Gem::Specification).to receive(:find_by_name).and_return(nil)
expect { subject.find_version!(gem) }.not_to raise_error
end
end

end

context "#new" do
Expand Down

0 comments on commit bd75161

Please sign in to comment.