1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
|
require 'spec_helper'
describe 'Mixlib::ShellOut::Windows', :windows_only do
describe 'Utils' do
describe '.should_run_under_cmd?' do
subject { Mixlib::ShellOut::Windows::Utils.should_run_under_cmd?(command) }
def self.with_command(_command, &example)
context "with command: #{_command}" do
let(:command) { _command }
it(&example)
end
end
context 'when unquoted' do
with_command(%q{ruby -e 'prints "foobar"'}) { is_expected.not_to be_truthy }
# https://github.com/opscode/mixlib-shellout/pull/2#issuecomment-4825574
with_command(%q{"C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\NETFX 4.0 Tools\gacutil.exe" /i "C:\Program Files (x86)\NUnit 2.6\bin\framework\nunit.framework.dll"}) { is_expected.not_to be_truthy }
with_command(%q{ruby -e 'exit 1' | ruby -e 'exit 0'}) { is_expected.to be_truthy }
with_command(%q{ruby -e 'exit 1' > out.txt}) { is_expected.to be_truthy }
with_command(%q{ruby -e 'exit 1' > out.txt 2>&1}) { is_expected.to be_truthy }
with_command(%q{ruby -e 'exit 1' < in.txt}) { is_expected.to be_truthy }
with_command(%q{ruby -e 'exit 1' || ruby -e 'exit 0'}) { is_expected.to be_truthy }
with_command(%q{ruby -e 'exit 1' && ruby -e 'exit 0'}) { is_expected.to be_truthy }
with_command(%q{@echo TRUE}) { is_expected.to be_truthy }
with_command(%q{echo %PATH%}) { is_expected.to be_truthy }
with_command(%q{run.exe %A}) { is_expected.to be_falsey }
with_command(%q{run.exe B%}) { is_expected.to be_falsey }
with_command(%q{run.exe %A B%}) { is_expected.to be_falsey }
with_command(%q{run.exe %A B% %PATH%}) { is_expected.to be_truthy }
with_command(%q{run.exe %A B% %_PATH%}) { is_expected.to be_truthy }
with_command(%q{run.exe %A B% %PATH_EXT%}) { is_expected.to be_truthy }
with_command(%q{run.exe %A B% %1%}) { is_expected.to be_falsey }
with_command(%q{run.exe %A B% %PATH1%}) { is_expected.to be_truthy }
with_command(%q{run.exe %A B% %_PATH1%}) { is_expected.to be_truthy }
context 'when outside quotes' do
with_command(%q{ruby -e "exit 1" | ruby -e "exit 0"}) { is_expected.to be_truthy }
with_command(%q{ruby -e "exit 1" > out.txt}) { is_expected.to be_truthy }
with_command(%q{ruby -e "exit 1" > out.txt 2>&1}) { is_expected.to be_truthy }
with_command(%q{ruby -e "exit 1" < in.txt}) { is_expected.to be_truthy }
with_command(%q{ruby -e "exit 1" || ruby -e "exit 0"}) { is_expected.to be_truthy }
with_command(%q{ruby -e "exit 1" && ruby -e "exit 0"}) { is_expected.to be_truthy }
with_command(%q{@echo "TRUE"}) { is_expected.to be_truthy }
context 'with unclosed quote' do
with_command(%q{ruby -e "exit 1" | ruby -e "exit 0}) { is_expected.to be_truthy }
with_command(%q{ruby -e "exit 1" > "out.txt}) { is_expected.to be_truthy }
with_command(%q{ruby -e "exit 1" > "out.txt 2>&1}) { is_expected.to be_truthy }
with_command(%q{ruby -e "exit 1" < "in.txt}) { is_expected.to be_truthy }
with_command(%q{ruby -e "exit 1" || "ruby -e "exit 0"}) { is_expected.to be_truthy }
with_command(%q{ruby -e "exit 1" && "ruby -e "exit 0"}) { is_expected.to be_truthy }
with_command(%q{@echo "TRUE}) { is_expected.to be_truthy }
with_command(%q{echo "%PATH%}) { is_expected.to be_truthy }
with_command(%q{run.exe "%A}) { is_expected.to be_falsey }
with_command(%q{run.exe "B%}) { is_expected.to be_falsey }
with_command(%q{run.exe "%A B%}) { is_expected.to be_falsey }
with_command(%q{run.exe "%A B% %PATH%}) { is_expected.to be_truthy }
with_command(%q{run.exe "%A B% %_PATH%}) { is_expected.to be_truthy }
with_command(%q{run.exe "%A B% %PATH_EXT%}) { is_expected.to be_truthy }
with_command(%q{run.exe "%A B% %1%}) { is_expected.to be_falsey }
with_command(%q{run.exe "%A B% %PATH1%}) { is_expected.to be_truthy }
with_command(%q{run.exe "%A B% %_PATH1%}) { is_expected.to be_truthy }
end
end
end
context 'when quoted' do
with_command(%q{run.exe "ruby -e 'exit 1' || ruby -e 'exit 0'"}) { is_expected.to be_falsey }
with_command(%q{run.exe "ruby -e 'exit 1' > out.txt"}) { is_expected.to be_falsey }
with_command(%q{run.exe "ruby -e 'exit 1' > out.txt 2>&1"}) { is_expected.to be_falsey }
with_command(%q{run.exe "ruby -e 'exit 1' < in.txt"}) { is_expected.to be_falsey }
with_command(%q{run.exe "ruby -e 'exit 1' || ruby -e 'exit 0'"}) { is_expected.to be_falsey }
with_command(%q{run.exe "ruby -e 'exit 1' && ruby -e 'exit 0'"}) { is_expected.to be_falsey }
with_command(%q{run.exe "%PATH%"}) { is_expected.to be_truthy }
with_command(%q{run.exe "%A"}) { is_expected.to be_falsey }
with_command(%q{run.exe "B%"}) { is_expected.to be_falsey }
with_command(%q{run.exe "%A B%"}) { is_expected.to be_falsey }
with_command(%q{run.exe "%A B% %PATH%"}) { is_expected.to be_truthy }
with_command(%q{run.exe "%A B% %_PATH%"}) { is_expected.to be_truthy }
with_command(%q{run.exe "%A B% %PATH_EXT%"}) { is_expected.to be_truthy }
with_command(%q{run.exe "%A B% %1%"}) { is_expected.to be_falsey }
with_command(%q{run.exe "%A B% %PATH1%"}) { is_expected.to be_truthy }
with_command(%q{run.exe "%A B% %_PATH1%"}) { is_expected.to be_truthy }
context 'with unclosed quote' do
with_command(%q{run.exe "ruby -e 'exit 1' || ruby -e 'exit 0'}) { is_expected.to be_falsey }
with_command(%q{run.exe "ruby -e 'exit 1' > out.txt}) { is_expected.to be_falsey }
with_command(%q{run.exe "ruby -e 'exit 1' > out.txt 2>&1}) { is_expected.to be_falsey }
with_command(%q{run.exe "ruby -e 'exit 1' < in.txt}) { is_expected.to be_falsey }
with_command(%q{run.exe "ruby -e 'exit 1' || ruby -e 'exit 0'}) { is_expected.to be_falsey }
with_command(%q{run.exe "ruby -e 'exit 1' && ruby -e 'exit 0'}) { is_expected.to be_falsey }
with_command(%q{run.exe "%PATH%}) { is_expected.to be_truthy }
with_command(%q{run.exe "%A}) { is_expected.to be_falsey }
with_command(%q{run.exe "B%}) { is_expected.to be_falsey }
with_command(%q{run.exe "%A B%}) { is_expected.to be_falsey }
with_command(%q{run.exe "%A B% %PATH%}) { is_expected.to be_truthy }
with_command(%q{run.exe "%A B% %_PATH%}) { is_expected.to be_truthy }
with_command(%q{run.exe "%A B% %PATH_EXT%}) { is_expected.to be_truthy }
with_command(%q{run.exe "%A B% %1%}) { is_expected.to be_falsey }
with_command(%q{run.exe "%A B% %PATH1%}) { is_expected.to be_truthy }
with_command(%q{run.exe "%A B% %_PATH1%}) { is_expected.to be_truthy }
end
end
end
end
# Caveat: Private API methods are subject to change without notice.
# Monkeypatch at your own risk.
context 'for private API methods' do
describe '::IS_BATCH_FILE' do
subject { candidate =~ Mixlib::ShellOut::Windows::IS_BATCH_FILE }
def self.with_candidate(_context, _options = {}, &example)
context "with #{_context}" do
let(:candidate) { _options[:candidate] }
it(&example)
end
end
with_candidate('valid .bat file', :candidate => 'autoexec.bat') { is_expected.to be_truthy }
with_candidate('valid .cmd file', :candidate => 'autoexec.cmd') { is_expected.to be_truthy }
with_candidate('valid quoted .bat file', :candidate => '"C:\Program Files\autoexec.bat"') { is_expected.to be_truthy }
with_candidate('valid quoted .cmd file', :candidate => '"C:\Program Files\autoexec.cmd"') { is_expected.to be_truthy }
with_candidate('invalid .bat file', :candidate => 'autoexecbat') { is_expected.not_to be_truthy }
with_candidate('invalid .cmd file', :candidate => 'autoexeccmd') { is_expected.not_to be_truthy }
with_candidate('bat in filename', :candidate => 'abattoir.exe') { is_expected.not_to be_truthy }
with_candidate('cmd in filename', :candidate => 'parse_cmd.exe') { is_expected.not_to be_truthy }
with_candidate('invalid quoted .bat file', :candidate => '"C:\Program Files\autoexecbat"') { is_expected.not_to be_truthy }
with_candidate('invalid quoted .cmd file', :candidate => '"C:\Program Files\autoexeccmd"') { is_expected.not_to be_truthy }
with_candidate('quoted bat in filename', :candidate => '"C:\Program Files\abattoir.exe"') { is_expected.not_to be_truthy }
with_candidate('quoted cmd in filename', :candidate => '"C:\Program Files\parse_cmd.exe"') { is_expected.not_to be_truthy }
end
describe '#command_to_run' do
subject { stubbed_shell_out.send(:command_to_run, cmd) }
let(:stubbed_shell_out) { fail NotImplemented, 'Must declare let(:stubbed_shell_out)' }
let(:shell_out) { Mixlib::ShellOut.new(cmd) }
let(:utils) { Mixlib::ShellOut::Windows::Utils }
let(:with_valid_exe_at_location) { lambda { |s| allow(utils).to receive(:find_executable).and_return(executable_path) } }
let(:with_invalid_exe_at_location) { lambda { |s| allow(utils).to receive(:find_executable).and_return(nil) } }
context 'with empty command' do
let(:stubbed_shell_out) { shell_out }
let(:cmd) { ' ' }
it 'should return with a nil executable' do
is_expected.to eql([nil, cmd])
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}\"" }
let(:cmd_exe) { "C:\\Windows\\system32\\cmd.exe" }
let(:cmd) { "#{executable_path}" }
context 'with .bat file' do
let(:executable_path) { '"C:\Program Files\Application\Start.bat"' }
# Examples taken from: https://github.com/opscode/mixlib-shellout/pull/2#issuecomment-4825574
context 'with executable path enclosed in double quotes' do
it 'should use specified batch file' do
is_expected.to eql([cmd_exe, cmd_invocation])
end
context 'with arguments' do
let(:cmd) { "#{executable_path} arguments" }
it 'should use specified executable' do
is_expected.to eql([cmd_exe, cmd_invocation])
end
end
context 'with quoted arguments' do
let(:cmd) { "#{executable_path} /i \"C:\Program Files (x86)\NUnit 2.6\bin\framework\nunit.framework.dll\"" }
it 'should use specified executable' do
is_expected.to eql([cmd_exe, cmd_invocation])
end
end
end
end
context 'with .cmd file' do
let(:executable_path) { '"C:\Program Files\Application\Start.cmd"' }
# Examples taken from: https://github.com/opscode/mixlib-shellout/pull/2#issuecomment-4825574
context 'with executable path enclosed in double quotes' do
it 'should use specified batch file' do
is_expected.to eql([cmd_exe, cmd_invocation])
end
context 'with arguments' do
let(:cmd) { "#{executable_path} arguments" }
it 'should use specified executable' do
is_expected.to eql([cmd_exe, cmd_invocation])
end
end
context 'with quoted arguments' do
let(:cmd) { "#{executable_path} /i \"C:\Program Files (x86)\NUnit 2.6\bin\framework\nunit.framework.dll\"" }
it 'should use specified executable' do
is_expected.to eql([cmd_exe, cmd_invocation])
end
end
end
end
end
context 'with valid executable at location' do
let(:stubbed_shell_out) { shell_out.tap(&with_valid_exe_at_location) }
context 'with executable path' do
let(:cmd) { "#{executable_path}" }
let(:executable_path) { 'C:\RUBY192\bin\ruby.exe' }
it 'should use specified executable' do
is_expected.to eql([executable_path, cmd])
end
context 'with arguments' do
let(:cmd) { "#{executable_path} arguments" }
it 'should use specified executable' do
is_expected.to eql([executable_path, cmd])
end
end
context 'with quoted arguments' do
let(:cmd) { "#{executable_path} -e \"print 'fee fie foe fum'\"" }
it 'should use specified executable' do
is_expected.to eql([executable_path, cmd])
end
end
end
# Examples taken from: https://github.com/opscode/mixlib-shellout/pull/2#issuecomment-4825574
context 'with executable path enclosed in double quotes' do
let(:cmd) { "#{executable_path}" }
let(:executable_path) { '"C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\NETFX 4.0 Tools\gacutil.exe"' }
it 'should use specified executable' do
is_expected.to eql([executable_path, cmd])
end
context 'with arguments' do
let(:cmd) { "#{executable_path} arguments" }
it 'should use specified executable' do
is_expected.to eql([executable_path, cmd])
end
end
context 'with quoted arguments' do
let(:cmd) { "#{executable_path} /i \"C:\Program Files (x86)\NUnit 2.6\bin\framework\nunit.framework.dll\"" }
it 'should use specified executable' do
is_expected.to eql([executable_path, cmd])
end
end
end
end
end
end
end
|