diff --git a/lib/vagrant/guest.rb b/lib/vagrant/guest.rb index f2667caa6..c9d1e9af1 100644 --- a/lib/vagrant/guest.rb +++ b/lib/vagrant/guest.rb @@ -38,6 +38,19 @@ module Vagrant raise Errors::GuestNotDetected end + # See {CapabilityHost#capability} + def capability(*args) + super + rescue Errors::CapabilityNotFound => e + raise Errors::GuestCapabilityNotFound, + cap: e.extra_data[:cap], + guest: name + rescue Errors::CapabilityInvalid => e + raise Errors::GuestCapabilityInvalid, + cap: e.extra_data[:cap], + guest: name + end + # Returns the specified or detected guest type name. # # @return [Symbol] diff --git a/test/unit/vagrant/guest_test.rb b/test/unit/vagrant/guest_test.rb index 7c9d11373..b41f584a2 100644 --- a/test/unit/vagrant/guest_test.rb +++ b/test/unit/vagrant/guest_test.rb @@ -18,6 +18,33 @@ describe Vagrant::Guest do subject { described_class.new(machine, guests, capabilities) } + describe "#capability" do + before(:each) do + guests[:foo] = [detect_class(true), nil] + capabilities[:foo] = { + foo_cap: cap_instance(:foo_cap), + corrupt_cap: cap_instance(:corrupt_cap, corrupt: true), + } + + subject.detect! + end + + it "executes existing capabilities" do + expect { subject.capability(:foo_cap) }. + to raise_error(RuntimeError, "cap: foo_cap [machine]") + end + + it "raises user-friendly error for non-existing caps" do + expect { subject.capability(:what_cap) }. + to raise_error(Vagrant::Errors::GuestCapabilityNotFound) + end + + it "raises a user-friendly error for corrupt caps" do + expect { subject.capability(:corrupt_cap) }. + to raise_error(Vagrant::Errors::GuestCapabilityInvalid) + end + end + describe "#detect!" do it "auto-detects if no explicit guest name given" do machine.config.vm.stub(guest: nil)