Add version capability to darwin host plugin

This commit is contained in:
Chris Roberts 2021-11-02 16:43:26 -07:00
parent 1cff8c7495
commit 6810c7b4bb
4 changed files with 79 additions and 0 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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