From 1b6538354d8689e7c04afec8631fcb4ac10ab904 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Fri, 22 Feb 2013 15:10:34 -0800 Subject: [PATCH] Be smarter about determining if plugin load failed --- lib/vagrant.rb | 13 +++++++++++-- test/unit/vagrant_test.rb | 13 +++++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/lib/vagrant.rb b/lib/vagrant.rb index e0cc788ff..c505d9e34 100644 --- a/lib/vagrant.rb +++ b/lib/vagrant.rb @@ -169,9 +169,18 @@ module Vagrant # Attempt the normal require begin require name - rescue LoadError - raise Errors::PluginLoadError, :plugin => name rescue Exception => e + # If it is a LoadError we first try to see if it failed loading + # the top-level entrypoint. If so, then we report a different error. + if e.is_a?(LoadError) + # Parse the message in order to get what failed to load, and + # add some extra protection around if the message is different. + parts = e.to_s.split(" -- ", 2) + if parts.length == 2 && parts[1] == name + raise Errors::PluginLoadError, :plugin => name + end + end + # Since this is a rare case, we create a one-time logger here # in order to output the error logger = Log4r::Logger.new("vagrant::root") diff --git a/test/unit/vagrant_test.rb b/test/unit/vagrant_test.rb index 5c61a3af2..d92a232d1 100644 --- a/test/unit/vagrant_test.rb +++ b/test/unit/vagrant_test.rb @@ -1,6 +1,8 @@ require File.expand_path("../base", __FILE__) describe Vagrant do + include_context "unit" + it "has the path to the source root" do described_class.source_root.should == Pathname.new(File.expand_path("../../../", __FILE__)) end @@ -39,5 +41,16 @@ describe Vagrant do expect { described_class.require_plugin("i_dont_exist") }. to raise_error(Vagrant::Errors::PluginLoadError) end + + it "should raise an error if the loading failed in some other way" do + plugin_dir = temporary_dir + plugin_path = plugin_dir.join("test.rb") + plugin_path.open("w") do |f| + f.write(%Q[require 'I_dont_exist']) + end + + expect { described_class.require_plugin(plugin_path.to_s) }. + to raise_error(Vagrant::Errors::PluginLoadFailed) + end end end