Reputation: 7253
I'm using cake in my projects to build, run unit tests, check code coverage and then generate an HTML report (using ReportGenerator). This is all working correctly, and I can open the generated report in my browser.
However, when I was previously using a dos batch file to do this, it would also launch my default browser and load the report after it was generated, but I can't find a way to do that with cake.
Here are the contents of the batch file I've been using:
@ECHO OFF
SET SearchDirectory=%~dp0Grapevine.Tests\bin\Debug
SET DllContainingTests=%~dp0Grapevine.Tests\bin\Debug\Grapevine.Tests.dll
for /R "%~dp0packages" %%a in (*) do if /I "%%~nxa"=="xunit.console.exe" SET TestRunnerExe=%%~dpnxa
for /R "%~dp0packages" %%a in (*) do if /I "%%~nxa"=="OpenCover.Console.exe" SET OpenCoverExe=%%~dpnxa
for /R "%~dp0packages" %%a in (*) do if /I "%%~nxa"=="ReportGenerator.exe" SET ReportGeneratorExe=%%~dpnxa
if not exist "%~dp0GeneratedReports" mkdir "%~dp0GeneratedReports"
call :RunOpenCoverUnitTestMetrics
if %errorlevel% equ 0 (
call :RunReportGeneratorOutput
)
if %errorlevel% equ 0 (
call :RunLaunchReport
)
exit /b %errorlevel%
:RunOpenCoverUnitTestMetrics
"%OpenCoverExe%" ^
-target:"%TestRunnerExe%" ^
-targetargs:"\"%DllContainingTests%\"" ^
-filter:"+[*]* -[*.Tests*]* -[*]*.*Config -[xunit*]* -[*]Grapevine.Interfaces.*" ^
-mergebyhash ^
-skipautoprops ^
-register:user ^
-output:"%~dp0GeneratedReports\CoverageReport.xml"^
-searchdirs:"%SearchDirectory%"
exit /b %errorlevel%
:RunReportGeneratorOutput
"%ReportGeneratorExe%" ^
-reports:"%~dp0\GeneratedReports\CoverageReport.xml" ^
-targetdir:"%~dp0\GeneratedReports\ReportGeneratorOutput"
exit /b %errorlevel%
:RunLaunchReport
start "report" "%~dp0\GeneratedReports\ReportGeneratorOutput\index.htm"
exit /b %errorlevel%
I have tried using the following:
StartProcess(new FilePath("./GeneratedReports/ReportGeneratorOutput/index.htm"));
To which I receive the following error:
An error occured when executing task 'generate-report'.
Error: The specified executable is not a valid application for this OS platform.
I have verified that the path is correct and the file exists, and that copy/pasting the file path on the command line indeed opens the file in my default browser.
Upvotes: 1
Views: 635
Reputation: 4757
I couldn't figure out a way to do this with just Cake, so I resorted to calling CMD with StartProcess
:
if (IsRunningOnWindows()) {
StartProcess("cmd", new ProcessSettings {
Arguments = $"/C start \"\" {testCoverageReportPath}index.htm"
});
}
This works great for my needs.
Upvotes: 1
Reputation: 7253
What finally worked for me was this:
if (IsRunningOnWindows())
{
StartProcess("explorer.exe", reportPath);
}
Obviously, this won't work on non-windows environments, but that's outside the scope of my needs. Everything else I tried produced an error either that the file could not be found or that the executable was invalid for the OS.
Upvotes: 0
Reputation: 5010
You can do this using the StartProcess
alias example:
FilePath reportpath = File("./GeneratedReports/ReportGeneratorOutput/index.htm");
StartProcess(reportpath);
Upvotes: 0