Skip to content

Instantly share code, notes, and snippets.

@pvandervelde
Created January 10, 2014 04:14
Show Gist options
  • Save pvandervelde/8346900 to your computer and use it in GitHub Desktop.
Save pvandervelde/8346900 to your computer and use it in GitHub Desktop.
A MsBuild inline task that checks if a Sherlock test has passed or failed by parsing the XML test report.
<Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003'
ToolsVersion="4.0">
<UsingTask TaskName="SherlockHasTestPassed"
TaskFactory="CodeTaskFactory"
AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll">
<ParameterGroup>
<ReportFile ParameterType="System.String" Required="true" />
</ParameterGroup>
<Task>
<Reference Include="System.Xml" />
<Reference Include="System.Xml.Linq" />
<Code Type="Method" Language="cs">
<![CDATA[
public override bool Execute()
{
/*
sherlock.report.xml
Sherlock 0.4.5.0:
XML report also dumped in the specific directory
XML report contains section indicating Passed or Failed under: sherlockreport -> header -> result
*/
Log.LogMessage(MessageImportance.Normal, "Parsing " + ReportFile);
var xDoc = System.Xml.Linq.XDocument.Load(ReportFile);
var node = xDoc.Element("sherlockreport").Element("header").Element("result");
var text = node.Value.Trim();
if (string.Equals("Failed", text, System.StringComparison.OrdinalIgnoreCase))
{
Log.LogError("Test failed");
}
else
{
Log.LogMessage(MessageImportance.High, "Test passed");
}
// Log.HasLoggedErrors is true if the task logged any errors -- even if they were logged
// from a task's constructor or property setter. As long as this task is written to always log an error
// when it fails, we can reliably return HasLoggedErrors.
return !Log.HasLoggedErrors;
}
]]>
</Code>
</Task>
</UsingTask>
</Project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment