Check for nil return from forwarded ports cap

Nil may be returned if the forwarded ports cap is called while the
machine is not in a running state.
This commit is contained in:
sophia 2020-08-17 10:13:57 -05:00
parent 2063111ab5
commit 649f69747f
2 changed files with 10 additions and 1 deletions

View File

@ -58,7 +58,7 @@ module VagrantPlugins
# ports.
port = nil
if machine.provider.capability?(:forwarded_ports)
machine.provider.capability(:forwarded_ports).each do |host, guest|
Array(machine.provider.capability(:forwarded_ports)).each do |host, guest|
if guest == machine.config.winrm.guest_port
port = host
break

View File

@ -147,6 +147,15 @@ describe VagrantPlugins::CommunicatorWinRM::Helper do
expect(subject.winrm_port(machine)).to eq(2456)
end
it "does not error when the provider capability returns nil result" do
machine.config.winrm.port = 22
machine.config.winrm.guest_port = 2222
machine.config.vm.network "forwarded_port", host: 1234, guest: 2222
allow(machine.provider).to receive(:capability).with(:forwarded_ports).and_return(nil)
expect(subject.winrm_port(machine)).to eq(1234)
end
end
describe ".winrm_info_invalid?" do