diff options
author | Ho-Sheng Hsiao <hosheng.hsiao@gmail.com> | 2012-03-30 13:18:47 -0400 |
---|---|---|
committer | Ho-Sheng Hsiao <hosh@opscode.com> | 2012-05-10 14:43:54 -0400 |
commit | 110a028f1b1782d7ce6f6d9b871059263ca05b69 (patch) | |
tree | bafe43433c2524202e14ea8b7822f1f646f2fc4d | |
parent | 125501593717ae7954ab00dfa39a09a02a74f6dc (diff) | |
download | chef-110a028f1b1782d7ce6f6d9b871059263ca05b69.tar.gz |
[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:
https://github.com/opscode/mixlib-shellout/pull/2#issuecomment-4837859
http://ss64.com/nt/syntax-esc.html
-rw-r--r-- | lib/mixlib/shellout/windows.rb | 6 | ||||
-rw-r--r-- | spec/mixlib/shellout/shellout_spec.rb | 32 |
2 files changed, 28 insertions, 10 deletions
diff --git a/lib/mixlib/shellout/windows.rb b/lib/mixlib/shellout/windows.rb index 89bdc22bbc..7f55da53a9 100644 --- a/lib/mixlib/shellout/windows.rb +++ b/lib/mixlib/shellout/windows.rb @@ -178,7 +178,11 @@ module Mixlib 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 diff --git a/spec/mixlib/shellout/shellout_spec.rb b/spec/mixlib/shellout/shellout_spec.rb index da8b2b5db4..b532827c63 100644 --- a/spec/mixlib/shellout/shellout_spec.rb +++ b/spec/mixlib/shellout/shellout_spec.rb @@ -383,17 +383,18 @@ describe Mixlib::ShellOut do 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 @@ describe Mixlib::ShellOut do 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 @@ describe Mixlib::ShellOut do end end + context 'with backslashes' do subject { stdout } let(:backslashes) { %q{\\"\\\\} } |