Backport which
from EE
This commit is contained in:
parent
d6435b68c4
commit
ab814e4dd3
2 changed files with 26 additions and 1 deletions
|
@ -46,5 +46,22 @@ module Gitlab
|
||||||
def random_string
|
def random_string
|
||||||
Random.rand(Float::MAX.to_i).to_s(36)
|
Random.rand(Float::MAX.to_i).to_s(36)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# See: http://stackoverflow.com/questions/2108727/which-in-ruby-checking-if-program-exists-in-path-from-ruby
|
||||||
|
# Cross-platform way of finding an executable in the $PATH.
|
||||||
|
#
|
||||||
|
# which('ruby') #=> /usr/bin/ruby
|
||||||
|
def which(cmd, env = ENV)
|
||||||
|
exts = env['PATHEXT'] ? env['PATHEXT'].split(';') : ['']
|
||||||
|
|
||||||
|
env['PATH'].split(File::PATH_SEPARATOR).each do |path|
|
||||||
|
exts.each do |ext|
|
||||||
|
exe = File.join(path, "#{cmd}#{ext}")
|
||||||
|
return exe if File.executable?(exe) && !File.directory?(exe)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
nil
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
require 'spec_helper'
|
require 'spec_helper'
|
||||||
|
|
||||||
describe Gitlab::Utils do
|
describe Gitlab::Utils do
|
||||||
delegate :to_boolean, :boolean_to_yes_no, :slugify, :random_string, to: :described_class
|
delegate :to_boolean, :boolean_to_yes_no, :slugify, :random_string, :which, to: :described_class
|
||||||
|
|
||||||
describe '.slugify' do
|
describe '.slugify' do
|
||||||
{
|
{
|
||||||
|
@ -59,4 +59,12 @@ describe Gitlab::Utils do
|
||||||
expect(random_string).to be_kind_of(String)
|
expect(random_string).to be_kind_of(String)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe '.which' do
|
||||||
|
it 'finds the full path to an executable binary' do
|
||||||
|
expect(File).to receive(:executable?).with('/bin/sh').and_return(true)
|
||||||
|
|
||||||
|
expect(which('sh', 'PATH' => '/bin')).to eq('/bin/sh')
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue