Fix format_windows_path

Should return a unix style path.
This commit is contained in:
Zhongcheng Lao 2019-06-21 13:56:12 +08:00
parent 4ce8d84f7e
commit fb2ea02d7f
2 changed files with 44 additions and 5 deletions

View File

@ -341,6 +341,25 @@ module Vagrant
wsl? && !path.to_s.downcase.start_with?("/mnt/")
end
# This takes any path and converts it from a Windows path to a
# WSL style path.
#
# @param [String path] Windows style path
# @return [String] WSL style path
def windows_to_wsl_path(path)
if wsl?
cmd = ["wslpath", "-u", "-a", path.to_s]
else
cmd = ["wsl", "--", "/bin/wslpath", "-u", "-a", path.gsub("\\", "/")]
end
begin
process = Subprocess.execute(*cmd)
return process.stdout.chomp
rescue Errors::CommandUnavailableWindows
end
end
# Compute the path to rootfs of currently active WSL.
#
# @return [String] A path to rootfs of a current WSL instance.
@ -473,11 +492,7 @@ module Vagrant
def format_windows_path(path, *args)
path = cygwin_path(path) if cygwin?
path = msys_path(path) if msys?
path = wsl_to_windows_path(path) if wsl?
if windows? || wsl?
path = windows_unc_path(path) if !args.include?(:disable_unc)
end
path = windows_to_wsl_path(path) if wsl?
path
end

View File

@ -534,4 +534,28 @@ EOF
end
end
end
describe "#windows_to_wsl_path" do
let(:path) { 'c:\my_test_path' }
let(:wsl_path) { "/mnt/c/expected_path" }
let(:process_result) { double("process", stdout: wsl_path)}
before do
allow(Vagrant::Util::Subprocess).to receive(:execute).and_return(process_result)
end
after { expect(subject.windows_to_wsl_path(path)).to eq(wsl_path) }
it "invokes wslpath directly" do
allow(subject).to receive(:wsl?).and_return(true)
expect(Vagrant::Util::Subprocess).to receive(:execute).with("wslpath", "-u", "-a", path)
end
it "invokes wslpath through wsl.exe" do
allow(subject).to receive(:wsl?).and_return(false)
expect(Vagrant::Util::Subprocess).to receive(:execute).
with("wsl", "--", "/bin/wslpath", "-u", "-a", "c:/my_test_path")
end
end
end