Prevent exception from raising on hyper-v check

This commit is contained in:
Chris Roberts 2018-10-24 17:05:20 -07:00
parent ed310a954b
commit ca81f9d6cb
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. # This class just contains some platform checking code.
class Platform class Platform
class << self class << self
def logger
if !defined?(@_logger)
@_logger = Log4r::Logger.new("vagrant::util::platform")
end
@_logger
end
def cygwin? def cygwin?
if !defined?(@_cygwin) if !defined?(@_cygwin)
@_cygwin = ENV["VAGRANT_DETECTED_OS"].to_s.downcase.include?("cygwin") || @_cygwin = ENV["VAGRANT_DETECTED_OS"].to_s.downcase.include?("cygwin") ||
@ -134,8 +142,13 @@ module Vagrant
@_windows_hyperv_enabled = -> { @_windows_hyperv_enabled = -> {
["Get-WindowsOptionalFeature", "Get-WindowsFeature"].each do |cmd_name| ["Get-WindowsOptionalFeature", "Get-WindowsFeature"].each do |cmd_name|
ps_cmd = "$(#{cmd_name} -FeatureName Microsoft-Hyper-V-Hypervisor).State" ps_cmd = "$(#{cmd_name} -FeatureName Microsoft-Hyper-V-Hypervisor).State"
output = Vagrant::Util::PowerShell.execute_cmd(ps_cmd) begin
return true if output == "Enabled" 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 end
return false return false
}.call }.call

View File

@ -262,6 +262,13 @@ describe Vagrant::Util::Platform do
expect(Vagrant::Util::Platform.windows_hyperv_enabled?).to be_falsey expect(Vagrant::Util::Platform.windows_hyperv_enabled?).to be_falsey
end 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 end
context "within the WSL" do context "within the WSL" do