Add missing test coverage for powershell util methods
This commit is contained in:
parent
5f4e661155
commit
1be87662da
|
@ -4,6 +4,9 @@ require 'vagrant/util/powershell'
|
||||||
|
|
||||||
describe Vagrant::Util::PowerShell do
|
describe Vagrant::Util::PowerShell do
|
||||||
include_context "unit"
|
include_context "unit"
|
||||||
|
|
||||||
|
after{ described_class.reset! }
|
||||||
|
|
||||||
describe ".version" do
|
describe ".version" do
|
||||||
before do
|
before do
|
||||||
allow(described_class).to receive(:executable)
|
allow(described_class).to receive(:executable)
|
||||||
|
@ -13,7 +16,6 @@ describe Vagrant::Util::PowerShell do
|
||||||
|
|
||||||
after do
|
after do
|
||||||
described_class.version
|
described_class.version
|
||||||
described_class.reset!
|
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should execute powershell command" do
|
it "should execute powershell command" do
|
||||||
|
@ -41,4 +43,246 @@ describe Vagrant::Util::PowerShell do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe ".executable" do
|
||||||
|
before{ allow(Vagrant::Util::Platform).to receive(:wsl?).and_return(false) }
|
||||||
|
|
||||||
|
context "when found in PATH" do
|
||||||
|
before{ expect(Vagrant::Util::Which).to receive(:which).with("powershell").and_return(true) }
|
||||||
|
|
||||||
|
it "should return powershell string" do
|
||||||
|
expect(described_class.executable).to eq("powershell")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context "when not found in PATH" do
|
||||||
|
before{ expect(Vagrant::Util::Which).to receive(:which).with("powershell").and_return(nil) }
|
||||||
|
|
||||||
|
it "should return nil" do
|
||||||
|
expect(described_class.executable).to be_nil
|
||||||
|
end
|
||||||
|
|
||||||
|
context "when within WSL" do
|
||||||
|
before{ expect(Vagrant::Util::Platform).to receive(:wsl?).and_return(true) }
|
||||||
|
|
||||||
|
it "should check PATH with .exe extension" do
|
||||||
|
expect(Vagrant::Util::Which).to receive(:which).with("powershell.exe")
|
||||||
|
described_class.executable
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should return powershell.exe when found" do
|
||||||
|
expect(Vagrant::Util::Which).to receive(:which).with("powershell.exe").and_return(true)
|
||||||
|
expect(described_class.executable).to eq("powershell.exe")
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should return nil when not found" do
|
||||||
|
expect(Vagrant::Util::Which).to receive(:which).with("powershell.exe").and_return(nil)
|
||||||
|
expect(described_class.executable).to be_nil
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe ".available?" do
|
||||||
|
context "when powershell executable is available" do
|
||||||
|
before{ expect(described_class).to receive(:executable).and_return("powershell") }
|
||||||
|
|
||||||
|
it "should be true" do
|
||||||
|
expect(described_class.available?).to be(true)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context "when powershell executable is not available" do
|
||||||
|
before{ expect(described_class).to receive(:executable).and_return(nil) }
|
||||||
|
|
||||||
|
it "should be false" do
|
||||||
|
expect(described_class.available?).to be(false)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe ".execute" do
|
||||||
|
before do
|
||||||
|
allow(described_class).to receive(:validate_install!)
|
||||||
|
allow(Vagrant::Util::Subprocess).to receive(:execute)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should validate installation before use" do
|
||||||
|
expect(described_class).to receive(:validate_install!)
|
||||||
|
described_class.execute("command")
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should include command to execute" do
|
||||||
|
expect(Vagrant::Util::Subprocess).to receive(:execute) do |*args|
|
||||||
|
comm = args.detect{|s| s.include?("custom-command") }
|
||||||
|
expect(comm.to_s).to include("custom-command")
|
||||||
|
end
|
||||||
|
described_class.execute("custom-command")
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should automatically include console resize" do
|
||||||
|
expect(Vagrant::Util::Subprocess).to receive(:execute) do |*args|
|
||||||
|
comm = args.detect{|s| s.include?("custom-command") }
|
||||||
|
expect(comm.to_s).to include("BufferSize")
|
||||||
|
end
|
||||||
|
described_class.execute("custom-command")
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should accept custom environment" do
|
||||||
|
expect(Vagrant::Util::Subprocess).to receive(:execute) do |*args|
|
||||||
|
comm = args.detect{|s| s.include?("custom-command") }
|
||||||
|
expect(comm.to_s).to include("$env:TEST_KEY=test-value")
|
||||||
|
end
|
||||||
|
described_class.execute("custom-command", env: {"TEST_KEY" => "test-value"})
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe ".execute_cmd" do
|
||||||
|
let(:result) do
|
||||||
|
Vagrant::Util::Subprocess::Result.new(
|
||||||
|
exit_code, stdout, stderr)
|
||||||
|
end
|
||||||
|
let(:exit_code){ 0 }
|
||||||
|
let(:stdout){ "" }
|
||||||
|
let(:stderr){ "" }
|
||||||
|
|
||||||
|
before do
|
||||||
|
allow(described_class).to receive(:validate_install!)
|
||||||
|
allow(Vagrant::Util::Subprocess).to receive(:execute).and_return(result)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should validate installation before use" do
|
||||||
|
expect(described_class).to receive(:validate_install!)
|
||||||
|
described_class.execute_cmd("command")
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should include command to execute" do
|
||||||
|
expect(Vagrant::Util::Subprocess).to receive(:execute) do |*args|
|
||||||
|
comm = args.detect{|s| s.include?("custom-command") }
|
||||||
|
expect(comm.to_s).to include("custom-command")
|
||||||
|
result
|
||||||
|
end
|
||||||
|
described_class.execute_cmd("custom-command")
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should automatically include console resize" do
|
||||||
|
expect(Vagrant::Util::Subprocess).to receive(:execute) do |*args|
|
||||||
|
comm = args.detect{|s| s.include?("custom-command") }
|
||||||
|
expect(comm.to_s).to include("BufferSize")
|
||||||
|
result
|
||||||
|
end
|
||||||
|
described_class.execute_cmd("custom-command")
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should accept custom environment" do
|
||||||
|
expect(Vagrant::Util::Subprocess).to receive(:execute) do |*args|
|
||||||
|
comm = args.detect{|s| s.include?("custom-command") }
|
||||||
|
expect(comm.to_s).to include("$env:TEST_KEY=test-value")
|
||||||
|
result
|
||||||
|
end
|
||||||
|
described_class.execute_cmd("custom-command", env: {"TEST_KEY" => "test-value"})
|
||||||
|
end
|
||||||
|
|
||||||
|
context "with command output" do
|
||||||
|
let(:stdout){ "custom-output" }
|
||||||
|
|
||||||
|
it "should return stdout" do
|
||||||
|
expect(described_class.execute_cmd("cmd")).to eq(stdout)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context "with failed command" do
|
||||||
|
let(:exit_code){ 1 }
|
||||||
|
|
||||||
|
it "should return nil" do
|
||||||
|
expect(described_class.execute_cmd("cmd")).to be_nil
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe ".execute_inline" do
|
||||||
|
let(:result) do
|
||||||
|
Vagrant::Util::Subprocess::Result.new(
|
||||||
|
exit_code, stdout, stderr)
|
||||||
|
end
|
||||||
|
let(:exit_code){ 0 }
|
||||||
|
let(:stdout){ "" }
|
||||||
|
let(:stderr){ "" }
|
||||||
|
|
||||||
|
before do
|
||||||
|
allow(described_class).to receive(:validate_install!)
|
||||||
|
allow(Vagrant::Util::Subprocess).to receive(:execute).and_return(result)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should validate installation before use" do
|
||||||
|
expect(described_class).to receive(:validate_install!)
|
||||||
|
described_class.execute_inline("command")
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should include command to execute" do
|
||||||
|
expect(Vagrant::Util::Subprocess).to receive(:execute) do |*args|
|
||||||
|
comm = args.detect{|s| s.include?("custom-command") }
|
||||||
|
expect(comm.to_s).to include("custom-command")
|
||||||
|
result
|
||||||
|
end
|
||||||
|
described_class.execute_inline("custom-command")
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should automatically include console resize" do
|
||||||
|
expect(Vagrant::Util::Subprocess).to receive(:execute) do |*args|
|
||||||
|
comm = args.detect{|s| s.include?("custom-command") }
|
||||||
|
expect(comm.to_s).to include("BufferSize")
|
||||||
|
result
|
||||||
|
end
|
||||||
|
described_class.execute_inline("custom-command")
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should accept custom environment" do
|
||||||
|
expect(Vagrant::Util::Subprocess).to receive(:execute) do |*args|
|
||||||
|
comm = args.detect{|s| s.include?("custom-command") }
|
||||||
|
expect(comm.to_s).to include("$env:TEST_KEY=test-value")
|
||||||
|
result
|
||||||
|
end
|
||||||
|
described_class.execute_inline("custom-command", env: {"TEST_KEY" => "test-value"})
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should return a result instance" do
|
||||||
|
expect(described_class.execute_inline("cmd")).to eq(result)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe ".validate_install!" do
|
||||||
|
before do
|
||||||
|
allow(described_class).to receive(:available?).and_return(true)
|
||||||
|
end
|
||||||
|
|
||||||
|
context "with version under minimum required" do
|
||||||
|
before{ expect(described_class).to receive(:version).and_return("2.1").at_least(:once) }
|
||||||
|
|
||||||
|
it "should raise an error" do
|
||||||
|
expect{ described_class.validate_install! }.to raise_error(Vagrant::Errors::PowerShellInvalidVersion)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context "with version above minimum required" do
|
||||||
|
before{ expect(described_class).to receive(:version).and_return("3.1").at_least(:once) }
|
||||||
|
|
||||||
|
it "should return true" do
|
||||||
|
expect(described_class.validate_install!).to be(true)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
describe ".resize_console" do
|
||||||
|
it "should return command string" do
|
||||||
|
expect(described_class.resize_console).to include("BufferSize")
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should return empty string when disabled" do
|
||||||
|
with_temp_env("VAGRANT_POWERSHELL_RESIZE_DISABLE" => "1") do
|
||||||
|
expect(described_class.resize_console).to be_empty
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue