Skip to content

Commit fbd01ad

Browse files
committed
Merging TeamCityServiceMessageWriter with TeamCityReporter
1 parent d66eaab commit fbd01ad

File tree

5 files changed

+168
-191
lines changed

5 files changed

+168
-191
lines changed

src/coverlet.core/Reporters/TeamCityReporter.cs

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,53 @@ public string Report(CoverageResult result)
2222

2323
// Report coverage
2424
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);
25+
OutputLineCoverage(overallLineCoverage, stringBuilder);
26+
OutputBranchCoverage(overallBranchCoverage, stringBuilder);
27+
OutputMethodCoverage(overallMethodCoverage, stringBuilder);
2928

3029
// Return a placeholder
3130
return stringBuilder.ToString();
3231
}
32+
33+
private void OutputLineCoverage(CoverageDetails coverageDetails, StringBuilder builder)
34+
{
35+
// The total number of lines
36+
OutputTeamCityServiceMessage("CodeCoverageL", coverageDetails.Total, builder);
37+
38+
// The number of covered lines
39+
OutputTeamCityServiceMessage("CodeCoverageAbsLCovered", coverageDetails.Covered, builder);
40+
41+
// Line-level code coverage
42+
OutputTeamCityServiceMessage("CodeCoverageAbsLTotal", coverageDetails.Percent * 100, builder);
43+
}
44+
45+
private void OutputBranchCoverage(CoverageDetails coverageDetails, StringBuilder builder)
46+
{
47+
// The total number of branches
48+
OutputTeamCityServiceMessage("CodeCoverageR", coverageDetails.Total, builder);
49+
50+
// The number of covered branches
51+
OutputTeamCityServiceMessage("CodeCoverageAbsRCovered", coverageDetails.Covered, builder);
52+
53+
// Branch-level code coverage
54+
OutputTeamCityServiceMessage("CodeCoverageAbsRTotal", coverageDetails.Percent * 100, builder);
55+
}
56+
57+
private void OutputMethodCoverage(CoverageDetails coverageDetails, StringBuilder builder)
58+
{
59+
// The total number of methods
60+
OutputTeamCityServiceMessage("CodeCoverageM", coverageDetails.Total, builder);
61+
62+
// The number of covered methods
63+
OutputTeamCityServiceMessage("CodeCoverageAbsMCovered", coverageDetails.Covered, builder);
64+
65+
// Method-level code coverage
66+
OutputTeamCityServiceMessage("CodeCoverageAbsMTotal", coverageDetails.Percent * 100, builder);
67+
}
68+
69+
private void OutputTeamCityServiceMessage(string key, object value, StringBuilder builder)
70+
{
71+
builder.AppendLine($"##teamcity[buildStatisticValue key='{key}' value='{value}']");
72+
}
3373
}
3474
}

src/coverlet.core/TeamCityServiceMessageWriter.cs

Lines changed: 0 additions & 68 deletions
This file was deleted.

test/coverlet.core.tests/Reporters/ReporterFactoryTests.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using coverlet.core.Reporters;
22
using Xunit;
33

44
namespace Coverlet.Core.Reporters.Tests
@@ -12,6 +12,7 @@ public void TestCreateReporter()
1212
Assert.Equal(typeof(LcovReporter), new ReporterFactory("lcov").CreateReporter().GetType());
1313
Assert.Equal(typeof(OpenCoverReporter), new ReporterFactory("opencover").CreateReporter().GetType());
1414
Assert.Equal(typeof(CoberturaReporter), new ReporterFactory("cobertura").CreateReporter().GetType());
15+
Assert.Equal(typeof(TeamCityReporter), new ReporterFactory("teamcity").CreateReporter().GetType());
1516
Assert.Null(new ReporterFactory("").CreateReporter());
1617
}
1718
}
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
using coverlet.core.Reporters;
2+
using System;
3+
using Xunit;
4+
5+
namespace Coverlet.Core.Reporters.Tests
6+
{
7+
public class TestCreateReporterTests
8+
{
9+
private readonly CoverageResult _result;
10+
private readonly TeamCityReporter _reporter;
11+
12+
public TestCreateReporterTests()
13+
{
14+
_reporter = new TeamCityReporter();
15+
_result = new CoverageResult();
16+
_result.Identifier = Guid.NewGuid().ToString();
17+
18+
var lines = new Lines { { 1, 1 }, { 2, 0 } };
19+
20+
var branches = new Branches
21+
{
22+
new BranchInfo
23+
{
24+
Line = 1,
25+
Hits = 1,
26+
Offset = 23,
27+
EndOffset = 24,
28+
Path = 0,
29+
Ordinal = 1
30+
},
31+
new BranchInfo
32+
{
33+
Line = 1,
34+
Hits = 0,
35+
Offset = 23,
36+
EndOffset = 27,
37+
Path = 1,
38+
Ordinal = 2
39+
}
40+
};
41+
42+
var methods = new Methods();
43+
var methodString = "System.Void Coverlet.Core.Reporters.Tests.CoberturaReporterTests::TestReport()";
44+
methods.Add(methodString, new Method());
45+
methods[methodString].Lines = lines;
46+
methods[methodString].Branches = branches;
47+
48+
var classes = new Classes { { "Coverlet.Core.Reporters.Tests.CoberturaReporterTests", methods } };
49+
50+
var documents = new Documents { { "doc.cs", classes } };
51+
52+
_result.Modules = new Modules { { "module", documents } };
53+
}
54+
55+
[Fact]
56+
public void OutputType_IsConsoleOutputType()
57+
{
58+
// Assert
59+
Assert.Equal(ReporterOutputType.Console, _reporter.OutputType);
60+
}
61+
62+
[Fact]
63+
public void Format_IsExpectedValue()
64+
{
65+
// Assert
66+
Assert.Equal("teamcity", _reporter.Format);
67+
}
68+
69+
[Fact]
70+
public void Format_IsNull()
71+
{
72+
// Assert
73+
Assert.Null(_reporter.Extension);
74+
}
75+
76+
[Fact]
77+
public void Report_ReturnsNonNullString()
78+
{
79+
// Act
80+
var output = _reporter.Report(_result);
81+
82+
// Assert
83+
Assert.False(string.IsNullOrWhiteSpace(output), "Output is not null or whitespace");
84+
}
85+
86+
[Fact]
87+
public void Report_ReportsLineCoverage()
88+
{
89+
// Act
90+
var output = _reporter.Report(_result);
91+
92+
// Assert
93+
Assert.Contains("##teamcity[buildStatisticValue key='CodeCoverageL' value='2']", output);
94+
Assert.Contains("##teamcity[buildStatisticValue key='CodeCoverageAbsLCovered' value='1']", output);
95+
Assert.Contains("##teamcity[buildStatisticValue key='CodeCoverageAbsLTotal' value='50']", output);
96+
}
97+
98+
[Fact]
99+
public void Report_ReportsBranchCoverage()
100+
{
101+
// Act
102+
var output = _reporter.Report(_result);
103+
104+
// Assert
105+
Assert.Contains("##teamcity[buildStatisticValue key='CodeCoverageR' value='2']", output);
106+
Assert.Contains("##teamcity[buildStatisticValue key='CodeCoverageAbsRCovered' value='1']", output);
107+
Assert.Contains("##teamcity[buildStatisticValue key='CodeCoverageAbsRTotal' value='50']", output);
108+
}
109+
110+
[Fact]
111+
public void Report_ReportsMethodCoverage()
112+
{
113+
// Act
114+
var output = _reporter.Report(_result);
115+
116+
// Assert
117+
Assert.Contains("##teamcity[buildStatisticValue key='CodeCoverageM' value='1']", output);
118+
Assert.Contains("##teamcity[buildStatisticValue key='CodeCoverageAbsMCovered' value='1']", output);
119+
Assert.Contains("##teamcity[buildStatisticValue key='CodeCoverageAbsMTotal' value='100']", output);
120+
}
121+
}
122+
}

test/coverlet.core.tests/TeamCityServiceMessageWriterTests.cs

Lines changed: 0 additions & 118 deletions
This file was deleted.

0 commit comments

Comments
 (0)