forked from stryker-mutator/stryker-net
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathValidateStrykerResults.cs
180 lines (150 loc) · 7.93 KB
/
ValidateStrykerResults.cs
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
using System;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
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")]
public async Task CSharp_NetFramework_SingleTestProject()
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
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")]
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: 649, ignored: 266, survived: 4, killed: 9, timeout: 2, nocoverage: 331);
CheckReportTestCounts(report, total: 11);
}
[Fact]
[Trait("Category", "MultipleTestProjects")]
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: 649, ignored: 113, survived: 5, killed: 11, timeout: 2, nocoverage: 481);
CheckReportTestCounts(report, total: 21);
}
[Fact]
[Trait("Category", "Solution")]
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: 649, ignored: 266, survived: 4, killed: 9, timeout: 2, nocoverage: 331);
CheckReportTestCounts(report, total: 23);
}
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 actualTotal = report.TestFiles.Sum(tf => tf.Value.Tests.Count);
actualTotal.ShouldBe(total);
}
}