-
Notifications
You must be signed in to change notification settings - Fork 238
Expand file tree
/
Copy pathValidateStrykerResults.cs
More file actions
315 lines (250 loc) · 13.7 KB
/
Copy pathValidateStrykerResults.cs
File metadata and controls
315 lines (250 loc) · 13.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
using System;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.Text;
using Shouldly;
using Stryker.Abstractions;
using Stryker.Abstractions.Reporting;
using Stryker.Core.Reporters.Json;
using Xunit;
namespace Validation;
public class ValidateStrykerResults
{
private readonly ReadOnlyCollection<SyntaxKind> _blacklistedSyntaxKindsForMutating =
new([
// Usings
SyntaxKind.UsingDirective,
SyntaxKind.UsingKeyword,
SyntaxKind.UsingStatement,
// Comments
SyntaxKind.DocumentationCommentExteriorTrivia,
SyntaxKind.EndOfDocumentationCommentToken,
SyntaxKind.MultiLineCommentTrivia,
SyntaxKind.MultiLineDocumentationCommentTrivia,
SyntaxKind.SingleLineCommentTrivia,
SyntaxKind.SingleLineDocumentationCommentTrivia,
SyntaxKind.XmlComment,
SyntaxKind.XmlCommentEndToken,
SyntaxKind.XmlCommentStartToken,
]
);
private readonly ReadOnlyCollection<SyntaxKind> _parentSyntaxKindsForMutating =
new([
SyntaxKind.MethodDeclaration,
SyntaxKind.PropertyDeclaration,
SyntaxKind.ConstructorDeclaration,
SyntaxKind.FieldDeclaration,
SyntaxKind.OperatorDeclaration,
SyntaxKind.IndexerDeclaration,
SyntaxKind.GlobalStatement,
]
);
private const string MutationReportJson = "mutation-report.json";
[Fact]
[Trait("Category", "SingleTestProject")]
[Trait("Runtime", "netframework")]
public async Task CSharp_NetFramework_SingleTestProject()
{
var directory = new DirectoryInfo("../../../../../TargetProjects/NetFramework/FullFrameworkApp.Test/StrykerOutput");
directory.GetFiles("*.json", SearchOption.AllDirectories).ShouldNotBeEmpty("No reports available to assert");
var latestReport = directory.GetFiles(MutationReportJson, SearchOption.AllDirectories)
.OrderByDescending(f => f.LastWriteTime)
.First();
using var strykerRunOutput = File.OpenRead(latestReport.FullName);
var report = await strykerRunOutput.DeserializeJsonReportAsync();
CheckReportMutants(report, total: 29, ignored: 7, survived: 3, killed: 7, timeout: 0, nocoverage: 11);
}
[Fact]
[Trait("Category", "SingleTestProject")]
[Trait("Runtime", "netcore")]
public async Task CSharp_NetCore_SingleTestProject()
{
var directory = new DirectoryInfo("../../../../../TargetProjects/NetCore/NetCoreTestProject.XUnit/StrykerOutput");
directory.GetFiles("*.json", SearchOption.AllDirectories).ShouldNotBeEmpty("No reports available to assert");
var latestReport = directory.GetFiles(MutationReportJson, SearchOption.AllDirectories)
.OrderByDescending(f => f.LastWriteTime)
.First();
using var strykerRunOutput = File.OpenRead(latestReport.FullName);
var report = await strykerRunOutput.DeserializeJsonReportAsync();
CheckReportMutants(report, total: 660, ignored: 271, survived: 4, killed: 9, timeout: 2, nocoverage: 340);
CheckReportTestCounts(report, total: 11);
}
[Fact]
[Trait("Category", "MultipleTestProjects")]
[Trait("Runtime", "netcore")]
public async Task CSharp_NetCore_WithTwoTestProjects()
{
var directory = new DirectoryInfo("../../../../../TargetProjects/NetCore/TargetProject/StrykerOutput");
directory.GetFiles("*.json", SearchOption.AllDirectories).ShouldNotBeEmpty("No reports available to assert");
var latestReport = directory.GetFiles(MutationReportJson, SearchOption.AllDirectories)
.OrderByDescending(f => f.LastWriteTime)
.First();
using var strykerRunOutput = File.OpenRead(latestReport.FullName);
var report = await strykerRunOutput.DeserializeJsonReportAsync();
CheckReportMutants(report, total: 660, ignored: 117, survived: 5, killed: 11, timeout: 2, nocoverage: 491);
CheckReportTestCounts(report, total: 21);
}
[Fact]
[Trait("Category", "MSTestMTP")]
[Trait("Runtime", "netcore")]
public async Task CSharp_NetCore_MSTestMTP()
{
var directory = new DirectoryInfo("../../../../../TargetProjects/MicrosoftTestPlatform/UnitTests.MSTest/StrykerOutput");
directory.GetFiles("*.json", SearchOption.AllDirectories).ShouldNotBeEmpty("No reports available to assert");
var latestReport = directory.GetFiles(MutationReportJson, SearchOption.AllDirectories)
.OrderByDescending(f => f.LastWriteTime)
.First();
using var strykerRunOutput = File.OpenRead(latestReport.FullName);
var report = await strykerRunOutput.DeserializeJsonReportAsync();
CheckReportMutants(report, total: 660, ignored: 271, survived: 2, killed: 1, timeout: 2, nocoverage: 350);
}
[Fact]
[Trait("Category", "XUnitMTP")]
[Trait("Runtime", "netcore")]
public async Task CSharp_NetCore_XUnitMTP()
{
var directory = new DirectoryInfo("../../../../../TargetProjects/MicrosoftTestPlatform/UnitTests.XUnit/StrykerOutput");
directory.GetFiles("*.json", SearchOption.AllDirectories).ShouldNotBeEmpty("No reports available to assert");
var latestReport = directory.GetFiles(MutationReportJson, SearchOption.AllDirectories)
.OrderByDescending(f => f.LastWriteTime)
.First();
using var strykerRunOutput = File.OpenRead(latestReport.FullName);
var report = await strykerRunOutput.DeserializeJsonReportAsync();
CheckReportMutants(report, total: 660, ignored: 271, survived: 1, killed: 1, timeout: 0, nocoverage: 353);
}
[Fact]
[Trait("Category", "NUnitMTP")]
[Trait("Runtime", "netcore")]
public async Task CSharp_NetCore_NUnitMTP()
{
var directory = new DirectoryInfo("../../../../../TargetProjects/MicrosoftTestPlatform/UnitTests.NUnit/StrykerOutput");
directory.GetFiles("*.json", SearchOption.AllDirectories).ShouldNotBeEmpty("No reports available to assert");
var latestReport = directory.GetFiles(MutationReportJson, SearchOption.AllDirectories)
.OrderByDescending(f => f.LastWriteTime)
.First();
using var strykerRunOutput = File.OpenRead(latestReport.FullName);
var report = await strykerRunOutput.DeserializeJsonReportAsync();
CheckReportMutants(report, total: 660, ignored: 271, survived: 1, killed: 1, timeout: 0, nocoverage: 353);
}
[Fact]
[Trait("Category", "TUnit")]
[Trait("Runtime", "netcore")]
public async Task CSharp_NetCore_TUnit()
{
var directory = new DirectoryInfo("../../../../../TargetProjects/MicrosoftTestPlatform/UnitTests.TUnit/StrykerOutput");
directory.GetFiles("*.json", SearchOption.AllDirectories).ShouldNotBeEmpty("No reports available to assert");
var latestReport = directory.GetFiles(MutationReportJson, SearchOption.AllDirectories)
.OrderByDescending(f => f.LastWriteTime)
.First();
using var strykerRunOutput = File.OpenRead(latestReport.FullName);
var report = await strykerRunOutput.DeserializeJsonReportAsync();
CheckReportMutants(report, total: 660, ignored: 271, survived: 1, killed: 1, timeout: 0, nocoverage: 353);
}
[Fact]
[Trait("Category", "MTPSolution")]
[Trait("Runtime", "netcore")]
public async Task CSharp_NetCore_MTPSolution()
{
var directory = new DirectoryInfo("../../../../../TargetProjects/StrykerOutput");
directory.GetFiles("*.json", SearchOption.AllDirectories).ShouldNotBeEmpty("No reports available to assert");
var latestReport = directory.GetFiles(MutationReportJson, SearchOption.AllDirectories)
.OrderByDescending(f => f.LastWriteTime)
.First();
using var strykerRunOutput = File.OpenRead(latestReport.FullName);
var report = await strykerRunOutput.DeserializeJsonReportAsync();
CheckReportMutants(report, total: 670, ignored: 274, survived: 1, killed: 1, timeout: 0, nocoverage: 360);
CheckReportTestCounts(report, total: 0); // MTP doesn't report tests yet
}
[Fact]
[Trait("Category", value: "WebApiWithOpenApi")]
[Trait("Runtime", "netcore")]
public async Task CSharp_NetCore_WebApiWithOpenApi()
{
var directory = new DirectoryInfo("../../../../../TargetProjects/NetCore/WebApiWithOpenApi/StrykerOutput");
directory.GetFiles("*.json", SearchOption.AllDirectories).ShouldNotBeEmpty("No reports available to assert");
var latestReport = directory.GetFiles(MutationReportJson, SearchOption.AllDirectories)
.OrderByDescending(f => f.LastWriteTime)
.First();
using var strykerRunOutput = File.OpenRead(latestReport.FullName);
var report = await strykerRunOutput.DeserializeJsonReportAsync();
CheckReportMutants(report, total: 11, ignored: 2, survived: 5, killed: 4, timeout: 0, nocoverage: 0);
CheckReportTestCounts(report, total: 3);
}
[Fact]
[Trait("Category", "Solution")]
[Trait("Runtime", "netcore")]
public async Task CSharp_NetCore_SolutionRun()
{
var directory = new DirectoryInfo("../../../../../TargetProjects/NetCore/StrykerOutput");
directory.GetFiles("*.json", SearchOption.AllDirectories).ShouldNotBeEmpty("No reports available to assert");
var latestReport = directory.GetFiles(MutationReportJson, SearchOption.AllDirectories)
.OrderByDescending(f => f.LastWriteTime)
.First();
using var strykerRunOutput = File.OpenRead(latestReport.FullName);
var report = await strykerRunOutput.DeserializeJsonReportAsync();
CheckReportMutants(report, total: 660, ignored: 271, survived: 4, killed: 9, timeout: 2, nocoverage: 340);
CheckReportTestCounts(report, total: 23);
}
[Fact]
[Trait("Category", "Solution")]
[Trait("Runtime", "netframework")]
public async Task CSharp_NetFramework_SolutionRun()
{
var directory = new DirectoryInfo("../../../../../TargetProjects/NetFramework/FullFrameworkApp.Test/StrykerOutput");
directory.GetFiles("*.json", SearchOption.AllDirectories).ShouldNotBeEmpty("No reports available to assert");
var latestReport = directory.GetFiles(MutationReportJson, SearchOption.AllDirectories)
.OrderByDescending(f => f.LastWriteTime)
.First();
using var strykerRunOutput = File.OpenRead(latestReport.FullName);
var report = await strykerRunOutput.DeserializeJsonReportAsync();
CheckReportMutants(report, total: 29, ignored: 7, survived: 3, killed: 7, timeout: 0, nocoverage: 11);
}
private void CheckMutationKindsValidity(IJsonReport report)
{
foreach (var file in report.Files)
{
var syntaxTreeRootNode = CSharpSyntaxTree.ParseText(file.Value.Source).GetRoot();
var textLines = SourceText.From(file.Value.Source).Lines;
foreach (var mutation in file.Value.Mutants)
{
var linePositionSpan = new LinePositionSpan(new LinePosition(mutation.Location.Start.Line - 1, mutation.Location.Start.Column), new LinePosition(mutation.Location.End.Line - 1, mutation.Location.End.Column));
var textSpan = textLines.GetTextSpan(linePositionSpan);
var node = syntaxTreeRootNode.FindNode(textSpan);
var nodeKind = node.Kind();
_blacklistedSyntaxKindsForMutating.ShouldNotContain(nodeKind);
node
.AncestorsAndSelf()
.ShouldContain(pn =>
_parentSyntaxKindsForMutating.Contains(pn.Kind()),
$"Mutation {mutation.MutatorName} on line {mutation.Location.Start.Line} in file {file.Key} does not have one of the known parent syntax kinds as it's parent.{Environment.NewLine}" +
$"Instead it has: {Environment.NewLine} {string.Join($",{Environment.NewLine}", node.AncestorsAndSelf().Select(n => n.Kind()))}");
}
}
}
private void CheckReportMutants(IJsonReport report, int total, int ignored, int survived, int killed, int timeout, int nocoverage)
{
var actualTotal = report.Files.Select(f => f.Value.Mutants.Count()).Sum();
var actualIgnored = report.Files.Select(f => f.Value.Mutants.Count(m => m.Status == MutantStatus.Ignored.ToString())).Sum();
var actualSurvived = report.Files.Select(f => f.Value.Mutants.Count(m => m.Status == MutantStatus.Survived.ToString())).Sum();
var actualKilled = report.Files.Select(f => f.Value.Mutants.Count(m => m.Status == MutantStatus.Killed.ToString())).Sum();
var actualTimeout = report.Files.Select(f => f.Value.Mutants.Count(m => m.Status == MutantStatus.Timeout.ToString())).Sum();
var actualNoCoverage = report.Files.Select(f => f.Value.Mutants.Count(m => m.Status == MutantStatus.NoCoverage.ToString())).Sum();
report.Files.ShouldSatisfyAllConditions(
() => actualTotal.ShouldBe(total),
() => actualIgnored.ShouldBe(ignored),
() => actualSurvived.ShouldBe(survived),
() => actualKilled.ShouldBe(killed),
() => actualTimeout.ShouldBe(timeout),
() => actualNoCoverage.ShouldBe(nocoverage)
);
CheckMutationKindsValidity(report);
}
private void CheckReportTestCounts(IJsonReport report, int total)
{
var actualTestCount = report.TestFiles.Sum(tf => tf.Value.Tests.Count);
actualTestCount.ShouldBe(total);
}
}