-
Notifications
You must be signed in to change notification settings - Fork 388
/
Copy pathCoverageResultTask.cs
287 lines (243 loc) · 11.2 KB
/
CoverageResultTask.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
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
// Copyright (c) Toni Solarin-Sodara
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using ConsoleTables;
using Coverlet.Core;
using Coverlet.Core.Abstractions;
using Coverlet.Core.Enums;
using Coverlet.Core.Reporters;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
using Microsoft.Extensions.DependencyInjection;
namespace Coverlet.MSbuild.Tasks
{
public class CoverageResultTask : BaseTask
{
private readonly MSBuildLogger _logger;
[Required]
public string Output { get; set; }
[Required]
public string OutputFormat { get; set; }
[Required]
public string Threshold { get; set; }
[Required]
public string ThresholdType { get; set; }
[Required]
public string ThresholdStat { get; set; }
[Required]
public string ThresholdAct { get; set; }
[Required]
public ITaskItem InstrumenterState { get; set; }
public string CoverletMultiTargetFrameworksCurrentTFM { get; set; }
[Output]
public ITaskItem[] ReportItems { get; set; }
public CoverageResultTask()
{
_logger = new MSBuildLogger(Log);
}
public override bool Execute()
{
try
{
_logger.LogInformation("\nCalculating coverage result...", true);
IFileSystem fileSystem = ServiceProvider.GetService<IFileSystem>();
if (InstrumenterState is null || !fileSystem.Exists(InstrumenterState.ItemSpec))
{
_logger.LogError("Result of instrumentation task not found");
return false;
}
Coverage coverage = null;
using (Stream instrumenterStateStream = fileSystem.NewFileStream(InstrumenterState.ItemSpec, FileMode.Open))
{
IInstrumentationHelper instrumentationHelper = ServiceProvider.GetService<IInstrumentationHelper>();
// Task.Log is teared down after a task and thus the new MSBuildLogger must be passed to the InstrumentationHelper
// https://github.com/microsoft/msbuild/issues/5153
instrumentationHelper.SetLogger(_logger);
coverage = new Coverage(CoveragePrepareResult.Deserialize(instrumenterStateStream), _logger, ServiceProvider.GetService<IInstrumentationHelper>(), fileSystem, ServiceProvider.GetService<ISourceRootTranslator>());
}
try
{
fileSystem.Delete(InstrumenterState.ItemSpec);
}
catch (Exception ex)
{
// We don't want to block coverage for I/O errors
_logger.LogInformation($"Exception during instrument state deletion, file name '{InstrumenterState.ItemSpec}'");
_logger.LogWarning(ex);
}
CoverageResult result = coverage.GetCoverageResult();
string directory = Path.GetDirectoryName(Output);
if (directory == string.Empty)
{
directory = Directory.GetCurrentDirectory();
}
else if (!Directory.Exists(directory))
{
Directory.CreateDirectory(directory);
}
string[] formats = OutputFormat.Split(',');
var coverageReportPaths = new List<ITaskItem>(formats.Length);
ISourceRootTranslator sourceRootTranslator = ServiceProvider.GetService<ISourceRootTranslator>();
foreach (string format in formats)
{
IReporter reporter = new ReporterFactory(format).CreateReporter();
if (reporter == null)
{
throw new Exception($"Specified output format '{format}' is not supported");
}
if (reporter.OutputType == ReporterOutputType.Console)
{
// Output to TaskLoggingHelper
Log.LogMessage(MessageImportance.High, " Outputting results to console");
Log.LogMessage(MessageImportance.High, reporter.Report(result, sourceRootTranslator));
}
else
{
ReportWriter writer = new(CoverletMultiTargetFrameworksCurrentTFM,
directory,
Output,
reporter,
fileSystem,
result,
sourceRootTranslator);
string path = writer.WriteReport();
Log.LogMessage(MessageImportance.High, $" Generating report '{path}'");
var metadata = new Dictionary<string, string> { ["Format"] = format };
coverageReportPaths.Add(new TaskItem(path, metadata));
}
}
ReportItems = coverageReportPaths.ToArray();
var thresholdTypeFlagQueue = new Queue<ThresholdTypeFlags>();
foreach (string thresholdType in ThresholdType.Split(',').Select(t => t.Trim()))
{
if (thresholdType.Equals("line", StringComparison.OrdinalIgnoreCase))
{
thresholdTypeFlagQueue.Enqueue(ThresholdTypeFlags.Line);
}
else if (thresholdType.Equals("branch", StringComparison.OrdinalIgnoreCase))
{
thresholdTypeFlagQueue.Enqueue(ThresholdTypeFlags.Branch);
}
else if (thresholdType.Equals("method", StringComparison.OrdinalIgnoreCase))
{
thresholdTypeFlagQueue.Enqueue(ThresholdTypeFlags.Method);
}
}
var thresholdTypeFlagValues = new Dictionary<ThresholdTypeFlags, double>();
if (Threshold.Contains(','))
{
IEnumerable<string> thresholdValues = Threshold.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(t => t.Trim());
if (thresholdValues.Count() != thresholdTypeFlagQueue.Count)
{
throw new Exception($"Threshold type flag count ({thresholdTypeFlagQueue.Count}) and values count ({thresholdValues.Count()}) doesn't match");
}
foreach (string threshold in thresholdValues)
{
if (double.TryParse(threshold, out double value))
{
thresholdTypeFlagValues[thresholdTypeFlagQueue.Dequeue()] = value;
}
else
{
throw new Exception($"Invalid threshold value must be numeric");
}
}
}
else
{
double thresholdValue = double.Parse(Threshold);
while (thresholdTypeFlagQueue.Any())
{
thresholdTypeFlagValues[thresholdTypeFlagQueue.Dequeue()] = thresholdValue;
}
}
ThresholdStatistic thresholdStat = ThresholdStatistic.Minimum;
if (ThresholdStat.Equals("average", StringComparison.OrdinalIgnoreCase))
{
thresholdStat = ThresholdStatistic.Average;
}
else if (ThresholdStat.Equals("total", StringComparison.OrdinalIgnoreCase))
{
thresholdStat = ThresholdStatistic.Total;
}
ThresholdAction thresholdAct = ThresholdAction.Fail;
if (ThresholdAct.Equals("fail", StringComparison.OrdinalIgnoreCase))
{
thresholdAct = ThresholdAction.Fail;
}
else if (ThresholdAct.Equals("warning", StringComparison.OrdinalIgnoreCase))
{
thresholdAct = ThresholdAction.Warning;
}
var coverageTable = new ConsoleTable("Module", "Line", "Branch", "Method");
var summary = new CoverageSummary();
CoverageDetails linePercentCalculation = summary.CalculateLineCoverage(result.Modules);
CoverageDetails branchPercentCalculation = summary.CalculateBranchCoverage(result.Modules);
CoverageDetails methodPercentCalculation = summary.CalculateMethodCoverage(result.Modules);
double totalLinePercent = linePercentCalculation.Percent;
double totalBranchPercent = branchPercentCalculation.Percent;
double totalMethodPercent = methodPercentCalculation.Percent;
double averageLinePercent = linePercentCalculation.AverageModulePercent;
double averageBranchPercent = branchPercentCalculation.AverageModulePercent;
double averageMethodPercent = methodPercentCalculation.AverageModulePercent;
foreach (KeyValuePair<string, Documents> module in result.Modules)
{
double linePercent = summary.CalculateLineCoverage(module.Value).Percent;
double branchPercent = summary.CalculateBranchCoverage(module.Value).Percent;
double methodPercent = summary.CalculateMethodCoverage(module.Value).Percent;
coverageTable.AddRow(Path.GetFileNameWithoutExtension(module.Key), $"{InvariantFormat(linePercent)}%", $"{InvariantFormat(branchPercent)}%", $"{InvariantFormat(methodPercent)}%");
}
Console.WriteLine();
Console.WriteLine(coverageTable.ToStringAlternative());
coverageTable.Columns.Clear();
coverageTable.Rows.Clear();
coverageTable.AddColumn(new[] { "", "Line", "Branch", "Method" });
coverageTable.AddRow("Total", $"{InvariantFormat(totalLinePercent)}%", $"{InvariantFormat(totalBranchPercent)}%", $"{InvariantFormat(totalMethodPercent)}%");
coverageTable.AddRow("Average", $"{InvariantFormat(averageLinePercent)}%", $"{InvariantFormat(averageBranchPercent)}%", $"{InvariantFormat(averageMethodPercent)}%");
Console.WriteLine(coverageTable.ToStringAlternative());
ThresholdTypeFlags thresholdTypeFlags = result.GetThresholdTypesBelowThreshold(summary, thresholdTypeFlagValues, thresholdStat);
if (thresholdTypeFlags != ThresholdTypeFlags.None)
{
var exceptionMessageBuilder = new StringBuilder();
if ((thresholdTypeFlags & ThresholdTypeFlags.Line) != ThresholdTypeFlags.None)
{
exceptionMessageBuilder.AppendLine(
$"The {thresholdStat.ToString().ToLower()} line coverage is below the specified {thresholdTypeFlagValues[ThresholdTypeFlags.Line]}");
}
if ((thresholdTypeFlags & ThresholdTypeFlags.Branch) != ThresholdTypeFlags.None)
{
exceptionMessageBuilder.AppendLine(
$"The {thresholdStat.ToString().ToLower()} branch coverage is below the specified {thresholdTypeFlagValues[ThresholdTypeFlags.Branch]}");
}
if ((thresholdTypeFlags & ThresholdTypeFlags.Method) != ThresholdTypeFlags.None)
{
exceptionMessageBuilder.AppendLine(
$"The {thresholdStat.ToString().ToLower()} method coverage is below the specified {thresholdTypeFlagValues[ThresholdTypeFlags.Method]}");
}
switch (thresholdAct)
{
case ThresholdAction.Warning:
_logger.LogWarning(exceptionMessageBuilder.ToString());
break;
case ThresholdAction.Fail:
throw new Exception(exceptionMessageBuilder.ToString());
default:
throw new ArgumentOutOfRangeException(nameof(thresholdAct), thresholdAct, "Unhandled threshold action");
}
}
}
catch (Exception ex)
{
Log.LogErrorFromException(ex, true);
return false;
}
return true;
}
private static string InvariantFormat(double value) => value.ToString(CultureInfo.InvariantCulture);
}
}