Skip to content

Commit

Permalink
Revert "Adding ability to support all different code coverage formats…
Browse files Browse the repository at this point in the history
… with V2 (#68)" (#70)

This reverts commit ccf072c.
  • Loading branch information
vinayakmsft authored Dec 15, 2023
1 parent ccf072c commit 1e442b6
Show file tree
Hide file tree
Showing 14 changed files with 44 additions and 238 deletions.
8 changes: 1 addition & 7 deletions azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,9 @@ steps:
- task: NuGetToolInstaller@1

- task: NuGetAuthenticate@1
displayName: 'NuGet Authenticate'


- task: NuGetCommand@2
displayName: 'NuGet restore'
inputs:
feedsToUse: config
nugetConfigPath: nuget.config
restoreSolution: '$(solution)'

- task: UseDotNet@2
displayName: 'Use .NET Core sdk 7.0.x'
Expand Down
5 changes: 0 additions & 5 deletions nuget.config
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,4 @@
<config>
<add key="globalPackagesFolder" value="packages" />
</config>
<packageSources>
<clear />
<add key="Nuget.org" value="https://api.nuget.org/v3/index.json" />
<add key="test-tools" value="https://pkgs.dev.azure.com/dnceng/public/_packaging/test-tools/nuget/v3/index.json" />
</packageSources>
</configuration>
44 changes: 41 additions & 3 deletions src/CoveragePublisher.Tests/CoverageProcessorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,20 +68,58 @@ public void WillNotPublishFileCoverageIfCountIsZero()
}

[TestMethod]
public void WillNotPublishFileCoverageIfCountIsZeroForDotCoverage()
public void WillCatchAndReportExceptions()
{
var logger = new TestLogger();
TraceLogger.Initialize(logger);

var token = new CancellationToken();
var processor = new CoverageProcessor(_mockPublisher.Object, _mockTelemetryDataCollector.Object);
var coverage = new List<FileCoverageInfo>();

_mockPublisher.Setup(x => x.IsFileCoverageJsonSupported()).Returns(true);
_mockParser.Setup(x => x.GetFileCoverageInfos(token)).Returns(coverage);
_mockParser.Setup(x => x.GetFileCoverageInfos()).Throws(new ParsingException("message", new Exception("error")));

processor.ParseAndPublishCoverage(_config, token, _mockParser.Object).Wait();

Assert.IsTrue(logger.Log.Contains("error: message System.Exception: error"));

logger.Log = "";

_mockParser.Setup(x => x.GetFileCoverageInfos()).Throws(new Exception("error"));

processor.ParseAndPublishCoverage(_config, token, _mockParser.Object).Wait();

Assert.IsTrue(logger.Log.Contains("error: An error occured while publishing coverage files. System.Exception: error"));
}

[TestMethod]
public void ParseAndPublishCoverageWillPublishFileAndCodeCoverageSummary()
{
var token = new CancellationToken();
var processor = new CoverageProcessor(_mockPublisher.Object, _mockTelemetryDataCollector.Object);
var coverage = new List<FileCoverageInfo>();
coverage.Add(new FileCoverageInfo());

var summary = new CoverageSummary();

summary.AddCoverageStatistics("", 3, 3, CoverageSummary.Priority.Class);

_mockPublisher.Setup(x => x.IsFileCoverageJsonSupported()).Returns(false);
_mockParser.Setup(x => x.GetCoverageSummary()).Returns(summary);

_mockPublisher.Setup(x => x.IsFileCoverageJsonSupported()).Returns(true);
_mockParser.Setup(x => x.GetFileCoverageInfos()).Returns(coverage);

processor.ParseAndPublishCoverage(_config, token, _mockParser.Object).Wait();

_mockPublisher.Verify(x => x.PublishCoverageSummary(
It.Is<CoverageSummary>(a => a == summary),
It.Is<CancellationToken>(b => b == token)));

_mockPublisher.Verify(x => x.PublishFileCoverage(
It.Is<List<FileCoverageInfo>>(a => a == coverage),
It.Is<CancellationToken>(b => b == token)), Times.Never);
It.Is<CancellationToken>(b => b == token)));
}

[TestMethod]
Expand Down
6 changes: 0 additions & 6 deletions src/CoveragePublisher.Tests/CoveragePublisher.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,6 @@
<None Update="SampleCoverage\JaCoCo.xml">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="SampleCoverage\DotCoverage.coverage">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="SampleCoverage\CoverageXFile.covx">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System;
using System.IO;
using System.Linq;
using System.Threading;
using Microsoft.Azure.Pipelines.CoveragePublisher;
using Microsoft.Azure.Pipelines.CoveragePublisher.Model;
using Microsoft.Azure.Pipelines.CoveragePublisher.Parsers;
Expand Down Expand Up @@ -85,19 +84,6 @@ public void WillReturnEmptyCoverageForNoInputFiles()
".Trim());
}

[TestMethod]
public void WillReturnEmptyCoverageForNoInputFilesAllFormats()
{
var parser = new ReportGeneratorTool(new PublisherConfiguration());
var token = new CancellationTokenSource();
var fileCoverage = parser.GetFileCoverageInfos(token.Token);
var summary = parser.GetCoverageSummary();

Assert.AreEqual(fileCoverage.Count, 0);
Assert.AreEqual(summary.CodeCoverageData.CoverageStats.Count, 0);

}

[TestMethod]
public void WillReturnEmptyCoverageForNonExistingFile()
{
Expand All @@ -115,44 +101,6 @@ public void WillReturnEmptyCoverageForNonExistingFile()
".Trim());
}

[TestMethod]
public void WillReturnCoverageStatsForDotCoverageFile()
{
var parser = new ReportGeneratorTool(new PublisherConfiguration() { CoverageFiles = new string[] { "SampleCoverage/DotCoverage.coverage" } });
var token = new CancellationTokenSource();
var fileCoverage = parser.GetFileCoverageInfos(token.Token);
var summary = parser.GetCoverageSummary();

Assert.AreEqual(fileCoverage.Count, 10);
Assert.AreEqual(summary.CodeCoverageData.CoverageStats[0].Total, 922);
Assert.AreEqual(summary.CodeCoverageData.CoverageStats[0].Covered, 786);

}

[TestMethod]
public void WillReturnCoverageStatsForCoverageXFile()
{
var parser = new ReportGeneratorTool(new PublisherConfiguration() { CoverageFiles = new string[] { "SampleCoverage/CoverageXFile.covx" } });
var token = new CancellationTokenSource();
var fileCoverage = parser.GetFileCoverageInfos(token.Token);
var summary = parser.GetCoverageSummary();

Assert.AreEqual(fileCoverage.Count, 400);
Assert.AreEqual(summary.CodeCoverageData.CoverageStats[0].Total, 23070);
Assert.AreEqual(summary.CodeCoverageData.CoverageStats[0].Covered , 8785);
}

[TestMethod]
public void WillReturnCoverageStatsForNotCoverageXFile()
{
var parser = new ReportGeneratorTool(new PublisherConfiguration() { CoverageFiles = new string[] { "SampleCoverage/sampleCoverage.coverage" } });
var token = new CancellationTokenSource();
var fileCoverage = parser.GetFileCoverageInfos();
var summary = parser.GetCoverageSummary();

Assert.AreEqual(fileCoverage.Count, 0);
}

[TestMethod]
[DataRow(new string[] { "SampleCoverage/Clover.xml" })]
[DataRow(new string[] { "SampleCoverage/Cobertura.xml" })]
Expand Down
Binary file not shown.
Binary file not shown.
8 changes: 1 addition & 7 deletions src/CoveragePublisher/CoverageProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@

using System;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Azure.Pipelines.CoveragePublisher.Model;
using Microsoft.Azure.Pipelines.CoveragePublisher.Parsers;
using Microsoft.Azure.Pipelines.CoveragePublisher.Publishers.DefaultPublisher;
using Microsoft.Azure.Pipelines.CoveragePublisher.Utils;
using Microsoft.VisualStudio.Services.WebApi;

Expand Down Expand Up @@ -47,11 +45,7 @@ public async Task ParseAndPublishCoverage(PublisherConfiguration config, Cancell

if (supportsFileCoverageJson)
{
bool hasDotCoverageFiles = config.CoverageFiles.Any(file => Path.GetExtension(file) == Constants.CoverageFormats.CoverageDotFileFormat);
bool hasCoverageXFiles = config.CoverageFiles.Any(file => Path.GetExtension(file) == Constants.CoverageFormats.CoverageXFileExtension);
bool hasCoverageBFiles = config.CoverageFiles.Any(file => Path.GetExtension(file) == Constants.CoverageFormats.CoverageBFileExtension);

var fileCoverage = (hasDotCoverageFiles || hasCoverageXFiles || hasCoverageBFiles) ? parser.GetFileCoverageInfos(token) : parser.GetFileCoverageInfos();
var fileCoverage = parser.GetFileCoverageInfos();

var summary = parser.GetCoverageSummary();

Expand Down
2 changes: 0 additions & 2 deletions src/CoveragePublisher/CoveragePublisher.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
<PackageReference Include="System.Net.NameResolution" Version="4.3.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="System.Private.Uri" Version="4.3.2" />
<PackageReference Include="Microsoft.CodeCoverage.Core" version="17.9.4-beta.23608.1" />
<PackageReference Include="Serilog" />
</ItemGroup>

<ItemGroup>
Expand Down
9 changes: 1 addition & 8 deletions src/CoveragePublisher/Model/ICoverageParserTool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.Collections.Generic;
using System.Threading;

namespace Microsoft.Azure.Pipelines.CoveragePublisher.Model
{
Expand All @@ -16,13 +15,7 @@ public interface ICoverageParserTool
/// </summary>
/// <returns>List of <see cref="FileCoverageInfo"/></returns>
List<FileCoverageInfo> GetFileCoverageInfos();

/// <summary>
/// Get coverage information for individual files. Specifically used for .coverage/.covx scenarios
/// </summary>
/// <returns>List of <see cref="FileCoverageInfo"/></returns>
List<FileCoverageInfo> GetFileCoverageInfos(CancellationToken token);


/// <summary>
/// Get coverage summary, contains combined coverage summary data.
/// </summary>
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

using Microsoft.Azure.Pipelines.CoveragePublisher.Model;
using Microsoft.TeamFoundation.TestManagement.WebApi;
using Microsoft.CodeCoverage.Core;
using Palmmedia.ReportGenerator.Core;
using Palmmedia.ReportGenerator.Core.CodeAnalysis;
using Palmmedia.ReportGenerator.Core.Parser;
Expand All @@ -13,16 +12,6 @@
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Serilog;
using Microsoft.Azure.Pipelines.CoveragePublisher.Publishers.DefaultPublisher;
using Microsoft.Azure.Pipelines.CoveragePublisher.Parsers.CoverageParserTools;
using CoverageStatus = Microsoft.TeamFoundation.TestManagement.WebApi.CoverageStatus;
using Microsoft.CodeCoverage.IO.Coverage;
using Microsoft.CodeCoverage.IO;

namespace Microsoft.Azure.Pipelines.CoveragePublisher.Parsers
{
Expand Down Expand Up @@ -81,81 +70,6 @@ public List<FileCoverageInfo> GetFileCoverageInfos()
return fileCoverages;
}

public List<FileCoverageInfo> GetFileCoverageInfos(CancellationToken token)
{
TraceLogger.Debug("ReportGeneratorTool.GetFileCoverageInfos: Generating file coverage info from coverage files.");

List<FileCoverageInfo> fileCoverages = new List<FileCoverageInfo>();

if (Configuration.CoverageFiles == null)
{
return fileCoverages;
}

var transformedXml = TransformCoverageFilesToXml(Configuration.CoverageFiles, token);

_parserResult = ParseCoverageFiles(transformedXml.Result);

foreach (var assembly in _parserResult.Assemblies)
{
foreach (var @class in assembly.Classes)
{
foreach (var file in @class.Files)
{
FileCoverageInfo resultFileCoverageInfo = new FileCoverageInfo { FilePath = file.Path, LineCoverageStatus = new Dictionary<uint, CoverageStatus>() };
int lineNumber = 0;

foreach (var line in file.LineCoverage)
{
if (line != -1 && lineNumber != 0)
{
resultFileCoverageInfo.LineCoverageStatus.Add((uint)lineNumber, line == 0 ? CoverageStatus.NotCovered : CoverageStatus.Covered);
}
++lineNumber;
}

fileCoverages.Add(resultFileCoverageInfo);
}
}
}

return fileCoverages;
}


private async Task<List<string>> TransformCoverageFilesToXml(IList<string> inputCoverageFiles, CancellationToken cancellationToken)
{

PublisherCoverageFileConfiguration GenericCoverageFileConfiguration = new PublisherCoverageFileConfiguration(ReadModules: true,
ReadSkippedModules: true,
ReadSkippedFunctions: true,
ReadSnapshotsData:true,
FixCoverageBuffersMismatch: true,
GenerateCoverageBufferFiles: true);


var utility = new CoverageFileUtilityV2(GenericCoverageFileConfiguration);

var transformedXmls = new List<string>();
foreach (var coverageFile in inputCoverageFiles)
{
if ((coverageFile.EndsWith(Constants.CoverageFormats.CoverageDotFileFormat) ||
coverageFile.EndsWith(Constants.CoverageFormats.CoverageXFileExtension) ||
coverageFile.EndsWith(Constants.CoverageFormats.CoverageBFileExtension)
))
{
string transformedXml = Path.ChangeExtension(coverageFile, ".xml");
await utility.ToXmlFileAsync(
path: coverageFile,
outputPath: transformedXml,
cancellationToken: cancellationToken);

transformedXmls.Add(transformedXml);
}
}
return transformedXmls;
}

public CoverageSummary GetCoverageSummary()
{
TraceLogger.Debug("ReportGeneratorTool.GetCoverageSummary: Generating coverage summary for the coverage files.");
Expand Down
Loading

0 comments on commit 1e442b6

Please sign in to comment.