[CMD_ROSTEST] Add tests for particular ERRORLEVEL behaviour in some commands.

This commit is contained in:
Hermès Bélusca-Maïto 2020-07-27 16:42:42 +02:00
parent a5d7a2cd61
commit 0b400bbb98
No known key found for this signature in database
GPG key ID: 3B2539C65E7B93D0
2 changed files with 131 additions and 0 deletions

View file

@ -396,6 +396,108 @@ del tmp.cmd
cd .. & rmdir /s/q foobar
::
:: ERRORLEVEL tests for special commands.
::
:: For some commands, the errorlevel is set differently,
:: whether or not they are run within a .BAT, a .CMD, or
:: directly from the command-line.
::
:: These commands are:
:: APPEND/DPATH, ASSOC, FTYPE, PATH, PROMPT, SET.
::
:: See https://ss64.com/nt/errorlevel.html for more details.
::
echo ---------- Testing ERRORLEVEL in .BAT and .CMD ----------
:: Use an auxiliary CMD file
mkdir foobar && cd foobar
echo --- In .BAT file
call :outputErrLvlTestToBatch tmp.bat
cmd /c tmp.bat
del tmp.bat
echo --- In .CMD file
call :outputErrLvlTestToBatch tmp.cmd
cmd /c tmp.cmd
del tmp.cmd
:: Cleanup
cd .. & rmdir /s/q foobar
:: Go to the next tests below.
goto :continue
:: ERRORLEVEL test helper function
:outputErrLvlTestToBatch (filename)
echo @echo off> %1
echo setlocal enableextensions>> %1
:: Reset the errorlevel
echo call :zeroErrLvl>> %1
:: dpath
:: echo %errorlevel%
:: dpath xxx
:: echo %errorlevel%
:: dpath
:: echo %errorlevel%
echo assoc^>NUL>> %1
echo echo %%errorlevel%%>> %1
echo assoc .nonexisting^>NUL>> %1
echo echo %%errorlevel%%>> %1
echo assoc .nonexisting^=^>NUL>> %1
echo echo %%errorlevel%%>> %1
:: ftype
:: echo %errorlevel%
:: ftype xxx
:: echo %errorlevel%
:: ftype
:: echo %errorlevel%
echo path^>NUL>> %1
echo echo %%errorlevel%%>> %1
echo path^;^>NUL>> %1
echo echo %%errorlevel%%>> %1
echo prompt^>NUL>> %1
echo echo %%errorlevel%%>> %1
echo prompt ^$p^$g^>NUL>> %1
echo echo %%errorlevel%%>> %1
echo prompt foobar^>NUL>> %1
echo echo %%errorlevel%%>> %1
echo set^>NUL>> %1
echo echo %%errorlevel%%>> %1
echo set nonexisting^>NUL>> %1
echo echo %%errorlevel%%>> %1
echo set nonexisting^=^>NUL>> %1
echo echo %%errorlevel%%>> %1
echo set nonexisting^=trololol^>NUL>> %1
echo echo %%errorlevel%%>> %1
echo set nonexisting^=^>NUL>> %1
echo echo %%errorlevel%%>> %1
echo goto :eof>> %1
:: Zero ERRORLEVEL
echo :zeroErrLvl>> %1
echo exit /B 0 >> %1
goto :eof
::
:: Next suite of tests.
::
:continue
::
:: Finished!
::

View file

@ -101,4 +101,33 @@ OK
OK
OK
OK
---------- Testing ERRORLEVEL in .BAT and .CMD ----------
--- In .BAT file
0
1
2
2
2
2
2
2
2
1
1
1
1
--- In .CMD file
0
1
2
0
0
0
0
0
0
1
0
0
0
--------- Finished --------------