diff options
author | Ho-Sheng Hsiao <hosheng.hsiao@gmail.com> | 2012-03-30 22:13:19 -0400 |
---|---|---|
committer | Ho-Sheng Hsiao <hosh@opscode.com> | 2012-05-10 14:51:43 -0400 |
commit | cbc6c7261db77d945791de8e9bdef5f36ccfa0c4 (patch) | |
tree | 79f2a8334d74adc48672c14079f0ecf4842bec7e | |
parent | 219f51cf15783077ce483b5bdbc56601e37827fd (diff) | |
download | mixlib-shellout-cbc6c7261db77d945791de8e9bdef5f36ccfa0c4.tar.gz |
[CHEF-2994][WINDOWS] Factored out utility methods to Mixlib::ShellOut::Windows::Utils
-rw-r--r-- | lib/mixlib/shellout/windows.rb | 42 | ||||
-rw-r--r-- | spec/mixlib/shellout/windows_spec.rb | 5 |
2 files changed, 27 insertions, 20 deletions
diff --git a/lib/mixlib/shellout/windows.rb b/lib/mixlib/shellout/windows.rb index 94095cf..fce16a8 100644 --- a/lib/mixlib/shellout/windows.rb +++ b/lib/mixlib/shellout/windows.rb @@ -166,8 +166,8 @@ module Mixlib return [ nil, command ] if candidate.length == 0 # Check if the exe exists directly. Otherwise, search PATH. - exe = find_exe_at_location(candidate) - exe = which(unquoted_executable_path(command)) if exe.nil? && exe !~ /[\\\/]/ + exe = Utils.find_executable(candidate) + exe = Utils.which(unquoted_executable_path(command)) if exe.nil? && exe !~ /[\\\/]/ if exe.nil? || exe =~ IS_BATCH_FILE # Batch files MUST use cmd; and if we couldn't find the command we're looking for, we assume it must be a cmd builtin. @@ -195,7 +195,6 @@ module Mixlib end end - def inherit_environment result = {} ENV.each_pair do |k,v| @@ -212,25 +211,32 @@ module Mixlib result end - def pathext - @pathext ||= ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') + [''] : [''] - end + module Utils + def self.pathext + @pathext ||= ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') + [''] : [''] + end - def which(cmd) - ENV['PATH'].split(File::PATH_SEPARATOR).each do |path| - exe = find_exe_at_location("#{path}/${cmd}") - return exe if exe + # which() mimicks the Unix which command + def self.which(cmd) + ENV['PATH'].split(File::PATH_SEPARATOR).each do |path| + exe = find_executable("#{path}/${cmd}") + return exe if exe + end + return nil end - return nil - end - def find_exe_at_location(path) - return path if File.executable? path - pathext.each do |ext| - exe = "#{path}#{ext}" - return exe if File.executable? exe + # Windows has a different notion of what "executable" means + # The OS will search through valid the extensions and look + # for a binary there. + def self.find_executable(path) + return path if File.executable? path + + pathext.each do |ext| + exe = "#{path}#{ext}" + return exe if File.executable? exe + end + return nil end - return nil end end # class end diff --git a/spec/mixlib/shellout/windows_spec.rb b/spec/mixlib/shellout/windows_spec.rb index 552e7f5..7296c66 100644 --- a/spec/mixlib/shellout/windows_spec.rb +++ b/spec/mixlib/shellout/windows_spec.rb @@ -38,8 +38,9 @@ describe Mixlib::ShellOut::Windows, :windows_only => true do let(:stubbed_shell_out) { fail NotImplemented, 'Must declare let(:stubbed_shell_out)' } let(:shell_out) { Mixlib::ShellOut.new(cmd) } - let(:with_valid_exe_at_location) { lambda { |s| s.stub!(:find_exe_at_location).and_return(executable_path) } } - let(:with_invalid_exe_at_location) { lambda { |s| s.stub!(:find_exe_at_location).and_return(nil) } } + let(:utils) { Mixlib::ShellOut::Windows::Utils } + let(:with_valid_exe_at_location) { lambda { |s| utils.stub!(:find_executable).and_return(executable_path) } } + let(:with_invalid_exe_at_location) { lambda { |s| utils.stub!(:find_executable).and_return(nil) } } context 'with empty command' do let(:stubbed_shell_out) { shell_out } |