forked from lucaslorentz/minicover
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
308 lines (249 loc) · 12.6 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
using Microsoft.Extensions.CommandLineUtils;
using Microsoft.Extensions.FileSystemGlobbing.Abstractions;
using MiniCover.Instrumentation;
using MiniCover.Model;
using MiniCover.Reports;
using Newtonsoft.Json;
using System;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
namespace MiniCover
{
class Program
{
static int Main(string[] args)
{
Console.OutputEncoding = Encoding.UTF8;
var commandLineApplication = new CommandLineApplication();
commandLineApplication.Name = "MiniCover";
commandLineApplication.Description = "MiniCover - Code coverage for .NET Core via assembly instrumentation";
commandLineApplication.Command("instrument", command =>
{
command.Description = "Instrument assemblies";
var workDirOption = CreateWorkdirOption(command);
var includeAssembliesOption = command.Option("--assemblies", "Pattern to include assemblies [default: **/*.dll]", CommandOptionType.MultipleValue);
var excludeAssembliesOption = command.Option("--exclude-assemblies", "Pattern to exclude assemblies", CommandOptionType.MultipleValue);
var includeSourceOption = command.Option("--sources", "Pattern to include source files [default: **/*]", CommandOptionType.MultipleValue);
var excludeSourceOption = command.Option("--exclude-sources", "Pattern to exclude source files", CommandOptionType.MultipleValue);
var hitsFileOption = command.Option("--hits-file", "Hits file name [default: coverage-hits.txt]", CommandOptionType.SingleValue);
var coverageFileOption = CreateCoverageFileOption(command);
command.HelpOption("-h | --help");
command.OnExecute(() =>
{
var workdir = UpdateWorkingDirectory(workDirOption);
var assemblies = GetFiles(includeAssembliesOption, excludeAssembliesOption, "**/*.dll");
if (assemblies.Length == 0)
throw new Exception("No assemblies found");
var sourceFiles = GetFiles(includeSourceOption, excludeSourceOption, "**/*.cs");
if (sourceFiles.Length == 0)
throw new Exception("No source files found");
var hitsFile = GetHitsFile(hitsFileOption);
var coverageFile = GetCoverageFile(coverageFileOption);
var instrumenter = new Instrumenter(assemblies, hitsFile, sourceFiles, workdir);
var result = instrumenter.Execute();
SaveCoverageFile(coverageFile, result);
return 0;
});
});
commandLineApplication.Command("uninstrument", command =>
{
command.Description = "Uninstrument assemblies";
var workDirOption = CreateWorkdirOption(command);
var coverageFileOption = CreateCoverageFileOption(command);
command.HelpOption("-h | --help");
command.OnExecute(() =>
{
UpdateWorkingDirectory(workDirOption);
var coverageFile = GetCoverageFile(coverageFileOption);
var result = LoadCoverageFile(coverageFile);
Uninstrumenter.Execute(result);
return 0;
});
});
commandLineApplication.Command("report", command =>
{
command.Description = "Outputs coverage report";
var workDirOption = CreateWorkdirOption(command);
var coverageFileOption = CreateCoverageFileOption(command);
var thresholdOption = CreateThresholdOption(command);
command.HelpOption("-h | --help");
command.OnExecute(() =>
{
UpdateWorkingDirectory(workDirOption);
var coverageFile = GetCoverageFile(coverageFileOption);
var threshold = GetThreshold(thresholdOption);
var result = LoadCoverageFile(coverageFile);
var consoleReport = new ConsoleReport();
return consoleReport.Execute(result, threshold);
});
});
commandLineApplication.Command("htmlreport", command =>
{
command.Description = "Write html report to folder";
var workDirOption = CreateWorkdirOption(command);
var coverageFileOption = CreateCoverageFileOption(command);
var thresholdOption = CreateThresholdOption(command);
var outputOption = command.Option("--output", "Output folder for html report [default: coverage-html]", CommandOptionType.SingleValue);
command.HelpOption("-h | --help");
command.OnExecute(() =>
{
UpdateWorkingDirectory(workDirOption);
var coverageFile = GetCoverageFile(coverageFileOption);
var threshold = GetThreshold(thresholdOption);
var result = LoadCoverageFile(coverageFile);
var output = GetHtmlReportOutput(outputOption);
var htmlReport = new HtmlReport(output);
return htmlReport.Execute(result, threshold);
});
});
commandLineApplication.Command("xmlreport", command =>
{
command.Description = "Write an NCover-formatted XML report to folder";
var workDirOption = CreateWorkdirOption(command);
var coverageFileOption = CreateCoverageFileOption(command);
var thresholdOption = CreateThresholdOption(command);
var outputOption = command.Option("--output", "Output file for NCover report [default: coverage.xml]", CommandOptionType.SingleValue);
command.HelpOption("-h | --help");
command.OnExecute(() =>
{
UpdateWorkingDirectory(workDirOption);
var coverageFile = GetCoverageFile(coverageFileOption);
var threshold = GetThreshold(thresholdOption);
var result = LoadCoverageFile(coverageFile);
var output = GetXmlReportOutput(outputOption);
XmlReport.Execute(result, output, threshold);
return 0;
});
});
commandLineApplication.Command("opencoverreport", command =>
{
command.Description = "Write an OpenCover-formatted XML report to folder";
var workDirOption = CreateWorkdirOption(command);
var coverageFileOption = CreateCoverageFileOption(command);
var thresholdOption = CreateThresholdOption(command);
var outputOption = command.Option("--output", "Output file for OpenCover report [default: opencovercoverage.xml]", CommandOptionType.SingleValue);
command.HelpOption("-h | --help");
command.OnExecute(() =>
{
UpdateWorkingDirectory(workDirOption);
var coverageFile = GetCoverageFile(coverageFileOption);
var threshold = GetThreshold(thresholdOption);
var result = LoadCoverageFile(coverageFile);
var output = GetOpenCoverXmlReportOutput(outputOption);
OpenCoverReport.Execute(result, output, threshold);
return 0;
});
});
commandLineApplication.Command("reset", command =>
{
command.Description = "Reset hits count";
var workDirOption = CreateWorkdirOption(command);
var coverageFileOption = CreateCoverageFileOption(command);
command.HelpOption("-h | --help");
command.OnExecute(() =>
{
UpdateWorkingDirectory(workDirOption);
var coverageFile = GetCoverageFile(coverageFileOption);
if (File.Exists(coverageFile))
{
var result = LoadCoverageFile(coverageFile);
if (File.Exists(result.HitsFile))
File.Delete(result.HitsFile);
}
return 0;
});
});
commandLineApplication.HelpOption("-h | --help");
commandLineApplication.OnExecute(() =>
{
commandLineApplication.ShowHelp();
return 0;
});
return commandLineApplication.Execute(args);
}
private static CommandOption CreateWorkdirOption(CommandLineApplication command)
{
return command.Option("--workdir", "Change working directory", CommandOptionType.SingleValue);
}
private static string UpdateWorkingDirectory(CommandOption workDirOption)
{
if (workDirOption.Value() != null)
{
var fullWorkDir = Path.GetFullPath(workDirOption.Value());
Directory.SetCurrentDirectory(fullWorkDir);
}
return Directory.GetCurrentDirectory();
}
private static CommandOption CreateThresholdOption(CommandLineApplication command)
{
return command.Option("--threshold", "Coverage percentage threshold (default: 90)", CommandOptionType.SingleValue);
}
private static CommandOption CreateCoverageFileOption(CommandLineApplication command)
{
return command.Option("--coverage-file", "Coverage file name [default: coverage.json]", CommandOptionType.SingleValue);
}
private static string GetHtmlReportOutput(CommandOption outputOption)
{
return outputOption.Value() ?? "coverage-html";
}
private static string GetXmlReportOutput(CommandOption outputOption)
{
return outputOption.Value() ?? "coverage.xml";
}
private static string GetOpenCoverXmlReportOutput(CommandOption outputOption)
{
return outputOption.Value() ?? "opencovercoverage.xml";
}
private static string GetCoverageFile(CommandOption coverageFileOption)
{
return coverageFileOption.Value() ?? "coverage.json";
}
private static string GetHitsFile(CommandOption hitsFileOption)
{
return Path.GetFullPath(hitsFileOption.Value() ?? "coverage-hits.txt");
}
private static string GetSourceDirectory(CommandOption sourceOption)
{
return Path.GetFullPath(sourceOption.Value() ?? Directory.GetCurrentDirectory());
}
private static string[] GetFiles(CommandOption includeOption, CommandOption excludeOption, string defaultInclude)
{
var matcher = new Microsoft.Extensions.FileSystemGlobbing.Matcher();
if (includeOption.HasValue())
{
foreach (var include in includeOption.Values)
{
matcher.AddInclude(include);
}
}
else if (!string.IsNullOrEmpty(defaultInclude))
{
matcher.AddInclude(defaultInclude);
}
foreach (var exclude in excludeOption.Values)
{
matcher.AddExclude(exclude);
}
var currentDirectoryInfo = new DirectoryInfo(Directory.GetCurrentDirectory());
var directoryInfoWrapper = new DirectoryInfoWrapper(currentDirectoryInfo);
var fileMatchResult = matcher.Execute(directoryInfoWrapper);
return fileMatchResult.Files.Select(f => Path.GetFullPath(f.Path)).ToArray();
}
private static void SaveCoverageFile(string coverageFile, InstrumentationResult result)
{
File.WriteAllText(coverageFile, JsonConvert.SerializeObject(result, Formatting.Indented));
}
private static InstrumentationResult LoadCoverageFile(string coverageFile)
{
if (!File.Exists(coverageFile))
throw new FileNotFoundException($"Coverage file {coverageFile} doesn't exist");
return JsonConvert.DeserializeObject<InstrumentationResult>(File.ReadAllText(coverageFile));
}
private static float GetThreshold(CommandOption thresholdOption)
{
return float.Parse(thresholdOption.Value() ?? "90", CultureInfo.InvariantCulture) / 100;
}
}
}