diff --git a/lib/vagrant/util/platform.rb b/lib/vagrant/util/platform.rb index 8eeecfbac..4cd8b2e7f 100644 --- a/lib/vagrant/util/platform.rb +++ b/lib/vagrant/util/platform.rb @@ -447,18 +447,20 @@ module Vagrant # are the same. Raise error if they do not match. def wsl_validate_matching_vagrant_versions! valid = false - result = Util::Subprocess.execute("vagrant.exe", "version") - if result.exit_code == 0 - windows_version = result.stdout.match(/Installed Version: (?[\w.-]+)/) - if windows_version - windows_version = windows_version[:version].strip - valid = windows_version == Vagrant::VERSION + if Util::Which.which("vagrant.exe") + result = Util::Subprocess.execute("vagrant.exe", "version") + if result.exit_code == 0 + windows_version = result.stdout.match(/Installed Version: (?[\w.-]+)/) + if windows_version + windows_version = windows_version[:version].strip + valid = windows_version == Vagrant::VERSION + end + end + if !valid + raise Vagrant::Errors::WSLVagrantVersionMismatch, + wsl_version: Vagrant::VERSION, + windows_version: windows_version || "unknown" end - end - if !valid - raise Vagrant::Errors::WSLVagrantVersionMismatch, - wsl_version: Vagrant::VERSION, - windows_version: windows_version || "unknown" end end diff --git a/test/unit/vagrant/util/platform_test.rb b/test/unit/vagrant/util/platform_test.rb index 88171ddae..6084a424a 100644 --- a/test/unit/vagrant/util/platform_test.rb +++ b/test/unit/vagrant/util/platform_test.rb @@ -182,4 +182,36 @@ describe Vagrant::Util::Platform do expect(subject.systemd?).to be_falsey end end + + describe ".wsl_validate_matching_vagrant_versions!" do + let(:exe_version){ Vagrant::VERSION.to_s } + + before do + allow(Vagrant::Util::Which).to receive(:which).and_return(true) + allow(Vagrant::Util::Subprocess).to receive(:execute).with("vagrant.exe", "version"). + and_return(double(exit_code: 0, stdout: "Installed Version: #{exe_version}")) + end + + it "should not raise an error" do + Vagrant::Util::Platform.wsl_validate_matching_vagrant_versions! + end + + context "when windows vagrant.exe is not installed" do + before{ expect(Vagrant::Util::Which).to receive(:which).with("vagrant.exe").and_return(nil) } + + it "should not raise an error" do + Vagrant::Util::Platform.wsl_validate_matching_vagrant_versions! + end + end + + context "when versions do not match" do + let(:exe_version){ "1.9.9" } + + it "should raise an error" do + expect { + Vagrant::Util::Platform.wsl_validate_matching_vagrant_versions! + }.to raise_error(Vagrant::Errors::WSLVagrantVersionMismatch) + end + end + end end