private void AnalyzeDupFinderReport(ITaskContext context, string dupFinderXmlReportFileName) { const string PropertyCodebaseCost = "CodebaseCost"; const string PropertyTotalDuplicatesCost = "TotalDuplicatesCost"; const string PropertyTotalFragmentsCost = "TotalFragmentsCost"; const string PropertyDuplicatesCount = "DuplicatesCount"; string xPathOverThresholdDuplicatesCount = string.Format( CultureInfo.InvariantCulture, "count(/DuplicatesReport/Duplicates/Duplicate[@Cost>{0}])", maxAllowedDuplicateCost); EvaluateXmlTask findViolationsTask = new EvaluateXmlTask(dupFinderXmlReportFileName) .AddExpression(PropertyCodebaseCost, "sum(/DuplicatesReport/Statistics/CodebaseCost)") .AddExpression(PropertyTotalDuplicatesCost, "sum(/DuplicatesReport/Statistics/TotalDuplicatesCost)") .AddExpression(PropertyTotalFragmentsCost, "sum(/DuplicatesReport/Statistics/TotalFragmentsCost)") .AddExpression(PropertyDuplicatesCount, xPathOverThresholdDuplicatesCount); findViolationsTask.Execute(context); bool shouldFailBuild = false; int?duplicatesCount = GetDupFinderProperyValue(context, PropertyDuplicatesCount); if (duplicatesCount.HasValue && duplicatesCount > 0) { context.WriteMessage( TaskMessageLevel.Warn, "There are {0} code duplicates that are above the {1} cost threshold", duplicatesCount, maxAllowedDuplicateCost); shouldFailBuild = true; } int?codebaseCost = GetDupFinderProperyValue(context, PropertyCodebaseCost); int?totalFragmentsCost = GetDupFinderProperyValue(context, PropertyTotalFragmentsCost); if (codebaseCost.HasValue && totalFragmentsCost.HasValue && codebaseCost > 0) { double fragmentsCostRatio = ((double)totalFragmentsCost.Value) / codebaseCost.Value; if (fragmentsCostRatio >= maxAllowedFragmentsCostRatio) { context.WriteMessage( TaskMessageLevel.Warn, "The fragments cost ratio ({0:P}) is above the max. allowed threshold ({1:P})", fragmentsCostRatio, maxAllowedFragmentsCostRatio); shouldFailBuild = true; } else { context.WriteInfo("Fragments cost ratio is: {0:P}", fragmentsCostRatio); } } if (shouldFailBuild) { context.Fail("Failing the build because of code duplication"); } }
private void AnalyzeCoverageResults(ITaskContext context) { const string PropertyTotalCoverage = "TotalTestCoverage"; const string PropertyClassesWithPoorCoverageCount = "PoorCoverageCount"; string totalCoverageExpression = "sum(/Root/Assembly[1]/@CoveragePercent)"; string classesWithPoorCoverageExpression = string.Format( CultureInfo.InvariantCulture, "count(/Root/Assembly/Namespace/Type[@CoveragePercent<{0}])", MinRequiredCoverage); EvaluateXmlTask countViolationsTask = new EvaluateXmlTask(coverageXmlReportFileName) .AddExpression( PropertyClassesWithPoorCoverageCount, classesWithPoorCoverageExpression) .AddExpression( PropertyTotalCoverage, totalCoverageExpression); countViolationsTask.Execute(context); int?totalCoverage = GetCoverageProperyValue( context, PropertyTotalCoverage); context.WriteInfo("Total test coverage is {0}%", totalCoverage); int?duplicatesCount = GetCoverageProperyValue( context, PropertyClassesWithPoorCoverageCount); if (duplicatesCount.HasValue && duplicatesCount > 0) { FailBuildAndPrintOutCoverageReport(context, duplicatesCount); } }