1 echo Tests
for cmd's builtin commands
4 echo ------------ Testing 'echo' [ON] ------------
6 echo 'singlequotedword
'
7 echo "doublequotedword"
20 echo word@space@@space@
24 echo@tab@word@tab@@space@
33 echo ------------ Testing 'echo' [OFF] ------------
35 echo 'singlequotedword
'
36 echo "doublequotedword"
49 echo word@space@@space@
53 echo@tab@word@tab@@space@
59 echo ------------ Testing mixed echo modes ------------
60 echo @echo on> mixedEchoModes.cmd
61 echo if 1==1 echo foo>> mixedEchoModes.cmd
62 echo if 1==1 @echo bar>> mixedEchoModes.cmd
63 echo @echo off>> mixedEchoModes.cmd
64 echo if 1==1 echo foo2>> mixedEchoModes.cmd
65 echo if 1==1 @echo bar2>> mixedEchoModes.cmd
66 type mixedEchoModes.cmd
67 cmd /c mixedEchoModes.cmd
68 del mixedEchoModes.cmd
70 echo ------------ Testing rem ------------
75 rem echo foo & echo bar
78 rem@tab@echo foo & echo bar
84 rem echo foo & echo bar
87 rem@tab@echo foo & echo bar
90 echo ------------ Testing redirection operators ------------
91 mkdir foobar & cd foobar
92 echo --- stdout redirection
101 echo foo@tab@1>@tab@foo
116 echo --- stdout appending
134 echo foo7 7>> foo || (echo not supported & del foo)
135 if exist foo (type foo) else echo not supported
136 echo --- redirections within IF statements
137 if 1==1 echo foo1>bar
140 if 1==1 (echo foo2>bar) else echo baz2>bar
142 if 1==1 (echo foo3) else echo baz3>bar
143 type bar || echo file does not exist, ok
144 if 1==1 (echo foo4>bar) else echo baz4>bar
146 if 1==0 (echo foo5>bar) else echo baz5>bar
148 if 1==0 (echo foo6) else echo baz6 1>bar
150 if 1==0 (echo foo7 1>bar) else echo baz7>bar
152 if 1==0 (echo foo8 1>bar) else echo baz8>bak
154 if 1==1 (echo foo>bar & echo baz)
161 (if 1==1 (echo A) else echo B) > C
163 (if 1==0 (echo A) else echo B) > C
165 (if 1==0 (echo A > B) else echo C)
166 cd .. & rd /s/q foobar
168 echo ------------ Testing circumflex escape character ------------
169 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
182 echo bak ^
&& echo baz
2> nul
192 rem FIXME: echoing %FOO% gives an error (baz not recognized) but prematurely
193 rem exits the script on windows; redirecting stdout and/or stderr doesn't help
202 echo ------------ Testing
'set' ------------
204 set FOOBAR
2> nul
> nul
208 echo %FOOBAR%FOOBAR
not defined
220 echo %FOOBAR%FOOBAR
not defined
222 set FOOBA
2> nul
> nul
232 echo %FOO%FOO
not defined
237 echo set "FOO=bar" should
not include the quotes in the variable value
250 echo ------------ Testing variable expansion
------------
252 echo ~dp0 should be directory containing batch file
268 echo %ERRORLEVEL%%ERRORLEVEL%
269 echo %ERRORLEVEL%ERRORLEVEL%
276 echo ------------ Testing variable substrings
------------
289 echo ------------ Testing variable substitution
------------
290 echo --- in
FOR variables
291 for %%i in
("A B" C
) do echo %%i
293 for %%i in
("A B" C
) do echo '%%~i'
294 rem fully qualified path
295 for %%f in
("C D" E
) do echo %%~ff
297 for %%i in
("F G" H
) do echo %%~di
299 for %%d in
("I J" K
) do echo %%~pd
301 for %%i in
("L M" N
) do echo %%~ni
303 for %%i in
("O. P.OOL" Q.TABC hello
) do echo '%%~xi'
304 rem path with short path names
305 for %%I in
("R S" T ABCDEFGHIJK.LMNOP
) do echo '%%~sI'
307 for %%i in
("U V" W
) do echo '%%~ai'
309 for %%i in
(foo
) do echo '%%~ai'
312 rem Not fully testable, until we can grep dir's output to get foo's creation time in an envvar...
313 for %%i in
("a b" c
) do echo '%%~ti'
315 rem Similar issues as above
316 for %%i in
("a b" c
) do echo '%%~zi'
318 for %%i in
("d e" f
) do echo %%~dpi
319 for %%i in
("g h" i
) do echo %%~sdi
320 for %%i in
("g h" i
) do echo %%~dsi
321 for %%i in
("j k" l.eh
) do echo '%%~xsi'
323 echo --- in parameters
324 for %%i in
("A B" C
) do call :echoFun %%i
326 for %%i in
("A B" C
) do call :echoFunQ %%i
327 rem fully qualified path
328 for %%f in
("C D" E
) do call :echoFunF %%f
330 for %%i in
("F G" H
) do call :echoFunD %%i
332 for %%d in
("I J" K
) do call :echoFunP %%d
334 for %%i in
("L M" N
) do call :echoFunN %%i
336 for %%i in
("O. P.OOL" Q.TABC hello
) do call :echoFunX %%i
337 rem path with short path names
338 for %%I in
("R S" T ABCDEFGHIJK.LMNOP
) do call :echoFunS %%I
339 rem NT4 aborts whole script execution when encountering ~a, ~t and ~z substitutions, preventing full testing
341 for %%i in
("d e" f
) do call :echoFunDP %%i
342 for %%i in
("g h" i
) do call :echoFunSD %%i
343 for %%i in
("g h" i
) do call :echoFunDS %%i
344 for %%i in
("j k" l.eh
) do call :echoFunXS %%i
376 rem some NT4 workaround
399 echo ------------ Testing variable delayed expansion
------------
400 rem NT4 doesn't support this
401 echo --- default
mode (load
-time expansion
)
407 if %FOO% == bar
(echo bar
) else echo foo
413 if !FOO
! == bar
(echo bar
) else echo foo
416 echo --- runtime
(delayed
) expansion
mode
417 setlocal EnableDelayedExpansion
423 if %FOO% == bar
(echo bar
) else echo foo
429 if !FOO
! == bar
(echo bar
) else echo foo
432 setlocal DisableDelayedExpansion
438 echo --- using
/V
cmd flag
439 echo @
echo off
> tmp.
cmd
440 echo set FOO
=foo
>> tmp.
cmd
441 echo echo %%FOO%%>> tmp.
cmd
442 echo echo !FOO
!>> tmp.
cmd
443 echo set FOO
=>> tmp.
cmd
445 cmd /V
:OfF /C tmp.
cmd
448 echo ------------ Testing conditional execution
------------
449 echo --- unconditional ampersand
450 call :setError 123 & echo foo1
451 echo bar2
& echo foo2
452 mkdir foobar
& cd foobar
454 cd ..
& rd /s
/q foobar
456 echo foobar
not deleted
!
459 ) else echo foobar deleted
460 echo --- on success conditional and
461 call :setError 456 && echo foo3
> foo3
465 ) else echo foo3
not created
466 echo bar4
&& echo foo4
467 echo --- on failure conditional or
468 call :setError 789 ||
echo foo5
469 echo foo6 ||
echo bar6
> bar6
475 echo ------------ Testing
cd ------------
478 echo blabla
> singleFile
480 echo Current
dir: %CD%
495 if not exist foobar
(cd ..
)
497 cd@tab@..@tab@@space@@tab@
499 if not exist foobar
(cd ..
)
507 cd "bar bak@space@"@tab@@space@
518 echo ------------ Testing
type ------------
528 echo ------------ Testing NUL
------------
529 md foobar
& cd foobar
530 rem NUL file (non) creation + case insensitivity
531 rem Note: "if exist" does not work with NUL, so to check for file existence we use a kludgy workaround
540 rem NUL not special everywhere
543 if not exist foo
(echo foo should have been created
) else (
547 rem Empty file creation
558 echo ------------ Testing
if/else ------------
559 echo if/else should work with blocks
561 echo if seems to work
563 echo if seems to be broken
566 echo else seems to be broken
568 echo else seems to work
571 echo if seems
not to detect
/c as parameter
573 echo parameter detection seems to be broken
575 echo Testing case sensitivity with and without
/i option
576 if bar
==BAR
echo if does
not default to case sensitivity
577 if not bar
==BAR
echo if seems to default to case sensitivity
578 if /i foo
==FOO
echo if /i seems to work
579 if /i
not foo
==FOO
echo if /i seems to be broken
580 if /I foo
==FOO
echo if /I seems to work
581 if /I
not foo
==FOO
echo if /I seems to be broken
582 echo Testing string comparisons
583 if abc
== abc
(echo equal
) else echo non equal
584 if abc
=="abc" (echo equal
) else echo non equal
585 if "abc"== abc
(echo equal
) else echo non equal
586 if "abc"== "abc" (echo equal
) else echo non equal
587 echo Testing tabs handling
588 if@tab@
1==1 echo doom
589 if @tab@
1==1 echo doom
590 if 1==1 (echo doom
) else@tab@
echo quake
591 if@tab@
not @tab@
1==@tab@
0 @tab@
echo lol
592 if 1==0@tab@
(echo doom
) else echo quake
593 if 1==0 (echo doom
)@tab@
else echo quake
594 if 1==0 (echo doom
) else@tab@
echo quake
596 echo ------------ Testing
for ------------
598 for %%i in
(A B C
) do echo %%i
599 for %%i in
(A B C
) do echo %%I
600 for %%i in
(A B C
) do echo %%j
601 for %%i in
(A B C
) do call :forTestFun1 %%i
602 for %%i in
(1,4,1) do echo %%i
603 for %%i in
(A
, B
,C
) do echo %%i
604 for %%i in
(X
) do echo %%i
605 for@tab@
%%i in
(X2
) do echo %%i
606 for %%i in@tab@
(X3
) do echo %%i
607 for %%i in
(@tab@ foo@tab@
) do echo %%i
608 for@tab@
%%i in@tab@
(@tab@M
) do echo %%i
609 for %%i@tab@in
(X
)@tab@
do@tab@
echo %%i
610 for@tab@
%%j in@tab@
(@tab@M
, N
, O@tab@
) do echo %%j
611 for %%i in
(`
echo A B`
) do echo %%i
612 for %%i in
('echo A B') do echo %%i
613 for %%i in
("echo A B") do echo %%i
614 for %%i in
("A B" C
) do echo %%i
620 echo --- imbricated FORs
627 for %%i in
(A B
) do (
628 for %%j in
(C D
) do (
630 for %%i in
(A B
) do (
631 for %%j in
(C D
) do (
632 call :forTestFun2 %%i
%%j
))
638 mkdir foobar
& cd foobar
643 echo --- basic wildcards
644 for %%i in
(ba
*) do echo %%i
646 for /d
%%i in
(baz foo bar
) do echo %%i
647 rem FIXME for /d incorrectly parses when wildcards are used
648 rem for /d %%i in (bazb*) do echo %%i
649 rem FIXME can't test wildcard expansion here since it's listed in directory
650 rem order, and not in alphabetic order.
651 rem Proper testing would need a currently missing "sort" program implementation.
652 rem for /d %%i in (ba*) do echo %%i>> tmp
655 rem for /d %%i in (?a*) do echo %%i>> tmp
658 rem for /d %%i in (*) do echo %%i>> tmp
661 cd ..
& rd /s
/Q foobar
663 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
664 for /L
%%i in
(1,2,0) do echo %%i
665 for@tab@
/L
%%i in
(1,2,0) do echo %%i
666 for /L
%%i in
(1,2,6) do echo %%i
667 for /l
%%i in
(1 ,2,6) do echo %%i
668 for /L
%%i in
(a
,2,3) do echo %%i
669 for /L
%%i in
(1,2,-1) do echo %%i
670 for /L
%%i in
(-4,-1,-1) do echo %%i
671 for /L
%%i in
(1,-2,-2) do echo %%i
672 for /L
%%i in
(1,2,a
) do echo %%i
673 echo ErrorLevel %ErrorLevel%
674 for /L
%%i in
(1,a
,b
) do echo %%i
675 echo ErrorLevel %ErrorLevel%
676 rem FIXME: following test cases cannot be currently tested due to an inconsistent/buggy 'for /L' parsing.
677 rem for /L %%i in (a,2,b) do echo %%i
678 rem for /L %%i in (1,1,1) do echo %%i
679 rem for /L %%i in (1,-2,-1) do echo %%i
680 rem for /L %%i in (-1,-1,-1) do echo %%i
681 rem for /L %%i in (1,2, 3) do echo %%i
683 rem No output when using "set expr" syntax, unless in interactive mode
684 rem Need to use "set envvar=expr" to use in a batch script
685 echo ------ individual operations
687 set /a var
=1 +2 & echo %var%
688 set /a var
=1 +-2 & echo %var%
689 set /a var
=1 --2 & echo %var%
690 set /a var
=2* 3 & echo %var%
691 set /a var
=-2* -5 & echo %var%
692 set /a var
=12/3 & echo %var%
693 set /a var
=13/3 & echo %var%
694 set /a var
=-13/3 & echo %var%
695 rem FIXME Divide by zero should return an error, but error messages cannot be tested with current infrastructure
696 set /a var
=5 %% 5 & echo %var%
697 set /a var
=5 %% 3 & echo %var%
698 set /a var
=5 %% -3 & echo %var%
699 set /a var
=-5 %% -3 & echo %var%
700 set /a var
=1 ^
<^
< 0 & echo %var%
701 set /a var
=1 ^
<^
< 2 & echo %var%
702 set /a var
=1 ^
<^
< -2 & echo %var%
703 set /a var
=-1 ^
<^
< -2 & echo %var%
704 set /a var
=-1 ^
<^
< 2 & echo %var%
705 set /a var
=9 ^
>^
> 0 & echo %var%
706 set /a var
=9 ^
>^
> 2 & echo %var%
707 set /a var
=9 ^
>^
> -2 & echo %var%
708 set /a var
=-9 ^
>^
> -2 & echo %var%
709 set /a var
=-9 ^
>^
> 2 & echo %var%
710 set /a var
=5 ^
& 0 & echo %var%
711 set /a var
=5 ^
& 1 & echo %var%
712 set /a var
=5 ^
& 3 & echo %var%
713 set /a var
=5 ^
& 4 & echo %var%
714 set /a var
=5 ^
& 1 & echo %var%
715 set /a var
=5 ^|
0 & echo %var%
716 set /a var
=5 ^|
1 & echo %var%
717 set /a var
=5 ^|
3 & echo %var%
718 set /a var
=5 ^|
4 & echo %var%
719 set /a var
=5 ^|
1 & echo %var%
720 set /a var
=5 ^^
0 & echo %var%
721 set /a var
=5 ^^
1 & echo %var%
722 set /a var
=5 ^^
3 & echo %var%
723 set /a var
=5 ^^
4 & echo %var%
724 set /a var
=5 ^^
1 & echo %var%
725 echo ------ precedence and grouping
726 set /a var
=4 + 2*3 & echo %var%
727 set /a var
=(4+2)*3 & echo %var%
728 set /a var
=4 * 3/5 & echo %var%
729 set /a var
=(4 * 3)/5 & echo %var%
730 set /a var
=4 * 5 %% 4 & echo %var%
731 set /a var
=4 * (5 %% 4) & echo %var%
732 set /a var
=3 %% (5 + 8 %% 3 ^^
2) & echo %var%
733 set /a var
=3 %% (5 + 8 %% 3 ^^
-2) & echo %var%
734 echo ------ octal and hexadecimal
735 set /a var
=0xf + 3 & echo %var%
736 set /a var
=0xF + 3 & echo %var%
737 set /a var
=015 + 2 & echo %var%
738 set /a var
=3, 8+3,0 & echo %var%
739 echo ------ variables
740 set /a var
=foo
=3, foo
+1 & echo %var%
741 if defined foo
(echo %foo%) else (
744 set /a var
=foo
=3, foo
+=1 & echo %var%
745 set /a var
=foo
=3, bar
=1, bar
+=foo
, bar
& echo %var%
746 set /a var
=foo
*= foo
& echo %var%
747 set /a var
=whateverNonExistingVar
& echo %var%
748 set /a var
=whateverNonExistingVar
+ bar
& echo %var%
749 set /a var
=foo
-= foo
+ 7 & echo %var%
750 set /a var
=foo
/= 3 + 2 & echo %var%
751 set /a var
=foo
=5, foo
%%=2 & echo %var%
752 set /a var
=foo ^
<^
<= 2 & echo %var%
753 set /a var
=foo ^
>^
>= 2 & echo %var%
754 set /a var
=foo ^
&= 2 & echo %var%
755 set /a var
=foo
=5, foo ^|
= 2 & echo %var%
756 set /a var
=foo
=5, foo ^^
= 2 & echo %var%
757 set /a var
=foo
=19, foo
%%= 4 + (bar
%%= 7) & echo.
762 mkdir foobar
& cd foobar
763 echo ------ string argument
764 for /F
%%i in
("a b c") do echo %%i
765 for /f
%%i in
("a ") do echo %%i
766 for /f
%%i in
("a") do echo %%i
767 fOr /f
%%i in
(" a") do echo %%i
768 for /f
%%i in
(" a ") do echo %%i
769 echo ------ fileset argument
770 echo --------- basic blank handling
772 for /f
%%i in
(foo
) do echo %%i
774 for /f
%%i in
(foo
) do echo %%i
776 for /f
%%i in
(foo
) do echo %%i
778 for /f
%%i in
(foo
) do echo %%i
780 for /f
%%i in
(foo
) do echo %%i
782 for /f
%%i in
(foo
) do echo %%i
785 for /f
%%i in
(foo
) do echo %%i
786 echo --------- multi
-line with empty lines
793 for /f
%%b in
(foo
) do echo %%b
794 echo --------- multiple
files
798 for /f
%%k in
(foo bar
) do echo %%k
799 for /f
%%k in
(bar foo
) do echo %%k
800 rem echo ------ command argument
801 rem Not implemented on NT4
802 rem FIXME: Not testable right now in wine: not implemented and would need
803 rem preliminary grep-like program implementation (e.g. like findstr or fc) even
804 rem for a simple todo_wine test
805 rem (for /f "usebackq" %%i in (`echo z a b`) do echo %%i) || echo not supported
806 rem (for /f usebackq %%i in (`echo z a b`) do echo %%i) || echo not supported
807 echo ------ eol option
808 for /f
"eol=@" %%i in
(" ad") do echo %%i
809 for /f
"eol=@" %%i in
(" z@y") do echo %%i
810 for /f
"eol=|" %%i in
("a|d") do echo %%i
811 for /f
"eol=@" %%i in
("@y") do echo %%i
> output_file
812 if not exist output_file
(echo no output
) else (del output_file
)
813 for /f
"eol==" %%i in
("=y") do echo %%i
> output_file
814 if not exist output_file
(echo no output
) else (del output_file
)
815 echo ------ delims option
816 for /f
"delims=|" %%i in
("a|d") do echo %%i
817 for /f
"delims=|" %%i in
("a |d") do echo %%i
818 for /f
"delims=|" %%i in
("a d|") do echo %%i
819 for /f
"delims=| " %%i in
("a d|") do echo %%i
820 for /f
"delims==" %%i in
("C r=d|") do echo %%i
821 for /f
"delims=" %%i in
("foo bar baz") do echo %%i
822 for /f
"delims=" %%i in
("c:\foo bar baz\..") do echo %%~fi
823 echo ------ skip option
827 for /f
"skip=2" %%i in
(foo
) do echo %%i
828 for /f
"skip=3" %%i in
(foo
) do echo %%i
> output_file
829 if not exist output_file
(echo no output
) else (del output_file
)
830 for /f
"skip=4" %%i in
(foo
) do echo %%i
> output_file
831 if not exist output_file
(echo no output
) else (del output_file
)
835 echo ------------ Testing
del /a
------------
836 del /f
/q
*.test
> nul
839 echo not-r
> not-r.test
841 if not exist not-r.test
echo not-r.test
not found before delete
, bad
843 if not exist not-r.test
echo not-r.test
not found after delete
, good
845 if not exist r.test
echo r.test
not found before delete
, bad
846 if exist r.test
echo r.test found before delete
, good
848 if not exist r.test
echo r.test
not found after delete
, good
849 if exist r.test
echo r.test found after delete
, bad
851 echo ------------ Testing
del /q
------------
856 rem If /q doesn't work, cmd will prompt and the test case should hang
858 for %%a in
(1 2.dat
) do if exist file
%%a
echo del /q
* failed on file
%%a
859 for %%a in
(1 2.dat
) do if not exist file
%%a
echo del /q
* succeeded on file
%%a
863 echo ------------ Testing
del /s
------------
868 echo there
> file2.dat
870 echo bye
> "file with spaces.dat"
872 del /s file1.dat
> nul
873 del file2.dat
/s
> nul
874 del "file3.dat" /s
> nul
875 del "file with spaces.dat" /s
> nul
877 for %%f in
(1 2 3) do if exist file
%%f.dat
echo Del /s failed on file
%%f
878 for %%f in
(1 2 3) do if exist file
%%f.dat
del file
%%f.dat
879 if exist "file with spaces.dat" echo Del /s failed on
"file with spaces.dat"
880 if exist "file with spaces.dat" del "file with spaces.dat"
885 echo ------------ Testing
rename ------------
886 mkdir foobar
& cd foobar
887 echo --- ren and
rename are synonymous
890 if exist foo
echo foo should be renamed
!
891 if exist bar
echo foo renamed to bar
893 if exist bar
echo bar should be renamed
!
894 if exist foo
echo bar renamed to foo
895 echo --- name collision
905 echo --- rename read
-only
files
911 echo read
-only file renamed
914 echo read
-only file
not renamed
!
916 echo --- rename directories
928 echo read
-only
dir renamed
931 echo --- rename in other directory
932 if not exist baz
\abc
(
933 echo rename impossible in other directory
934 if exist foo
echo original file still present
936 echo shouldn
't rename in other directory!
937 if not exist foo echo original file not present anymore
939 cd .. & rd /s/q foobar
941 echo ------------ Testing move ------------
942 mkdir foobar & cd foobar
945 move foo bar > nul 2>&1
948 echo file move succeeded
953 move /Y bar baz > nul 2>&1
956 echo file move with overwrite succeeded
959 echo file overwrite impossible!
965 move baz bazro > nul 2>&1
968 echo read-only files are moveable
969 move bazro baz > nul 2>&1
972 echo read-only file not moved!
976 move baz rep > nul 2>&1
979 echo file moved in subdirectory
983 move rep\baz . > nul 2>&1
984 move /Y baz baz > nul 2>&1
986 echo moving a file to itself should be a no-op!
988 echo moving a file to itself is a no-op
990 echo ErrorLevel: %ErrorLevel%
993 echo --- directory move
997 move baz foo\bar > nul 2>&1
999 if exist foo\bar\baz\baz2 (
1000 echo simple directory move succeeded
1005 move baz baz > nul 2>&1
1006 echo moving a directory to itself gives error; errlevel %ErrorLevel%
1007 echo ------ dir in dir move
1012 move foo bar > nul 2>&1
1020 cd .. & rd /s/q foobar
1022 echo ------------ Testing mkdir ------------
1024 echo --- md and mkdir are synonymous
1031 echo --- creating an already existing directory/file must fail
1040 echo --- multilevel path creation
1052 mkdir ..\..\..\foo\bar2
1055 mkdir ..\..\..\foo\bar2
1057 rmdir ..\..\..\foo\bar2
1065 echo --- trailing backslashes
1068 if exist foo (rmdir foo & echo dir created
1069 ) else ( echo dir not created )
1071 echo --- invalid chars
1073 echo mkdir ? gives errorlevel %ErrorLevel%
1076 echo mkdir ?\foo gives errorlevel %ErrorLevel%
1079 echo mkdir foo\? gives errorlevel %ErrorLevel%
1080 if exist foo (rmdir foo & echo ok, foo created
1081 ) else ( echo foo not created )
1084 echo mkdir foo\bar\? gives errorlevel %ErrorLevel%
1087 echo bad, foo not created
1091 echo ok, foo\bar created
1097 echo --- multiple directories at once
1098 mkdir foobaz & cd foobaz
1099 mkdir foo bar\baz foobar
1100 if exist foo (echo foo created) else echo foo not created!
1101 if exist bar (echo bar created) else echo bar not created!
1102 if exist foobar (echo foobar created) else echo foobar not created!
1103 if exist bar\baz (echo bar\baz created) else echo bar\baz not created!
1104 cd .. & rd /s/q foobaz
1107 echo mkdir foo\* errorlevel %ErrorLevel%
1108 if exist foo (rmdir foo & echo ok, foo created
1109 ) else ( echo bad, foo not created )
1111 echo ------------ Testing rmdir ------------
1113 rem rd and rmdir are synonymous
1117 if not exist foobar echo dir removed
1121 if not exist foobar echo dir removed
1122 rem Removing nonexistent directory
1125 rem Removing single-level directories
1129 if exist foo echo file not removed
1135 if exist foo echo non-empty dir not removed
1140 if exist foo echo non-empty dir not removed
1147 echo recursive rmdir succeeded
1154 echo foo > foo\bar\brol
1157 echo recursive rmdir succeeded
1164 rem multiples directories at once
1165 mkdir foobaz & cd foobaz
1169 rd /s/q foo bar foobar
1170 if not exist foo (echo foo removed) else echo foo not removed!
1171 if not exist bar (echo bar removed) else echo bar not removed!
1172 if not exist foobar (echo foobar removed) else echo foobar not removed!
1173 if not exist bar\baz (echo bar\baz removed) else echo bar\baz not removed!
1174 cd .. & rd /s/q foobaz
1176 echo ------------ Testing pushd/popd ------------
1178 echo --- popd is no-op when dir stack is empty
1181 echo --- pushing non-existing dir
1184 echo --- basic behaviour
1209 echo ------------ Testing attrib ------------
1210 rem FIXME Add tests for archive, hidden and system attributes + mixed attributes modifications
1211 mkdir foobar & cd foobar
1212 echo foo original contents> foo
1215 echo --- read-only attribute
1216 rem Read-only files cannot be altered or deleted, unless forced
1224 echo Read-only file not deleted
1226 echo Should not delete read-only file!
1230 echo Read-only file forcibly deleted
1232 echo Should delete read-only file with del /F!
1236 cd .. & rd /s/q foobar
1237 echo --- recursive behaviour
1238 mkdir foobar\baz & cd foobar
1244 attrib +R l*vel? /S > nul 2>&1
1250 cd .. & rd /s/q foobar
1251 echo --- folders processing
1257 attrib +r baz /s /d > nul 2>&1
1263 rem Oddly windows allows file creation in a read-only directory...
1264 if exist baz\lala (echo file created in read-only dir) else echo file not created
1265 cd .. & rd /s/q foobar
1267 echo ------------ Testing assoc ------------
1268 rem FIXME Can't test error messages in the current test system
, so we have to
use some kludges
1269 rem FIXME Revise once || conditional execution is fixed
1270 mkdir foobar
& cd foobar
1271 echo --- setting association
1279 rem association set system-wide
1280 echo @
echo off
> tmp.
cmd
1281 echo echo +++>> tmp.
cmd
1282 echo assoc .foo
>> tmp.
cmd
1285 echo --- resetting association
1291 rem association removal set system-wide
1292 cmd /c tmp.
cmd > baz
1295 cd ..
& rd /s
/q foobar
1297 echo ------------ Testing ftype
------------
1298 rem FIXME Can't test error messages in the current test system, so we have to use some kludges
1299 rem FIXME Revise once || conditional execution is fixed
1300 mkdir foobar
& cd foobar
1301 echo --- setting association
1306 ftype footype
=foo_opencmd
1310 rem association set system-wide
1311 echo @
echo off
> tmp.
cmd
1312 echo echo +++>> tmp.
cmd
1313 echo ftype footype
>> tmp.
cmd
1316 echo --- resetting association
1319 rem Removing a file type association doesn't work on XP due to a bug, so a workaround is needed
1320 setlocal EnableDelayedExpansion
1321 set FOO
=original value
1324 for /F
%%i in
('type baz') do (set FOO
=buggyXP
)
1325 rem Resetting actually works on wine/NT4, but is reported as failing due to the peculiar test (and non-support for EnabledDelayedExpansion)
1326 rem FIXME Revisit once a grep-like program like ftype is implemented
1327 rem (e.g. to check baz's size using dir /b instead)
1330 rem cleanup registry
1331 echo REGEDIT4
> regCleanup.reg
1332 echo.
>> regCleanup.reg
1333 echo [-HKEY_CLASSES_ROOT
\footype
]>> regCleanup.reg
1334 regedit
/s regCleanup.reg
1337 cd ..
& rd /s
/q foobar
1339 echo ------------ Testing
CALL ------------
1340 mkdir foobar
& cd foobar
1341 echo --- external script
1342 echo echo foo
%%1> foo.
cmd
1345 echo echo %%1 %%2 > foo.
cmd
1347 call foo.
cmd foo bar
1354 echo --- internal routines
1355 call :testRoutine :testRoutine
1356 goto :endTestRoutine
1362 call :testRoutineArgs foo
1363 call :testRoutineArgs foo bar
1364 call :testRoutineArgs foo
""
1365 call :testRoutineArgs "" bar
1366 call :testRoutineArgs foo
''
1367 call :testRoutineArgs '' bar
1368 goto :endTestRoutineArgs
1374 echo --- with builtins
1377 if exist foo
(echo foo created
) else echo foo should
exist!
1379 set FOOBAZ_VAR
=foobaz
1380 call echo Should expand
%FOOBAZ_VAR%
1385 if 1==1 call del batfile
1387 if exist batfile
echo batfile shouldn
't exist
1388 rem ... but not for 'if' or 'for'
1389 call if 1==1 echo bar 2> nul
1392 call for %%i in (foo bar baz) do echo %%i 2> nul
1394 rem First look for programs in the path before trying a builtin
1395 echo echo non-builtin dir> dir.cmd
1397 cd .. & rd /s/q foobar
1399 echo ------------ Testing SHIFT ------------
1401 call :shiftFun p1 p2 p3 p4 p5
1405 echo '%1' '%2' '%3' '%4' '%5'
1407 echo '%1' '%2' '%3' '%4' '%5'
1409 echo '%1' '%2' '%3' '%4' '%5'
1411 echo '%1' '%2' '%3' '%4' '%5'
1413 echo '%1' '%2' '%3' '%4' '%5'
1415 echo '%1' '%2' '%3' '%4' '%5'
1419 echo ------------ Testing cmd invocation ------------
1420 rem FIXME: only a stub ATM
1421 echo --- a batch file can delete itself
1422 echo del foo.cmd>foo.cmd
1424 if not exist foo.cmd (
1425 echo file correctly deleted
1427 echo file should be deleted!
1430 echo --- a batch file can alter itself
1431 echo echo bar^>foo.cmd>foo.cmd
1432 cmd /q /c foo.cmd > NUL 2>&1
1437 echo file not created!
1440 echo ------------ Testing setlocal/endlocal ------------
1442 rem Note: setlocal EnableDelayedExpansion already tested in the variable delayed expansion test section
1443 mkdir foobar & cd foobar
1444 echo --- enable/disable extensions
1445 setlocal DisableEXTensions
1446 echo ErrLev: %ErrorLevel%
1448 echo ErrLev: %ErrorLevel%
1449 echo @echo off> tmp.cmd
1450 echo echo ErrLev: %%ErrorLevel%%>> tmp.cmd
1451 rem Enabled by default
1453 cmd /E:OfF /C tmp.cmd
1454 cmd /e:oN /C tmp.cmd
1456 rem FIXME: creating file before setting envvar value to prevent parsing-time evaluation (due to EnableDelayedExpansion not being implemented/available yet)
1457 echo --- setlocal with corresponding endlocal
1458 echo @echo off> test.cmd
1459 echo echo %%VAR%%>> test.cmd
1460 echo setlocal>> test.cmd
1461 echo set VAR=localval>> test.cmd
1462 echo echo %%VAR%%>> test.cmd
1463 echo endlocal>> test.cmd
1464 echo echo %%VAR%%>> test.cmd
1469 echo --- setlocal with no corresponding endlocal
1470 echo @echo off> test.cmd
1471 echo echo %%VAR%%>> test.cmd
1472 echo setlocal>> test.cmd
1473 echo set VAR=localval>> test.cmd
1474 echo echo %%VAR%%>> test.cmd
1479 cd .. & rd /q/s foobar
1481 echo ------------ Testing Errorlevel ------------
1482 rem WARNING: Do *not* add tests using ErrorLevel after this section
1483 should_not_exist 2> nul > nul
1485 rem nt 4.0 doesn't really support a way of setting
errorlevel, so this is weak
1486 rem See http://www.robvanderwoude.com/exit.php
1489 if errorlevel 2 echo errorlevel too high
, bad
1490 if errorlevel 1 echo errorlevel just right
, good
1491 if errorlevel 01 echo errorlevel with leading zero just right
, good
1492 if errorlevel -1 echo errorlevel with negative number OK
1493 if errorlevel 0x1 echo hexa should
not be recognized
!
1494 if errorlevel 1a
echo invalid error level recognized
!
1496 echo abc
%ErrorLevel%def
1497 if errorlevel 1 echo errorlevel nonzero
, bad
1498 if not errorlevel 1 echo errorlevel zero
, good
1499 if not errorlevel 0x1 echo hexa should
not be recognized
!
1500 if not errorlevel 1a
echo invalid error level recognized
!
1501 rem Now verify that setting a real variable hides its magic variable
1503 echo %ErrorLevel% should be
7
1504 if errorlevel 7 echo setting var worked too well
, bad
1506 echo %ErrorLevel% should still be
7
1508 echo ------------ Testing
GOTO ------------
1511 echo goto with no leading space worked
1514 echo goto with a leading space worked
1517 echo goto with a leading tab worked
1520 echo goto with a following space worked
1522 echo ------------ Testing combined CALLs
/GOTOs
------------
1523 echo @
echo off
>foo.
cmd
1524 echo goto :eof>>foot.
cmd
1526 echo echo world
>>foo.
cmd
1528 echo @
echo off
>foot.
cmd
1529 echo echo cheball
>>foot.
cmd
1531 echo call :bar>>foot.
cmd
1532 echo if "%%1"=="deleteMe" (del foot.
cmd)>>foot.
cmd
1533 echo goto :eof>>foot.
cmd
1536 echo echo barbare
>>foot.
cmd
1537 echo goto :eof>>foot.
cmd
1543 rem Script execution stops after the following line
1566 echo Final message is
not output since earlier
'foot' processing stops script execution
1567 echo Do NOT add any tests below this line
1569 echo ------------ Done
, jumping to EOF
-----------
1571 rem Subroutine to set errorlevel and return
1572 rem in windows nt 4.0, this always sets errorlevel 1, since /b isn't supported
1575 rem This line runs under cmd in windows NT 4, but not in more modern versions.