Merge pull request #10332 from chrisroberts/e-hyperv-pwsh-vr

Prevent exception from raising on hyper-v check
This commit is contained in:
Chris Roberts 2018-10-25 09:01:26 -07:00 committed by GitHub
commit 58e6cbb01d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 2 deletions

View File

@ -13,6 +13,14 @@ module Vagrant
# This class just contains some platform checking code.
class Platform
class << self
def logger
if !defined?(@_logger)
@_logger = Log4r::Logger.new("vagrant::util::platform")
end
@_logger
end
def cygwin?
if !defined?(@_cygwin)
@_cygwin = ENV["VAGRANT_DETECTED_OS"].to_s.downcase.include?("cygwin") ||
@ -134,8 +142,13 @@ module Vagrant
@_windows_hyperv_enabled = -> {
["Get-WindowsOptionalFeature", "Get-WindowsFeature"].each do |cmd_name|
ps_cmd = "$(#{cmd_name} -FeatureName Microsoft-Hyper-V-Hypervisor).State"
output = Vagrant::Util::PowerShell.execute_cmd(ps_cmd)
return true if output == "Enabled"
begin
output = Vagrant::Util::PowerShell.execute_cmd(ps_cmd)
return true if output == "Enabled"
rescue Errors::PowerShellInvalidVersion
logger.warn("Invalid PowerShell version detected during Hyper-V enable check")
return false
end
end
return false
}.call

View File

@ -262,6 +262,13 @@ describe Vagrant::Util::Platform do
expect(Vagrant::Util::Platform.windows_hyperv_enabled?).to be_falsey
end
it "should return false if PowerShell cannot be validated" do
allow_any_instance_of(Vagrant::Errors::PowerShellInvalidVersion).to receive(:translate_error)
allow(Vagrant::Util::PowerShell).to receive(:execute_cmd).and_raise(Vagrant::Errors::PowerShellInvalidVersion)
expect(Vagrant::Util::Platform.windows_hyperv_enabled?).to be_falsey
end
end
context "within the WSL" do