-
Notifications
You must be signed in to change notification settings - Fork 388
/
Copy pathProgram.cs
420 lines (371 loc) · 22 KB
/
Program.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
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
// 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.CommandLine;
using System.ComponentModel;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ConsoleTables;
using Coverlet.Console.Logging;
using Coverlet.Core;
using Coverlet.Core.Abstractions;
using Coverlet.Core.Enums;
using Coverlet.Core.Helpers;
using Coverlet.Core.Reporters;
using Coverlet.Core.Symbols;
using Microsoft.Extensions.DependencyInjection;
namespace Coverlet.Console
{
public static class Program
{
static int Main(string[] args)
{
var moduleOrAppDirectory = new Argument<string>("path", "Path to the test assembly or application directory.");
var target = new Option<string>(new[] { "--target", "-t" }, "Path to the test runner application.") { Arity = ArgumentArity.ZeroOrOne, IsRequired = true };
var targs = new Option<string>(new[] { "--targetargs", "-a" }, "Arguments to be passed to the test runner.") { Arity = ArgumentArity.ZeroOrOne };
var output = new Option<string>(new[] { "--output", "-o" }, "Output of the generated coverage report") { Arity = ArgumentArity.ZeroOrOne };
var verbosity = new Option<LogLevel>(new[] { "--verbosity", "-v" }, () => LogLevel.Normal, "Sets the verbosity level of the command. Allowed values are quiet, minimal, normal, detailed.") { Arity = ArgumentArity.ZeroOrOne };
var formats = new Option<string[]>(new[] { "--format", "-f" }, () => new[] { "json" }, "Format of the generated coverage report.") { Arity = ArgumentArity.ZeroOrMore, AllowMultipleArgumentsPerToken = true };
var threshold = new Option<string>("--threshold", "Exits with error if the coverage % is below value.") { Arity = ArgumentArity.ZeroOrOne };
var thresholdTypes = new Option<List<string>>("--threshold-type", () => new List<string>(new string[] { "line", "branch", "method" }), "Coverage type to apply the threshold to.").FromAmong("line", "branch", "method");
var thresholdStat = new Option<ThresholdStatistic>("--threshold-stat", () => ThresholdStatistic.Minimum, "Coverage statistic used to enforce the threshold value.") { Arity = ArgumentArity.ZeroOrOne };
var thresholdAct = new Option<ThresholdAction>("--threshold-act", () => ThresholdAction.Fail, "The action to take when coverage is below the threshold value. Defaults to failing the build.") { Arity = ArgumentArity.ZeroOrOne };
var excludeFilters = new Option<string[]>("--exclude", "Filter expressions to exclude specific modules and types.") { Arity = ArgumentArity.ZeroOrMore, AllowMultipleArgumentsPerToken = true };
var includeFilters = new Option<string[]>("--include", "Filter expressions to include only specific modules and types.") { Arity = ArgumentArity.ZeroOrMore, AllowMultipleArgumentsPerToken = true };
var excludedSourceFiles = new Option<string[]>("--exclude-by-file", "Glob patterns specifying source files to exclude.") { Arity = ArgumentArity.ZeroOrMore, AllowMultipleArgumentsPerToken = true };
var includeDirectories = new Option<string[]>("--include-directory", "Include directories containing additional assemblies to be instrumented.") { Arity = ArgumentArity.ZeroOrMore, AllowMultipleArgumentsPerToken = true };
var excludeAttributes = new Option<string[]>("--exclude-by-attribute", "Attributes to exclude from code coverage.") { Arity = ArgumentArity.ZeroOrMore, AllowMultipleArgumentsPerToken = true };
var includeTestAssembly = new Option<bool>("--include-test-assembly", "Specifies whether to report code coverage of the test assembly.") { Arity = ArgumentArity.Zero };
var singleHit = new Option<bool>("--single-hit", "Specifies whether to limit code coverage hit reporting to a single hit for each location") { Arity = ArgumentArity.Zero };
var skipAutoProp = new Option<bool>("--skipautoprops", "Neither track nor record auto-implemented properties.") { Arity = ArgumentArity.Zero };
var mergeWith = new Option<string>("--merge-with", "Path to existing coverage result to merge.") { Arity = ArgumentArity.ZeroOrOne };
var useSourceLink = new Option<bool>("--use-source-link", "Specifies whether to use SourceLink URIs in place of file system paths.") { Arity = ArgumentArity.Zero };
var doesNotReturnAttributes = new Option<string[]>("--does-not-return-attribute", "Attributes that mark methods that do not return") { Arity = ArgumentArity.ZeroOrMore, AllowMultipleArgumentsPerToken = true };
var excludeAssembliesWithoutSources = new Option<string>("--exclude-assemblies-without-sources", "Specifies behaviour of heuristic to ignore assemblies with missing source documents.") { Arity = ArgumentArity.ZeroOrOne };
var sourceMappingFile = new Option<string>("--source-mapping-file", "Specifies the path to a SourceRootsMappings file.") { Arity = ArgumentArity.ZeroOrOne };
RootCommand rootCommand = new()
{
moduleOrAppDirectory,
target,
targs,
output,
verbosity,
formats,
threshold,
thresholdTypes,
thresholdStat,
thresholdAct,
excludeFilters,
includeFilters,
excludedSourceFiles,
includeDirectories,
excludeAttributes,
includeTestAssembly,
singleHit,
skipAutoProp,
mergeWith,
useSourceLink,
doesNotReturnAttributes,
excludeAssembliesWithoutSources,
sourceMappingFile
};
rootCommand.Description = "Cross platform .NET Core code coverage tool";
rootCommand.SetHandler(async (context) =>
{
string moduleOrAppDirectoryValue = context.ParseResult.GetValueForArgument(moduleOrAppDirectory);
string targetValue = context.ParseResult.GetValueForOption(target);
string targsValue = context.ParseResult.GetValueForOption(targs);
string outputValue = context.ParseResult.GetValueForOption(output);
LogLevel verbosityValue = context.ParseResult.GetValueForOption(verbosity);
string[] formatsValue = context.ParseResult.GetValueForOption(formats);
string thresholdValue = context.ParseResult.GetValueForOption(threshold);
List<string> thresholdTypesValue = context.ParseResult.GetValueForOption(thresholdTypes);
ThresholdStatistic thresholdStatValue = context.ParseResult.GetValueForOption(thresholdStat);
ThresholdAction thresholdActValue = context.ParseResult.GetValueForOption(thresholdAct);
string[] excludeFiltersValue = context.ParseResult.GetValueForOption(excludeFilters);
string[] includeFiltersValue = context.ParseResult.GetValueForOption(includeFilters);
string[] excludedSourceFilesValue = context.ParseResult.GetValueForOption(excludedSourceFiles);
string[] includeDirectoriesValue = context.ParseResult.GetValueForOption(includeDirectories);
string[] excludeAttributesValue = context.ParseResult.GetValueForOption(excludeAttributes);
bool includeTestAssemblyValue = context.ParseResult.GetValueForOption(includeTestAssembly);
bool singleHitValue = context.ParseResult.GetValueForOption(singleHit);
bool skipAutoPropValue = context.ParseResult.GetValueForOption(skipAutoProp);
string mergeWithValue = context.ParseResult.GetValueForOption(mergeWith);
bool useSourceLinkValue = context.ParseResult.GetValueForOption(useSourceLink);
string[] doesNotReturnAttributesValue = context.ParseResult.GetValueForOption(doesNotReturnAttributes);
string excludeAssembliesWithoutSourcesValue = context.ParseResult.GetValueForOption(excludeAssembliesWithoutSources);
string sourceMappingFileValue = context.ParseResult.GetValueForOption(sourceMappingFile);
if (string.IsNullOrEmpty(moduleOrAppDirectoryValue) || string.IsNullOrWhiteSpace(moduleOrAppDirectoryValue))
throw new ArgumentException("No test assembly or application directory specified.");
var taskStatus = await HandleCommand(moduleOrAppDirectoryValue,
targetValue,
targsValue,
outputValue,
verbosityValue,
formatsValue,
thresholdValue,
thresholdTypesValue,
thresholdStatValue,
thresholdActValue,
excludeFiltersValue,
includeFiltersValue,
excludedSourceFilesValue,
includeDirectoriesValue,
excludeAttributesValue,
includeTestAssemblyValue,
singleHitValue,
skipAutoPropValue,
mergeWithValue,
useSourceLinkValue,
doesNotReturnAttributesValue,
excludeAssembliesWithoutSourcesValue,
sourceMappingFileValue);
context.ExitCode = taskStatus;
});
return rootCommand.Invoke(args);
}
private static Task<int> HandleCommand(string moduleOrAppDirectory,
string target,
string targs,
string output,
LogLevel verbosity,
string[] formats,
string threshold,
List<string> thresholdTypes,
ThresholdStatistic thresholdStat,
ThresholdAction thresholdAct,
string[] excludeFilters,
string[] includeFilters,
string[] excludedSourceFiles,
string[] includeDirectories,
string[] excludeAttributes,
bool includeTestAssembly,
bool singleHit,
bool skipAutoProp,
string mergeWith,
bool useSourceLink,
string[] doesNotReturnAttributes,
string excludeAssembliesWithoutSources,
string sourceMappingFile
)
{
IServiceCollection serviceCollection = new ServiceCollection();
serviceCollection.AddTransient<IRetryHelper, RetryHelper>();
serviceCollection.AddTransient<IProcessExitHandler, ProcessExitHandler>();
serviceCollection.AddTransient<IFileSystem, FileSystem>();
serviceCollection.AddTransient<ILogger, ConsoleLogger>();
// We need to keep singleton/static semantics
serviceCollection.AddSingleton<IInstrumentationHelper, InstrumentationHelper>();
serviceCollection.AddSingleton<ISourceRootTranslator, SourceRootTranslator>(provider => new SourceRootTranslator(sourceMappingFile, provider.GetRequiredService<ILogger>(), provider.GetRequiredService<IFileSystem>()));
serviceCollection.AddSingleton<ICecilSymbolHelper, CecilSymbolHelper>();
ServiceProvider serviceProvider = serviceCollection.BuildServiceProvider();
var logger = (ConsoleLogger)serviceProvider.GetService<ILogger>();
IFileSystem fileSystem = serviceProvider.GetService<IFileSystem>();
// Adjust log level based on user input.
logger.Level = verbosity;
int exitCode = (int)CommandExitCodes.Success;
try
{
CoverageParameters parameters = new()
{
IncludeFilters = includeFilters,
IncludeDirectories = includeDirectories,
ExcludeFilters = excludeFilters,
ExcludedSourceFiles = excludedSourceFiles,
ExcludeAttributes = excludeAttributes,
IncludeTestAssembly = includeTestAssembly,
SingleHit = singleHit,
MergeWith = mergeWith,
UseSourceLink = useSourceLink,
SkipAutoProps = skipAutoProp,
DoesNotReturnAttributes = doesNotReturnAttributes,
ExcludeAssembliesWithoutSources = excludeAssembliesWithoutSources
};
ISourceRootTranslator sourceRootTranslator = serviceProvider.GetRequiredService<ISourceRootTranslator>();
Coverage coverage = new(moduleOrAppDirectory,
parameters,
logger,
serviceProvider.GetRequiredService<IInstrumentationHelper>(),
fileSystem,
sourceRootTranslator,
serviceProvider.GetRequiredService<ICecilSymbolHelper>());
coverage.PrepareModules();
Process process = new();
process.StartInfo.FileName = target;
process.StartInfo.Arguments = targs ?? string.Empty;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.OutputDataReceived += (sender, eventArgs) =>
{
if (!string.IsNullOrEmpty(eventArgs.Data))
logger.LogInformation(eventArgs.Data, important: true);
};
process.ErrorDataReceived += (sender, eventArgs) =>
{
if (!string.IsNullOrEmpty(eventArgs.Data))
logger.LogError(eventArgs.Data);
};
process.Start();
process.BeginErrorReadLine();
process.BeginOutputReadLine();
process.WaitForExit();
string dOutput = output != null ? output : Directory.GetCurrentDirectory() + Path.DirectorySeparatorChar.ToString();
logger.LogInformation("\nCalculating coverage result...");
CoverageResult result = coverage.GetCoverageResult();
string directory = Path.GetDirectoryName(dOutput);
if (directory == string.Empty)
{
directory = Directory.GetCurrentDirectory();
}
else if (!Directory.Exists(directory))
{
Directory.CreateDirectory(directory);
}
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 console
logger.LogInformation(" Outputting results to console", important: true);
logger.LogInformation(reporter.Report(result, sourceRootTranslator), important: true);
}
else
{
// Output to file
string filename = Path.GetFileName(dOutput);
filename = (filename == string.Empty) ? $"coverage.{reporter.Extension}" : filename;
filename = Path.HasExtension(filename) ? filename : $"{filename}.{reporter.Extension}";
string report = Path.Combine(directory, filename);
logger.LogInformation($" Generating report '{report}'", important: true);
fileSystem.WriteAllText(report, reporter.Report(result, sourceRootTranslator));
}
}
var thresholdTypeFlagQueue = new Queue<ThresholdTypeFlags>();
foreach (string thresholdType in thresholdTypes)
{
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 (!string.IsNullOrEmpty(threshold) && threshold.Contains(','))
{
IEnumerable<string> thresholdValues = threshold.Split(',', 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 thresholdValue in thresholdValues)
{
if (double.TryParse(thresholdValue, out double value))
{
thresholdTypeFlagValues[thresholdTypeFlagQueue.Dequeue()] = value;
}
else
{
throw new Exception($"Invalid threshold value must be numeric");
}
}
}
else
{
double thresholdValue = threshold != null ? double.Parse(threshold) : 0;
while (thresholdTypeFlagQueue.Any())
{
thresholdTypeFlagValues[thresholdTypeFlagQueue.Dequeue()] = thresholdValue;
}
}
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)}%");
}
logger.LogInformation(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)}%");
logger.LogInformation(coverageTable.ToStringAlternative());
if (process.ExitCode > 0)
{
exitCode += (int)CommandExitCodes.TestFailed;
}
ThresholdTypeFlags thresholdTypeFlags = result.GetThresholdTypesBelowThreshold(summary, thresholdTypeFlagValues, thresholdStat);
if (thresholdTypeFlags != ThresholdTypeFlags.None)
{
exitCode += (int)CommandExitCodes.CoverageBelowThreshold;
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:
exitCode += (int)CommandExitCodes.CoverageBelowThreshold;
throw new Exception(exceptionMessageBuilder.ToString());
default:
throw new ArgumentOutOfRangeException(nameof(thresholdAct), thresholdAct, "Unhandled threshold action");
}
}
return Task.FromResult(exitCode);
}
catch (Win32Exception we) when (we.Source == "System.Diagnostics.Process")
{
logger.LogError($"Start process '{target}' failed with '{we.Message}'");
return Task.FromResult(exitCode > 0 ? exitCode : (int)CommandExitCodes.Exception);
}
catch (Exception ex)
{
logger.LogError(ex.Message);
return Task.FromResult(exitCode > 0 ? exitCode : (int)CommandExitCodes.Exception);
}
}
static string InvariantFormat(double value) => value.ToString(CultureInfo.InvariantCulture);
}
}