From ca81f9d6cbcc10ed226dd88d100206d5e39a14de Mon Sep 17 00:00:00 2001 From: Chris Roberts Date: Wed, 24 Oct 2018 17:05:20 -0700 Subject: [PATCH] Prevent exception from raising on hyper-v check --- lib/vagrant/util/platform.rb | 17 +++++++++++++++-- test/unit/vagrant/util/platform_test.rb | 7 +++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/lib/vagrant/util/platform.rb b/lib/vagrant/util/platform.rb index b695546fe..891623011 100644 --- a/lib/vagrant/util/platform.rb +++ b/lib/vagrant/util/platform.rb @@ -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 diff --git a/test/unit/vagrant/util/platform_test.rb b/test/unit/vagrant/util/platform_test.rb index 82721b0e1..c4aaa32a5 100644 --- a/test/unit/vagrant/util/platform_test.rb +++ b/test/unit/vagrant/util/platform_test.rb @@ -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