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 variable delayed expansion
--------------
172 rem NT4 doesn't support this
173 echo ...default
mode (load
-time expansion
)
179 if %FOO% == bar
(echo bar
) else echo foo
185 if !FOO
! == bar
(echo bar
) else echo foo
188 echo ...runtime
(delayed
) expansion
mode
189 setlocal EnableDelayedExpansion
195 if %FOO% == bar
(echo bar
) else echo foo
201 if !FOO
! == bar
(echo bar
) else echo foo
204 setlocal DisableDelayedExpansion
211 echo ------------ Testing conditional execution
--------------
212 echo ...unconditional ^
&
213 call :setError 123 & echo foo1
214 echo bar2
& echo foo2
215 mkdir foobar
& cd foobar
217 cd ..
& rd /s
/q foobar
219 echo foobar
not deleted
!
222 ) else echo foobar deleted
223 echo ...on success conditional ^
&^
&
224 call :setError 456 && echo foo3
> foo3
228 ) else echo foo3
not created
229 echo bar4
&& echo foo4
230 echo ...on failure conditional ^|^|
231 call :setError 789 ||
echo foo5
232 echo foo6 ||
echo bar6
> bar6
238 echo ------------ Testing
type ------------
248 echo ------------ Testing NUL
------------
249 md foobar
& cd foobar
250 rem NUL file (non) creation + case insensitivity
251 rem Note: "if exist" does not work with NUL, so to check for file existence we use a kludgy workaround
260 rem NUL not special everywhere
263 if not exist foo
(echo foo should have been created
) else (
267 rem Empty file creation
279 echo ------------ Testing
if/else --------------
280 echo if/else should work with blocks
282 echo if seems to work
284 echo if seems to be broken
287 echo else seems to be broken
289 echo else seems to work
292 echo if seems
not to detect
/c as parameter
294 echo parameter detection seems to be broken
296 echo Testing case sensitivity with and without
/i option
297 if bar
==BAR
echo if does
not default to case sensitivity
298 if not bar
==BAR
echo if seems to default to case sensitivity
299 if /i foo
==FOO
echo if /i seems to work
300 if /i
not foo
==FOO
echo if /i seems to be broken
301 if /I foo
==FOO
echo if /I seems to work
302 if /I
not foo
==FOO
echo if /I seems to be broken
304 echo -----------Testing
for -----------
306 for %%i in
(A B C
) do echo %%i
307 for %%i in
(A B C
) do echo %%I
308 for %%i in
(A B C
) do echo %%j
309 for %%i in
(A B C
) do call :forTestFun1 %%i
310 for %%i in
(1,4,1) do echo %%i
311 for %%i in
(A
, B
,C
) do echo %%i
317 echo ...imbricated FORs
324 for %%i in
(A B
) do (
325 for %%j in
(C D
) do (
327 for %%i in
(A B
) do (
328 for %%j in
(C D
) do (
329 call :forTestFun2 %%i
%%j
))
335 mkdir foobar
& cd foobar
340 echo ...basic wildcards
341 for %%i in
(ba
*) do echo %%i
343 for /d
%%i in
(baz foo bar
) do echo %%i
344 rem FIXME for /d incorrectly parses when wildcards are used
345 rem for /d %%i in (bazb*) do echo %%i
346 rem FIXME can't test wildcard expansion here since it's listed in directory
347 rem order, and not in alphabetic order.
348 rem Proper testing would need a currently missing "sort" program implementation.
349 rem for /d %%i in (ba*) do echo %%i>> tmp
352 rem for /d %%i in (?a*) do echo %%i>> tmp
355 rem for /d %%i in (*) do echo %%i>> tmp
361 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
362 for /L
%%i in
(1,2,0) do echo %%i
363 for /L
%%i in
(1,2,6) do echo %%i
364 for /l
%%i in
(1 ,2,6) do echo %%i
365 for /L
%%i in
(a
,2,3) do echo %%i
366 for /L
%%i in
(1,2,-1) do echo %%i
367 for /L
%%i in
(-4,-1,-1) do echo %%i
368 for /L
%%i in
(1,-2,-2) do echo %%i
369 for /L
%%i in
(1,2,a
) do echo %%i
370 echo ErrorLevel %ErrorLevel%
371 for /L
%%i in
(1,a
,b
) do echo %%i
372 echo ErrorLevel %ErrorLevel%
373 rem FIXME: following test cases cannot be currently tested due to an inconsistent/buggy 'for /L' parsing.
374 rem for /L %%i in (a,2,b) do echo %%i
375 rem for /L %%i in (1,1,1) do echo %%i
376 rem for /L %%i in (1,-2,-1) do echo %%i
377 rem for /L %%i in (-1,-1,-1) do echo %%i
378 rem for /L %%i in (1,2, 3) do echo %%i
380 echo -----------Testing
del /a
-----------
381 del /f
/q
*.test
> nul
384 echo not-r
> not-r.test
386 if not exist not-r.test
echo not-r.test
not found before delete
, bad
388 if not exist not-r.test
echo not-r.test
not found after delete
, good
390 if not exist r.test
echo r.test
not found before delete
, bad
391 if exist r.test
echo r.test found before delete
, good
393 if not exist r.test
echo r.test
not found after delete
, good
394 if exist r.test
echo r.test found after delete
, bad
396 echo ------------ Testing
del /q
--------------
401 rem If /q doesn't work, cmd will prompt and the test case should hang
403 for %%a in
(1 2.dat
) do if exist file
%%a
echo del /q
* failed on file
%%a
404 for %%a in
(1 2.dat
) do if not exist file
%%a
echo del /q
* succeeded on file
%%a
408 echo ------------ Testing
del /s
--------------
412 echo there
> file2.dat
414 echo bye
> "file with spaces.dat"
416 del /s file1.dat
> nul
417 del file2.dat
/s
> nul
418 del "file3.dat" /s
> nul
419 del "file with spaces.dat" /s
> nul
421 for %%f in
(1 2 3) do if exist file
%%f.dat
echo Del /s failed on file
%%f
422 for %%f in
(1 2 3) do if exist file
%%f.dat
del file
%%f.dat
423 if exist "file with spaces.dat" echo Del /s failed on
"file with spaces.dat"
424 if exist "file with spaces.dat" del "file with spaces.dat"
428 echo ----------- Testing
mkdir -----------
430 rem md and mkdir are synonymous
437 rem Creating an already existing directory/file must fail
446 rem Multi-level path creation
458 mkdir ..\..\..
\foo
\bar
2
461 mkdir ..\..\..
\foo
\bar
2
463 rmdir ..\..\..
\foo
\bar
2
471 rem Trailing backslashes
474 if exist foo
(rmdir foo
& echo dir created
475 ) else ( echo dir not created
)
486 if exist foo
(rmdir foo
& echo ok
, foo created
487 ) else ( echo foo
not created
)
493 echo bad
, foo
not created
497 echo ok
, foo
\bar created
503 rem multiples directories at once
504 mkdir foobaz
& cd foobaz
505 mkdir foo bar
\baz foobar
506 if exist foo
(echo foo created
) else echo foo
not created
!
507 if exist bar
(echo bar created
) else echo bar
not created
!
508 if exist foobar
(echo foobar created
) else echo foobar
not created
!
509 if exist bar
\baz
(echo bar
\baz created
) else echo bar
\baz
not created
!
513 echo ----------- Testing
rmdir -----------
515 rem rd and rmdir are synonymous
519 if not exist foobar
echo dir removed
523 if not exist foobar
echo dir removed
524 rem Removing non-existent directory
527 rem Removing single-level directories
531 if exist foo
echo file
not removed
537 if exist foo
echo non
-empty
dir not removed
542 if exist foo
echo non
-empty
dir not removed
549 echo recursive
rmdir succeeded
556 echo foo
> foo
\bar
\brol
559 echo recursive
rmdir succeeded
566 rem multiples directories at once
567 mkdir foobaz
& cd foobaz
571 rd /s
/q foo bar foobar
572 if not exist foo
(echo foo removed
) else echo foo
not removed
!
573 if not exist bar
(echo bar removed
) else echo bar
not removed
!
574 if not exist foobar
(echo foobar removed
) else echo foobar
not removed
!
575 if not exist bar
\baz
(echo bar
\baz removed
) else echo bar
\baz
not removed
!
579 echo ------------ Testing
CALL --------------
580 mkdir foobar
& cd foobar
582 echo echo foo
%%1> foo.
cmd
585 echo echo %%1 %%2 > foo.
cmd
593 rem Internal routines
594 call :testRoutine :testRoutine
601 call :testRoutineArgs foo
602 call :testRoutineArgs foo bar
603 call :testRoutineArgs foo
""
604 call :testRoutineArgs "" bar
605 call :testRoutineArgs foo
''
606 call :testRoutineArgs '' bar
607 goto :endTestRoutineArgs
613 rem Should work for builtins...
616 if exist foo
(echo foo created
) else echo foo should
exist!
618 set FOOBAZ_VAR
=foobaz
619 call echo Should expand
%FOOBAZ_VAR%
624 if 1==1 call del batfile
626 if exist batfile
echo batfile shouldn
't exist
627 rem ... but not for 'if' or 'for'
628 call if 1==1 echo bar 2> nul
631 call for %%i in (foo bar baz) do echo %%i 2> nul
633 rem First look for programs in the path before trying a builtin
634 echo echo non-builtin dir> dir.cmd
639 echo -----------Testing Errorlevel-----------
640 rem WARNING: Do *not* add tests using ErrorLevel after this section
641 should_not_exist 2> nul > nul
643 rem nt 4.0 doesn't really support a way of setting
errorlevel, so this is weak
644 rem See http://www.robvanderwoude.com/exit.php
647 if errorlevel 2 echo errorlevel too high
, bad
648 if errorlevel 1 echo errorlevel just right
, good
650 echo abc
%ErrorLevel%def
651 if errorlevel 1 echo errorlevel nonzero
, bad
652 if not errorlevel 1 echo errorlevel zero
, good
653 rem Now verify that setting a real variable hides its magic variable
655 echo %ErrorLevel% should be
7
656 if errorlevel 7 echo setting var worked too well
, bad
658 echo %ErrorLevel% should still be
7
660 echo -----------Testing
GOTO-----------
663 echo goto with no leading space worked
666 echo goto with a leading space worked
669 echo goto with a leading tab worked
672 echo goto with a following space worked
674 echo -----------Done
, jumping to EOF
-----------
676 rem Subroutine to set errorlevel and return
677 rem in windows nt 4.0, this always sets errorlevel 1, since /b isn't supported
680 rem This line runs under cmd in windows NT 4, but not in more modern versions.