Add option to Which utility for using original path on lookup
This commit is contained in:
parent
343e252574
commit
6f6e936451
|
@ -11,8 +11,10 @@ module Vagrant
|
||||||
# http://stackoverflow.com/questions/2108727/which-in-ruby-checking-if-program-exists-in-path-from-ruby
|
# http://stackoverflow.com/questions/2108727/which-in-ruby-checking-if-program-exists-in-path-from-ruby
|
||||||
#
|
#
|
||||||
# @param [String] cmd The command to search for in the PATH.
|
# @param [String] cmd The command to search for in the PATH.
|
||||||
|
# @param [Hash] opts Optional flags
|
||||||
|
# @option [Boolean] :original_path Search within original path if available
|
||||||
# @return [String] The full path to the executable or `nil` if not found.
|
# @return [String] The full path to the executable or `nil` if not found.
|
||||||
def self.which(cmd)
|
def self.which(cmd, **opts)
|
||||||
exts = nil
|
exts = nil
|
||||||
|
|
||||||
if !Platform.windows? || ENV['PATHEXT'].nil?
|
if !Platform.windows? || ENV['PATHEXT'].nil?
|
||||||
|
@ -29,8 +31,14 @@ module Vagrant
|
||||||
exts = ENV['PATHEXT'].split(';')
|
exts = ENV['PATHEXT'].split(';')
|
||||||
end
|
end
|
||||||
|
|
||||||
|
if opts[:original_path]
|
||||||
|
search_path = ENV.fetch('VAGRANT_OLD_ENV_PATH', ENV['PATH'])
|
||||||
|
else
|
||||||
|
search_path = ENV['PATH']
|
||||||
|
end
|
||||||
|
|
||||||
SilenceWarnings.silence! do
|
SilenceWarnings.silence! do
|
||||||
ENV['PATH'].encode('UTF-8', 'binary', invalid: :replace, undef: :replace, replace: '').split(File::PATH_SEPARATOR).each do |path|
|
search_path.encode('UTF-8', 'binary', invalid: :replace, undef: :replace, replace: '').split(File::PATH_SEPARATOR).each do |path|
|
||||||
exts.each do |ext|
|
exts.each do |ext|
|
||||||
exe = "#{path}#{File::SEPARATOR}#{cmd}#{ext}"
|
exe = "#{path}#{File::SEPARATOR}#{cmd}#{ext}"
|
||||||
return exe if File.executable? exe
|
return exe if File.executable? exe
|
||||||
|
|
|
@ -13,10 +13,8 @@ describe Vagrant::Util::Which do
|
||||||
file.chmod(mode)
|
file.chmod(mode)
|
||||||
|
|
||||||
# set the path to the directory where the file is located
|
# set the path to the directory where the file is located
|
||||||
savepath = ENV['PATH']
|
allow(ENV).to receive(:[]).with("PATH").and_return(dir.to_s)
|
||||||
ENV['PATH'] = dir.to_s
|
|
||||||
block.call filename + test_extension
|
block.call filename + test_extension
|
||||||
ENV['PATH'] = savepath
|
|
||||||
|
|
||||||
file.unlink
|
file.unlink
|
||||||
end
|
end
|
||||||
|
@ -40,4 +38,13 @@ describe Vagrant::Util::Which do
|
||||||
expect(described_class.which(name)).to be_nil
|
expect(described_class.which(name)).to be_nil
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
context "original_path option" do
|
||||||
|
before{ allow(ENV).to receive(:[]).with("PATH").and_return("") }
|
||||||
|
|
||||||
|
it "should use the original path when instructed" do
|
||||||
|
expect(ENV).to receive(:fetch).with("VAGRANT_OLD_ENV_PATH", any_args).and_return("")
|
||||||
|
described_class.which("file", original_path: true)
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue