Question

I have a very basic bat file with code (below)

@echo off


%SystemRoot%\Microsoft.NET\Framework\v4.0.30319\msbuild CaptainHook.sln /m /nr:false /p:Platform="Any CPU" /v:M /fl /flp:LogFile=bin\msbuild.log;Verbosity=Normal
%SystemRoot%\Microsoft.NET\Framework\v4.0.30319\msbuild Tests.sln /m /nr:false /p:Platform="Any CPU" /v:M /fl /flp:LogFile=bin\msbuild.log;Verbosity=Normal
"%ProgramFiles(x86)%\Microsoft Visual Studio 11.0\Common7\IDE\mstest.exe" /testcontainer:%CD%\src\UnitTests\bin\Debug\UnitTests.dll" 

I am able to execute this as preoutgoing.build_test hook however even if the build fails,, commit is not aborted, but if a test fails, commit gets aborted. What am I missing ?

Was it helpful?

Solution

You are unconditionally running the tests after the build. If the last executed command returns a zero exit code then it will overwrite any non-zero exit code from the previous commands.

So you need to check the exit code (ERRORLEVEL in Windows batch) between each command and exit if non-zero. Alternatively, if you want to run the tests even if the build fails, save the exit code and exit with it at the end.

To exit immediately:

@ECHO OFF

%SystemRoot%\Microsoft.NET\Framework\v4.0.30319\msbuild CaptainHook.sln /m /nr:false /p:Platform="Any CPU" /v:M /fl /flp:LogFile=bin\msbuild.log;Verbosity=Normal
IF ERRORLEVEL 1 GOTO :EOF

%SystemRoot%\Microsoft.NET\Framework\v4.0.30319\msbuild Tests.sln /m /nr:false /p:Platform="Any CPU" /v:M /fl /flp:LogFile=bin\msbuild.log;Verbosity=Normal
IF ERRORLEVEL 1 GOTO :EOF

"%ProgramFiles(x86)%\Microsoft Visual Studio 11.0\Common7\IDE\mstest.exe" /testcontainer:%CD%\src\UnitTests\bin\Debug\UnitTests.dll"
IF ERRORLEVEL 1 GOTO :EOF

(the extra line at the end allows adding more commands easily).

To execute all and exit with highest errorlevel:

@ECHO OFF

SETLOCAL
SET EXITCODE=0

%SystemRoot%\Microsoft.NET\Framework\v4.0.30319\msbuild CaptainHook.sln /m /nr:false /p:Platform="Any CPU" /v:M /fl /flp:LogFile=bin\msbuild.log;Verbosity=Normal
IF %ERRORLEVEL% GTR %EXITCODE% SET EXITCODE=%ERRORLEVEL%

%SystemRoot%\Microsoft.NET\Framework\v4.0.30319\msbuild Tests.sln /m /nr:false /p:Platform="Any CPU" /v:M /fl /flp:LogFile=bin\msbuild.log;Verbosity=Normal
IF %ERRORLEVEL% GTR %EXITCODE% SET EXITCODE=%ERRORLEVEL%

"%ProgramFiles(x86)%\Microsoft Visual Studio 11.0\Common7\IDE\mstest.exe" /testcontainer:%CD%\src\UnitTests\bin\Debug\UnitTests.dll"
IF %ERRORLEVEL% GTR %EXITCODE% SET EXITCODE=%ERRORLEVEL%

EXIT /B %EXITCODE%
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top