Permalink
Browse files

[CHEF-2994][WINDOWS] Fixed multiple quotes in command

CMD.exe does not parse multiple quotes well unless the whole thing is wrapped up in quotes.
Workaround:
  #2 (comment)
  http://ss64.com/nt/syntax-esc.html
  • Loading branch information...
1 parent 1255015 commit 110a028f1b1782d7ce6f6d9b871059263ca05b69 @hosh hosh committed with hosh Mar 30, 2012
Showing with 28 additions and 10 deletions.
  1. +5 −1 lib/mixlib/shellout/windows.rb
  2. +23 −9 spec/mixlib/shellout/shellout_spec.rb
@@ -178,7 +178,11 @@ def command_to_run
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.
- [ ENV['COMSPEC'], "cmd /c #{command}" ]
+ #
+ # cmd does not parse multiple quotes well unless the whole thing is wrapped up in quotes.
+ # https://github.com/opscode/mixlib-shellout/pull/2#issuecomment-4837859
+ # http://ss64.com/nt/syntax-esc.html
+ [ ENV['COMSPEC'], "cmd /c \"#{command}\"" ]
else
[ exe, command ]
end
@@ -383,17 +383,18 @@
end
context "when running different types of command" do
+ let(:script) { open_file.tap(&write_file).tap(&:close).tap(&make_executable) }
+ let(:file_name) { "#{dir}/Setup Script.cmd" }
+ let(:script_name) { "\"#{script.path}\"" }
+
+ let(:open_file) { File.open(file_name, 'w') }
+ let(:write_file) { lambda { |f| f.write(script_content) } }
+ let(:make_executable) { lambda { |f| File.chmod(0755, f.path) } }
+
context 'with spaces in the path' do
subject { chomped_stdout }
let(:cmd) { script_name }
- let(:script) { open_file.tap(&write_file).tap(&:close).tap(&make_executable) }
- let(:file_name) { "#{dir}/blah blah.cmd" }
- let(:script_name) { "\"#{script.path}\"" }
-
- let(:open_file) { File.open(file_name, 'w') }
- let(:write_file) { lambda { |f| f.write(script_content) } }
- let(:make_executable) { lambda { |f| File.chmod(0755, f.path) } }
context 'when running under Unix', :unix_only => true do
let(:script_content) { 'echo blah' }
@@ -404,14 +405,26 @@
end
context 'when running under Windows', :windows_only => true do
- let(:script_content) { '@echo blah' }
+ let(:cmd) { "#{script_name} #{argument}" }
+ let(:script_content) { '@echo %1' }
+ let(:argument) { rand(10000).to_s }
it 'should execute' do
- should eql('blah')
+ should eql(argument)
+ end
+ context 'with multiple quotes in the command and args' do
+ context 'when using a batch file' do
+ let(:argument) { "\"Random #{rand(10000)}\"" }
+
+ it 'should execute' do
+ should eql(argument)
+ end
+ end
end
end
end
+
context 'with lots of long arguments' do
subject { chomped_stdout }
@@ -436,6 +449,7 @@
end
end
+
context 'with backslashes' do
subject { stdout }
let(:backslashes) { %q{\\"\\\\} }

0 comments on commit 110a028

Please sign in to comment.