From 6c820fac93c59c9de24bae853f4abf8392748b9a Mon Sep 17 00:00:00 2001 From: Teemu Matilainen Date: Sat, 11 Jan 2014 13:25:07 -0300 Subject: [PATCH] Adapt plugin loading for Vagrant 1.5 `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). --- lib/vagrant-proxyconf/plugin.rb | 28 +++++++++++++--- spec/unit/vagrant-proxyconf/plugin_spec.rb | 38 +++++++++++++++------- 2 files changed, 50 insertions(+), 16 deletions(-) diff --git a/lib/vagrant-proxyconf/plugin.rb b/lib/vagrant-proxyconf/plugin.rb index afb5783..8f50848 100644 --- a/lib/vagrant-proxyconf/plugin.rb +++ b/lib/vagrant-proxyconf/plugin.rb @@ -1,4 +1,5 @@ require 'vagrant' +require_relative 'logger' module VagrantPlugins module ProxyConf @@ -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 diff --git a/spec/unit/vagrant-proxyconf/plugin_spec.rb b/spec/unit/vagrant-proxyconf/plugin_spec.rb index d502040..d6e8d86 100644 --- a/spec/unit/vagrant-proxyconf/plugin_spec.rb +++ b/spec/unit/vagrant-proxyconf/plugin_spec.rb @@ -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