wscript: Implemented Host_get_ScriptName.
[wine] / programs / cmd / tests / test_builtins.cmd
1 echo Tests for cmd's builtin commands
2
3 @echo on
4 echo ------------ Testing 'echo' [ON] --------------
5 echo word
6 echo 'singlequotedword'
7 echo "doublequotedword"
8 @echo at-echoed-word
9 echo "/?"
10 echo.
11 echo .
12 echo.word
13 echo .word
14 echo word@space@
15 echo word@space@@space@
16
17 @echo off
18 echo ------------ Testing 'echo' [OFF] --------------
19 echo word
20 echo 'singlequotedword'
21 echo "doublequotedword"
22 @echo at-echoed-word
23 echo "/?"
24 echo.
25 echo .
26 echo.word
27 echo .word
28 echo word@space@
29 echo word@space@@space@
30
31 echo ------------ Testing 'set' --------------
32 echo %ErrorLevel%
33 set FOOBAR 2> nul > nul
34 echo %ErrorLevel%
35 set FOOBAR =  baz
36 echo %ErrorLevel%
37 echo %FOOBAR%FOOBAR not defined
38 echo %FOOBAR %
39 set FOOBAR 2> nul
40 set FOOBAR =  baz2
41 echo %ErrorLevel%
42 echo %fOObAr %
43 set FOOBAR= bar
44 echo %ErrorLevel%
45 echo %FOOBAR%
46 set FOO
47 set FOOBAR=
48 set FOOB
49 echo %FOOBAR%FOOBAR not defined
50 set FOOBAR =
51 set FOOBA 2> nul > nul
52 echo %ErrorLevel%
53 set FOO=bar
54 echo %FOO%
55 set FOO=foo
56 set BAR=bar
57 echo %FOO%%BAR%
58 set BAR=
59 set FOO=
60 set FOO=%FOO%
61 echo %FOO%FOO not defined
62 set BAZ%=bazbaz
63 set BA
64 echo %BAZ%%
65 set BAZ%=
66 echo set "FOO=bar" should not include the quotes in the variable value
67 set "FOO=bar"
68 echo %FOO%
69 set FOO=
70
71 echo ------------ Testing variable expansion --------------
72 call :setError 0
73 echo ~dp0 should be directory containing batch file
74 echo %~dp0
75 mkdir dummydir
76 cd dummydir
77 echo %~dp0
78 cd ..
79 rmdir dummydir
80 echo CD value %CD%
81 echo %%
82 echo P%
83 echo %P
84 echo %UNKNOWN%S
85 echo P%UNKNOWN%
86 echo P%UNKNOWN%S
87 echo %ERRORLEVEL
88 echo %ERRORLEVEL%
89 echo %ERRORLEVEL%%ERRORLEVEL%
90 echo %ERRORLEVEL%ERRORLEVEL%
91 echo %ERRORLEVEL%%
92 echo %ERRORLEVEL%%%
93 echo P%ERRORLEVEL%
94 echo %ERRORLEVEL%S
95 echo P%ERRORLEVEL%S
96
97 echo ------------ Testing type ------------
98 echo bar> foobaz
99 @echo on
100 type foobaz
101 echo ***
102 @echo off
103 type foobaz
104 echo ***
105 del foobaz
106
107 echo ------------ Testing NUL ------------
108 md foobar && cd foobar
109 rem NUL file (non) creation + case insensitivity
110 rem Note: "if exist" does not work with NUL, so to check for file existence we use a kludgy workaround
111 echo > bar
112 echo foo > NUL
113 dir /b /a-d
114 echo foo > nul
115 dir /b /a-d
116 echo foo > NuL
117 dir /b /a-d
118 del bar
119 rem NUL not special everywhere
120 call :setError 123
121 echo NUL> foo
122 if not exist foo (echo foo should have been created) else (
123     type foo
124     del foo
125 )
126 rem Empty file creation
127 copy nul foo > nul
128 if exist foo (
129     echo foo created
130     del foo
131     type foo
132 ) else (
133     echo ***
134 )
135 cd ..
136 rd foobar
137
138 echo ------------ Testing if/else --------------
139 echo if/else should work with blocks
140 if 0 == 0 (
141   echo if seems to work
142 ) else (
143   echo if seems to be broken
144 )
145 if 1 == 0 (
146   echo else seems to be broken
147 ) else (
148   echo else seems to work
149 )
150 if /c==/c (
151   echo if seems not to detect /c as parameter
152 ) else (
153   echo parameter detection seems to be broken
154 )
155 echo Testing case sensitivity with and without /i option
156 if bar==BAR echo if does not default to case sensitivity
157 if not bar==BAR echo if seems to default to case sensitivity
158 if /i foo==FOO echo if /i seems to work
159 if /i not foo==FOO echo if /i seems to be broken
160 if /I foo==FOO echo if /I seems to work
161 if /I not foo==FOO echo if /I seems to be broken
162
163 echo -----------Testing for -----------
164 for %%i in (A B C) do echo %%i
165 for %%i in (A B C) do call :forTestFun1 %%i
166 goto :endForTestFun1
167 :forTestFun1
168 echo %1
169 goto :eof
170 :endForTestFun1
171 for %%i in (X) do (
172     for %%j in (Y) do (
173         echo %%i %%j ))
174 for %%i in (A B) do (
175     for %%j in (C D) do (
176         echo %%i %%j ))
177 for %%i in (A B) do (
178     for %%j in (C D) do (
179         call :forTestFun2 %%i %%j ))
180 goto :endForTestFun2
181 :forTestFun2
182 echo %1 %2
183 goto :eof
184 :endForTestFun2
185
186 echo -----------Testing del /a-----------
187 del /f/q *.test > nul
188 echo r > r.test
189 attrib +r r.test
190 echo not-r > not-r.test
191
192 if not exist not-r.test echo not-r.test not found before delete, bad
193 del /a:-r *.test
194 if not exist not-r.test echo not-r.test not found after delete, good
195
196 if not exist r.test echo r.test not found before delete, bad
197 if exist r.test echo r.test found before delete, good
198 del /a:r *.test
199 if not exist r.test echo r.test not found after delete, good
200 if exist r.test echo r.test found after delete, bad
201
202 echo ------------ Testing del /q --------------
203 mkdir del_q_dir
204 cd del_q_dir
205 echo abc > file1
206 echo abc > file2.dat
207 rem If /q doesn't work, cmd will prompt and the test case should hang
208 del /q * > nul
209 for %%a in (1 2.dat) do if exist file%%a echo del /q * failed on file%%a
210 for %%a in (1 2.dat) do if not exist file%%a echo del /q * succeeded on file%%a
211 cd ..
212 rmdir del_q_dir
213
214 echo ------------ Testing del /s --------------
215 mkdir "foo bar"
216 cd "foo bar"
217 echo hi > file1.dat
218 echo there > file2.dat
219 echo bub > file3.dat
220 echo bye > "file with spaces.dat"
221 cd ..
222 del /s file1.dat > nul
223 del file2.dat /s > nul
224 del "file3.dat" /s > nul
225 del "file with spaces.dat" /s > nul
226 cd "foo bar"
227 for %%f in (1 2 3) do if exist file%%f.dat echo Del /s failed on file%%f
228 for %%f in (1 2 3) do if exist file%%f.dat del file%%f.dat
229 if exist "file with spaces.dat" echo Del /s failed on "file with spaces.dat"
230 if exist "file with spaces.dat" del "file with spaces.dat"
231 cd ..
232 rmdir "foo bar"
233
234 echo ----------- Testing mkdir -----------
235 call :setError 0
236 rem md and mkdir are synonymous
237 mkdir foobar
238 echo %ErrorLevel%
239 rmdir foobar
240 md foobar
241 echo %ErrorLevel%
242 rmdir foobar
243 rem Creating an already existing directory/file must fail
244 mkdir foobar
245 md foobar
246 echo %ErrorLevel%
247 rmdir foobar
248 echo > foobar
249 mkdir foobar
250 echo %ErrorLevel%
251 del foobar
252 rem Multi-level path creation
253 mkdir foo
254 echo %ErrorLevel%
255 mkdir foo\bar\baz
256 echo %ErrorLevel%
257 cd foo
258 echo %ErrorLevel%
259 cd bar
260 echo %ErrorLevel%
261 cd baz
262 echo %ErrorLevel%
263 echo > ..\..\bar2
264 mkdir ..\..\..\foo\bar2
265 echo %ErrorLevel%
266 del ..\..\bar2
267 mkdir ..\..\..\foo\bar2
268 echo %ErrorLevel%
269 rmdir ..\..\..\foo\bar2
270 cd ..
271 rmdir baz
272 cd ..
273 rmdir bar
274 cd ..
275 rmdir foo
276 echo %ErrorLevel%
277 rem Trailing backslashes
278 mkdir foo\\\\
279 echo %ErrorLevel%
280 if exist foo (rmdir foo && echo dir created
281 ) else ( echo dir not created )
282 echo %ErrorLevel%
283 rem Invalid chars
284 mkdir ?
285 echo %ErrorLevel%
286 call :setError 0
287 mkdir ?\foo
288 echo %ErrorLevel%
289 call :setError 0
290 mkdir foo\?
291 echo %ErrorLevel%
292 if exist foo (rmdir foo && echo ok, foo created
293 ) else ( echo foo not created )
294 call :setError 0
295 mkdir foo\bar\?
296 echo %ErrorLevel%
297 call :setError 0
298 if not exist foo (
299     echo bad, foo not created
300 ) else (
301     cd foo
302     if exist bar (
303         echo ok, foo\bar created
304         rmdir bar
305     )
306     cd ..
307     rmdir foo
308 )
309
310 echo ----------- Testing rmdir -----------
311 call :setError 0
312 rem rd and rmdir are synonymous
313 mkdir foobar
314 rmdir foobar
315 echo %ErrorLevel%
316 if not exist foobar echo dir removed
317 mkdir foobar
318 rd foobar
319 echo %ErrorLevel%
320 if not exist foobar echo dir removed
321 rem Removing non-existent directory
322 rmdir foobar
323 echo %ErrorLevel%
324 rem Removing single-level directories
325 echo > foo
326 rmdir foo
327 echo %ErrorLevel%
328 if exist foo echo file not removed
329 del foo
330 mkdir foo
331 echo > foo\bar
332 rmdir foo
333 echo %ErrorLevel%
334 if exist foo echo non-empty dir not removed
335 del foo\bar
336 mkdir foo\bar
337 rmdir foo
338 echo %ErrorLevel%
339 if exist foo echo non-empty dir not removed
340 rmdir foo\bar
341 rmdir foo
342 rem Recursive rmdir
343 mkdir foo\bar\baz
344 rmdir /s /Q foo
345 if not exist foo (
346     echo recursive rmdir succeeded
347 ) else (
348     rd foo\bar\baz
349     rd foo\bar
350     rd foo
351 )
352 mkdir foo\bar\baz
353 echo foo > foo\bar\brol
354 rmdir /s /Q foo
355 if not exist foo (
356     echo recursive rmdir succeeded
357 ) else (
358     rd foo\bar\baz
359     del foo\bar\brol
360     rd foo\bar
361     rd foo
362 )
363
364 echo ------------ Testing CALL --------------
365 mkdir foobar & cd foobar
366 rem External script
367 echo echo foo %%1 > foo.cmd
368 call foo
369 call foo.cmd 8
370 del foo.cmd
371 rem Internal routines
372 call :testRoutine :testRoutine
373 goto :endTestRoutine
374 :testRoutine
375 echo bar %1
376 goto :eof
377 :endTestRoutine
378 rem Should work for builtins...
379 call mkdir foo
380 echo %ErrorLevel%
381 if exist foo (echo foo created) else echo foo should exist!
382 rmdir foo
383 set FOOBAZ_VAR=foobaz
384 call echo Should expand %FOOBAZ_VAR%
385 set FOOBAZ_VAR=
386 echo>batfile
387 call dir /b
388 echo>robinfile
389 if 1==1 call del batfile
390 dir /b
391 if exist batfile echo batfile shouldn't exist
392 rem ... but not for 'if' or 'for'
393 call if 1==1 echo bar 2> nul
394 echo %ErrorLevel%
395 call :setError 0
396 call for %%i in (foo bar baz) do echo %%i 2> nul
397 echo %ErrorLevel%
398 rem First look for programs in the path before trying a builtin
399 echo echo non-builtin dir > dir.cmd
400 call dir /b
401 cd ..
402 rd /s/q foobar
403
404 echo -----------Testing Errorlevel-----------
405 rem WARNING: Do *not* add tests using ErrorLevel after this section
406 should_not_exist 2> nul > nul
407 echo %ErrorLevel%
408 rem nt 4.0 doesn't really support a way of setting errorlevel, so this is weak
409 rem See http://www.robvanderwoude.com/exit.php
410 call :setError 1
411 echo %ErrorLevel%
412 if errorlevel 2 echo errorlevel too high, bad
413 if errorlevel 1 echo errorlevel just right, good
414 call :setError 0
415 echo abc%ErrorLevel%def
416 if errorlevel 1 echo errorlevel nonzero, bad
417 if not errorlevel 1 echo errorlevel zero, good
418 rem Now verify that setting a real variable hides its magic variable
419 set errorlevel=7
420 echo %ErrorLevel% should be 7
421 if errorlevel 7 echo setting var worked too well, bad
422 call :setError 3
423 echo %ErrorLevel% should still be 7
424
425 echo -----------Testing GOTO-----------
426 if a==a goto dest1
427 :dest1
428 echo goto with no leading space worked
429 if b==b goto dest2
430  :dest2
431 echo goto with a leading space worked
432 if c==c goto dest3
433         :dest3
434 echo goto with a leading tab worked
435 if d==d goto dest4
436 :dest4@space@
437 echo goto with a following space worked
438
439 echo -----------Done, jumping to EOF-----------
440 goto :eof
441 rem Subroutine to set errorlevel and return
442 rem in windows nt 4.0, this always sets errorlevel 1, since /b isn't supported
443 :setError
444 exit /B %1
445 rem This line runs under cmd in windows NT 4, but not in more modern versions.