Skip to content

Commit 0eb8ce4

Browse files
committed
Added teamcity reporter
1 parent dab248f commit 0eb8ce4

File tree

10 files changed

+81
-16
lines changed

10 files changed

+81
-16
lines changed

src/coverlet.console/Program.cs

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,23 @@ static int Main(string[] args)
7474
if (reporter == null)
7575
throw new Exception($"Specified output format '{format}' is not supported");
7676

77-
var filename = Path.GetFileName(dOutput);
78-
filename = (filename == string.Empty) ? $"coverage.{reporter.Extension}" : filename;
79-
filename = Path.HasExtension(filename) ? filename : $"{filename}.{reporter.Extension}";
80-
81-
var report = Path.Combine(directory, filename);
82-
logger.LogInformation($" Generating report '{report}'");
83-
File.WriteAllText(report, reporter.Report(result));
77+
if (reporter.UseConsoleOutput)
78+
{
79+
// Output to console
80+
logger.LogInformation(" Outputting results to console");
81+
logger.LogInformation(reporter.Report(result));
82+
}
83+
else
84+
{
85+
// Output to file
86+
var filename = Path.GetFileName(dOutput);
87+
filename = (filename == string.Empty) ? $"coverage.{reporter.Extension}" : filename;
88+
filename = Path.HasExtension(filename) ? filename : $"{filename}.{reporter.Extension}";
89+
90+
var report = Path.Combine(directory, filename);
91+
logger.LogInformation($" Generating report '{report}'");
92+
File.WriteAllText(report, reporter.Report(result));
93+
}
8494
}
8595

8696
var summary = new CoverageSummary();

src/coverlet.core/Reporters/CoberturaReporter.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ namespace Coverlet.Core.Reporters
1010
{
1111
public class CoberturaReporter : IReporter
1212
{
13+
public bool UseConsoleOutput => false;
14+
1315
public string Format => "cobertura";
1416

1517
public string Extension => "cobertura.xml";

src/coverlet.core/Reporters/IReporter.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ namespace Coverlet.Core.Reporters
22
{
33
public interface IReporter
44
{
5+
bool UseConsoleOutput { get; }
56
string Format { get; }
67
string Extension { get; }
78
string Report(CoverageResult result);

src/coverlet.core/Reporters/JsonReporter.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ namespace Coverlet.Core.Reporters
44
{
55
public class JsonReporter : IReporter
66
{
7+
public bool UseConsoleOutput => false;
8+
79
public string Format => "json";
810

911
public string Extension => "json";

src/coverlet.core/Reporters/LcovReporter.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ namespace Coverlet.Core.Reporters
66
{
77
public class LcovReporter : IReporter
88
{
9+
public bool UseConsoleOutput => false;
10+
911
public string Format => "lcov";
1012

1113
public string Extension => "info";

src/coverlet.core/Reporters/OpenCoverReporter.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ namespace Coverlet.Core.Reporters
99
{
1010
public class OpenCoverReporter : IReporter
1111
{
12+
public bool UseConsoleOutput => false;
13+
1214
public string Format => "opencover";
1315

1416
public string Extension => "opencover.xml";

src/coverlet.core/Reporters/ReporterFactory.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Linq;
33
using System.Collections.Generic;
4+
using coverlet.core.Reporters;
45

56
namespace Coverlet.Core.Reporters
67
{
@@ -14,7 +15,8 @@ public ReporterFactory(string format)
1415
_format = format;
1516
_reporters = new IReporter[] {
1617
new JsonReporter(), new LcovReporter(),
17-
new OpenCoverReporter(), new CoberturaReporter()
18+
new OpenCoverReporter(), new CoberturaReporter(),
19+
new TeamCityReporter()
1820
};
1921
}
2022

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using Coverlet.Core;
2+
using Coverlet.Core.Reporters;
3+
using System.Text;
4+
5+
namespace coverlet.core.Reporters
6+
{
7+
public class TeamCityReporter : IReporter
8+
{
9+
public bool UseConsoleOutput => true;
10+
11+
public string Format => "teamcity";
12+
13+
public string Extension => null;
14+
15+
public string Report(CoverageResult result)
16+
{
17+
// Calculate coverage
18+
var summary = new CoverageSummary();
19+
var overallLineCoverage = summary.CalculateLineCoverage(result.Modules);
20+
var overallBranchCoverage = summary.CalculateBranchCoverage(result.Modules);
21+
var overallMethodCoverage = summary.CalculateMethodCoverage(result.Modules);
22+
23+
// Report coverage
24+
var stringBuilder = new StringBuilder();
25+
var teamCityServiceMessageWriter = new TeamCityServiceMessageWriter(s => stringBuilder.AppendLine(s));
26+
teamCityServiceMessageWriter.OutputLineCoverage(overallLineCoverage);
27+
teamCityServiceMessageWriter.OutputBranchCoverage(overallBranchCoverage);
28+
teamCityServiceMessageWriter.OutputMethodCoverage(overallMethodCoverage);
29+
30+
// Return a placeholder
31+
return stringBuilder.ToString();
32+
}
33+
}
34+
}

src/coverlet.core/TeamCityServiceMessageWriter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public void OutputMethodCoverage(CoverageDetails coverageDetails)
6262

6363
private void OutputTeamCityServiceMessage(string key, object value)
6464
{
65-
_writer($"##teamcity[buildStatisticValue key='{key}' value='{value}']");
65+
_writer?.Invoke($"##teamcity[buildStatisticValue key='{key}' value='{value}']");
6666
}
6767
}
6868
}

src/coverlet.msbuild.tasks/CoverageResultTask.cs

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,23 @@ public override bool Execute()
7575
if (reporter == null)
7676
throw new Exception($"Specified output format '{format}' is not supported");
7777

78-
var filename = Path.GetFileName(_output);
79-
filename = (filename == string.Empty) ? $"coverage.{reporter.Extension}" : filename;
80-
filename = Path.HasExtension(filename) ? filename : $"{filename}.{reporter.Extension}";
81-
82-
var report = Path.Combine(directory, filename);
83-
Console.WriteLine($" Generating report '{report}'");
84-
File.WriteAllText(report, reporter.Report(result));
78+
if (reporter.UseConsoleOutput)
79+
{
80+
// Output to console
81+
Console.WriteLine(" Outputting results to console");
82+
Console.WriteLine(reporter.Report(result));
83+
}
84+
else
85+
{
86+
// Output to file
87+
var filename = Path.GetFileName(_output);
88+
filename = (filename == string.Empty) ? $"coverage.{reporter.Extension}" : filename;
89+
filename = Path.HasExtension(filename) ? filename : $"{filename}.{reporter.Extension}";
90+
91+
var report = Path.Combine(directory, filename);
92+
Console.WriteLine($" Generating report '{report}'");
93+
File.WriteAllText(report, reporter.Report(result));
94+
}
8595
}
8696

8797
var thresholdFailed = false;

0 commit comments

Comments
 (0)