From bd7516108faf6af1358fd4d823fa68e2f6b38275 Mon Sep 17 00:00:00 2001 From: Pere Urbon-Bayes Date: Sun, 21 Jun 2015 12:37:01 +0200 Subject: [PATCH] workarround the version check for pre released plugins as looks like 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 #3476 --- lib/logstash/util/plugin_version.rb | 16 ++++++++++++++-- spec/util/plugin_version_spec.rb | 19 +++++++++++++++++-- 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/lib/logstash/util/plugin_version.rb b/lib/logstash/util/plugin_version.rb index ed6cb154304..1266646e684 100644 --- a/lib/logstash/util/plugin_version.rb +++ b/lib/logstash/util/plugin_version.rb @@ -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 @@ -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 diff --git a/spec/util/plugin_version_spec.rb b/spec/util/plugin_version_spec.rb index 62ad8955a28..21c35f87dff 100644 --- a/spec/util/plugin_version_spec.rb +++ b/spec/util/plugin_version_spec.rb @@ -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