diff --git a/lib/vagrant/errors.rb b/lib/vagrant/errors.rb index 6d27f3d5d..2ac521a1b 100644 --- a/lib/vagrant/errors.rb +++ b/lib/vagrant/errors.rb @@ -356,6 +356,10 @@ module Vagrant error_key(:darwin_mount_failed) end + class DarwinVersionFailed < VagrantError + error_key(:darwin_version_failed) + end + class DestroyRequiresForce < VagrantError error_key(:destroy_requires_force) end diff --git a/plugins/hosts/darwin/cap/version.rb b/plugins/hosts/darwin/cap/version.rb new file mode 100644 index 000000000..0b655294a --- /dev/null +++ b/plugins/hosts/darwin/cap/version.rb @@ -0,0 +1,23 @@ +module VagrantPlugins + module HostDarwin + module Cap + class Version + def self.version(env) + r = Vagrant::Util::Subprocess.execute("sw_vers", "-productVersion") + if r.exit_code != 0 + raise Vagrant::Errors::DarwinVersionFailed, + version: r.stdout, + error: r.stderr + end + begin + Gem::Version.new(r.stdout) + rescue => err + raise Vagrant::Errors::DarwinVersionFailed, + version: r.stdout, + error: err.message + end + end + end + end + end +end diff --git a/plugins/hosts/darwin/plugin.rb b/plugins/hosts/darwin/plugin.rb index f3a7e6a55..f8d57cfdf 100644 --- a/plugins/hosts/darwin/plugin.rb +++ b/plugins/hosts/darwin/plugin.rb @@ -70,6 +70,11 @@ module VagrantPlugins require_relative "cap/nfs" Cap::NFS end + + host_capability("darwin", "version") do + require_relative "cap/version" + Cap::Version + end end end end diff --git a/test/unit/plugins/hosts/darwin/cap/version_test.rb b/test/unit/plugins/hosts/darwin/cap/version_test.rb new file mode 100644 index 000000000..d961efb59 --- /dev/null +++ b/test/unit/plugins/hosts/darwin/cap/version_test.rb @@ -0,0 +1,47 @@ +require_relative "../../../../base" +require_relative "../../../../../../plugins/hosts/darwin/cap/version" + +describe VagrantPlugins::HostDarwin::Cap::Version do + describe ".version" do + let(:product_version) { "10.5.1" } + let(:env) { double(:env) } + let(:exit_code) { 0 } + let(:stderr) { "" } + let(:stdout) { product_version } + let(:result) { + Vagrant::Util::Subprocess::Result.new(exit_code, stdout, stderr) + } + + before do + allow(Vagrant::Util::Subprocess).to receive(:execute). + with("sw_vers", "-productVersion"). + and_return(result) + end + + it "should return a Gem::Version" do + expect(described_class.version(env)).to be_a(Gem::Version) + end + + it "should equal the defined version" do + expect(described_class.version(env)).to eq(Gem::Version.new(product_version)) + end + + context "when version cannot be parsed" do + let(:product_version) { "invalid" } + + it "should raise a failure error" do + expect { described_class.version(env) }. + to raise_error(Vagrant::Errors::DarwinVersionFailed) + end + end + + context "when command execution fails" do + let(:exit_code) { 1 } + + it "should raise a failure error" do + expect { described_class.version(env) }. + to raise_error(Vagrant::Errors::DarwinVersionFailed) + end + end + end +end