Unverified Commit 17f2933d authored by Tim Smith's avatar Tim Smith Committed by GitHub
Browse files

Merge pull request #21 from chef/chef-sugar


Signed-off-by: default avatarTim Smith <tsmith@chef.io>
parents 037f3ed2 8e721efb
......@@ -22,26 +22,29 @@ class Chef
module Shell
extend self
#
# Finds a command in $PATH
#
# @param [String] cmd
# the command to find
#
# @return [String, nil]
#
def which(cmd)
if Pathname.new(cmd).absolute?
File.executable?(cmd) ? cmd : nil
else
paths = ENV['PATH'].split(::File::PATH_SEPARATOR) + %w(/bin /usr/bin /sbin /usr/sbin)
paths.each do |path|
possible = File.join(path, cmd)
return possible if File.executable?(possible)
# this helper has been moved to core chef
if !defined?(Chef::VERSION) || Gem::Requirement.new("< 16.0.257").satisfied_by?(Gem::Version.new(Chef::VERSION))
#
# Finds a command in $PATH
#
# @param [String] cmd
# the command to find
#
# @return [String, nil]
#
def which(cmd)
if Pathname.new(cmd).absolute?
File.executable?(cmd) ? cmd : nil
else
paths = ENV['PATH'].split(::File::PATH_SEPARATOR) + %w(/bin /usr/bin /sbin /usr/sbin)
paths.each do |path|
possible = File.join(path, cmd)
return possible if File.executable?(possible)
end
nil
end
nil
end
end
......@@ -128,8 +131,11 @@ class Chef
end
module DSL
# @see Chef::Sugar::Shell#which
def which(cmd); Chef::Sugar::Shell.which(cmd); end
# this helper has been moved to core chef
if !defined?(Chef::VERSION) || Gem::Requirement.new("< 16.0.257").satisfied_by?(Gem::Version.new(Chef::VERSION))
# @see Chef::Sugar::Shell#which
def which(cmd); Chef::Sugar::Shell.which(cmd); end
end
# @see Chef::Sugar::Shell#dev_null
def dev_null; Chef::Sugar::Shell.dev_null(node); end
......
require 'rspec'
require 'chefspec'
require 'chef/sugar'
require 'chef/version'
require_relative 'support/shared_examples'
......@@ -22,4 +23,6 @@ RSpec.configure do |config|
# ChefSpec configuration
config.log_level = :fatal
config.filter_run_excluding pre_chef16_only: Gem::Requirement.new(">= 16.0.257").satisfied_by?(Gem::Version.new(Chef::VERSION))
end
require 'spec_helper'
describe Chef::Sugar::Shell do
describe '#which' do
describe '#which', :pre_chef16_only do
it 'returns the first executable matching the command' do
allow(File).to receive(:executable?).and_return(false)
allow(File).to receive(:executable?).with('/usr/bin/mongo').and_return(true)
......@@ -46,7 +46,7 @@ describe Chef::Sugar::Shell do
end
it 'returns false if the given binary does not exist' do
allow(File).to receive(:executable?).and_return(false)
allow(described_class).to receive(:which).with('node').and_return(nil)
expect(described_class.installed?('node')).to be false
end
end
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment