1 echo Tests
for cmd's builtin commands
4 echo ------------ Testing 'echo' [ON] --------------
6 echo 'singlequotedword
'
7 echo "doublequotedword"
19 echo word@space@@space@
22 echo ------------ Testing 'echo' [OFF] --------------
24 echo 'singlequotedword
'
25 echo "doublequotedword"
37 echo word@space@@space@
39 echo ------------ Testing redirection operators --------------
40 mkdir foobar & cd foobar
41 echo ...stdout redirection
52 echo ...stdout appending
71 echo ------------ Testing ^^ escape character --------------
72 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
85 echo bak ^
&& echo baz
2> nul
95 rem FIXME: echoing %FOO% gives an error (baz not recognized) but prematurely
96 rem exits the script on windows; redirecting stdout and/or stderr doesn't help
105 echo ------------ Testing
'set' --------------
107 set FOOBAR
2> nul
> nul
111 echo %FOOBAR%FOOBAR
not defined
123 echo %FOOBAR%FOOBAR
not defined
125 set FOOBA
2> nul
> nul
135 echo %FOO%FOO
not defined
140 echo set "FOO=bar" should
not include the quotes in the variable value
145 echo ------------ Testing variable expansion
--------------
147 echo ~dp0 should be directory containing batch file
163 echo %ERRORLEVEL%%ERRORLEVEL%
164 echo %ERRORLEVEL%ERRORLEVEL%
171 echo ------------ Testing conditional execution
--------------
172 echo ...unconditional ^
&
173 call :setError 123 & echo foo1
174 echo bar2
& echo foo2
175 mkdir foobar
& cd foobar
177 cd ..
& rd /s
/q foobar
179 echo foobar
not deleted
!
182 ) else echo foobar deleted
183 echo ...on success conditional ^
&^
&
184 call :setError 456 && echo foo3
> foo3
188 ) else echo foo3
not created
189 echo bar4
&& echo foo4
190 echo ...on failure conditional ^|^|
191 call :setError 789 ||
echo foo5
192 echo foo6 ||
echo bar6
> bar6
198 echo ------------ Testing
type ------------
208 echo ------------ Testing NUL
------------
209 md foobar
& cd foobar
210 rem NUL file (non) creation + case insensitivity
211 rem Note: "if exist" does not work with NUL, so to check for file existence we use a kludgy workaround
220 rem NUL not special everywhere
223 if not exist foo
(echo foo should have been created
) else (
227 rem Empty file creation
239 echo ------------ Testing
if/else --------------
240 echo if/else should work with blocks
242 echo if seems to work
244 echo if seems to be broken
247 echo else seems to be broken
249 echo else seems to work
252 echo if seems
not to detect
/c as parameter
254 echo parameter detection seems to be broken
256 echo Testing case sensitivity with and without
/i option
257 if bar
==BAR
echo if does
not default to case sensitivity
258 if not bar
==BAR
echo if seems to default to case sensitivity
259 if /i foo
==FOO
echo if /i seems to work
260 if /i
not foo
==FOO
echo if /i seems to be broken
261 if /I foo
==FOO
echo if /I seems to work
262 if /I
not foo
==FOO
echo if /I seems to be broken
264 echo -----------Testing
for -----------
266 for %%i in
(A B C
) do echo %%i
267 for %%i in
(A B C
) do echo %%I
268 for %%i in
(A B C
) do echo %%j
269 for %%i in
(A B C
) do call :forTestFun1 %%i
270 for %%i in
(1,4,1) do echo %%i
271 for %%i in
(A
, B
,C
) do echo %%i
277 echo ...imbricated FORs
284 for %%i in
(A B
) do (
285 for %%j in
(C D
) do (
287 for %%i in
(A B
) do (
288 for %%j in
(C D
) do (
289 call :forTestFun2 %%i
%%j
))
295 mkdir foobar
& cd foobar
300 echo ...basic wildcards
301 for %%i in
(ba
*) do echo %%i
303 for /d
%%i in
(baz foo bar
) do echo %%i
304 rem FIXME for /d incorrectly parses when wildcards are used
305 rem for /d %%i in (bazb*) do echo %%i
306 rem FIXME can't test wildcard expansion here since it's listed in directory
307 rem order, and not in alphabetic order.
308 rem Proper testing would need a currently missing "sort" program implementation.
309 rem for /d %%i in (ba*) do echo %%i>> tmp
312 rem for /d %%i in (?a*) do echo %%i>> tmp
315 rem for /d %%i in (*) do echo %%i>> tmp
321 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
322 for /L
%%i in
(1,2,0) do echo %%i
323 for /L
%%i in
(1,2,6) do echo %%i
324 for /l
%%i in
(1 ,2,6) do echo %%i
325 for /L
%%i in
(a
,2,3) do echo %%i
326 for /L
%%i in
(1,2,-1) do echo %%i
327 for /L
%%i in
(-4,-1,-1) do echo %%i
328 for /L
%%i in
(1,-2,-2) do echo %%i
329 for /L
%%i in
(1,2,a
) do echo %%i
330 echo ErrorLevel %ErrorLevel%
331 for /L
%%i in
(1,a
,b
) do echo %%i
332 echo ErrorLevel %ErrorLevel%
333 rem FIXME: following test cases cannot be currently tested due to an inconsistent/buggy 'for /L' parsing.
334 rem for /L %%i in (a,2,b) do echo %%i
335 rem for /L %%i in (1,1,1) do echo %%i
336 rem for /L %%i in (1,-2,-1) do echo %%i
337 rem for /L %%i in (-1,-1,-1) do echo %%i
338 rem for /L %%i in (1,2, 3) do echo %%i
340 echo -----------Testing
del /a
-----------
341 del /f
/q
*.test
> nul
344 echo not-r
> not-r.test
346 if not exist not-r.test
echo not-r.test
not found before delete
, bad
348 if not exist not-r.test
echo not-r.test
not found after delete
, good
350 if not exist r.test
echo r.test
not found before delete
, bad
351 if exist r.test
echo r.test found before delete
, good
353 if not exist r.test
echo r.test
not found after delete
, good
354 if exist r.test
echo r.test found after delete
, bad
356 echo ------------ Testing
del /q
--------------
361 rem If /q doesn't work, cmd will prompt and the test case should hang
363 for %%a in
(1 2.dat
) do if exist file
%%a
echo del /q
* failed on file
%%a
364 for %%a in
(1 2.dat
) do if not exist file
%%a
echo del /q
* succeeded on file
%%a
368 echo ------------ Testing
del /s
--------------
372 echo there
> file2.dat
374 echo bye
> "file with spaces.dat"
376 del /s file1.dat
> nul
377 del file2.dat
/s
> nul
378 del "file3.dat" /s
> nul
379 del "file with spaces.dat" /s
> nul
381 for %%f in
(1 2 3) do if exist file
%%f.dat
echo Del /s failed on file
%%f
382 for %%f in
(1 2 3) do if exist file
%%f.dat
del file
%%f.dat
383 if exist "file with spaces.dat" echo Del /s failed on
"file with spaces.dat"
384 if exist "file with spaces.dat" del "file with spaces.dat"
388 echo ----------- Testing
mkdir -----------
390 rem md and mkdir are synonymous
397 rem Creating an already existing directory/file must fail
406 rem Multi-level path creation
418 mkdir ..\..\..
\foo
\bar
2
421 mkdir ..\..\..
\foo
\bar
2
423 rmdir ..\..\..
\foo
\bar
2
431 rem Trailing backslashes
434 if exist foo
(rmdir foo
& echo dir created
435 ) else ( echo dir not created
)
446 if exist foo
(rmdir foo
& echo ok
, foo created
447 ) else ( echo foo
not created
)
453 echo bad
, foo
not created
457 echo ok
, foo
\bar created
463 rem multiples directories at once
464 mkdir foobaz
& cd foobaz
465 mkdir foo bar
\baz foobar
466 if exist foo
(echo foo created
) else echo foo
not created
!
467 if exist bar
(echo bar created
) else echo bar
not created
!
468 if exist foobar
(echo foobar created
) else echo foobar
not created
!
469 if exist bar
\baz
(echo bar
\baz created
) else echo bar
\baz
not created
!
473 echo ----------- Testing
rmdir -----------
475 rem rd and rmdir are synonymous
479 if not exist foobar
echo dir removed
483 if not exist foobar
echo dir removed
484 rem Removing non-existent directory
487 rem Removing single-level directories
491 if exist foo
echo file
not removed
497 if exist foo
echo non
-empty
dir not removed
502 if exist foo
echo non
-empty
dir not removed
509 echo recursive
rmdir succeeded
516 echo foo
> foo
\bar
\brol
519 echo recursive
rmdir succeeded
526 rem multiples directories at once
527 mkdir foobaz
& cd foobaz
531 rd /s
/q foo bar foobar
532 if not exist foo
(echo foo removed
) else echo foo
not removed
!
533 if not exist bar
(echo bar removed
) else echo bar
not removed
!
534 if not exist foobar
(echo foobar removed
) else echo foobar
not removed
!
535 if not exist bar
\baz
(echo bar
\baz removed
) else echo bar
\baz
not removed
!
539 echo ------------ Testing
CALL --------------
540 mkdir foobar
& cd foobar
542 echo echo foo
%%1> foo.
cmd
546 rem Internal routines
547 call :testRoutine :testRoutine
553 rem Should work for builtins...
556 if exist foo
(echo foo created
) else echo foo should
exist!
558 set FOOBAZ_VAR
=foobaz
559 call echo Should expand
%FOOBAZ_VAR%
564 if 1==1 call del batfile
566 if exist batfile
echo batfile shouldn
't exist
567 rem ... but not for 'if' or 'for'
568 call if 1==1 echo bar 2> nul
571 call for %%i in (foo bar baz) do echo %%i 2> nul
573 rem First look for programs in the path before trying a builtin
574 echo echo non-builtin dir> dir.cmd
579 echo -----------Testing Errorlevel-----------
580 rem WARNING: Do *not* add tests using ErrorLevel after this section
581 should_not_exist 2> nul > nul
583 rem nt 4.0 doesn't really support a way of setting
errorlevel, so this is weak
584 rem See http://www.robvanderwoude.com/exit.php
587 if errorlevel 2 echo errorlevel too high
, bad
588 if errorlevel 1 echo errorlevel just right
, good
590 echo abc
%ErrorLevel%def
591 if errorlevel 1 echo errorlevel nonzero
, bad
592 if not errorlevel 1 echo errorlevel zero
, good
593 rem Now verify that setting a real variable hides its magic variable
595 echo %ErrorLevel% should be
7
596 if errorlevel 7 echo setting var worked too well
, bad
598 echo %ErrorLevel% should still be
7
600 echo -----------Testing
GOTO-----------
603 echo goto with no leading space worked
606 echo goto with a leading space worked
609 echo goto with a leading tab worked
612 echo goto with a following space worked
614 echo -----------Done
, jumping to EOF
-----------
616 rem Subroutine to set errorlevel and return
617 rem in windows nt 4.0, this always sets errorlevel 1, since /b isn't supported
620 rem This line runs under cmd in windows NT 4, but not in more modern versions.