From ada1d582f7235a59e70b99a04a05ea6f2c30677c Mon Sep 17 00:00:00 2001 From: Chris Roberts Date: Mon, 8 Nov 2021 11:29:48 -0800 Subject: [PATCH] Update test to use full environment and host instances --- .../plugins/hosts/darwin/cap/path_test.rb | 76 ++++++++++++++----- 1 file changed, 57 insertions(+), 19 deletions(-) diff --git a/test/unit/plugins/hosts/darwin/cap/path_test.rb b/test/unit/plugins/hosts/darwin/cap/path_test.rb index 9e1adb8a6..95de29f34 100644 --- a/test/unit/plugins/hosts/darwin/cap/path_test.rb +++ b/test/unit/plugins/hosts/darwin/cap/path_test.rb @@ -3,32 +3,66 @@ require_relative "../../../../../../plugins/hosts/darwin/cap/path" require_relative "../../../../../../plugins/hosts/darwin/cap/version" describe VagrantPlugins::HostDarwin::Cap::Path do + include_context "unit" + + let(:caps) do + VagrantPlugins::HostDarwin::Plugin + .components + .host_capabilities[:darwin] + end + + let(:host) { @host } + let(:hosts) { + { + darwin: [VagrantPlugins::HostDarwin::Host, :bsd] + } + } + + let(:env) do + ienv = isolated_environment + ienv.vagrantfile("") + env = ienv.create_vagrant_env + @host = Vagrant::Host.new( + :darwin, + hosts, + {darwin: caps}, + env + ) + env.instance_variable_set(:@host, @host) + env + end + describe ".resolve_host_path" do - let(:env) { double("environment") } - let(:host) { double("host") } + let(:cap) { caps.get(:resolve_host_path) } let(:path) { "/test/vagrant/path" } let(:firmlink_map) { {} } let(:macos_version) { Gem::Version.new("10.15.1") } + let(:result) { + Vagrant::Util::Subprocess::Result.new(0, macos_version, "") + } before do - allow(env).to receive(:host). - and_return(host) - allow(host).to receive(:capability). - with(:version). - and_return(macos_version) + # allow(env).to receive(:host). + # and_return(host) + # allow(host).to receive(:capability). + # with(:version). + # and_return(macos_version) + allow(Vagrant::Util::Subprocess).to receive(:execute). + with("sw_vers", "-productVersion"). + and_return(result) allow(described_class).to receive(:firmlink_map). and_return(firmlink_map) end it "should not change the path when no firmlinks are defined" do - expect(described_class.resolve_host_path(env, path)).to eq(path) + expect(cap.resolve_host_path(env, path)).to eq(path) end context "when firmlink map contains non-matching values" do let(:firmlink_map) { {"/users" => "users", "/system" => "system"} } it "should not change the path" do - expect(described_class.resolve_host_path(env, path)).to eq(path) + expect(cap.resolve_host_path(env, path)).to eq(path) end end @@ -36,11 +70,11 @@ describe VagrantPlugins::HostDarwin::Cap::Path do let(:firmlink_map) { {"/users" => "users", "/test" => "test"} } it "should update the path" do - expect(described_class.resolve_host_path(env, path)).not_to eq(path) + expect(cap.resolve_host_path(env, path)).not_to eq(path) end it "should prefix the path with the defined data path" do - expect(described_class.resolve_host_path(env, path)).to start_with(described_class.const_get(:FIRMLINK_DATA_PATH)) + expect(cap.resolve_host_path(env, path)).to start_with(described_class.const_get(:FIRMLINK_DATA_PATH)) end end @@ -48,15 +82,16 @@ describe VagrantPlugins::HostDarwin::Cap::Path do let(:firmlink_map) { {"/users" => "users", "/test" => "other"} } it "should update the path" do - expect(described_class.resolve_host_path(env, path)).not_to eq(path) + expect(cap.resolve_host_path(env, path)).not_to eq(path) end it "should prefix the path with the defined data path" do - expect(described_class.resolve_host_path(env, path)).to start_with(described_class.const_get(:FIRMLINK_DATA_PATH)) + expect(cap.resolve_host_path(env, path)). + to start_with(described_class.const_get(:FIRMLINK_DATA_PATH)) end it "should include the updated path name" do - expect(described_class.resolve_host_path(env, path)).to include("other") + expect(cap.resolve_host_path(env, path)).to include("other") end end @@ -64,20 +99,23 @@ describe VagrantPlugins::HostDarwin::Cap::Path do let(:macos_version) { Gem::Version.new("10.16.1") } it "should not update the path" do - expect(described_class.resolve_host_path(env, path)).to eq(path) + expect(cap.resolve_host_path(env, path)).to eq(path) end it "should not prefix the path with the defined data path" do - expect(described_class.resolve_host_path(env, path)).not_to start_with(described_class.const_get(:FIRMLINK_DATA_PATH)) + expect(cap.resolve_host_path(env, path)). + not_to start_with(described_class.const_get(:FIRMLINK_DATA_PATH)) end end end describe ".firmlink_map" do + let(:cap) { caps.get(:firmlink_map) } before { described_class.reset! } context "when firmlink definition file does not exist" do - before { expect(File).to receive(:exist?).with(described_class.const_get(:FIRMLINK_DEFS)).and_return(false) } + before { expect(File).to receive(:exist?). + with(described_class.const_get(:FIRMLINK_DEFS)).and_return(false) } it "should return an empty hash" do expect(described_class.firmlink_map).to eq({}) @@ -102,9 +140,9 @@ describe VagrantPlugins::HostDarwin::Cap::Path do end it "should only load values once" do - result = describe_class.firmlink_app + describe_class.firmlink_app expect(File).not_to receive(:readlines) - result = describe_class.firmlink_app + describe_class.firmlink_app end end end