summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatt Wrock <matt@mattwrock.com>2015-09-11 15:17:39 -0700
committerMatt Wrock <matt@mattwrock.com>2015-09-11 15:17:39 -0700
commit9d3bf17599d1f7fe2a055a61e922ba1848fbcfa7 (patch)
tree28038822e052d37a479da8cf5f58f182b2ae12cd
parent9b28d7470c521d737ae12bd71199c906d799bfd1 (diff)
parent8446b81929d3a1c3ef2dd24d5d93802add1791dc (diff)
downloadmixlib-shellout-9d3bf17599d1f7fe2a055a61e922ba1848fbcfa7.tar.gz
Merge pull request #107 from chef/mwrock/dir_in_command
prevent shellout from attempting to execute a directory on windows
-rw-r--r--lib/mixlib/shellout/windows.rb8
-rw-r--r--spec/mixlib/shellout/windows_spec.rb31
2 files changed, 37 insertions, 2 deletions
diff --git a/lib/mixlib/shellout/windows.rb b/lib/mixlib/shellout/windows.rb
index bcda96a..6003ae8 100644
--- a/lib/mixlib/shellout/windows.rb
+++ b/lib/mixlib/shellout/windows.rb
@@ -301,14 +301,18 @@ module Mixlib
# 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
+ return path if executable? path
pathext.each do |ext|
exe = "#{path}#{ext}"
- return exe if File.executable? exe
+ return exe if executable? exe
end
return nil
end
+
+ def self.executable?(path)
+ File.executable?(path) && !File.directory?(path)
+ end
end
end # class
end
diff --git a/spec/mixlib/shellout/windows_spec.rb b/spec/mixlib/shellout/windows_spec.rb
index d3fb26b..dd8a80b 100644
--- a/spec/mixlib/shellout/windows_spec.rb
+++ b/spec/mixlib/shellout/windows_spec.rb
@@ -159,6 +159,37 @@ describe 'Mixlib::ShellOut::Windows', :windows_only do
end
end
+ context 'with extensionless executable' do
+ let(:stubbed_shell_out) { shell_out }
+ let(:executable_path) { 'C:\Windows\system32/ping.EXE' }
+ let(:cmd) { 'ping' }
+
+ before do
+ allow(ENV).to receive(:[]).with('PATH').and_return('C:\Windows\system32')
+ allow(ENV).to receive(:[]).with('PATHEXT').and_return('.EXE')
+ allow(ENV).to receive(:[]).with('COMSPEC').and_return('C:\Windows\system32\cmd.exe')
+ allow(File).to receive(:executable?).and_return(false)
+ allow(File).to receive(:executable?).with(executable_path).and_return(true)
+ allow(File).to receive(:directory?).and_return(false)
+ end
+
+ it 'should return with full path with extension' do
+ is_expected.to eql([executable_path, cmd])
+ end
+
+ context 'there is a directory named after command' do
+ before do
+ # File.executable? returns true for directories
+ allow(File).to receive(:executable?).with(cmd).and_return(true)
+ allow(File).to receive(:directory?).with(cmd).and_return(true)
+ end
+
+ it 'should return with full path with extension' do
+ is_expected.to eql([executable_path, cmd])
+ end
+ end
+ end
+
context 'with batch files' do
let(:stubbed_shell_out) { shell_out.tap(&with_valid_exe_at_location) }
let(:cmd_invocation) { "cmd /c \"#{cmd}\"" }