Skip to content

Commit

Permalink
Adapt plugin loading for Vagrant 1.5
Browse files Browse the repository at this point in the history
`Vagrant.require_plugin` will be deprecated in Vagrant 1.5.

In new Vagrant versions we now use `require` directly for loading the
optional dependencies. All exceptions are logged but ignored. Vagrant
itself will handle the real errors later (or has already errored out).
  • Loading branch information
tmatilai committed Jan 11, 2014
1 parent 58a034d commit 6c820fa
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 16 deletions.
28 changes: 23 additions & 5 deletions lib/vagrant-proxyconf/plugin.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'vagrant'
require_relative 'logger'

module VagrantPlugins
module ProxyConf
Expand Down Expand Up @@ -42,19 +43,36 @@ def self.setup_i18n
end

# Ensures a dependent plugin is loaded before us if it is installed.
# Ignores {Vagrant::Errors::PluginLoadError} but passes other exceptions.
# Ignores Errors while loading, as Vagrant itself anyway shows them to
# used when *it* tries to load the plugin.
#
# @param plugin [String] the plugin name
def self.load_optional_dependency(plugin)
begin
Vagrant.require_plugin plugin
rescue Vagrant::Errors::PluginLoadError; end
logger = ProxyConf.logger
logger.info "Trying to load #{plugin}"

if check_vagrant_version('< 1.5.0.dev')
begin
Vagrant.require_plugin plugin
rescue Vagrant::Errors::PluginLoadError
logger.info "Ignoring the load error of #{plugin}"
end
else
begin
require plugin
rescue Exception => e
logger.info "Failed to load #{plugin}: #{e.inspect}"
logger.info "Ignoring the error"
end
end
end

# Loads the plugins to ensure their action hooks are registered before us.
# Uses alphabetical order to not change the default behaviour otherwise.
def self.load_optional_dependencies
OPTIONAL_PLUGIN_DEPENDENCIES.sort.each { |plugin| load_optional_dependency plugin }
OPTIONAL_PLUGIN_DEPENDENCIES.sort.each do |plugin|
load_optional_dependency plugin
end
end

setup_i18n
Expand Down
38 changes: 27 additions & 11 deletions spec/unit/vagrant-proxyconf/plugin_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,19 +67,35 @@
subject { described_class.load_optional_dependency(plugin_name) }
let(:plugin_name) { 'vagrant-foo' }

it "loads the specified plugin" do
expect(Vagrant).to receive(:require_plugin).with(plugin_name)
subject
end
# Vagrant plugin loading API changed in v1.5.0
if Gem::Version.new(Vagrant::VERSION) < Gem::Version.new('1.5.0.dev')
it "loads the specified plugin" do
expect(Vagrant).to receive(:require_plugin).with(plugin_name)
subject
end

it "ignores PluginLoadError" do
expect(Vagrant).to receive(:require_plugin).and_raise(Vagrant::Errors::PluginLoadError, plugin: plugin_name)
expect { subject }.not_to raise_error
end
it "ignores PluginLoadError" do
expect(Vagrant).to receive(:require_plugin).
and_raise(Vagrant::Errors::PluginLoadError, plugin: plugin_name)
expect { subject }.not_to raise_error
end

it "won't ignore other error" do
expect(Vagrant).to receive(:require_plugin).and_raise(Vagrant::Errors::PluginLoadFailed, plugin: plugin_name)
expect { subject }.to raise_error(Vagrant::Errors::PluginLoadFailed)
it "won't ignore other error" do
expect(Vagrant).to receive(:require_plugin).
and_raise(Vagrant::Errors::PluginLoadFailed, plugin: plugin_name)
expect { subject }.to raise_error(Vagrant::Errors::PluginLoadFailed)
end
else
it "loads the specified plugin" do
expect(described_class).to receive(:require).with(plugin_name)
subject
end

it "ignores errors" do
expect(described_class).to receive(:require).
and_raise(LoadError, path: plugin_name)
expect { subject }.not_to raise_error
end
end
end

Expand Down

0 comments on commit 6c820fa

Please sign in to comment.