1 echo Tests
for cmd's builtin commands
4 echo ------------ Testing 'echo' [ON] --------------
6 echo 'singlequotedword
'
7 echo "doublequotedword"
15 echo word@space@@space@
18 echo ------------ Testing 'echo' [OFF] --------------
20 echo 'singlequotedword
'
21 echo "doublequotedword"
29 echo word@space@@space@
31 echo ------------ Testing redirection operators --------------
32 mkdir foobar & cd foobar
33 echo ...stdout redirection
44 echo ...stdout appending
63 echo ------------ Testing ^^ escape character --------------
64 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
77 echo bak ^
&& echo baz
2> nul
87 rem FIXME: echoing %FOO% gives an error (baz not recognized) but prematurely
88 rem exits the script on windows; redirecting stdout and/or stderr doesn't help
97 echo ------------ Testing
'set' --------------
99 set FOOBAR
2> nul
> nul
103 echo %FOOBAR%FOOBAR
not defined
115 echo %FOOBAR%FOOBAR
not defined
117 set FOOBA
2> nul
> nul
127 echo %FOO%FOO
not defined
132 echo set "FOO=bar" should
not include the quotes in the variable value
137 echo ------------ Testing variable expansion
--------------
139 echo ~dp0 should be directory containing batch file
155 echo %ERRORLEVEL%%ERRORLEVEL%
156 echo %ERRORLEVEL%ERRORLEVEL%
163 echo ------------ Testing conditional execution
--------------
164 echo ...unconditional ^
&
165 call :setError 123 & echo foo1
166 echo bar2
& echo foo2
167 mkdir foobar
& cd foobar
169 cd ..
& rd /s
/q foobar
171 echo foobar
not deleted
!
174 ) else echo foobar deleted
175 echo ...on success conditional ^
&^
&
176 call :setError 456 && echo foo3
> foo3
180 ) else echo foo3
not created
181 echo bar4
&& echo foo4
182 echo ...on failure conditional ^|^|
183 call :setError 789 ||
echo foo5
184 echo foo6 ||
echo bar6
> bar6
190 echo ------------ Testing
type ------------
200 echo ------------ Testing NUL
------------
201 md foobar
& cd foobar
202 rem NUL file (non) creation + case insensitivity
203 rem Note: "if exist" does not work with NUL, so to check for file existence we use a kludgy workaround
212 rem NUL not special everywhere
215 if not exist foo
(echo foo should have been created
) else (
219 rem Empty file creation
231 echo ------------ Testing
if/else --------------
232 echo if/else should work with blocks
234 echo if seems to work
236 echo if seems to be broken
239 echo else seems to be broken
241 echo else seems to work
244 echo if seems
not to detect
/c as parameter
246 echo parameter detection seems to be broken
248 echo Testing case sensitivity with and without
/i option
249 if bar
==BAR
echo if does
not default to case sensitivity
250 if not bar
==BAR
echo if seems to default to case sensitivity
251 if /i foo
==FOO
echo if /i seems to work
252 if /i
not foo
==FOO
echo if /i seems to be broken
253 if /I foo
==FOO
echo if /I seems to work
254 if /I
not foo
==FOO
echo if /I seems to be broken
256 echo -----------Testing
for -----------
257 for %%i in
(A B C
) do echo %%i
258 for %%i in
(A B C
) do call :forTestFun1 %%i
267 for %%i in
(A B
) do (
268 for %%j in
(C D
) do (
270 for %%i in
(A B
) do (
271 for %%j in
(C D
) do (
272 call :forTestFun2 %%i
%%j
))
279 echo -----------Testing
del /a
-----------
280 del /f
/q
*.test
> nul
283 echo not-r
> not-r.test
285 if not exist not-r.test
echo not-r.test
not found before delete
, bad
287 if not exist not-r.test
echo not-r.test
not found after delete
, good
289 if not exist r.test
echo r.test
not found before delete
, bad
290 if exist r.test
echo r.test found before delete
, good
292 if not exist r.test
echo r.test
not found after delete
, good
293 if exist r.test
echo r.test found after delete
, bad
295 echo ------------ Testing
del /q
--------------
300 rem If /q doesn't work, cmd will prompt and the test case should hang
302 for %%a in
(1 2.dat
) do if exist file
%%a
echo del /q
* failed on file
%%a
303 for %%a in
(1 2.dat
) do if not exist file
%%a
echo del /q
* succeeded on file
%%a
307 echo ------------ Testing
del /s
--------------
311 echo there
> file2.dat
313 echo bye
> "file with spaces.dat"
315 del /s file1.dat
> nul
316 del file2.dat
/s
> nul
317 del "file3.dat" /s
> nul
318 del "file with spaces.dat" /s
> nul
320 for %%f in
(1 2 3) do if exist file
%%f.dat
echo Del /s failed on file
%%f
321 for %%f in
(1 2 3) do if exist file
%%f.dat
del file
%%f.dat
322 if exist "file with spaces.dat" echo Del /s failed on
"file with spaces.dat"
323 if exist "file with spaces.dat" del "file with spaces.dat"
327 echo ----------- Testing
mkdir -----------
329 rem md and mkdir are synonymous
336 rem Creating an already existing directory/file must fail
345 rem Multi-level path creation
357 mkdir ..\..\..
\foo
\bar
2
360 mkdir ..\..\..
\foo
\bar
2
362 rmdir ..\..\..
\foo
\bar
2
370 rem Trailing backslashes
373 if exist foo
(rmdir foo
& echo dir created
374 ) else ( echo dir not created
)
385 if exist foo
(rmdir foo
& echo ok
, foo created
386 ) else ( echo foo
not created
)
392 echo bad
, foo
not created
396 echo ok
, foo
\bar created
402 rem multiples directories at once
403 mkdir foobaz
& cd foobaz
404 mkdir foo bar
\baz foobar
405 if exist foo
(echo foo created
) else echo foo
not created
!
406 if exist bar
(echo bar created
) else echo bar
not created
!
407 if exist foobar
(echo foobar created
) else echo foobar
not created
!
408 if exist bar
\baz
(echo bar
\baz created
) else echo bar
\baz
not created
!
412 echo ----------- Testing
rmdir -----------
414 rem rd and rmdir are synonymous
418 if not exist foobar
echo dir removed
422 if not exist foobar
echo dir removed
423 rem Removing non-existent directory
426 rem Removing single-level directories
430 if exist foo
echo file
not removed
436 if exist foo
echo non
-empty
dir not removed
441 if exist foo
echo non
-empty
dir not removed
448 echo recursive
rmdir succeeded
455 echo foo
> foo
\bar
\brol
458 echo recursive
rmdir succeeded
465 rem multiples directories at once
466 mkdir foobaz
& cd foobaz
470 rd /s
/q foo bar foobar
471 if not exist foo
(echo foo removed
) else echo foo
not removed
!
472 if not exist bar
(echo bar removed
) else echo bar
not removed
!
473 if not exist foobar
(echo foobar removed
) else echo foobar
not removed
!
474 if not exist bar
\baz
(echo bar
\baz removed
) else echo bar
\baz
not removed
!
478 echo ------------ Testing
CALL --------------
479 mkdir foobar
& cd foobar
481 echo echo foo
%%1 > foo.
cmd
485 rem Internal routines
486 call :testRoutine :testRoutine
492 rem Should work for builtins...
495 if exist foo
(echo foo created
) else echo foo should
exist!
497 set FOOBAZ_VAR
=foobaz
498 call echo Should expand
%FOOBAZ_VAR%
503 if 1==1 call del batfile
505 if exist batfile
echo batfile shouldn
't exist
506 rem ... but not for 'if' or 'for'
507 call if 1==1 echo bar 2> nul
510 call for %%i in (foo bar baz) do echo %%i 2> nul
512 rem First look for programs in the path before trying a builtin
513 echo echo non-builtin dir > dir.cmd
518 echo -----------Testing Errorlevel-----------
519 rem WARNING: Do *not* add tests using ErrorLevel after this section
520 should_not_exist 2> nul > nul
522 rem nt 4.0 doesn't really support a way of setting
errorlevel, so this is weak
523 rem See http://www.robvanderwoude.com/exit.php
526 if errorlevel 2 echo errorlevel too high
, bad
527 if errorlevel 1 echo errorlevel just right
, good
529 echo abc
%ErrorLevel%def
530 if errorlevel 1 echo errorlevel nonzero
, bad
531 if not errorlevel 1 echo errorlevel zero
, good
532 rem Now verify that setting a real variable hides its magic variable
534 echo %ErrorLevel% should be
7
535 if errorlevel 7 echo setting var worked too well
, bad
537 echo %ErrorLevel% should still be
7
539 echo -----------Testing
GOTO-----------
542 echo goto with no leading space worked
545 echo goto with a leading space worked
548 echo goto with a leading tab worked
551 echo goto with a following space worked
553 echo -----------Done
, jumping to EOF
-----------
555 rem Subroutine to set errorlevel and return
556 rem in windows nt 4.0, this always sets errorlevel 1, since /b isn't supported
559 rem This line runs under cmd in windows NT 4, but not in more modern versions.