Merge pull request #10574 from ngyuki/wslpath

Use wslpath command for customized root on WSL
This commit is contained in:
Chris Roberts 2019-01-09 10:45:36 -08:00 committed by GitHub
commit 88793bc706
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 99 additions and 0 deletions

View File

@ -366,6 +366,12 @@ module Vagrant
# Lowercase the drive letter, skip the next symbol (which is a
# colon from a Windows path) and convert path to UNIX style.
check_path = "/mnt/#{path[0, 1].downcase}#{path[2..-1].tr('\\', '/')}/rootfs"
begin
process = Subprocess.execute("wslpath", "-u", "-a", path)
check_path = "#{process.stdout.chomp}/rootfs" if process.exit_code == 0
rescue Errors::CommandUnavailable => e
# pass
end
logger.debug("checking `#{path}` for current WSL instance")
begin
@ -432,6 +438,12 @@ module Vagrant
path = path.to_s
if wsl? && wsl_windows_access? && !path.match(/^[a-zA-Z]:/)
path = File.expand_path(path)
begin
process = Subprocess.execute("wslpath", "-w", "-a", path)
return process.stdout.chomp if process.exit_code == 0
rescue Errors::CommandUnavailable => e
# pass
end
if wsl_path?(path)
parts = path.split("/")
parts.delete_if(&:empty?)
@ -502,6 +514,14 @@ module Vagrant
def wsl_windows_accessible_path
if !defined?(@_wsl_windows_accessible_path)
access_path = ENV["VAGRANT_WSL_WINDOWS_ACCESS_USER_HOME_PATH"]
if access_path.to_s.empty?
begin
process = Subprocess.execute("wslpath", "-u", "-a", wsl_windows_home)
access_path = process.stdout.chomp if process.exit_code == 0
rescue Errors::CommandUnavailable => e
# pass
end
end
if access_path.to_s.empty?
access_path = wsl_windows_home.gsub("\\", "/").sub(":", "")
access_path[0] = access_path[0].downcase

View File

@ -344,6 +344,33 @@ describe Vagrant::Util::Platform do
end
end
end
context "when wslpath command success" do
it "should check path returned by command" do
expect(Vagrant::Util::Subprocess).to receive(:execute).and_return(double("process", exit_code: 0, stdout: "/c/Custom/Path"))
expect(Dir).to receive(:open).with(/^\/c\/Custom\/Path\//).and_yield(double("path", path: appdata_path))
expect(File).to receive(:exist?).and_return(true)
expect(subject.wsl_rootfs).to include(appdata_path)
end
end
context "when wslpath command failed" do
it "should check fallback path" do
expect(Vagrant::Util::Subprocess).to receive(:execute).and_return(double("process", exit_code: 1))
expect(Dir).to receive(:open).with(/\/mnt\//).and_yield(double("path", path: appdata_path))
expect(File).to receive(:exist?).and_return(true)
expect(subject.wsl_rootfs).to include(appdata_path)
end
end
context "when wslpath command raise error" do
it "should check fallback path" do
expect(Vagrant::Util::Subprocess).to receive(:execute).and_raise(Vagrant::Errors::CommandUnavailable, file: "wslpath")
expect(Dir).to receive(:open).with(/\/mnt\//).and_yield(double("path", path: appdata_path))
expect(File).to receive(:exist?).and_return(true)
expect(subject.wsl_rootfs).to include(appdata_path)
end
end
end
describe ".wsl_to_windows_path" do
@ -403,6 +430,58 @@ describe Vagrant::Util::Platform do
expect(subject.wsl_to_windows_path(Pathname.new("/tmp/test"))).to include("rootfs")
end
end
context "when wslpath command success" do
it "should return path returned by command" do
expect(Vagrant::Util::Subprocess).to receive(:execute).and_return(double("process", exit_code: 0, stdout: "C:\\Custom\\Path"))
expect(subject.wsl_to_windows_path(path)).to eq("C:\\Custom\\Path")
end
end
context "when wslpath command failed" do
it "should return path by fallback" do
expect(Vagrant::Util::Subprocess).to receive(:execute).and_return(double("process", exit_code: 1))
expect(subject.wsl_to_windows_path(path)).to eq("#{rootfs_path}#{path.gsub("/", "\\")}")
end
end
context "when wslpath command raise error" do
it "should return path by fallback" do
expect(Vagrant::Util::Subprocess).to receive(:execute).and_raise(Vagrant::Errors::CommandUnavailable, file: "wslpath")
expect(subject.wsl_to_windows_path(path)).to eq("#{rootfs_path}#{path.gsub("/", "\\")}")
end
end
end
end
end
describe ".wsl_windows_accessible_path" do
context "when within WSL" do
before do
allow(subject).to receive(:wsl?).and_return(true)
allow(subject).to receive(:wsl_windows_home).and_return("C:\\Users\\vagrant")
allow(ENV).to receive(:[]).with("VAGRANT_WSL_WINDOWS_ACCESS_USER_HOME_PATH").and_return(nil)
end
context "when wslpath command success" do
it "should return path returned by command" do
expect(Vagrant::Util::Subprocess).to receive(:execute).and_return(double("process", exit_code: 0, stdout: "/d/vagrant"))
expect(subject.wsl_windows_accessible_path.to_s).to eq("/d/vagrant")
end
end
context "when wslpath command failed" do
it "should return path by fallback" do
expect(Vagrant::Util::Subprocess).to receive(:execute).and_return(double("process", exit_code: 1))
expect(subject.wsl_windows_accessible_path.to_s).to eq("/mnt/c/Users/vagrant")
end
end
context "when wslpath command raise error" do
it "should return path by fallback" do
expect(Vagrant::Util::Subprocess).to receive(:execute).and_raise(Vagrant::Errors::CommandUnavailable, file: "wslpath")
expect(subject.wsl_windows_accessible_path.to_s).to eq("/mnt/c/Users/vagrant")
end
end
end
end