[CMD_WINETEST] Sync with Wine Staging 2.2. CORE-12823
[reactos.git] / rostests / winetests / cmd / 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:
15 echo :
16 echo:word
17 echo :word
18 echo/
19 echo /
20 echo/word
21 echo /word
22 echo off now
23 echo word@space@
24 echo word@space@@space@
25 echo word
26 echo@tab@word
27 echo@tab@word @tab@
28 echo@tab@word@tab@@space@
29 @tab@echo word
30 echo @tab@word
31 echo @tab@word
32 echo@tab@@tab@word
33 echo @tab@ on @space@
34 @echo --- @ with chains and brackets
35 (echo the @ character chains until&&@echo we leave the current depth||(
36 echo hidden
37 @echo hidden
38 ))&&echo and can hide brackets||(@echo command hidden)||@(echo brackets hidden)
39 @echo ---
40
41 @echo off
42 echo off@tab@@space@
43 @echo noecho1
44 @echo noecho2
45 @@@@@echo echo3
46 echo ------------ Testing 'echo' [OFF] ------------
47 echo word
48 echo 'singlequotedword'
49 echo "doublequotedword"
50 @echo at-echoed-word
51 echo "/?"
52 echo.
53 echo .
54 echo.word
55 echo .word
56 echo:
57 echo :
58 echo:word
59 echo :word
60 echo/
61 echo /
62 echo/word
63 echo /word
64 echo on again
65 echo word@space@
66 echo word@space@@space@
67 echo word
68 echo@tab@word
69 echo@tab@word @tab@
70 echo@tab@word@tab@@space@
71 @tab@echo word
72 echo @tab@word
73 echo @tab@word
74 echo@tab@@tab@word
75
76 echo ------------ Testing mixed echo modes ------------
77 echo @echo on> mixedEchoModes.cmd
78 echo if 1==1 echo foo>> mixedEchoModes.cmd
79 echo if 1==1 @echo bar>> mixedEchoModes.cmd
80 echo @echo off>> mixedEchoModes.cmd
81 echo if 1==1 echo foo2>> mixedEchoModes.cmd
82 echo if 1==1 @echo bar2>> mixedEchoModes.cmd
83 type mixedEchoModes.cmd
84 cmd /c mixedEchoModes.cmd
85 del mixedEchoModes.cmd
86
87 echo ------------ Testing parameterization ------------
88 call :TestParm a b c
89 call :TestParm "a b c"
90 call :TestParm "a b"\c
91 call :TestParm a=~`+,.{}!+b
92 call :TestParm a;b
93 call :TestParm "a;b"
94 call :TestParm a^;b
95 call :TestParm a[b]{c}(d)e
96 call :TestParm a&echo second line
97 call :TestParm a b,,,c
98 call :TestParm a==b;;c
99 call :TestParm a,,, b
100 goto :TestRem
101
102 :TestParm
103 echo '%1', '%2', '%3'
104 goto :eof
105
106 :TestRem
107 echo ------------ Testing rem ------------
108 rem Hello
109 rem Hello
110 rem Hello || foo
111 rem echo lol
112 rem echo foo & echo bar
113 rem @tab@ Hello
114 rem@tab@ Hello
115 rem@tab@echo foo & echo bar
116 @echo on
117 rem Hello
118 rem Hello
119 rem Hello || foo
120 rem echo lol
121 rem echo foo & echo bar
122 rem @tab@ Hello
123 rem@tab@ Hello
124 rem@tab@echo foo & echo bar
125 @echo off
126
127 echo ------------ Testing redirection operators ------------
128 mkdir foobar & cd foobar
129 echo --- stdout redirection
130 echo foo>foo
131 type foo
132 echo foo 1> foo
133 type foo
134 echo foo@tab@1> foo
135 type foo
136 echo foo 1>@tab@foo
137 type foo
138 echo foo@tab@1>@tab@foo
139 type foo
140 echo foo7 7> foo
141 type foo
142 echo foo9 9> foo
143 type foo
144 echo foo1> foo
145 type foo
146 echo foo11> foo
147 type foo
148 echo foo12> foo
149 type foo
150 echo foo13>"foo"
151 type foo
152 echo foo14>."\foo"
153 type foo
154 echo foo15>."\f"oo
155 type foo
156 del foo
157 echo1>foo
158 type foo
159 echo --- stdout appending
160 echo foo>foo
161 echo foo >>foo
162 type foo
163 del foo
164 echo foob >> foo
165 type foo
166 echo fooc 1>>foo
167 type foo
168 echo food1>>foo
169 type foo
170 echo food2>>"foo"
171 type foo
172 del foo
173 echo food21>>foo
174 type foo
175 del foo
176 echo foo> foo
177 echo foo7 7>> foo || (echo not supported & del foo)
178 if exist foo (type foo) else echo not supported
179 echo --- redirections within IF statements
180 if 1==1 echo foo1>bar
181 type bar & del bar
182 echo -----
183 if 1==1 (echo foo2>bar) else echo baz2>bar
184 type bar & del bar
185 if 1==1 (echo foo3) else echo baz3>bar
186 type bar || echo file does not exist, ok
187 if 1==1 (echo foo4>bar) else echo baz4>bar
188 type bar & del bar
189 if 1==0 (echo foo5>bar) else echo baz5>bar
190 type bar & del bar
191 if 1==0 (echo foo6) else echo baz6 1>bar
192 type bar & del bar
193 if 1==0 (echo foo7 1>bar) else echo baz7>bar
194 type bar & del bar
195 if 1==0 (echo foo8 1>bar) else echo baz8>bak
196 type bak
197 if 1==1 (echo foo>bar & echo baz)
198 type bar
199 if 1==1 (
200 echo foo>bar
201 echo baz
202 )
203 type bar
204 (if 1==1 (echo A) else echo B) > C
205 type C
206 (if 1==0 (echo A) else echo B) > C
207 type C
208 (if 1==0 (echo A > B) else echo C)
209 cd .. & rd /s/q foobar
210
211 echo ------------ Testing circumflex escape character ------------
212 rem Using something like "echo foo^" asks for an additional char after a "More?" prompt on the following line; it's not possible to currently test that non-interactively
213 echo ^hell^o, world
214 echo hell^o, world
215 echo hell^^o, world
216 echo hell^^^o, world
217 echo hello^
218 world
219 echo hello^
220
221 world
222 echo hello^
223
224
225 echo finished
226 mkdir foobar
227 echo baz> foobar\baz
228 type foobar\baz
229 type foobar^\baz
230 rd /s/q foobar
231 echo foo ^| echo bar
232 echo foo ^& echo bar
233 call :setError 0
234 echo bak ^&& echo baz 2> nul
235 echo %ErrorLevel%
236 echo foo ^> foo
237 echo ^<> foo
238 type foo
239 del foo
240 set WINE_FOO=oof
241 echo ff^%WINE_FOO%
242 set WINE_FOO=bar ^| baz
243 set WINE_FOO
244 rem FIXME: echoing %WINE_FOO% gives an error (baz not recognized) but prematurely
245 rem exits the script on windows; redirecting stdout and/or stderr doesn't help
246 echo %ErrorLevel%
247 call :setError 0
248 set WINE_FOO=bar ^^^| baz
249 set WINE_FOO
250 echo %WINE_FOO%
251 echo %ErrorLevel%
252 set WINE_FOO=
253
254 echo ------------ Testing chains ------------
255 rem The chain operators have the following bottom-up precedence:
256 rem 'else' precedes nothing and matches the closest unmatched 'if' in the same bracket depth
257 rem '&' precedes 'else'
258 rem '||' precedes '&' and 'else'
259 rem '&&' precedes '||', '&' and 'else'
260 rem '|' precedes '&&', '||', '&' and 'else'
261 rem
262 rem Example: 'if 1==1 if 2==2 if 3==3 a | b && c || d & e else f else g' is interpreted as
263 rem 'if 1==1 (if 2==2 (if 3==3 ((((a | b) && c) || d) & e) else f) else g)'
264 goto :cfailend
265 :cfail
266 echo %1
267 call :setError 1
268 goto :eof
269 :cfailend
270 echo --- chain success
271 echo a1&echo a2
272 echo b1&&echo b2
273 echo c1||echo c2
274 echo ---
275 echo d1&echo d2&echo d3
276 echo e1&echo e2&&echo e3
277 echo f1&echo f2||echo f3
278 echo ---
279 echo g1&&echo g2&echo g3
280 echo h1&&echo h2&&echo h3
281 echo i1&&echo i2||echo i3
282 echo ---
283 echo j1||echo j2&echo j3
284 echo ---
285 echo k1||echo k2&&echo k3
286 echo ---
287 echo l1||echo l2||echo l3
288 echo ---
289 echo --- chain failure
290 call :cfail a1&call :cfail a2
291 call :cfail b1&&call :cfail b2
292 echo ---
293 call :cfail c1||call :cfail c2
294 call :cfail d1&call :cfail d2&call :cfail d3
295 call :cfail e1&call :cfail e2&&call :cfail e3
296 echo ---
297 call :cfail f1&call :cfail f2||call :cfail f3
298 call :cfail g1&&call :cfail g2&call :cfail g3
299 echo ---
300 call :cfail h1&&call :cfail h2&&call :cfail h3
301 echo ---
302 call :cfail i1&&call :cfail i2||call :cfail i3
303 echo ---
304 call :cfail j1||call :cfail j2&call :cfail j3
305 call :cfail k1||call :cfail k2&&call :cfail k3
306 echo ---
307 call :cfail l1||call :cfail l2||call :cfail l3
308 echo --- chain brackets
309 rem Brackets are like regular commands, they support redirections
310 rem and have the same precedence as regular commands.
311 echo a1&(echo a2&echo a3)
312 echo b1&(echo b2&&echo b3)
313 echo c1&(echo c2||echo c3)
314 echo ---
315 echo d1&&(echo d2&echo d3)
316 echo e1&&(echo e2&&echo e3)
317 echo f1&&(echo f2||echo f3)
318 echo ---
319 echo g1||(echo g2&echo g3)
320 echo ---
321 echo h1||(echo h2&&echo h3)
322 echo ---
323 echo i1||(echo i2||echo i3)
324 echo ---
325 call :cfail j1&(call :cfail j2&call :cfail j3)
326 call :cfail k1&(call :cfail k2&&call :cfail k3)
327 echo ---
328 call :cfail l1&(call :cfail l2||call :cfail l3)
329 call :cfail m1&&(call :cfail m2&call :cfail m3)
330 echo ---
331 call :cfail n1&&(call :cfail n2&&call :cfail n3)
332 echo ---
333 call :cfail o1&&(call :cfail o2||call :cfail o3)
334 echo ---
335 call :cfail p1||(call :cfail p2&call :cfail p3)
336 call :cfail q1||(call :cfail q2&&call :cfail q3)
337 echo ---
338 call :cfail r1||(call :cfail r2||call :cfail r3)
339 echo --- chain pipe
340 rem Piped commands run at the same time, so the print order varies.
341 rem Additionally, they don't run in the batch script context, as shown by
342 rem 'call :existing_label|echo read the error message'.
343 (echo a 1>&2|echo a 1>&2) 2>&1
344 echo ---
345 echo b1|echo b2
346 echo c1&&echo c2|echo c3
347 echo d1||echo d2|echo d3
348 echo ---
349 echo e1&echo e2|echo e3
350 echo f1|echo f2&&echo f3
351 echo g1|echo g2||echo g3
352 echo ---
353 echo h1|echo h2&echo h3
354 echo i1|echo i2|echo i3
355 echo --- chain pipe input
356 rem The output data of the left side of a pipe can disappear, probably
357 rem because it finished too fast and closed the pipe before it could be read,
358 rem which means we can get broken results for the tests of this section.
359 echo @echo off> tmp.cmd
360 echo set IN=X>> tmp.cmd
361 echo set /p IN=%%1:>> tmp.cmd
362 echo setlocal EnableDelayedExpansion>> tmp.cmd
363 echo echo [!IN!,%%1]>> tmp.cmd
364 echo endlocal>> tmp.cmd
365 echo set IN=>> tmp.cmd
366 echo a1|cmd /ctmp.cmd a2
367 echo b1|cmd /ctmp.cmd b2|cmd /ctmp.cmd b3
368 echo c1|cmd /ctmp.cmd c2|cmd /ctmp.cmd c3|cmd /ctmp.cmd c4
369 echo d1|call tmp.cmd d2
370 echo e1|call tmp.cmd e2|call tmp.cmd e3
371 echo f1|call tmp.cmd f2|call tmp.cmd f3|call tmp.cmd f4
372 rem FIXME these 3 tests cause "unexpected end of output"
373 rem test : echo g1|tmp.cmd g2
374 rem result: g2:[g1,g2]
375 rem test : echo h1|tmp.cmd h2|tmp.cmd h3
376 rem result: h3:[h2:[h1,h2],h3]@or_broken@h3:[h2:,h3]
377 rem test : echo i1|tmp.cmd i2|tmp.cmd i3|tmp.cmd i4
378 rem result: i4:[i3:[i2:[i1,i2],i3],i4]@or_broken@i4:[i3:[i2:,i3],i4]@or_broken@i4:[i3:,i4]
379 del tmp.cmd
380 echo --- chain else
381 rem Command arguments are gready and eat up the 'else' unless terminated by
382 rem brackets, which means the 'else' can only be recognized when the
383 rem 'if true' command chain ends with brackets.
384 if 1==1 if 2==2 if 3==3 (echo a1) else (echo a2) else echo a3
385 if 1==1 if 2==2 if 3==0 (echo b1) else (echo b2) else echo b3
386 echo ---
387 if 1==1 if 2==0 if 3==3 (echo c1) else (echo c2) else echo c3
388 echo ---
389 if 1==1 if 2==0 if 3==0 (echo d1) else (echo d2) else echo d3
390 echo ---
391 if 1==0 if 2==2 if 3==3 (echo e1) else (echo e2) else echo e3
392 echo ---
393 if 1==0 if 2==2 if 3==0 (echo f1) else (echo f2) else echo f3
394 echo ---
395 if 1==0 if 2==0 if 3==3 (echo g1) else (echo g2) else echo g3
396 echo ---
397 if 1==0 if 2==0 if 3==0 (echo h1) else (echo h2) else echo h3
398 echo ---
399 echo --- chain else (if true)
400 if 1==1 echo a1 else echo a2
401 if 1==1 echo b1|echo b2 else echo b3
402 if 1==1 echo c1&&echo c2 else echo c3
403 if 1==1 echo d1||echo d2 else echo d3
404 echo ---
405 if 1==1 echo e1&echo e2 else echo e3
406 if 1==1 echo f1 else echo f2|echo f3
407 if 1==1 echo g1 else echo g2&&echo g3
408 if 1==1 echo h1 else echo h2||echo h3
409 echo ---
410 if 1==1 echo i1 else echo i2&echo i3
411 if 1==1 echo j1|(echo j2) else echo j3
412 echo ---
413 if 1==1 echo k1&&(echo k2) else echo k3
414 if 1==1 echo l1||(echo l2) else echo l3
415 echo ---
416 if 1==1 echo m1&(echo m2) else echo m3
417 if 1==1 (echo n1) else echo n2|echo n3
418 if 1==1 (echo o1) else echo o2&&echo o3
419 if 1==1 (echo p1) else echo p2||echo p3
420 if 1==1 (echo q1) else echo q2&echo q3
421 echo --- chain else (if false)
422 if 1==0 echo a1 else echo a2
423 if 1==0 echo b1|echo b2 else echo b3
424 if 1==0 echo c1&&echo c2 else echo c3
425 if 1==0 echo d1||echo d2 else echo d3
426 if 1==0 echo e1&echo e2 else echo e3
427 if 1==0 echo f1 else echo f2|echo f3
428 if 1==0 echo g1 else echo g2&&echo g3
429 if 1==0 echo h1 else echo h2||echo h3
430 if 1==0 echo i1 else echo i2&echo i3
431 if 1==0 echo j1|(echo j2) else echo j3
432 echo ---
433 if 1==0 echo k1&&(echo k2) else echo k3
434 if 1==0 echo l1||(echo l2) else echo l3
435 if 1==0 echo m1&(echo m2) else echo m3
436 if 1==0 (echo n1) else echo n2|echo n3
437 if 1==0 (echo o1) else echo o2&&echo o3
438 if 1==0 (echo p1) else echo p2||echo p3
439 echo ---
440 if 1==0 (echo q1) else echo q2&echo q3
441 echo ------------ Testing 'set' ------------
442 call :setError 0
443 rem Remove any WINE_FOO* WINE_BA* environment variables from shell before proceeding
444 for /f "delims==" %%i in ('set WINE_ba') do set %%i=
445 for /f "delims==" %%i in ('set WINE_foo') do set %%i=
446 set WINE_FOOBAR 2> nul > nul
447 echo %ErrorLevel%
448 set WINE_FOOBAR = baz
449 echo %ErrorLevel%
450 echo %WINE_FOOBAR%WINE_FOOBAR not defined
451 echo %WINE_FOOBAR %
452 set WINE_FOOBAR 2> nul
453 set WINE_FOOBAR = baz2
454 echo %ErrorLevel%
455 echo %WINE_fOObAr %
456 set WINE_FOOBAR= bar
457 echo %ErrorLevel%
458 echo %WINE_FOOBAR%
459 set WINE_FOO
460 set WINE_FOOBAR=
461 set WINE_FOOB
462 echo %WINE_FOOBAR%WINE_FOOBAR not defined
463 set WINE_FOOBAR =
464 set WINE_FOOBA 2> nul > nul
465 echo %ErrorLevel%
466 set WINE_FOO=bar
467 echo %WINE_FOO%
468 set WINE_FOO=foo
469 set WINE_BAR=bar
470 echo %WINE_FOO%%WINE_BAR%
471 set WINE_BAR=
472 set WINE_FOO=
473 set WINE_FOO=%WINE_FOO%
474 echo %WINE_FOO%WINE_FOO not defined
475 set WINE_BAZ%=bazbaz
476 set WINE_BA
477 echo %WINE_BAZ%%
478 set WINE_BAZ%=
479 echo set "WINE_FOO=bar" should not include the quotes in the variable value
480 set "WINE_FOO=bar"
481 echo %WINE_FOO%
482 set@tab@WINE_FOO=foo
483 echo %WINE_FOO%
484 set@tab@WINE_FOO=
485 echo '%WINE_FOO%'
486 set WINE_FOO=foo@space@
487 echo '%WINE_FOO%'
488 set WINE_FOO=foo@tab@
489 echo '%WINE_FOO%'
490 rem Space symbol must appear in `var`
491 set WINE_FOO=value@space@
492 echo '%WINE_FOO%'
493 rem Space symbol must NOT appear in `var`
494 set "WINE_FOO=value"@space@
495 echo '%WINE_FOO%'
496 rem Mixed examples:
497 set WINE_FOO=jim fred
498 echo '%WINE_FOO%'
499 set WINE_FOO="jim" fred
500 echo '%WINE_FOO%'
501 set "WINE_FOO=jim fred"
502 echo '%WINE_FOO%'
503 set "WINE_FOO=jim" fred
504 echo '%WINE_FOO%'
505 rem Only the final quote ends the string
506 set "WINE_FOO=apple"banana"grape"orange
507 echo '%WINE_FOO%'
508 set WINE_FOO=
509
510 echo ------------ Testing variable expansion ------------
511 call :setError 0
512 echo ~p0 should be path containing batch file
513 echo %~p0
514 mkdir dummydir
515 cd dummydir
516 echo %~p0
517 cd ..
518 rmdir dummydir
519 echo ~dp0 should be directory containing batch file
520 echo %~dp0
521 mkdir dummydir
522 cd dummydir
523 echo %~dp0
524 cd ..
525 rmdir dummydir
526 echo CD value %CD%
527 echo %%
528 echo P%
529 echo %P
530 echo %WINE_UNKNOWN%S
531 echo P%WINE_UNKNOWN%
532 echo P%WINE_UNKNOWN%S
533 echo %ERRORLEVEL
534 echo %ERRORLEVEL%
535 echo %ERRORLEVEL%%ERRORLEVEL%
536 echo %ERRORLEVEL%ERRORLEVEL%
537 echo %ERRORLEVEL%%
538 echo %ERRORLEVEL%%%
539 echo P%ERRORLEVEL%
540 echo %ERRORLEVEL%S
541 echo P%ERRORLEVEL%S
542
543 echo ------------ Testing variable substrings ------------
544 set WINE_VAR=qwerty
545 echo %WINE_VAR:~0,1%
546 echo %WINE_VAR:~0,3%
547 echo %WINE_VAR:~2,2%
548 echo '%WINE_VAR:~-2,3%'
549 echo '%WINE_VAR:~-2,1%'
550 echo %WINE_VAR:~2,-1%
551 echo %WINE_VAR:~2,-3%
552 echo '%WINE_VAR:~-2,-4%'
553 echo %WINE_VAR:~-3,-2%
554 set WINE_VAR=
555
556 echo ------------ Testing variable substitution ------------
557 echo --- in FOR variables
558 for %%i in ("A B" C) do echo %%i
559 rem check works when prefix with @
560 @for %%i in ("A B" C) do echo %%i
561 rem quotes removal
562 for %%i in ("A B" C) do echo '%%~i'
563 rem fully qualified path
564 for %%f in ("C D" E) do echo %%~ff
565 rem drive letter
566 for %%i in ("F G" H) do echo %%~di
567 rem path
568 for %%d in ("I J" K) do echo %%~pd
569 rem filename
570 for %%i in ("L M" N) do echo %%~ni
571 rem file extension
572 for %%i in ("O. P.OOL" Q.TABC hello) do echo '%%~xi'
573 rem path with short path names
574 for %%I in ("R S" T ABCDEFGHIJK.LMNOP) do echo '%%~sI'
575 rem file attribute
576 for %%i in ("U V" W) do echo '%%~ai'
577 echo foo> foo
578 for %%i in (foo) do echo '%%~ai'
579 for %%i in (foo) do echo '%%~zi'
580 del foo
581 rem file date/time
582 rem Not fully testable, until we can grep dir's output to get foo's creation time in an envvar...
583 for %%i in ("a b" c) do echo '%%~ti'
584 rem file size
585 rem Similar issues as above
586 for %%i in ("a b" c) do echo '%%~zi'
587 rem combined options
588 for %%i in ("d e" f) do echo %%~dpi
589 for %%i in ("g h" i) do echo %%~sdi
590 for %%i in ("g h" i) do echo %%~dsi
591 for %%i in ("j k" l.eh) do echo '%%~xsi'
592 for %%i in ("") do echo '%%~i,%%~fi,%%~di,%%~pi,%%~ni,%%~xi,%%~si,%%~ai,%%~ti,%%~zi'
593
594 echo --- in parameters
595 for %%i in ("A B" C) do call :echoFun %%i
596 rem quotes removal
597 for %%i in ("A B" C) do call :echoFunQ %%i
598 rem fully qualified path
599 for %%f in ("C D" E) do call :echoFunF %%f
600 rem drive letter
601 for %%i in ("F G" H) do call :echoFunD %%i
602 rem path
603 for %%d in ("I J" K) do call :echoFunP %%d
604 rem filename
605 for %%i in ("L M" N) do call :echoFunN %%i
606 rem file extension
607 for %%i in ("O. P.OOL" Q.TABC hello) do call :echoFunX %%i
608 rem path with short path names
609 for %%I in ("R S" T ABCDEFGHIJK.LMNOP) do call :echoFunS %%I
610 rem NT4 aborts whole script execution when encountering ~a, ~t and ~z substitutions, preventing full testing
611 rem combined options
612 for %%i in ("d e" f) do call :echoFunDP %%i
613 for %%i in ("g h" i) do call :echoFunSD %%i
614 for %%i in ("g h" i) do call :echoFunDS %%i
615 for %%i in ("j k" l.eh) do call :echoFunXS %%i
616
617 goto :endEchoFuns
618 :echoFun
619 echo %1
620 goto :eof
621
622 :echoFunQ
623 echo '%~1'
624 goto :eof
625
626 :echoFunF
627 echo %~f1
628 goto :eof
629
630 :echoFunD
631 echo %~d1
632 goto :eof
633
634 :echoFunP
635 echo %~p1
636 goto :eof
637
638 :echoFunN
639 echo %~n1
640 goto :eof
641
642 :echoFunX
643 echo '%~x1'
644 goto :eof
645
646 :echoFunS
647 rem some NT4 workaround
648 set WINE_VAR='%~s1'
649 echo %WINE_VAR%
650 set WINE_VAR=
651 goto :eof
652
653 :echoFunDP
654 echo %~dp1
655 goto :eof
656
657 :echoFunSD
658 echo %~sd1
659 goto :eof
660
661 :echoFunDS
662 echo %~ds1
663 goto :eof
664
665 :echoFunXS
666 echo '%~xs1'
667 goto :eof
668 :endEchoFuns
669
670 echo ------------ Testing variable delayed expansion ------------
671 rem NT4 doesn't support this
672 echo --- default mode (load-time expansion)
673 set WINE_FOO=foo
674 echo %WINE_FOO%
675 echo !WINE_FOO!
676 if %WINE_FOO% == foo (
677 set WINE_FOO=bar
678 if %WINE_FOO% == bar (echo bar) else echo foo
679 )
680
681 set WINE_FOO=foo
682 if %WINE_FOO% == foo (
683 set WINE_FOO=bar
684 if !WINE_FOO! == bar (echo bar) else echo foo
685 )
686
687 echo --- runtime (delayed) expansion mode
688 setlocal EnableDelayedExpansion
689 set WINE_FOO=foo
690 echo %WINE_FOO%
691 echo !WINE_FOO!
692 if %WINE_FOO% == foo (
693 set WINE_FOO=bar
694 if %WINE_FOO% == bar (echo bar) else echo foo
695 )
696
697 set WINE_FOO=foo
698 if %WINE_FOO% == foo (
699 set WINE_FOO=bar
700 if !WINE_FOO! == bar (echo bar) else echo foo
701 )
702 echo %ErrorLevel%
703 setlocal DisableDelayedExpansion
704 echo %ErrorLevel%
705 set WINE_FOO=foo
706 echo %WINE_FOO%
707 echo !WINE_FOO!
708 set WINE_FOO=
709 echo --- using /V cmd flag
710 echo @echo off> tmp.cmd
711 echo set WINE_FOO=foo>> tmp.cmd
712 echo echo %%WINE_FOO%%>> tmp.cmd
713 echo echo !WINE_FOO!>> tmp.cmd
714 echo set WINE_FOO=>> tmp.cmd
715 cmd /V:ON /C tmp.cmd
716 cmd /V:OfF /C tmp.cmd
717 del tmp.cmd
718
719 echo ------------ Testing conditional execution ------------
720 echo --- unconditional ampersand
721 call :setError 123 & echo foo1
722 echo bar2 & echo foo2
723 mkdir foobar & cd foobar
724 echo > foobazbar
725 cd .. & rd /s/q foobar
726 if exist foobazbar (
727 echo foobar not deleted!
728 cd ..
729 rd /s/q foobar
730 ) else echo foobar deleted
731 echo --- on success conditional and
732 call :setError 456 && echo foo3 > foo3
733 if exist foo3 (
734 echo foo3 created
735 del foo3
736 ) else echo foo3 not created
737 echo bar4 && echo foo4
738 echo --- on failure conditional or
739 call :setError 789 || echo foo5
740 echo foo6 || echo bar6 > bar6
741 if exist bar6 (
742 echo bar6 created
743 del bar6
744 )
745
746 echo ------------ Testing cd ------------
747 mkdir foobar
748 cd foobar
749 echo blabla > singleFile
750 dir /b
751 echo Current dir: %CD%
752 cd
753 cd ..
754 cd
755 cd foobar@space@
756 cd
757 cd ..
758 cd
759 cd @space@foobar
760 cd
761 cd..
762 cd
763 cd foobar
764 cd..@space@
765 cd
766 if not exist foobar (cd ..)
767 cd foobar
768 cd@tab@..@tab@@space@@tab@
769 cd
770 if not exist foobar (cd ..)
771 cd foobar
772 mkdir "bar bak"
773 cd "bar bak"
774 cd
775 cd ..
776 cd ".\bar bak"
777 cd
778 cd ..
779 cd .\"bar bak"
780 cd
781 cd ..
782 cd bar bak
783 cd
784 cd "bar bak@space@"@tab@@space@
785 cd
786 cd ..\..
787 cd
788 rd /Q/s foobar
789 mkdir foobar
790 cd /d@tab@foobar
791 cd
792 cd ..
793 rd /q/s foobar
794
795 echo ------------ Testing type ------------
796 echo bar> foobaz
797 @echo on
798 type foobaz
799 echo ---
800 @echo off
801 type foobaz@tab@
802 echo ---1
803 type ."\foobaz"
804 echo ---2
805 type ".\foobaz"
806 echo ---3
807 del foobaz
808
809 echo ------------ Testing NUL ------------
810 md foobar & cd foobar
811 rem NUL file (non) creation + case insensitivity
812 rem Note: "if exist" does not work with NUL, so to check for file existence we use a kludgy workaround
813 echo > bar
814 echo foo > NUL
815 dir /b /a-d
816 echo foo > nul
817 dir /b /a-d
818 echo foo > NuL
819 @tab@dir /b@tab@/a-d
820 del bar
821 rem NUL not special everywhere
822 call :setError 123
823 echo NUL> foo
824 if not exist foo (echo foo should have been created) else (
825 type foo
826 del foo
827 )
828 rem Empty file creation
829 copy nul foo > nul
830 if exist foo (
831 echo foo created
832 del foo
833 type foo
834 ) else (
835 echo ***
836 )
837 echo 1234 >a.a
838 copy a.a+NUL b.b >nul
839 call :CheckFileSize a.a 7 b.b 8
840 copy NUL+a.a b.b >nul
841 call :CheckFileSize a.a 7 b.b 8
842 mkdir subdir
843 copy a.a+NUL subdir\ >nul
844 call :CheckFileSize a.a 7 subdir\a.a 8
845 del subdir\a.a
846 cd subdir
847 copy ..\a.a NUL >nul
848 if exist a.a echo Failed
849 cd ..
850 rd subdir /s /q
851 del a.a b.b
852 cd .. & rd foobar /s /q
853
854 echo ------------ Testing if/else ------------
855 echo --- if/else should work with blocks
856 if 0 == 0 (
857 echo if seems to work
858 ) else (
859 echo if seems to be broken
860 )
861 if 1 == 0 (
862 echo else seems to be broken
863 ) else (
864 echo else seems to work
865 )
866 if /c==/c (
867 echo if seems not to detect /c as parameter
868 ) else (
869 echo parameter detection seems to be broken
870 )
871 SET elseIF=0
872 if 1 == 1 (
873 SET /a elseIF=%elseIF%+1
874 ) else if 1 == 1 (
875 SET /a elseIF=%elseIF%+2
876 ) else (
877 SET /a elseIF=%elseIF%+2
878 )
879 if %elseIF% == 1 (
880 echo else if seems to work
881 ) else (
882 echo else if seems to be broken
883 )
884 SET elseIF=0
885 if 1 == 2 (
886 SET /a elseIF=%elseIF%+2
887 ) else if 1 == 1 (
888 SET /a elseIF=%elseIF%+1
889 ) else (
890 SET /a elseIF=%elseIF%+2
891 )
892 if %elseIF% == 1 (
893 echo else if seems to work
894 ) else (
895 echo else if seems to be broken
896 )
897 SET elseIF=0
898 if 1 == 2 (
899 SET /a elseIF=%elseIF%+2
900 ) else if 1 == 2 (
901 SET /a elseIF=%elseIF%+2
902 ) else (
903 SET /a elseIF=%elseIF%+1
904 )
905 if %elseIF% == 1 (
906 echo else if seems to work
907 ) else (
908 echo else if seems to be broken
909 )
910 echo --- case sensitivity with and without /i option
911 if bar==BAR echo if does not default to case sensitivity
912 if not bar==BAR echo if seems to default to case sensitivity
913 if /i foo==FOO echo if /i seems to work
914 if /i not foo==FOO echo if /i seems to be broken
915 if /I foo==FOO echo if /I seems to work
916 if /I not foo==FOO echo if /I seems to be broken
917
918 echo --- string comparisons
919 if abc == abc (echo equal) else echo non equal
920 if abc =="abc" (echo equal) else echo non equal
921 if "abc"== abc (echo equal) else echo non equal
922 if "abc"== "abc" (echo equal) else echo non equal
923
924 echo --- tabs handling
925 if@tab@1==1 echo doom
926 if @tab@1==1 echo doom
927 if 1==1 (echo doom) else@tab@echo quake
928 if@tab@not @tab@1==@tab@0 @tab@echo lol
929 if 1==0@tab@(echo doom) else echo quake
930 if 1==0 (echo doom)@tab@else echo quake
931 if 1==0 (echo doom) else@tab@echo quake
932
933 echo --- comparison operators
934 rem NT4 misevaluates conditionals in for loops so we have to use subroutines as workarounds
935 echo ------ for strings
936 rem NT4 stops processing of the whole batch file as soon as it finds a
937 rem comparison operator non fully uppercased, such as lss instead of LSS, so we
938 rem can't test those here.
939 if LSS LSS LSSfoo (echo LSS string can be used as operand for LSS comparison)
940 if LSS LSS LSS (echo bar)
941 if 1.1 LSS 1.10 (echo floats are handled as strings)
942 if "9" LSS "10" (echo numbers in quotes recognized!) else echo numbers in quotes are handled as strings
943 if not "-1" LSS "1" (echo negative numbers as well) else echo NT4
944 if /i foo LSS FoOc echo if /i seems to work for LSS
945 if /I not foo LSS FOOb echo if /I seems to be broken for LSS
946 set WINE_STR_PARMS=A B AB BA AA
947 for %%i in (%WINE_STR_PARMS%) do (
948 for %%j in (%WINE_STR_PARMS%) do (
949 call :LSStest %%i %%j))
950 if b LSS B (echo b LSS B) else echo NT4
951 if /I b LSS B echo b LSS B insensitive
952 if b LSS A echo b LSS A
953 if /I b LSS A echo b LSS A insensitive
954 if a LSS B (echo a LSS B) else echo NT4
955 if /I a LSS B echo a LSS B insensitive
956 if A LSS b echo A LSS b
957 if /I A LSS b echo A LSS b insensitive
958 for %%i in (%WINE_STR_PARMS%) do (
959 for %%j in (%WINE_STR_PARMS%) do (
960 call :LEQtest %%i %%j))
961 if b LEQ B (echo b LEQ B) else echo NT4
962 if /I b LEQ B echo b LEQ B insensitive
963 if b LEQ A echo b LEQ A
964 if /I b LEQ A echo b LEQ A insensitive
965 if a LEQ B (echo a LEQ B) else echo NT4
966 if /I a LEQ B echo a LEQ B insensitive
967 if A LEQ b echo A LEQ b
968 if /I A LEQ b echo A LEQ b insensitive
969 for %%i in (%WINE_STR_PARMS%) do (
970 for %%j in (%WINE_STR_PARMS%) do (
971 call :EQUtest %%i %%j))
972 if /I A EQU a echo A EQU a insensitive
973 for %%i in (%WINE_STR_PARMS%) do (
974 for %%j in (%WINE_STR_PARMS%) do (
975 call :NEQtest %%i %%j))
976 for %%i in (%WINE_STR_PARMS%) do (
977 for %%j in (%WINE_STR_PARMS%) do (
978 call :GEQtest %%i %%j))
979 for %%i in (%WINE_STR_PARMS%) do (
980 for %%j in (%WINE_STR_PARMS%) do (
981 call :GTRtest %%i %%j))
982 echo ------ for numbers
983 if -1 LSS 1 (echo negative numbers handled)
984 if not -1 LSS -10 (echo negative numbers handled)
985 if not 9 LSS 010 (echo octal handled)
986 if not -010 LSS -8 (echo also in negative form)
987 if 4 LSS 0x5 (echo hexa handled)
988 if not -1 LSS -0x1A (echo also in negative form)
989 if 11 LSS 101 (echo 11 LSS 101)
990 set WINE_INT_PARMS=0 1 10 9
991 for %%i in (%WINE_INT_PARMS%) do (
992 for %%j in (%WINE_INT_PARMS%) do (
993 call :LSStest %%i %%j))
994 for %%i in (%WINE_INT_PARMS%) do (
995 for %%j in (%WINE_INT_PARMS%) do (
996 call :LEQtest %%i %%j))
997 for %%i in (%WINE_INT_PARMS%) do (
998 for %%j in (%WINE_INT_PARMS%) do (
999 call :EQUtest %%i %%j))
1000 if 011 EQU 9 (echo octal ok)
1001 if 0xA1 EQU 161 (echo hexa ok)
1002 if 0xA1 EQU "161" (echo hexa should be recognized) else (echo string/hexa compare ok)
1003 if "0xA1" EQU 161 (echo hexa should be recognized) else (echo string/hexa compare ok)
1004 for %%i in (%WINE_INT_PARMS%) do (
1005 for %%j in (%WINE_INT_PARMS%) do (
1006 call :NEQtest %%i %%j))
1007 for %%i in (%WINE_INT_PARMS%) do (
1008 for %%j in (%WINE_INT_PARMS%) do (
1009 call :GEQtest %%i %%j))
1010 for %%i in (%WINE_INT_PARMS%) do (
1011 for %%j in (%WINE_INT_PARMS%) do (
1012 call :GTRtest %%i %%j))
1013 echo ------ for numbers and stringified numbers
1014 if not "1" EQU 1 (echo strings and integers not equal) else echo foo
1015 if not 1 EQU "1" (echo strings and integers not equal) else echo foo
1016 if '1' EQU 1 echo '1' EQU 1
1017 if 1 EQU '1' echo 1 EQU '1'
1018 if not "1" GEQ 1 (echo foo) else echo bar
1019 if "10" GEQ "1" echo "10" GEQ "1"
1020 if '1' GEQ 1 (echo '1' GEQ 1) else echo NT4
1021 if 1 GEQ "1" echo 1 GEQ "1"
1022 if "1" GEQ "1" echo "1" GEQ "1"
1023 if '1' GEQ "1" echo '1' GEQ "1"
1024 if "10" GEQ "1" echo "10" GEQ "1"
1025 if not 1 GEQ '1' (echo non NT4) else echo 1 GEQ '1'
1026 for %%i in ("1" '1') do call :GEQtest %%i '1'
1027 if "10" GEQ '1' (echo "10" GEQ '1') else echo foo
1028 if 1 GEQ "10" (echo 1 GEQ "10") else echo foo
1029 if "1" GEQ "10" (echo 1 GEQ "10") else echo foo
1030 if '1' GEQ "10" (echo '1' GEQ "10") else echo foo
1031 if "10" GEQ "10" (echo "10" GEQ "10")
1032 echo --- unconditional ampersand after if one line
1033 if "0"=="0" echo 1 & echo 2 & echo 3 else echo 4
1034 echo ---
1035 echo x & if "0"=="1" echo 1 & echo 2
1036 echo ---
1037 echo x & if "0"=="1" echo 1 & echo 2 & echo 3
1038 echo ---
1039 echo x & if "0"=="1" (echo 1 & echo 2 & echo 3)
1040 echo ---
1041 echo x & if "0"=="1" echo 1 & echo 2 & echo 3 else echo 4
1042 echo ---
1043 goto :endIfCompOpsSubroutines
1044
1045 rem IF subroutines helpers
1046 :LSStest
1047 if %1 LSS %2 echo %1 LSS %2
1048 goto :eof
1049 :LEQtest
1050 if %1 LEQ %2 echo %1 LEQ %2
1051 goto :eof
1052 :EQUtest
1053 if %1 EQU %2 echo %1 EQU %2
1054 goto :eof
1055 :NEQtest
1056 if %1 NEQ %2 echo %1 NEQ %2
1057 goto :eof
1058 :GEQtest
1059 if %1 GEQ %2 echo %1 GEQ %2
1060 goto :eof
1061 :GTRtest
1062 if %1 GTR %2 echo %1 GTR %2
1063 goto :eof
1064
1065 :endIfCompOpsSubroutines
1066 set WINE_STR_PARMS=
1067 set WINE_INT_PARMS=
1068
1069 echo ------------ Testing for ------------
1070 echo --- plain FOR
1071 for %%i in (A B C) do echo %%i
1072 for %%i in (A B C) do echo %%I
1073 for %%i in (A B C) do echo %%j
1074 for %%i in (A B C) do call :forTestFun1 %%i
1075 for %%i in (1,4,1) do echo %%i
1076 for %%i in (A, B,C) do echo %%i
1077 for %%i in (X) do echo %%i
1078 for@tab@%%i in (X2) do echo %%i
1079 for %%i in@tab@(X3) do echo %%i
1080 for %%i in (@tab@ foo@tab@) do echo %%i
1081 for@tab@ %%i in@tab@(@tab@M) do echo %%i
1082 for %%i@tab@in (X)@tab@do@tab@echo %%i
1083 for@tab@ %%j in@tab@(@tab@M, N, O@tab@) do echo %%j
1084 for %%i in (`echo A B`) do echo %%i
1085 for %%i in ('echo A B') do echo %%i
1086 for %%i in ("echo A B") do echo %%i
1087 for %%i in ("A B" C) do echo %%i
1088 goto :endForTestFun1
1089 :forTestFun1
1090 echo %1
1091 goto :eof
1092 :endForTestFun1
1093 echo --- imbricated FORs
1094 for %%i in (X) do (
1095 for %%j in (Y) do (
1096 echo %%i %%j))
1097 for %%i in (X) do (
1098 for %%I in (Y) do (
1099 echo %%i %%I))
1100 for %%i in (A B) do (
1101 for %%j in (C D) do (
1102 echo %%i %%j))
1103 for %%i in (A B) do (
1104 for %%j in (C D) do (
1105 call :forTestFun2 %%i %%j ))
1106 goto :endForTestFun2
1107 :forTestFun2
1108 echo %1 %2
1109 goto :eof
1110 :endForTestFun2
1111 mkdir foobar & cd foobar
1112 mkdir foo
1113 mkdir bar
1114 mkdir baz
1115 echo > bazbaz
1116 echo --- basic wildcards
1117 for %%i in (ba*) do echo %%i
1118 echo --- for /d
1119 for /d %%i in (baz foo bar) do echo %%i 2>&1
1120 rem Confirm we don't match files:
1121 for /d %%i in (bazb*) do echo %%i 2>&1
1122 for /d %%i in (bazb2*) do echo %%i 2>&1
1123 rem Show we pass through non wildcards
1124 for /d %%i in (PASSED) do echo %%i
1125 for /d %%i in (xxx) do (
1126 echo %%i - Should be xxx
1127 echo Expected second line
1128 )
1129 rem Show we issue no messages on failures
1130 for /d %%i in (FAILED?) do echo %%i 2>&1
1131 for /d %%i in (FAILED?) do (
1132 echo %%i - Unexpected!
1133 echo FAILED Unexpected second line
1134 )
1135 for /d %%i in (FAILED*) do echo %%i 2>&1
1136 for /d %%i in (FAILED*) do (
1137 echo %%i - Unexpected!
1138 echo FAILED Unexpected second line
1139 )
1140 rem FIXME can't test wildcard expansion here since it's listed in directory
1141 rem order, and not in alphabetic order.
1142 rem Proper testing would need a currently missing "sort" program implementation.
1143 rem for /d %%i in (ba*) do echo %%i>> tmp
1144 rem sort < tmp
1145 rem del tmp
1146 rem for /d %%i in (?a*) do echo %%i>> tmp
1147 rem sort < tmp
1148 rem del tmp
1149 rem for /d %%i in (*) do echo %%i>> tmp
1150 rem sort < tmp
1151 rem del tmp
1152 echo > baz\bazbaz
1153 goto :TestForR
1154
1155 :SetExpected
1156 del temp.bat 2>nul
1157 call :WriteLine set WINE_found=N
1158 for /l %%i in (1,1,%WINE_expectedresults%) do (
1159 call :WriteLine if "%%%%WINE_expectedresults.%%i%%%%"=="%%%%~1" set WINE_found=Y
1160 call :WriteLine if "%%%%WINE_found%%%%"=="Y" set WINE_expectedresults.%%i=
1161 call :WriteLine if "%%%%WINE_found%%%%"=="Y" goto :eof
1162 )
1163 call :WriteLine echo Got unexpected result: "%%%%~1"
1164 goto :eof
1165
1166 :WriteLine
1167 echo %*>> temp.bat
1168 goto :EOF
1169
1170 :ValidateExpected
1171 del temp.bat 2>nul
1172 for /l %%i in (1,1,%WINE_expectedresults%) do (
1173 call :WriteLine if not "%%%%WINE_expectedresults.%%i%%%%"=="" echo Found missing result: "%%%%WINE_expectedresults.%%i%%%%"
1174 )
1175 call temp.bat
1176 del temp.bat 2>nul
1177 goto :eof
1178
1179 :TestForR
1180 rem %CD% does not work on NT4 so use the following workaround
1181 for /d %%i in (.) do set WINE_CURDIR=%%~dpnxi
1182
1183 echo --- for /R
1184 echo Plain directory enumeration
1185 set WINE_expectedresults=4
1186 set WINE_expectedresults.1=%WINE_CURDIR%\.
1187 set WINE_expectedresults.2=%WINE_CURDIR%\bar\.
1188 set WINE_expectedresults.3=%WINE_CURDIR%\baz\.
1189 set WINE_expectedresults.4=%WINE_CURDIR%\foo\.
1190 call :SetExpected
1191 for /R %%i in (.) do call temp.bat "%%i"
1192 call :ValidateExpected
1193
1194 echo Plain directory enumeration from provided root
1195 set WINE_expectedresults=4
1196 set WINE_expectedresults.1=%WINE_CURDIR%\.
1197 set WINE_expectedresults.2=%WINE_CURDIR%\bar\.
1198 set WINE_expectedresults.3=%WINE_CURDIR%\baz\.
1199 set WINE_expectedresults.4=%WINE_CURDIR%\foo\.
1200 if "%CD%"=="" goto :SkipBrokenNT4
1201 call :SetExpected
1202 for /R "%WINE_CURDIR%" %%i in (.) do call temp.bat "%%i"
1203 call :ValidateExpected
1204 :SkipBrokenNT4
1205
1206 echo File enumeration
1207 set WINE_expectedresults=2
1208 set WINE_expectedresults.1=%WINE_CURDIR%\baz\bazbaz
1209 set WINE_expectedresults.2=%WINE_CURDIR%\bazbaz
1210 call :SetExpected
1211 for /R %%i in (baz*) do call temp.bat "%%i"
1212 call :ValidateExpected
1213
1214 echo File enumeration from provided root
1215 set WINE_expectedresults=2
1216 set WINE_expectedresults.1=%WINE_CURDIR%\baz\bazbaz
1217 set WINE_expectedresults.2=%WINE_CURDIR%\bazbaz
1218 call :SetExpected
1219 for /R %%i in (baz*) do call temp.bat "%%i"
1220 call :ValidateExpected
1221
1222 echo Mixed enumeration
1223 set WINE_expectedresults=6
1224 set WINE_expectedresults.1=%WINE_CURDIR%\.
1225 set WINE_expectedresults.2=%WINE_CURDIR%\bar\.
1226 set WINE_expectedresults.3=%WINE_CURDIR%\baz\.
1227 set WINE_expectedresults.4=%WINE_CURDIR%\baz\bazbaz
1228 set WINE_expectedresults.5=%WINE_CURDIR%\bazbaz
1229 set WINE_expectedresults.6=%WINE_CURDIR%\foo\.
1230 call :SetExpected
1231 for /R %%i in (. baz*) do call temp.bat "%%i"
1232 call :ValidateExpected
1233
1234 echo Mixed enumeration from provided root
1235 set WINE_expectedresults=6
1236 set WINE_expectedresults.1=%WINE_CURDIR%\.
1237 set WINE_expectedresults.2=%WINE_CURDIR%\bar\.
1238 set WINE_expectedresults.3=%WINE_CURDIR%\baz\.
1239 set WINE_expectedresults.4=%WINE_CURDIR%\baz\bazbaz
1240 set WINE_expectedresults.5=%WINE_CURDIR%\bazbaz
1241 set WINE_expectedresults.6=%WINE_CURDIR%\foo\.
1242 call :SetExpected
1243 for /R %%i in (. baz*) do call temp.bat "%%i"
1244 call :ValidateExpected
1245
1246 echo With duplicates enumeration
1247 set WINE_expectedresults=12
1248 set WINE_expectedresults.1=%WINE_CURDIR%\bar\bazbaz
1249 set WINE_expectedresults.2=%WINE_CURDIR%\bar\fred
1250 set WINE_expectedresults.3=%WINE_CURDIR%\baz\bazbaz
1251 set WINE_expectedresults.4=%WINE_CURDIR%\baz\bazbaz
1252 set WINE_expectedresults.5=%WINE_CURDIR%\baz\bazbaz
1253 set WINE_expectedresults.6=%WINE_CURDIR%\baz\fred
1254 set WINE_expectedresults.7=%WINE_CURDIR%\bazbaz
1255 set WINE_expectedresults.8=%WINE_CURDIR%\bazbaz
1256 set WINE_expectedresults.9=%WINE_CURDIR%\bazbaz
1257 set WINE_expectedresults.10=%WINE_CURDIR%\foo\bazbaz
1258 set WINE_expectedresults.11=%WINE_CURDIR%\foo\fred
1259 set WINE_expectedresults.12=%WINE_CURDIR%\fred
1260 call :SetExpected
1261 for /R %%i in (baz* bazbaz fred ba*) do call temp.bat "%%i"
1262 call :ValidateExpected
1263
1264 echo Strip missing wildcards, keep unwildcarded names
1265 set WINE_expectedresults=6
1266 set WINE_expectedresults.1=%WINE_CURDIR%\bar\jim
1267 set WINE_expectedresults.2=%WINE_CURDIR%\baz\bazbaz
1268 set WINE_expectedresults.3=%WINE_CURDIR%\baz\jim
1269 set WINE_expectedresults.4=%WINE_CURDIR%\bazbaz
1270 set WINE_expectedresults.5=%WINE_CURDIR%\foo\jim
1271 set WINE_expectedresults.6=%WINE_CURDIR%\jim
1272 call :SetExpected
1273 for /R %%i in (baz* fred* jim) do call temp.bat "%%i"
1274 call :ValidateExpected
1275
1276 echo for /R passed
1277 echo --- Complex wildcards unix and windows slash
1278 cd ..
1279 echo Windows slashes, valid path
1280 for %%f in (foobar\baz\bazbaz) do echo ASIS: %%f
1281 for %%f in (foobar\baz\*) do echo WC : %%f
1282 echo Windows slashes, invalid path
1283 for %%f in (foobar\jim\bazbaz) do echo ASIS: %%f
1284 for %%f in (foobar\jim\*) do echo WC : %%f
1285 echo Unix slashes, valid path
1286 for %%f in (foobar/baz/bazbaz) do echo ASIS: %%f
1287 for %%f in (foobar/baz/*) do echo WC : %%f
1288 echo Unix slashes, invalid path
1289 for %%f in (foobar/jim/bazbaz) do echo ASIS: %%f
1290 for %%f in (foobar/jim/*) do echo WC : %%f
1291 echo Done
1292 rd /s/Q foobar
1293 echo --- for /L
1294 rem Some cases loop forever writing 0s, like e.g. (1,0,1), (1,a,3) or (a,b,c); those can't be tested here
1295 for /L %%i in (1,2,0) do echo %%i
1296 for@tab@/L %%i in (1,2,0) do echo %%i
1297 for /L %%i in (1,2,6) do echo %%i
1298 for /l %%i in (1 ,2,6) do echo %%i
1299 for /L %%i in (a,2,3) do echo %%i
1300 for /L %%i in (1,2,-1) do echo %%i
1301 for /L %%i in (-4,-1,-1) do echo %%i
1302 for /L %%i in (1,-2,-2) do echo %%i
1303 for /L %%i in (1,2,a) do echo %%i
1304 echo ErrorLevel %ErrorLevel%
1305 for /L %%i in (1,a,b) do echo %%i
1306 echo ErrorLevel %ErrorLevel%
1307 rem Test boundaries
1308 for /l %%i in (1,1,4) do echo %%i
1309 for /l %%i in (1,2,4) do echo %%i
1310 for /l %%i in (4,-1,1) do echo %%i
1311 for /l %%i in (4,-2,1) do echo %%i
1312 for /l %%i in (1,-1,4) do echo %%i
1313 for /l %%i in (4,1,1) do echo %%i
1314 for /L %%i in (a,2,b) do echo %%i
1315 for /L %%i in (1,1,1) do echo %%i
1316 for /L %%i in (1,-2,-1) do echo %%i
1317 for /L %%i in (-1,-1,-1) do echo %%i
1318 for /L %%i in (1,2, 3) do echo %%i
1319 rem Test zero iteration skips the body of the for
1320 for /L %%i in (2,2,1) do (
1321 echo %%i
1322 echo FAILED
1323 )
1324 echo --- set /a
1325 goto :testseta
1326
1327 Rem Ideally for /f can be used rather than building a command to execute
1328 rem but that does not work on NT4
1329 :checkenvvars
1330 if "%1"=="" goto :eof
1331 call :executecmd set wine_result=%%%1%%
1332 if "%wine_result%"=="%2" (
1333 echo %1 correctly %2
1334 ) else echo ERROR: %1 incorrectly %wine_result% [%2]
1335 set %1=
1336 shift
1337 shift
1338 rem shift
1339 goto :checkenvvars
1340 :executecmd
1341 %*
1342 goto :eof
1343
1344 :testseta
1345 rem No output when using "set expr" syntax, unless in interactive mode
1346 rem Need to use "set envvar=expr" to use in a batch script
1347 echo ------ individual operations
1348 set WINE_foo=0
1349 set /a WINE_foo=1 +2 & call :checkenvvars WINE_foo 3
1350 set /a WINE_foo=1 +-2 & call :checkenvvars WINE_foo -1
1351 set /a WINE_foo=1 --2 & call :checkenvvars WINE_foo 3
1352 set /a WINE_foo=2* 3 & call :checkenvvars WINE_foo 6
1353 set /a WINE_foo=-2* -5 & call :checkenvvars WINE_foo 10
1354 set /a WINE_foo=12/3 & call :checkenvvars WINE_foo 4
1355 set /a WINE_foo=13/3 & call :checkenvvars WINE_foo 4
1356 set /a WINE_foo=-13/3 & call :checkenvvars WINE_foo -4
1357 rem FIXME Divide by zero should return an error, but error messages cannot be tested with current infrastructure
1358 set /a WINE_foo=5 %% 5 & call :checkenvvars WINE_foo 0
1359 set /a WINE_foo=5 %% 3 & call :checkenvvars WINE_foo 2
1360 set /a WINE_foo=5 %% -3 & call :checkenvvars WINE_foo 2
1361 set /a WINE_foo=-5 %% -3 & call :checkenvvars WINE_foo -2
1362 set /a WINE_foo=1 ^<^< 0 & call :checkenvvars WINE_foo 1
1363 set /a WINE_foo=1 ^<^< 2 & call :checkenvvars WINE_foo 4
1364 set /a WINE_foo=1 ^<^< -2 & call :checkenvvars WINE_foo 0
1365 set /a WINE_foo=-1 ^<^< -2 & call :checkenvvars WINE_foo 0
1366 set /a WINE_foo=-1 ^<^< 2 & call :checkenvvars WINE_foo -4
1367 set /a WINE_foo=9 ^>^> 0 & call :checkenvvars WINE_foo 9
1368 set /a WINE_foo=9 ^>^> 2 & call :checkenvvars WINE_foo 2
1369 set /a WINE_foo=9 ^>^> -2 & call :checkenvvars WINE_foo 0
1370 set /a WINE_foo=-9 ^>^> -2 & call :checkenvvars WINE_foo -1
1371 set /a WINE_foo=-9 ^>^> 2 & call :checkenvvars WINE_foo -3
1372 set /a WINE_foo=5 ^& 0 & call :checkenvvars WINE_foo 0
1373 set /a WINE_foo=5 ^& 1 & call :checkenvvars WINE_foo 1
1374 set /a WINE_foo=5 ^& 3 & call :checkenvvars WINE_foo 1
1375 set /a WINE_foo=5 ^& 4 & call :checkenvvars WINE_foo 4
1376 set /a WINE_foo=5 ^& 1 & call :checkenvvars WINE_foo 1
1377 set /a WINE_foo=5 ^| 0 & call :checkenvvars WINE_foo 5
1378 set /a WINE_foo=5 ^| 1 & call :checkenvvars WINE_foo 5
1379 set /a WINE_foo=5 ^| 3 & call :checkenvvars WINE_foo 7
1380 set /a WINE_foo=5 ^| 4 & call :checkenvvars WINE_foo 5
1381 set /a WINE_foo=5 ^| 1 & call :checkenvvars WINE_foo 5
1382 set /a WINE_foo=5 ^^ 0 & call :checkenvvars WINE_foo 5
1383 set /a WINE_foo=5 ^^ 1 & call :checkenvvars WINE_foo 4
1384 set /a WINE_foo=5 ^^ 3 & call :checkenvvars WINE_foo 6
1385 set /a WINE_foo=5 ^^ 4 & call :checkenvvars WINE_foo 1
1386 set /a WINE_foo=5 ^^ 1 & call :checkenvvars WINE_foo 4
1387 echo ------ precedence and grouping
1388 set /a WINE_foo=4 + 2*3 & call :checkenvvars WINE_foo 10
1389 set /a WINE_foo=(4+2)*3 & call :checkenvvars WINE_foo 18
1390 set /a WINE_foo=4 * 3/5 & call :checkenvvars WINE_foo 2
1391 set /a WINE_foo=(4 * 3)/5 & call :checkenvvars WINE_foo 2
1392 set /a WINE_foo=4 * 5 %% 4 & call :checkenvvars WINE_foo 0
1393 set /a WINE_foo=4 * (5 %% 4) & call :checkenvvars WINE_foo 4
1394 set /a WINE_foo=3 %% (5 + 8 %% 3 ^^ 2) & call :checkenvvars WINE_foo 3
1395 set /a WINE_foo=3 %% (5 + 8 %% 3 ^^ -2) & call :checkenvvars WINE_foo 3
1396 echo ------ octal and hexadecimal
1397 set /a WINE_foo=0xf + 3 & call :checkenvvars WINE_foo 18
1398 set /a WINE_foo=0xF + 3 & call :checkenvvars WINE_foo 18
1399 set /a WINE_foo=015 + 2 & call :checkenvvars WINE_foo 15
1400 set /a WINE_foo=3, 8+3,0 & call :checkenvvars WINE_foo 3
1401 echo ------ variables
1402 set /a WINE_foo=WINE_bar=3, WINE_bar+1 & call :checkenvvars WINE_foo 3 WINE_bar 3
1403 set /a WINE_foo=WINE_bar=3, WINE_bar+=1 & call :checkenvvars WINE_foo 3 WINE_bar 4
1404 set /a WINE_foo=WINE_bar=3, WINE_baz=1, WINE_baz+=WINE_bar, WINE_baz & call :checkenvvars WINE_foo 3 WINE_bar 3 WINE_baz 4
1405 set WINE_bar=3
1406 set /a WINE_foo=WINE_bar*= WINE_bar & call :checkenvvars WINE_foo 9 WINE_bar 9
1407 set /a WINE_foo=WINE_whateverNonExistingVar & call :checkenvvars WINE_foo 0
1408 set WINE_bar=4
1409 set /a WINE_foo=WINE_whateverNonExistingVar + WINE_bar & call :checkenvvars WINE_foo 4 WINE_bar 4
1410 set WINE_bar=4
1411 set /a WINE_foo=WINE_bar -= WINE_bar + 7 & call :checkenvvars WINE_foo -7 WINE_bar -7
1412 set WINE_bar=-7
1413 set /a WINE_foo=WINE_bar /= 3 + 2 & call :checkenvvars WINE_foo -1 WINE_bar -1
1414 set /a WINE_foo=WINE_bar=5, WINE_bar %%=2 & call :checkenvvars WINE_foo 5 WINE_bar 1
1415 set WINE_bar=1
1416 set /a WINE_foo=WINE_bar ^<^<= 2 & call :checkenvvars WINE_foo 4 WINE_bar 4
1417 set WINE_bar=4
1418 set /a WINE_foo=WINE_bar ^>^>= 2 & call :checkenvvars WINE_foo 1 WINE_bar 1
1419 set WINE_bar=1
1420 set /a WINE_foo=WINE_bar ^&= 2 & call :checkenvvars WINE_foo 0 WINE_bar 0
1421 set /a WINE_foo=WINE_bar=5, WINE_bar ^|= 2 & call :checkenvvars WINE_foo 5 WINE_bar 7
1422 set /a WINE_foo=WINE_bar=5, WINE_bar ^^= 2 & call :checkenvvars WINE_foo 5 WINE_bar 7
1423 set WINE_baz=4
1424 set /a WINE_foo=WINE_bar=19, WINE_bar %%= 4 + (WINE_baz %%= 7) & call :checkenvvars WINE_foo 19 WINE_bar 3 WINE_baz 4
1425 echo --- quotes
1426 set /a WINE_foo=1
1427 call :checkenvvars WINE_foo 1
1428 set /a "WINE_foo=1"
1429 call :checkenvvars WINE_foo 1
1430 set /a WINE_foo=1,WINE_bar=2
1431 call :checkenvvars WINE_foo 1 WINE_bar 2
1432 set /a "WINE_foo=1,WINE_bar=2"
1433 call :checkenvvars WINE_foo 1 WINE_bar 2
1434 set /a "WINE_foo=1","WINE_bar=2"
1435 call :checkenvvars WINE_foo 1 WINE_bar 2
1436 set /a ""WINE_foo=1","WINE_bar=2""
1437 call :checkenvvars WINE_foo 1 WINE_bar 2
1438 set /a WINE_foo=1,WINE_bar=2,WINE_baz=3
1439 call :checkenvvars WINE_foo 1 WINE_bar 2 WINE_baz 3
1440 set /a "WINE_foo=1,WINE_bar=2,WINE_baz=3"
1441 call :checkenvvars WINE_foo 1 WINE_bar 2 WINE_baz 3
1442 set /a "WINE_foo=1","WINE_bar=2","WINE_baz=3"
1443 call :checkenvvars WINE_foo 1 WINE_bar 2 WINE_baz 3
1444 set /a ""WINE_foo=1","WINE_bar=2","WINE_baz=3""
1445 call :checkenvvars WINE_foo 1 WINE_bar 2 WINE_baz 3
1446 set /a ""WINE_foo=1","WINE_bar=2"","WINE_baz=3"
1447 call :checkenvvars WINE_foo 1 WINE_bar 2 WINE_baz 3
1448 set /a """"""WINE_foo=1""""""
1449 call :checkenvvars WINE_foo 1
1450 set /a """"""WINE_foo=1","WINE_bar=5""","WINE_baz=2""
1451 call :checkenvvars WINE_foo 1 WINE_bar 5 WINE_baz 2
1452 set /a WINE_foo="3"+"4"+"5+6"
1453 call :checkenvvars WINE_foo 18
1454 set WINE_foo=3
1455 set /a WINE_bar="WINE_""foo"+4
1456 call :checkenvvars WINE_foo 3 WINE_bar 7
1457 echo --- whitespace are ignored between double char operators
1458 set WINE_foo=4
1459 set WINE_bar=5
1460 set /a WINE_foo + = 6
1461 set /a WINE_bar * = WINE_foo
1462 call :checkenvvars WINE_foo 10 WINE_bar 50
1463 set WINE_foo=4
1464 set WINE_bar=5
1465 set /a WINE_foo + = "6 < < 7"
1466 set /a WINE_bar * = WINE_foo + WINE_foo
1467 call :checkenvvars WINE_foo 772 WINE_bar 7720
1468 set /a WINE_foo=6 7
1469 set /a WINE_ var1=8
1470 set WINE_foo=
1471 echo --- invalid operator sequence
1472 set WINE_foo=4
1473 set /a =4
1474 set /a *=4
1475 set /a ^>=4"
1476 set /a ^<=4"
1477 set /a WINE_foo^>^<=4
1478 echo %WINE_foo%
1479 set /a WINE_foo^>^>^>=4
1480 echo %WINE_foo%
1481 echo ----- negative prefix
1482 set /a WINE_foo=-1
1483 call :checkenvvars WINE_foo -1
1484 set /a WINE_foo=--1
1485 call :checkenvvars WINE_foo 1
1486 set /a WINE_foo=3--3
1487 call :checkenvvars WINE_foo 6
1488 set /a WINE_foo=3---3
1489 call :checkenvvars WINE_foo 0
1490 set /a WINE_foo=3----3
1491 call :checkenvvars WINE_foo 6
1492 set /a WINE_foo=-~1
1493 call :checkenvvars WINE_foo 2
1494 set /a WINE_foo=~-1
1495 call :checkenvvars WINE_foo 0
1496 set /a WINE_foo=3+-~1
1497 call :checkenvvars WINE_foo 5
1498 set /a WINE_foo=3+~-1
1499 call :checkenvvars WINE_foo 3
1500 echo ----- assignment tests involving the end destination
1501 set WINE_foo=3
1502 set /a WINE_foo+=3+(WINE_foo=4)
1503 call :checkenvvars WINE_foo 11
1504 set WINE_foo=2
1505 set /a WINE_bar=3+(WINE_foo=6)
1506 call :checkenvvars WINE_foo 6 WINE_bar 9
1507 set WINE_foo=2
1508 set /a WINE_bar=3+(WINE_foo=6,WINE_baz=7)
1509 call :checkenvvars WINE_foo 6 WINE_bar 10 WINE_baz 7
1510 set WINE_foo=2
1511 set /a WINE_bar=WINE_foo=7
1512 call :checkenvvars WINE_foo 7 WINE_bar 7
1513 echo ----- equal precedence on stack
1514 rem Unary - don't reduce if precedence is equal
1515 set /a WINE_foo=!!1
1516 call :checkenvvars WINE_foo 1
1517 set /a WINE_foo=!!0
1518 call :checkenvvars WINE_foo 0
1519 set /a WINE_foo=~~1
1520 call :checkenvvars WINE_foo 1
1521 set /a WINE_foo=~~0
1522 call :checkenvvars WINE_foo 0
1523 set /a WINE_foo=--1
1524 call :checkenvvars WINE_foo 1
1525 set /a WINE_foo=+-1
1526 call :checkenvvars WINE_foo -1
1527 set /a WINE_foo=-+1
1528 call :checkenvvars WINE_foo -1
1529 set /a WINE_foo=++1
1530 call :checkenvvars WINE_foo 1
1531 set /a WINE_foo=!~1
1532 call :checkenvvars WINE_foo 0
1533 set /a WINE_foo=~!1
1534 call :checkenvvars WINE_foo -1
1535 set /a WINE_foo=!-1
1536 call :checkenvvars WINE_foo 0
1537 set /a WINE_foo=-!1
1538 call :checkenvvars WINE_foo 0
1539 set /a WINE_foo=!-0
1540 call :checkenvvars WINE_foo 1
1541 set /a WINE_foo=-!0
1542 call :checkenvvars WINE_foo -1
1543 rem Aritmatic - Reduce if precedence is equal
1544 set /a WINE_foo=10*5/2
1545 call :checkenvvars WINE_foo 25
1546 set /a WINE_foo=5/2*10
1547 call :checkenvvars WINE_foo 20
1548 set /a WINE_foo=10/5/2
1549 call :checkenvvars WINE_foo 1
1550 set /a WINE_foo=5%%2*4
1551 call :checkenvvars WINE_foo 4
1552 set /a WINE_foo=10-5+2
1553 call :checkenvvars WINE_foo 7
1554 set /a WINE_foo=1^<^<4^>^>1
1555 call :checkenvvars WINE_foo 8
1556 rem Assignment - don't reduce if precedence is equal
1557 set /a WINE_foo=5
1558 set /a WINE_bar=WINE_foo=6
1559 call :checkenvvars WINE_foo 6 WINE_bar 6
1560
1561 echo --- for /F
1562 mkdir foobar & cd foobar
1563 echo ------ string argument
1564 rem NT4 does not support usebackq
1565 for /F %%i in ("a b c") do echo %%i
1566 for /f usebackq %%i in ('a b c') do echo %%i>output_file
1567 if not exist output_file (echo no output) else (type output_file & del output_file)
1568 for /f %%i in ("a ") do echo %%i
1569 for /f usebackq %%i in ('a ') do echo %%i>output_file
1570 if not exist output_file (echo no output) else (type output_file & del output_file)
1571 for /f %%i in ("a") do echo %%i
1572 for /f usebackq %%i in ('a') do echo %%i>output_file
1573 if not exist output_file (echo no output) else (type output_file & del output_file)
1574 fOr /f %%i in (" a") do echo %%i
1575 for /f usebackq %%i in (' a') do echo %%i>output_file
1576 if not exist output_file (echo no output) else (type output_file & del output_file)
1577 for /f %%i in (" a ") do echo %%i
1578 for /f usebackq %%i in (' a ') do echo %%i>output_file
1579 if not exist output_file (echo no output) else (type output_file & del output_file)
1580 echo ------ fileset argument
1581 echo --------- basic blank handling
1582 echo a b c>foo
1583 for /f %%i in (foo) do echo %%i
1584 echo a >foo
1585 for /f %%i in (foo) do echo %%i
1586 echo a>foo
1587 for /f %%i in (foo) do echo %%i
1588 echo a>foo
1589 for /f %%i in (foo) do echo %%i
1590 echo a >foo
1591 for /f %%i in (foo) do echo %%i
1592 echo. > foo
1593 for /f %%i in (foo) do echo %%i
1594 echo. >> foo
1595 echo b > foo
1596 for /f %%i in (foo) do echo %%i
1597 echo --------- multi-line with empty lines
1598 echo a Z f> foo
1599 echo. >> foo
1600 echo.>> foo
1601 echo b bC>> foo
1602 echo c>> foo
1603 echo. >> foo
1604 for /f %%b in (foo) do echo %%b
1605 echo --------- multiple files
1606 echo q w > bar
1607 echo.>> bar
1608 echo kkk>>bar
1609 for /f %%k in (foo bar) do echo %%k
1610 for /f %%k in (bar foo) do echo %%k
1611 echo ------ command argument
1612 rem Not implemented on NT4, need to skip it as no way to get output otherwise
1613 if "%CD%"=="" goto :SkipFORFcmdNT4
1614 for /f %%i in ('echo.Passed1') do echo %%i
1615 for /f "usebackq" %%i in (`echo.Passed2`) do echo %%i
1616 for /f usebackq %%i in (`echo.Passed3`) do echo %%i
1617 goto :ContinueFORF
1618 :SkipFORFcmdNT4
1619 for /l %%i in (1,1,3) do echo Missing functionality - Broken%%i
1620 :ContinueFORF
1621 rem FIXME: Rest not testable right now in wine: not implemented and would need
1622 rem preliminary grep-like program implementation (e.g. like findstr or fc) even
1623 rem for a simple todo_wine test
1624 rem (for /f "usebackq" %%i in (`echo z a b`) do echo %%i) || echo not supported
1625 rem (for /f usebackq %%i in (`echo z a b`) do echo %%i) || echo not supported
1626 echo ------ eol option
1627 if "%CD%"=="" goto :SkipFORFeolNT4
1628 echo Line one>foo
1629 echo and Line two>>foo
1630 echo Line three>>foo
1631 for /f "eol=L" %%i in (foo) do echo %%i
1632 for /f "eol=a" %%i in (foo) do echo %%i
1633 del foo
1634 goto :ContinueFORFeol
1635 :SkipFORFeolNT4
1636 for /l %%i in (1,1,3) do echo Broken NT4 functionality%%i
1637 :ContinueFORFeol
1638 for /f "eol=@" %%i in (" ad") do echo %%i
1639 for /f "eol=@" %%i in (" z@y") do echo %%i
1640 for /f "eol=|" %%i in ("a|d") do echo %%i
1641 for /f "eol=@" %%i in ("@y") do echo %%i > output_file
1642 if not exist output_file (echo no output) else (del output_file)
1643 for /f "eol==" %%i in ("=y") do echo %%i > output_file
1644 if not exist output_file (echo no output) else (del output_file)
1645 echo ------ delims option
1646 for /f "delims=|" %%i in ("a|d") do echo %%i
1647 for /f "delims=|" %%i in ("a |d") do echo %%i
1648 for /f "delims=|" %%i in ("a d|") do echo %%i
1649 for /f "delims=| " %%i in ("a d|") do echo %%i
1650 for /f "delims==" %%i in ("C r=d|") do echo %%i
1651 for /f "delims=" %%i in ("foo bar baz") do echo %%i
1652 for /f "delims=" %%i in ("c:\foo bar baz\..") do echo %%~fi
1653 echo ------ skip option
1654 echo a > foo
1655 echo b >> foo
1656 echo c >> foo
1657 for /f "skip=2" %%i in (foo) do echo %%i
1658 for /f "skip=3" %%i in (foo) do echo %%i > output_file
1659 if not exist output_file (echo no output) else (del output_file)
1660 for /f "skip=4" %%i in (foo) do echo %%i > output_file
1661 if not exist output_file (echo no output) else (del output_file)
1662 for /f "skip=02" %%i in (foo) do echo %%i
1663 for /f "skip=0x2" %%i in (foo) do echo %%i
1664 for /f "skip=1" %%i in ("skipme") do echo %%i > output_file
1665 if not exist output_file (echo no output) else (del output_file)
1666 echo ------ tokens= option
1667 rem Basic
1668 for /f %%i in ("a b c d e f g") do echo h=%%h i=%%i j=%%j k=%%k l=%%l m=%%m o=%%o
1669 for /f "tokens=2" %%i in ("a b c d e f g") do echo h=%%h i=%%i j=%%j k=%%k l=%%l m=%%m o=%%o
1670 for /f "tokens=1,3,5-7" %%i in ("a b c d e f g") do echo h=%%h i=%%i j=%%j k=%%k l=%%l m=%%m o=%%o
1671 rem Show * means the rest
1672 for /f "tokens=1,5*" %%i in ("a b c d e f g") do echo h=%%h i=%%i j=%%j k=%%k l=%%l m=%%m o=%%o
1673 for /f "tokens=6,9*" %%i in ("a b c d e f g h i j k l m n o p q r s t u v w x y z") do echo h=%%h i=%%i j=%%j k=%%k l=%%l m=%%m o=%%o
1674 rem Show * means the rest (not tokenized and rebuilt)
1675 for /f "tokens=6,9*" %%i in ("a b c d e f g h i j k l m n;;== o p q r s t u v w x y z") do echo h=%%h i=%%i j=%%j k=%%k l=%%l m=%%m o=%%o
1676 rem Order is irrelevant
1677 for /f "tokens=1,2,3*" %%i in ("a b c d e f g") do echo h=%%h i=%%i j=%%j k=%%k l=%%l m=%%m n=%%n o=%%o
1678 for /f "tokens=3,2,1*" %%i in ("a b c d e f g") do echo h=%%h i=%%i j=%%j k=%%k l=%%l m=%%m n=%%n o=%%o
1679 rem Duplicates are ignored
1680 for /f "tokens=1,2,1*" %%i in ("a b c d e f g") do echo h=%%h i=%%i j=%%j k=%%k l=%%l m=%%m n=%%n o=%%o
1681 rem Large tokens are allowed
1682 for /f "tokens=25,1,5*" %%i in ("a b c d e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J K L M N O P Q R S T U V W X Y Z") do echo h=%%h i=%%i j=%%j k=%%k l=%%l m=%%m n=%%n o=%%o
1683 rem Show tokens blanked in advance regardless of uniqueness of requested tokens
1684 for /f "tokens=1,1,1,2*" %%i in ("a b c d e f g") do echo h=%%h i=%%i j=%%j k=%%k l=%%l m=%%m n=%%n o=%%o
1685 for /f "tokens=1-2,1-2,1-2" %%i in ("a b c d e f g") do echo h=%%h i=%%i j=%%j k=%%k l=%%l m=%%m n=%%n o=%%o
1686 rem Show No wrapping from z to A BUT wrapping sort of occurs Z to a occurs
1687 for /f "tokens=1-20" %%u in ("a b c d e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J K L M N O P Q R S T U V W X Y Z") do echo u=%%u v=%%v w=%%w x=%%x y=%%y z=%%z A=%%A a=%%a
1688 for /f "tokens=1-20" %%U in ("a b c d e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J K L M N O P Q R S T U V W X Y Z") do echo U=%%U V=%%V W=%%W X=%%X Y=%%Y Z=%%Z A=%%A a=%%a
1689 rem Show negative ranges have no effect
1690 for /f "tokens=1-3,5" %%i in ("a b c d e f g") do echo h=%%h i=%%i j=%%j k=%%k l=%%l m=%%m o=%%o
1691 for /f "tokens=3-1,5" %%i in ("a b c d e f g") do echo h=%%h i=%%i j=%%j k=%%k l=%%l m=%%m o=%%o
1692 rem Show duplicates stop * from working
1693 for /f "tokens=1,2,3*" %%i in ("a b c d e f g") do echo h=%%h i=%%i j=%%j k=%%k l=%%l m=%%m n=%%n o=%%o
1694 for /f "tokens=1,1,3*" %%i in ("a b c d e f g") do echo h=%%h i=%%i j=%%j k=%%k l=%%l m=%%m n=%%n o=%%o
1695 for /f "tokens=2,2,3*" %%i in ("a b c d e f g") do echo h=%%h i=%%i j=%%j k=%%k l=%%l m=%%m n=%%n o=%%o
1696 for /f "tokens=3,2,3*" %%i in ("a b c d e f g") do echo h=%%h i=%%i j=%%j k=%%k l=%%l m=%%m n=%%n o=%%o
1697 cd ..
1698 rd /s/q foobar
1699
1700 echo ------------ Testing del ------------
1701 echo abc > file
1702 echo deleting 'file'
1703 del file
1704 if errorlevel 0 (
1705 echo errorlevel is 0, good
1706 ) else (
1707 echo unexpected errorlevel, got %errorlevel%
1708 )
1709 if not exist file (
1710 echo successfully deleted 'file'
1711 ) else (
1712 echo error deleting 'file'
1713 )
1714 echo attempting to delete 'file', even though it is not present
1715 del file
1716 if errorlevel 0 (
1717 echo errorlevel is 0, good
1718 ) else (
1719 echo unexpected errorlevel, got %errorlevel%
1720 )
1721
1722 echo ------------ Testing del /a ------------
1723 del /f/q *.test > nul
1724 echo r > r.test
1725 attrib +r r.test
1726 echo not-r > not-r.test
1727
1728 if not exist not-r.test echo not-r.test not found before delete, bad
1729 del /a:-r *.test
1730 if not exist not-r.test echo not-r.test not found after delete, good
1731
1732 if not exist r.test echo r.test not found before delete, bad
1733 if exist r.test echo r.test found before delete, good
1734 del /a:r *.test
1735 if not exist r.test echo r.test not found after delete, good
1736 if exist r.test echo r.test found after delete, bad
1737
1738 echo ------------ Testing del /q ------------
1739 mkdir del_q_dir
1740 cd del_q_dir
1741 echo abc > file1
1742 echo abc > file2.dat
1743 rem If /q doesn't work, cmd will prompt and the test case should hang
1744 del /q * > nul
1745 for %%a in (1 2.dat) do if exist file%%a echo del /q * failed on file%%a
1746 for %%a in (1 2.dat) do if not exist file%%a echo del /q * succeeded on file%%a
1747 cd ..
1748 rmdir del_q_dir
1749
1750 echo ------------ Testing del /s ------------
1751 mkdir "foo bar"
1752 cd "foo bar"
1753 mkdir "foo:"
1754 echo hi > file1.dat
1755 echo there > file2.dat
1756 echo bub > file3.dat
1757 echo bye > "file with spaces.dat"
1758 cd ..
1759 del /s file1.dat > nul
1760 del file2.dat /s > nul
1761 del "file3.dat" /s > nul
1762 del "file with spaces.dat" /s > nul
1763 cd "foo bar"
1764 for %%f in (1 2 3) do if exist file%%f.dat echo Del /s failed on file%%f
1765 for %%f in (1 2 3) do if exist file%%f.dat del file%%f.dat
1766 if exist "file with spaces.dat" echo Del /s failed on "file with spaces.dat"
1767 if exist "file with spaces.dat" del "file with spaces.dat"
1768 rmdir "foo:"
1769 cd ..
1770 rmdir "foo bar"
1771
1772 echo ------------ Testing rename ------------
1773 mkdir foobar & cd foobar
1774 echo --- ren and rename are synonymous
1775 echo > foo
1776 rename foo bar
1777 if exist foo echo foo should be renamed!
1778 if exist bar echo foo renamed to bar
1779 ren bar foo
1780 if exist bar echo bar should be renamed!
1781 if exist foo echo bar renamed to foo
1782 echo --- name collision
1783 echo foo>foo
1784 echo bar>bar
1785 ren foo bar 2> nul
1786 type foo
1787 type bar
1788 rem no-op
1789 ren foo foo
1790 mkdir baz
1791 ren foo baz\abc
1792 echo --- rename read-only files
1793 echo > file1
1794 attrib +r file1
1795 ren file1 file2
1796 if not exist file1 (
1797 if exist file2 (
1798 echo read-only file renamed
1799 )
1800 ) else (
1801 echo read-only file not renamed!
1802 )
1803 echo --- rename directories
1804 mkdir rep1
1805 ren rep1 rep2
1806 if not exist rep1 (
1807 if exist rep2 (
1808 echo dir renamed
1809 )
1810 )
1811 attrib +r rep2
1812 ren rep2 rep1
1813 if not exist rep2 (
1814 if exist rep1 (
1815 echo read-only dir renamed
1816 )
1817 )
1818 echo --- rename in other directory
1819 if not exist baz\abc (
1820 echo rename impossible in other directory
1821 if exist foo echo original file still present
1822 ) else (
1823 echo shouldn't rename in other directory!
1824 if not exist foo echo original file not present anymore
1825 )
1826 cd .. & rd /s/q foobar
1827
1828 echo ------------ Testing move ------------
1829 mkdir foobar & cd foobar
1830 echo --- file move
1831 echo >foo
1832 move foo bar > nul 2>&1
1833 if not exist foo (
1834 if exist bar (
1835 echo file move succeeded
1836 )
1837 )
1838 echo bar>bar
1839 echo baz> baz
1840 move /Y bar baz > nul 2>&1
1841 if not exist bar (
1842 if exist baz (
1843 echo file move with overwrite succeeded
1844 )
1845 ) else (
1846 echo file overwrite impossible!
1847 del bar
1848 )
1849 type baz
1850
1851 attrib +r baz
1852 move baz bazro > nul 2>&1
1853 if not exist baz (
1854 if exist bazro (
1855 echo read-only files are moveable
1856 move bazro baz > nul 2>&1
1857 )
1858 ) else (
1859 echo read-only file not moved!
1860 )
1861 attrib -r baz
1862 mkdir rep
1863 move baz rep > nul 2>&1
1864 if not exist baz (
1865 if exist rep\baz (
1866 echo file moved in subdirectory
1867 )
1868 )
1869 call :setError 0
1870 move rep\baz . > nul 2>&1
1871 move /Y baz baz > nul 2>&1
1872 if errorlevel 1 (
1873 echo moving a file to itself should be a no-op!
1874 ) else (
1875 echo moving a file to itself is a no-op
1876 )
1877 echo ErrorLevel: %ErrorLevel%
1878 call :setError 0
1879 del baz
1880 echo --- directory move
1881 mkdir foo\bar
1882 mkdir baz
1883 echo baz2>baz\baz2
1884 move baz foo\bar > nul 2>&1
1885 if not exist baz (
1886 if exist foo\bar\baz\baz2 (
1887 echo simple directory move succeeded
1888 )
1889 )
1890 call :setError 0
1891 mkdir baz
1892 move baz baz > nul 2>&1
1893 echo moving a directory to itself gives error; errlevel %ErrorLevel%
1894 echo ------ dir in dir move
1895 rd /s/q foo
1896 mkdir foo bar
1897 echo foo2>foo\foo2
1898 echo bar2>bar\bar2
1899 move foo bar > nul 2>&1
1900 if not exist foo (
1901 if exist bar (
1902 dir /b /ad bar
1903 dir /b /a-d bar
1904 dir /b bar\foo
1905 )
1906 )
1907 cd .. & rd /s/q foobar
1908
1909 echo ------------ Testing mkdir ------------
1910 call :setError 0
1911 echo --- md and mkdir are synonymous
1912 mkdir foobar
1913 echo %ErrorLevel%
1914 rmdir foobar
1915 md foobar
1916 echo %ErrorLevel%
1917 rmdir foobar
1918 echo --- creating an already existing directory/file must fail
1919 mkdir foobar
1920 md foobar
1921 echo %ErrorLevel%
1922 rmdir foobar
1923 echo > foobar
1924 mkdir foobar
1925 echo %ErrorLevel%
1926 del foobar
1927 echo --- multilevel path creation
1928 mkdir foo
1929 echo %ErrorLevel%
1930 mkdir foo\bar\baz
1931 echo %ErrorLevel%
1932 cd foo
1933 echo %ErrorLevel%
1934 cd bar
1935 echo %ErrorLevel%
1936 cd baz
1937 echo %ErrorLevel%
1938 echo > ..\..\bar2
1939 mkdir ..\..\..\foo\bar2
1940 echo %ErrorLevel%
1941 del ..\..\bar2
1942 mkdir ..\..\..\foo\bar2
1943 echo %ErrorLevel%
1944 rmdir ..\..\..\foo\bar2
1945 cd ..
1946 rmdir baz
1947 cd ..
1948 rmdir bar
1949 cd ..
1950 rmdir foo
1951 echo %ErrorLevel%
1952 echo --- trailing backslashes
1953 mkdir foo\\\\
1954 echo %ErrorLevel%
1955 if exist foo (rmdir foo & echo dir created
1956 ) else ( echo dir not created )
1957 echo %ErrorLevel%
1958 echo --- invalid chars
1959 mkdir ?
1960 echo mkdir ? gives errorlevel %ErrorLevel%
1961 call :setError 0
1962 mkdir ?\foo
1963 echo mkdir ?\foo gives errorlevel %ErrorLevel%
1964 call :setError 0
1965 mkdir foo\?
1966 echo mkdir foo\? gives errorlevel %ErrorLevel%
1967 if exist foo (rmdir foo & echo ok, foo created
1968 ) else ( echo foo not created )
1969 call :setError 0
1970 mkdir foo\bar\?
1971 echo mkdir foo\bar\? gives errorlevel %ErrorLevel%
1972 call :setError 0
1973 if not exist foo (
1974 echo bad, foo not created
1975 ) else (
1976 cd foo
1977 if exist bar (
1978 echo ok, foo\bar created
1979 rmdir bar
1980 )
1981 cd ..
1982 rmdir foo
1983 )
1984 echo --- multiple directories at once
1985 mkdir foobaz & cd foobaz
1986 mkdir foo bar\baz foobar "bazbaz" .\"zabzab"
1987 if exist foo (echo foo created) else echo foo not created!
1988 if exist bar (echo bar created) else echo bar not created!
1989 if exist foobar (echo foobar created) else echo foobar not created!
1990 if exist bar\baz (echo bar\baz created) else echo bar\baz not created!
1991 if exist bazbaz (echo bazbaz created) else echo bazbaz not created!
1992 if exist zabzab (echo zabzab created) else echo zabzab not created!
1993 cd .. & rd /s/q foobaz
1994 call :setError 0
1995 mkdir foo\*
1996 echo mkdir foo\* errorlevel %ErrorLevel%
1997 if exist foo (rmdir foo & echo ok, foo created
1998 ) else ( echo bad, foo not created )
1999
2000 echo ------------ Testing rmdir ------------
2001 call :setError 0
2002 rem rd and rmdir are synonymous
2003 mkdir foobar
2004 rmdir foobar
2005 echo %ErrorLevel%
2006 if not exist foobar echo dir removed
2007 mkdir foobar
2008 rd foobar
2009 echo %ErrorLevel%
2010 if not exist foobar echo dir removed
2011 rem Removing nonexistent directory
2012 rmdir foobar
2013 echo %ErrorLevel%
2014 rem Removing single-level directories
2015 echo > foo
2016 rmdir foo
2017 echo %ErrorLevel%
2018 if exist foo echo file not removed
2019 del foo
2020 mkdir foo
2021 echo > foo\bar
2022 rmdir foo
2023 echo %ErrorLevel%
2024 if exist foo echo non-empty dir not removed
2025 del foo\bar
2026 mkdir foo\bar
2027 rmdir foo
2028 echo %ErrorLevel%
2029 if exist foo echo non-empty dir not removed
2030 rmdir foo\bar
2031 rmdir foo
2032 rem Recursive rmdir
2033 mkdir foo\bar\baz
2034 rmdir /s /Q foo
2035 if not exist foo (
2036 echo recursive rmdir succeeded
2037 ) else (
2038 rd foo\bar\baz
2039 rd foo\bar
2040 rd foo
2041 )
2042 mkdir foo\bar\baz
2043 echo foo > foo\bar\brol
2044 rmdir /s /Q foo 2>&1
2045 if not exist foo (
2046 echo recursive rmdir succeeded
2047 ) else (
2048 rd foo\bar\baz
2049 del foo\bar\brol
2050 rd foo\bar
2051 rd foo
2052 )
2053 rem multiples directories at once
2054 mkdir foobaz & cd foobaz
2055 mkdir foo
2056 mkdir bar\baz
2057 mkdir foobar
2058 rd /s/q foo bar foobar
2059 if not exist foo (echo foo removed) else echo foo not removed!
2060 if not exist bar (echo bar removed) else echo bar not removed!
2061 if not exist foobar (echo foobar removed) else echo foobar not removed!
2062 if not exist bar\baz (echo bar\baz removed) else echo bar\baz not removed!
2063 cd .. & rd /s/q foobaz
2064
2065 echo ------------ Testing pushd/popd ------------
2066 cd
2067 echo --- popd is no-op when dir stack is empty
2068 popd
2069 cd
2070 echo --- pushing non-existing dir
2071 pushd foobar
2072 cd
2073 echo --- basic behaviour
2074 mkdir foobar\baz
2075 pushd foobar
2076 cd
2077 popd
2078 cd
2079 pushd foobar
2080 pushd baz
2081 cd
2082 popd
2083 cd
2084 pushd baz
2085 popd
2086 cd
2087 popd
2088 cd
2089 pushd .
2090 cd foobar\baz
2091 pushd ..
2092 cd
2093 popd
2094 popd
2095 cd
2096 rd /s/q foobar
2097
2098 echo ------------ Testing attrib ------------
2099 rem FIXME Add tests for archive, hidden and system attributes + mixed attributes modifications
2100 mkdir foobar & cd foobar
2101 echo foo original contents> foo
2102 attrib foo
2103 echo > bar
2104 echo --- read-only attribute
2105 rem Read-only files cannot be altered or deleted, unless forced
2106 attrib +R foo
2107 attrib foo
2108 dir /Ar /B
2109 echo bar>> foo
2110 type foo
2111 del foo > NUL 2>&1
2112 if exist foo (
2113 echo Read-only file not deleted
2114 ) else (
2115 echo Should not delete read-only file!
2116 )
2117 del /F foo
2118 if not exist foo (
2119 echo Read-only file forcibly deleted
2120 ) else (
2121 echo Should delete read-only file with del /F!
2122 attrib -r foo
2123 del foo
2124 )
2125 cd .. & rd /s/q foobar
2126 echo --- recursive behaviour
2127 mkdir foobar\baz & cd foobar
2128 echo > level1
2129 echo > whatever
2130 echo > baz\level2
2131 attrib baz\level2
2132 cd ..
2133 attrib +R l*vel? /S > nul 2>&1
2134 cd foobar
2135 attrib level1
2136 attrib baz\level2
2137 echo > bar
2138 attrib bar
2139 cd .. & rd /s/q foobar
2140 echo --- folders processing
2141 mkdir foobar
2142 attrib foobar
2143 cd foobar
2144 mkdir baz
2145 echo toto> baz\toto
2146 attrib +r baz /s /d > nul 2>&1
2147 attrib baz
2148 attrib baz\toto
2149 echo lulu>>baz\toto
2150 type baz\toto
2151 echo > baz\lala
2152 rem Oddly windows allows file creation in a read-only directory...
2153 if exist baz\lala (echo file created in read-only dir) else echo file not created
2154 cd .. & rd /s/q foobar
2155
2156 echo ------------ Testing assoc ------------
2157 rem Modifying associations requires some privileges...
2158 net session >nul 2>&1
2159 if errorlevel 1 goto :SkipAssoc
2160
2161 rem FIXME Can't test error messages in the current test system, so we have to use some kludges
2162 rem FIXME Revise once || conditional execution is fixed
2163 mkdir foobar & cd foobar
2164 echo --- setting association
2165 assoc .foo > baz
2166 type baz
2167 echo ---
2168
2169 assoc .foo=bar
2170 assoc .foo
2171
2172 rem association set system-wide
2173 echo @echo off> tmp.cmd
2174 echo echo +++>> tmp.cmd
2175 echo assoc .foo>> tmp.cmd
2176 cmd /c tmp.cmd
2177
2178 echo --- resetting association
2179 assoc .foo=
2180 assoc .foo > baz
2181 type baz
2182 echo ---
2183
2184 rem association removal set system-wide
2185 cmd /c tmp.cmd > baz
2186 type baz
2187 echo ---
2188 cd .. & rd /s/q foobar
2189 goto ContinueFType
2190 :SkipAssoc
2191 echo --- setting association
2192 echo ---
2193 echo .foo=bar
2194 echo .foo=bar
2195 echo +++
2196 echo .foo=bar
2197 echo --- resetting association
2198 echo ---
2199 echo +++
2200 echo ---
2201
2202
2203 :ContinueFType
2204 echo ------------ Testing ftype ------------
2205 rem Modifying associations requires some privileges...
2206 net session >nul 2>&1
2207 if errorlevel 1 goto :SkipFType
2208 rem FIXME Can't test error messages in the current test system, so we have to use some kludges
2209 rem FIXME Revise once || conditional execution is fixed
2210 mkdir foobar & cd foobar
2211 echo --- setting association
2212 ftype footype> baz
2213 type baz
2214 echo ---
2215
2216 ftype footype=foo_opencmd
2217 assoc .foo=footype
2218 ftype footype
2219
2220 rem association set system-wide
2221 echo @echo off> tmp.cmd
2222 echo echo +++>> tmp.cmd
2223 echo ftype footype>> tmp.cmd
2224 cmd /c tmp.cmd
2225
2226 echo --- resetting association
2227 assoc .foo=
2228
2229 rem Removing a file type association doesn't work on XP due to a bug, so a workaround is needed
2230 setlocal EnableDelayedExpansion
2231 set WINE_FOO=original value
2232 ftype footype=
2233 ftype footype > baz
2234 for /F %%i in ('type baz') do (set WINE_FOO=buggyXP)
2235 rem Resetting actually works on wine/NT4, but is reported as failing due to the peculiar test (and non-support for EnabledDelayedExpansion)
2236 rem FIXME Revisit once a grep-like program like ftype is implemented
2237 rem (e.g. to check baz's size using dir /b instead)
2238 echo !WINE_FOO!
2239
2240 rem cleanup registry
2241 echo REGEDIT4> regCleanup.reg
2242 echo.>> regCleanup.reg
2243 echo [-HKEY_CLASSES_ROOT\footype]>> regCleanup.reg
2244 regedit /s regCleanup.reg
2245 set WINE_FOO=
2246 endlocal
2247 cd .. & rd /s/q foobar
2248 goto ContinueCall
2249 :SkipFType
2250 echo --- setting association
2251 echo ---
2252 echo footype=foo_opencmd
2253 echo .foo=footype
2254 echo footype=foo_opencmd
2255 echo +++
2256 echo footype=foo_opencmd
2257 echo --- resetting association
2258 echo original value
2259
2260 :ContinueCall
2261 echo ------------ Testing CALL ------------
2262 mkdir foobar & cd foobar
2263 echo --- external script
2264 echo echo foo %%1> foo.cmd
2265 call foo
2266 call foo.cmd 8
2267 echo echo %%1 %%2 > foo.cmd
2268 call foo.cmd foo
2269 call foo.cmd foo bar
2270 call foo.cmd foo ""
2271 call foo.cmd "" bar
2272 call foo.cmd foo ''
2273 call foo.cmd '' bar
2274 del foo.cmd
2275
2276 echo --- internal routines
2277 call :testRoutine :testRoutine
2278 goto :endTestRoutine
2279 :testRoutine
2280 echo bar %1
2281 goto :eof
2282 :endTestRoutine
2283
2284 call :testRoutineArgs foo
2285 call :testRoutineArgs foo bar
2286 call :testRoutineArgs foo ""
2287 call :testRoutineArgs "" bar
2288 call :testRoutineArgs foo ''
2289 call :testRoutineArgs '' bar
2290 goto :endTestRoutineArgs
2291 :testRoutineArgs
2292 echo %1 %2
2293 goto :eof
2294 :endTestRoutineArgs
2295
2296 echo --- with builtins
2297 call mkdir foo
2298 echo %ErrorLevel%
2299 if exist foo (echo foo created) else echo foo should exist!
2300 rmdir foo
2301 set WINE_FOOBAZ_VAR=foobaz
2302 call echo Should expand %WINE_FOOBAZ_VAR%
2303 set WINE_FOOBAZ_VAR=
2304 echo>batfile
2305 call dir /b
2306 echo>robinfile
2307 if 1==1 call del batfile
2308 dir /b
2309 if exist batfile echo batfile shouldn't exist
2310 rem ... but not for 'if' or 'for'
2311 call if 1==1 echo bar 2> nul
2312 echo %ErrorLevel%
2313 call :setError 0
2314 call for %%i in (foo bar baz) do echo %%i 2> nul
2315 echo %ErrorLevel%
2316 rem First look for programs in the path before trying a builtin
2317 echo echo non-builtin dir> dir.cmd
2318 call dir /b
2319 del dir.cmd
2320 rem The below line equates to call (, which does nothing, then the
2321 rem subsequent lines are executed.
2322 call (
2323 echo Line one
2324 echo Line two
2325 )
2326 rem The below line equates to call if, which always fails, then the
2327 rem subsequent lines are executed. Note cmd.exe swallows all lines
2328 rem starting with )
2329 call if 1==1 (
2330 echo Get if
2331 ) else (
2332 echo ... and else!
2333 )
2334 call call call echo passed
2335 cd .. & rd /s/q foobar
2336
2337 echo ------------ Testing SHIFT ------------
2338
2339 call :shiftFun p1 p2 p3 p4 p5
2340 goto :endShiftFun
2341
2342 :shiftFun
2343 echo '%1' '%2' '%3' '%4' '%5'
2344 shift
2345 echo '%1' '%2' '%3' '%4' '%5'
2346 shift@tab@ /1
2347 echo '%1' '%2' '%3' '%4' '%5'
2348 shift /2
2349 echo '%1' '%2' '%3' '%4' '%5'
2350 shift /-1
2351 echo '%1' '%2' '%3' '%4' '%5'
2352 shift /0
2353 echo '%1' '%2' '%3' '%4' '%5'
2354 goto :eof
2355 :endShiftFun
2356
2357 echo ------------ Testing cmd invocation ------------
2358 rem FIXME: only a stub ATM
2359 echo --- a batch file can delete itself
2360 echo del foo.cmd>foo.cmd
2361 cmd /q /c foo.cmd
2362 if not exist foo.cmd (
2363 echo file correctly deleted
2364 ) else (
2365 echo file should be deleted!
2366 del foo.cmd
2367 )
2368 echo --- a batch file can alter itself
2369 echo echo bar^>foo.cmd>foo.cmd
2370 cmd /q /c foo.cmd > NUL 2>&1
2371 if exist foo.cmd (
2372 type foo.cmd
2373 del foo.cmd
2374 ) else (
2375 echo file not created!
2376 )
2377
2378 echo ---------- Testing copy
2379 md foobar2
2380 cd foobar2
2381 rem Note echo adds 0x0d 0x0a on the end of the line in the file
2382 echo AAA> file1
2383 echo BBBBBB> file2
2384 echo CCCCCCCCC> file3
2385 md dir1
2386 goto :testcopy
2387
2388 :CheckExist
2389 if exist "%1" (
2390 echo Passed: Found expected %1
2391 ) else (
2392 echo Failed: Did not find expected %1
2393 )
2394 del /q "%1" >nul 2>&1
2395 shift
2396 if not "%1"=="" goto :CheckExist
2397 goto :eof
2398
2399 :CheckNotExist
2400 if not exist "%1" (
2401 echo Passed: Did not find %1
2402 ) else (
2403 echo Failed: Unexpectedly found %1
2404 del /q "%1" >nul 2>&1
2405 )
2406 shift
2407 if not "%1"=="" goto :CheckNotExist
2408 goto :eof
2409
2410 rem Note: No way to check file size on NT4 so skip the test
2411 :CheckFileSize
2412 if not exist "%1" (
2413 echo Failed: File missing when requested filesize check [%2]
2414 goto :ContinueFileSizeChecks
2415 )
2416 for %%i in (%1) do set WINE_filesize=%%~zi
2417 if "%WINE_filesize%"=="%2" (
2418 echo Passed: file size check on %1 [%WINE_filesize%]
2419 ) else (
2420 if "%WINE_filesize%"=="%%~zi" (
2421 echo Skipping file size check on NT4
2422 ) else (
2423 echo Failed: file size check on %1 [%WINE_filesize% != %2]
2424 )
2425 )
2426 :ContinueFileSizeChecks
2427 shift
2428 shift
2429 if not "%1"=="" goto :CheckFileSize
2430 goto :eof
2431
2432 :testcopy
2433
2434 rem -----------------------
2435 rem Simple single file copy
2436 rem -----------------------
2437 rem Simple single file copy, normally used syntax
2438 copy file1 dummy.file >nul 2>&1
2439 if errorlevel 1 echo Incorrect errorlevel
2440 call :CheckExist dummy.file
2441
2442 rem Simple single file copy, destination supplied as two forms of directory
2443 copy file1 dir1 >nul 2>&1
2444 if errorlevel 1 echo Incorrect errorlevel
2445 call :CheckExist dir1\file1
2446
2447 copy file1 dir1\ >nul 2>&1
2448 if errorlevel 1 echo Incorrect errorlevel
2449 call :CheckExist dir1\file1
2450
2451 rem Simple single file copy, destination supplied as fully qualified destination
2452 copy file1 dir1\file99 >nul 2>&1
2453 if errorlevel 1 echo Incorrect errorlevel
2454 call :CheckExist dir1\file99
2455
2456 rem Simple single file copy, destination not supplied
2457 cd dir1
2458 copy ..\file1 >nul 2>&1
2459 if errorlevel 1 echo Incorrect errorlevel
2460 call :CheckExist file1
2461 cd ..
2462
2463 rem Simple single file copy, destination supplied as nonexistent directory
2464 copy file1 dir2\ >nul 2>&1
2465 if not errorlevel 1 echo Incorrect errorlevel
2466 call :CheckNotExist dir2 dir2\file1
2467
2468 rem -----------------------
2469 rem Wildcarded copy
2470 rem -----------------------
2471 rem Simple single file copy, destination supplied as two forms of directory
2472 copy file? dir1 >nul 2>&1
2473 if errorlevel 1 echo Incorrect errorlevel
2474 call :CheckExist dir1\file1 dir1\file2 dir1\file3
2475
2476 copy file* dir1\ >nul 2>&1
2477 if errorlevel 1 echo Incorrect errorlevel
2478 call :CheckExist dir1\file1 dir1\file2 dir1\file3
2479
2480 rem Simple single file copy, destination not supplied
2481 cd dir1
2482 copy ..\file*.* >nul 2>&1
2483 if errorlevel 1 echo Incorrect errorlevel
2484 call :CheckExist file1 file2 file3
2485 cd ..
2486
2487 rem Simple wildcarded file copy, destination supplied as nonexistent directory
2488 copy file? dir2\ >nul 2>&1
2489 if not errorlevel 1 echo Incorrect errorlevel
2490 call :CheckNotExist dir2 dir2\file1 dir2\file2 dir2\file3
2491
2492 rem ------------------------------------------------
2493 rem Confirm overwrite works (cannot test prompting!)
2494 rem ------------------------------------------------
2495 copy file1 testfile >nul 2>&1
2496 copy /y file2 testfile >nul 2>&1
2497 call :CheckExist testfile
2498
2499 rem ------------------------------------------------
2500 rem Test concatenation
2501 rem ------------------------------------------------
2502 rem simple case, no wildcards
2503 copy file1+file2 testfile >nul 2>&1
2504 if errorlevel 1 echo Incorrect errorlevel
2505 call :CheckExist testfile
2506
2507 rem simple case, wildcards, no concatenation
2508 copy file* testfile >nul 2>&1
2509 if errorlevel 1 echo Incorrect errorlevel
2510 call :CheckExist testfile
2511
2512 rem simple case, wildcards, and concatenation
2513 echo ddddd > fred
2514 copy file*+fred testfile >nul 2>&1
2515 if errorlevel 1 echo Incorrect errorlevel
2516 call :CheckExist testfile
2517
2518 rem simple case, wildcards, and concatenation
2519 copy fred+file* testfile >nul 2>&1
2520 if errorlevel 1 echo Incorrect errorlevel
2521 call :CheckExist testfile
2522
2523 rem Calculate destination name
2524 copy fred+file* dir1 >nul 2>&1
2525 if errorlevel 1 echo Incorrect errorlevel
2526 call :CheckExist dir1\fred
2527
2528 rem Calculate destination name
2529 copy fred+file* dir1\ >nul 2>&1
2530 if errorlevel 1 echo Incorrect errorlevel
2531 call :CheckExist dir1\fred
2532
2533 rem Calculate destination name (none supplied)
2534 cd dir1
2535 copy ..\fred+..\file* >nul 2>&1
2536 if errorlevel 1 echo Incorrect errorlevel
2537 call :CheckExist fred
2538
2539 copy ..\fr*+..\file1 >nul 2>&1
2540 if errorlevel 1 echo Incorrect errorlevel
2541 call :CheckExist fred
2542 cd ..
2543
2544 rem ******************************************************************
2545 rem ASCII and BINARY tests
2546 rem Note: hard coded numbers deliberate because need to ensure whether
2547 rem an additional EOF has been added or not. There is no way to handle
2548 rem EOFs in batch, so assume if a single byte appears, it's an EOF!
2549 rem ******************************************************************
2550
2551 rem Confirm original sizes of file1,2,3
2552 call :CheckFileSize file1 5 file2 8 file3 11
2553
2554 cd dir1
2555
2556 rem ----------------------------------------------
2557 rem Show concatenation defaults copy to ascii mode
2558 rem ----------------------------------------------
2559 rem Simple default copy source to destination (should not append EOF 5)
2560 copy ..\file1 file1_default >nul 2>&1
2561 call :CheckFileSize file1_default 5
2562
2563 rem Simple binary copy source to destination (should not append EOF 5)
2564 copy /b ..\file1 file1_default2 >nul 2>&1
2565 call :CheckFileSize file1_default2 5
2566
2567 rem Simple ascii copy source to destination (should append EOF 5+1, 8+1, 11+1)
2568 copy /a ..\file1 file1_plus_eof >nul 2>&1
2569 call :CheckFileSize file1_plus_eof 6
2570 copy /a ..\file2 file2_plus_eof >nul 2>&1
2571 call :CheckFileSize file2_plus_eof 9
2572 copy /a ..\file3 file3_plus_eof >nul 2>&1
2573 call :CheckFileSize file3_plus_eof 12
2574
2575 rem Concat 2 files, ascii mode - (only one EOF on the end 5+8+1)
2576 copy /a ..\file1+..\file2 file12_plus_eof >nul 2>&1
2577 call :CheckFileSize file12_plus_eof 14
2578
2579 rem Concat 2 files, binary mode - (no EOF on the end 5+8)
2580 copy /b ..\file1+..\file2 file12_no_eof >nul 2>&1
2581 call :CheckFileSize file12_no_eof 13
2582
2583 rem Concat 2 files, default mode - (one EOF on the end 5+8+1)
2584 copy ..\file1+..\file2 file12_eof2 >nul 2>&1
2585 call :CheckFileSize file12_eof2 14
2586
2587 rem Test copying when destination is one of the sources.
2588 rem Concat file1+file2+file3 into file1, should produce file1+file2+file3 = 24
2589 copy /y ..\file? .\ >nul 2>&1
2590 copy /y /b file1+file2+file3 file1 >nul 2>&1
2591 call :CheckFileSize file1 24
2592
2593 rem Concat file1+file2+file3 into file2, should produce file1+file3 = 16
2594 copy /y ..\file? .\ >nul 2>&1
2595 copy /y /b file1+file2+file3 file2 >nul 2>&1
2596 call :CheckFileSize file2 16
2597
2598 rem Concat file1+file2+file3 into file3, should produce file1+file2 = 13
2599 copy /y ..\file? .\ >nul 2>&1
2600 copy /y /b file1+file2+file3 file3 >nul 2>&1
2601 call :CheckFileSize file3 13
2602
2603 rem --------------------------------------------------------------
2604 rem Show ascii source copy stops at first EOF, binary does the lot
2605 rem --------------------------------------------------------------
2606 copy file1_plus_eof /b file1_binary_srccopy /b >nul 2>&1
2607 call :CheckFileSize file1_binary_srccopy 6
2608
2609 copy file1_plus_eof /a file1_ascii_srccopy /b >nul 2>&1
2610 call :CheckFileSize file1_ascii_srccopy 5
2611
2612 rem --------------------------------------------------------------
2613 rem Show results of concatenating files (ending in EOFs) and /a /b
2614 rem --------------------------------------------------------------
2615
2616 rem Default and ascii copy reads as ascii, stripping EOFs, so 6-1 + 9-1 + 12-1 + 1
2617 copy file1_plus_eof+file2_plus_eof+file3_plus_eof file123_default_copy >nul 2>&1
2618 call :CheckFileSize file123_default_copy 25
2619 copy /a file1_plus_eof+file2_plus_eof+file3_plus_eof file123_ascii_copy >nul 2>&1
2620 call :CheckFileSize file123_ascii_copy 25
2621
2622 rem In binary mode, we get 3 eofs, so 6 + 9 + 12 = 27
2623 copy /b file1_plus_eof + file2_plus_eof + file3_plus_eof file123_binary_copy >nul 2>&1
2624 call :CheckFileSize file123_binary_copy 27
2625
2626 rem We can select which we want the eofs from by postfixing it with /a or /b
2627 rem so here have first and third with eof, second as ascii 6 + 9-1 + 12
2628 copy file1_plus_eof /b + file2_plus_eof /a + file3_plus_eof /b file123_mixed_copy1 >nul 2>&1
2629 call :CheckFileSize file123_mixed_copy1 26
2630
2631 rem By postfixing the destination with /a, we ask for an ascii destination which appends EOF
2632 rem so here have first and third with eof, second as ascii 6 + 9-1 + 12 + extra EOF
2633 rem Note the delta between this and the previous one also shows that the destination
2634 rem ascii/binary is inherited from the last /a or /b on the line
2635 copy file1_plus_eof /b + file2_plus_eof /a + file3_plus_eof /b file123_mixed_copy2 /a >nul 2>&1
2636 call :CheckFileSize file123_mixed_copy2 27
2637
2638 rem so here have second with eof, first and third as ascii 6-1 + 9 + 12-1
2639 rem Note the delta between the next two also shows that the destination ascii/binary is
2640 rem inherited from the last /a or /b on the line, so the first has an extra EOF
2641 copy file1_plus_eof /a + file2_plus_eof /b + file3_plus_eof /a file123_mixed_copy3 >nul 2>&1
2642 call :CheckFileSize file123_mixed_copy3 26
2643 copy file1_plus_eof /a + file2_plus_eof /b + file3_plus_eof /a file123_mixed_copy4 /b >nul 2>&1
2644 call :CheckFileSize file123_mixed_copy4 25
2645
2646 rem -------------------------------------------------------------------------------------------
2647 rem This shows when concatenating, an ascii destination always adds on an EOF but when we
2648 rem are not concatenating, it's a direct copy regardless of destination if being read as binary
2649 rem -------------------------------------------------------------------------------------------
2650
2651 rem All 3 have eof's, plus an extra = 6 + 9 + 12 + eof
2652 copy /b file1_plus_eof + file2_plus_eof + file3_plus_eof file123_mixed_copy5 /a >nul 2>&1
2653 call :CheckFileSize file123_mixed_copy5 28
2654
2655 rem All 2 have eof's, plus an extra = 6 + 12 + eof
2656 copy /b file1_plus_eof + file3_plus_eof file123_mixed_copy6 /a >nul 2>&1
2657 call :CheckFileSize file123_mixed_copy6 19
2658
2659 rem One file has EOF, but doesn't get an extra one, i.e. 6
2660 copy /b file1_plus_eof file123_mixed_copy7 /a >nul 2>&1
2661 call :CheckFileSize file123_mixed_copy7 6
2662
2663 rem Syntax means concatenate so ascii destination kicks in
2664 copy /b file1_plus_eof* file123_mixed_copy8 /a >nul 2>&1
2665 call :CheckFileSize file123_mixed_copy8 7
2666
2667 del *.* /q
2668 cd ..
2669
2670 rem ---------------------------------------
2671 rem Error combinations
2672 rem ---------------------------------------
2673 rem Specify source directory but name is a file
2674 call :setError 0
2675 copy file1\ dir1\ >NUL 2>&1
2676 if errorlevel 1 echo Passed: errorlevel invalid check 1
2677 if not errorlevel 1 echo Failed: errorlevel invalid check 1
2678 call :CheckNotExist dir1\file1
2679
2680 rem Overwrite same file
2681 call :setError 0
2682 copy file1 file1 >NUL 2>&1
2683 if errorlevel 1 echo Passed: errorlevel invalid check 2
2684 if not errorlevel 1 echo Failed: errorlevel invalid check 2
2685
2686 rem Supply same file identified as a directory
2687 call :setError 0
2688 copy file1 file1\ >NUL 2>&1
2689 if errorlevel 1 echo Passed: errorlevel invalid check 3
2690 if not errorlevel 1 echo Failed: errorlevel invalid check 3
2691
2692 cd ..
2693 rd foobar2 /s /q
2694
2695 echo ------------ Testing setlocal/endlocal ------------
2696 call :setError 0
2697 rem Note: setlocal EnableDelayedExpansion already tested in the variable delayed expansion test section
2698 mkdir foobar & cd foobar
2699 echo --- enable/disable extensions
2700 setlocal DisableEXTensions
2701 echo ErrLev: %ErrorLevel%
2702 endlocal
2703 echo ErrLev: %ErrorLevel%
2704 echo @echo off> tmp.cmd
2705 echo echo ErrLev: %%ErrorLevel%%>> tmp.cmd
2706 rem Enabled by default
2707 cmd /C tmp.cmd
2708 cmd /E:OfF /C tmp.cmd
2709 cmd /e:oN /C tmp.cmd
2710
2711 rem FIXME: creating file before setting envvar value to prevent parsing-time evaluation (due to EnableDelayedExpansion not being implemented/available yet)
2712 echo --- setlocal with corresponding endlocal
2713 rem %CD% does not work on NT4 so use the following workaround
2714 for /d %%i in (.) do set WINE_CURDIR=%%~dpnxi
2715 echo @echo off> test.cmd
2716 echo echo %%WINE_VAR%%>> test.cmd
2717 echo setlocal>> test.cmd
2718 echo set WINE_VAR=localval>> test.cmd
2719 echo md foobar2>> test.cmd
2720 echo cd foobar2>> test.cmd
2721 echo echo %%WINE_VAR%%>> test.cmd
2722 echo for /d %%%%i in (.) do echo %%%%~dpnxi>> test.cmd
2723 echo endlocal>> test.cmd
2724 echo echo %%WINE_VAR%%>> test.cmd
2725 echo for /d %%%%i in (.) do echo %%%%~dpnxi>> test.cmd
2726 set WINE_VAR=globalval
2727 call test.cmd
2728 echo %WINE_VAR%
2729 for /d %%i in (.) do echo %%~dpnxi
2730 cd /d %WINE_CURDIR%
2731 rd foobar2
2732 set WINE_VAR=
2733 echo --- setlocal with no corresponding endlocal
2734 echo @echo off> test.cmd
2735 echo echo %%WINE_VAR%%>> test.cmd
2736 echo setlocal>> test.cmd
2737 echo set WINE_VAR=localval>> test.cmd
2738 echo md foobar2>> test.cmd
2739 echo cd foobar2>> test.cmd
2740 echo echo %%WINE_VAR%%>> test.cmd
2741 echo for /d %%%%i in (.) do echo %%%%~dpnxi>> test.cmd
2742 set WINE_VAR=globalval
2743 rem %CD% does not work on NT4 so use the following workaround
2744 for /d %%i in (.) do set WINE_CURDIR=%%~dpnxi
2745 call test.cmd
2746 echo %WINE_VAR%
2747 for /d %%i in (.) do echo %%~dpnxi
2748 cd /d %WINE_CURDIR%
2749 rd foobar2
2750 set WINE_VAR=
2751 echo --- setlocal within same batch program
2752 set WINE_var1=one
2753 set WINE_var2=
2754 set WINE_var3=
2755 rem %CD% does not work on NT4 so use the following workaround
2756 for /d %%i in (.) do set WINE_CURDIR=%%~dpnxi
2757 setlocal
2758 set WINE_var2=two
2759 mkdir foobar2
2760 cd foobar2
2761 setlocal
2762 set WINE_var3=three
2763 if "%WINE_var1%"=="one" echo Var1 ok 1
2764 if "%WINE_var2%"=="two" echo Var2 ok 2
2765 if "%WINE_var3%"=="three" echo Var3 ok 3
2766 for /d %%i in (.) do set WINE_curdir2=%%~dpnxi
2767 if "%WINE_curdir2%"=="%WINE_CURDIR%\foobar2" echo Directory is ok 1
2768 endlocal
2769 if "%WINE_var1%"=="one" echo Var1 ok 1
2770 if "%WINE_var2%"=="two" echo Var2 ok 2
2771 if "%WINE_var3%"=="" echo Var3 ok 3
2772 for /d %%i in (.) do set WINE_curdir2=%%~dpnxi
2773 if "%WINE_curdir2%"=="%WINE_CURDIR%\foobar2" echo Directory is ok 2
2774 endlocal
2775 if "%WINE_var1%"=="one" echo Var1 ok 1
2776 if "%WINE_var2%"=="" echo Var2 ok 2
2777 if "%WINE_var3%"=="" echo Var3 ok 3
2778 for /d %%i in (.) do set WINE_curdir2=%%~dpnxi
2779 if "%WINE_curdir2%"=="%WINE_CURDIR%" echo Directory is ok 3
2780 rd foobar2 /s /q
2781 set WINE_var1=
2782
2783 echo --- Mismatched set and end locals
2784 mkdir foodir2 2>nul
2785 mkdir foodir3 2>nul
2786 mkdir foodir4 2>nul
2787 rem %CD% does not work on NT4 so use the following workaround
2788 for /d %%i in (.) do set WINE_curdir=%%~dpnxi
2789
2790 echo @echo off> 2set1end.cmd
2791 echo echo %%WINE_var%%>> 2set1end.cmd
2792 echo setlocal>> 2set1end.cmd
2793 echo set WINE_VAR=2set1endvalue1>> 2set1end.cmd
2794 echo cd ..\foodir3>> 2set1end.cmd
2795 echo setlocal>> 2set1end.cmd
2796 echo set WINE_VAR=2set1endvalue2>> 2set1end.cmd
2797 echo cd ..\foodir4>> 2set1end.cmd
2798 echo endlocal>> 2set1end.cmd
2799 echo echo %%WINE_var%%>> 2set1end.cmd
2800 echo for /d %%%%i in (.) do echo %%%%~dpnxi>> 2set1end.cmd
2801
2802 echo @echo off> 1set2end.cmd
2803 echo echo %%WINE_var%%>> 1set2end.cmd
2804 echo setlocal>> 1set2end.cmd
2805 echo set WINE_VAR=1set2endvalue1>> 1set2end.cmd
2806 echo cd ..\foodir3>> 1set2end.cmd
2807 echo endlocal>> 1set2end.cmd
2808 echo echo %%WINE_var%%>> 1set2end.cmd
2809 echo for /d %%%%i in (.) do echo %%%%~dpnxi>> 1set2end.cmd
2810 echo endlocal>> 1set2end.cmd
2811 echo echo %%WINE_var%%>> 1set2end.cmd
2812 echo for /d %%%%i in (.) do echo %%%%~dpnxi>> 1set2end.cmd
2813
2814 echo --- Extra setlocal in called batch
2815 set WINE_VAR=value1
2816 rem -- setlocal1 == this batch, should never be used inside a called routine
2817 setlocal
2818 set WINE_var=value2
2819 cd foodir2
2820 call "%WINE_CURDIR%\2set1end.cmd"
2821 echo Finished:
2822 echo %WINE_VAR%
2823 for /d %%i in (.) do echo %%~dpnxi
2824 endlocal
2825 echo %WINE_VAR%
2826 for /d %%i in (.) do echo %%~dpnxi
2827 cd /d %WINE_CURDIR%
2828
2829 echo --- Extra endlocal in called batch
2830 set WINE_VAR=value1
2831 rem -- setlocal1 == this batch, should never be used inside a called routine
2832 setlocal
2833 set WINE_var=value2
2834 cd foodir2
2835 call "%WINE_CURDIR%\1set2end.cmd"
2836 echo Finished:
2837 echo %WINE_VAR%
2838 for /d %%i in (.) do echo %%~dpnxi
2839 endlocal
2840 echo %WINE_VAR%
2841 for /d %%i in (.) do echo %%~dpnxi
2842 cd /d %WINE_CURDIR%
2843
2844 echo --- endlocal in called function rather than batch pgm is ineffective
2845 @echo off
2846 set WINE_var=1
2847 set WINE_var2=1
2848 setlocal
2849 set WINE_var=2
2850 call :endlocalroutine
2851 echo %WINE_var%
2852 endlocal
2853 echo %WINE_var%
2854 goto :endlocalfinished
2855 :endlocalroutine
2856 echo %WINE_var%
2857 endlocal
2858 echo %WINE_var%
2859 setlocal
2860 set WINE_var2=2
2861 endlocal
2862 echo %WINE_var2%
2863 endlocal
2864 echo %WINE_var%
2865 echo %WINE_var2%
2866 goto :eof
2867 :endlocalfinished
2868 echo %WINE_var%
2869
2870 set WINE_var=
2871 set WINE_var2=
2872 cd .. & rd /q/s foobar
2873
2874 echo ------------ Testing Errorlevel ------------
2875 rem WARNING: Do *not* add tests using ErrorLevel after this section
2876 should_not_exist 2> nul > nul
2877 echo %ErrorLevel%
2878 rem nt 4.0 doesn't really support a way of setting errorlevel, so this is weak
2879 rem See http://www.robvanderwoude.com/exit.php
2880 call :setError 1
2881 echo %ErrorLevel%
2882 if errorlevel 2 echo errorlevel too high, bad
2883 if errorlevel 1 echo errorlevel just right, good
2884 if errorlevel 01 echo errorlevel with leading zero just right, good
2885 if errorlevel -1 echo errorlevel with negative number OK
2886 if errorlevel 0x1 echo hexa should not be recognized!
2887 if errorlevel 1a echo invalid error level recognized!
2888 call :setError 0
2889 echo abc%ErrorLevel%def
2890 if errorlevel 1 echo errorlevel nonzero, bad
2891 if not errorlevel 1 echo errorlevel zero, good
2892 if not errorlevel 0x1 echo hexa should not be recognized!
2893 if not errorlevel 1a echo invalid error level recognized!
2894 rem Now verify that setting a real variable hides its magic variable
2895 set errorlevel=7
2896 echo %ErrorLevel% should be 7
2897 if errorlevel 7 echo setting var worked too well, bad
2898 call :setError 3
2899 echo %ErrorLevel% should still be 7
2900
2901 echo ------------ Testing GOTO ------------
2902 if a==a goto dest1
2903 echo FAILURE at dest 1
2904 :dest1
2905 echo goto with no leading space worked
2906 if a==a goto :dest1b
2907 echo FAILURE at dest 1b
2908 :dest1b
2909 echo goto with colon and no leading space worked
2910 if b==b goto dest2
2911 echo FAILURE at dest 2
2912 :dest2
2913 echo goto with a leading space worked
2914 if c==c goto dest3
2915 echo FAILURE at dest 3
2916 :dest3
2917 echo goto with a leading tab worked
2918 if d==d goto dest4
2919 echo FAILURE at dest 4
2920 :dest4@space@
2921 echo goto with a following space worked
2922 if e==e goto dest5
2923 echo FAILURE at dest 5
2924 :dest5&& echo FAILURE
2925 echo goto with following amphersands worked
2926
2927 del failure.txt >nul 2>&1
2928 if f==f goto dest6
2929 echo FAILURE at dest 6
2930 :dest6>FAILURE.TXT
2931 if exist FAILURE.TXT echo FAILURE at dest 6 as file exists
2932 echo goto with redirections worked
2933 del FAILURE.TXT >nul 2>&1
2934
2935 :: some text that is ignored | dir >cmd_output | another test
2936 if exist cmd_output echo FAILURE at dest 6 as file exists
2937 echo Ignoring double colons worked
2938 del cmd_output >nul 2>&1
2939
2940 rem goto a label which does not exist issues an error message and
2941 rem acts the same as goto :EOF, and ensure ::label is never matched
2942 del testgoto.bat >nul 2>&1
2943 echo goto :dest7 ^>nul 2^>^&1 >> testgoto.bat
2944 echo echo FAILURE at dest 7 - Should have not found label and issued an error plus ended the batch>> testgoto.bat
2945 echo ::dest7>> testgoto.bat
2946 echo echo FAILURE at dest 7 - Incorrectly went to label >> testgoto.bat
2947 call testgoto.bat
2948 del testgoto.bat >nul 2>&1
2949
2950 del testgoto.bat >nul 2>&1
2951 echo goto ::dest8 ^>nul 2^>^&1 >> testgoto.bat
2952 echo echo FAILURE at dest 8 - Should have not found label and issued an error plus ended the batch>> testgoto.bat
2953 echo ::dest8>> testgoto.bat
2954 echo echo FAILURE at dest 8 - Incorrectly went to label >> testgoto.bat
2955 call testgoto.bat
2956 del testgoto.bat >nul 2>&1
2957
2958 if g==g goto dest9
2959 echo FAILURE at dest 9
2960 :dest91
2961 echo FAILURE at dest 91
2962 @ : dest9>rubbish
2963 echo label with mixed whitespace and no echo worked
2964
2965 if h==h goto :dest10:this is ignored
2966 echo FAILURE at dest 10
2967 :dest10:this is also ignored
2968 echo Correctly ignored trailing information
2969
2970 echo ------------ Testing PATH ------------
2971 set WINE_backup_path=%path%
2972 set path=original
2973 path
2974 path try2
2975 path
2976 path=try3
2977 path
2978 set path=%WINE_backup_path%
2979 set WINE_backup_path=
2980
2981 echo ------------ Testing combined CALLs/GOTOs ------------
2982 echo @echo off>foo.cmd
2983 echo goto :eof>>foot.cmd
2984 echo :eof>>foot.cmd
2985 echo echo world>>foo.cmd
2986
2987 echo @echo off>foot.cmd
2988 echo echo cheball>>foot.cmd
2989 echo.>>foot.cmd
2990 echo call :bar>>foot.cmd
2991 echo if "%%1"=="deleteMe" (del foot.cmd)>>foot.cmd
2992 echo goto :eof>>foot.cmd
2993 echo.>>foot.cmd
2994 echo :bar>>foot.cmd
2995 echo echo barbare>>foot.cmd
2996 echo goto :eof>>foot.cmd
2997
2998 call foo.cmd
2999 call foot
3000 call :bar
3001 del foo.cmd
3002 rem Script execution stops after the following line
3003 foot deleteMe
3004 call :foo
3005 call :foot
3006 goto :endFuns
3007
3008 :foot
3009 echo foot
3010
3011 :foo
3012 echo foo
3013 goto :eof
3014
3015 :endFuns
3016
3017 :bar
3018 echo bar
3019 call :foo
3020
3021 :baz
3022 echo baz
3023 goto :eof
3024
3025 echo Final message is not output since earlier 'foot' processing stops script execution
3026 echo Do NOT add any tests below this line
3027
3028 echo ------------ Done, jumping to EOF -----------
3029 goto :eof
3030 rem Subroutine to set errorlevel and return
3031 rem in windows nt 4.0, this always sets errorlevel 1, since /b isn't supported
3032 :setError
3033 exit /B %1
3034 rem This line runs under cmd in windows NT 4, but not in more modern versions.