forked from dart-lang/tools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathformat_coverage.dart
More file actions
338 lines (306 loc) · 9.94 KB
/
format_coverage.dart
File metadata and controls
338 lines (306 loc) · 9.94 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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'dart:io';
import 'package:args/args.dart';
import 'package:coverage/coverage.dart';
import 'package:coverage/src/coverage_options.dart';
import 'package:glob/glob.dart';
import 'package:path/path.dart' as p;
/// [Environment] stores gathered arguments information.
class Environment {
Environment({
required this.baseDirectory,
required this.bazel,
required this.bazelWorkspace,
required this.checkIgnore,
required this.input,
required this.lcov,
required this.output,
required this.packagesPath,
required this.packagePath,
required this.prettyPrint,
required this.prettyPrintFunc,
required this.prettyPrintBranch,
required this.reportOn,
required this.ignoreFiles,
required this.sdkRoot,
required this.verbose,
required this.workers,
});
String? baseDirectory;
bool bazel;
String bazelWorkspace;
bool checkIgnore;
String input;
bool lcov;
String? output;
String? packagesPath;
String packagePath;
bool prettyPrint;
bool prettyPrintFunc;
bool prettyPrintBranch;
List<String>? reportOn;
List<String>? ignoreFiles;
String? sdkRoot;
bool verbose;
int workers;
}
Future<void> main(List<String> arguments) async {
final defaultOptions = CoverageOptionsProvider().coverageOptions;
final env = parseArgs(arguments, defaultOptions);
final files = filesToProcess(env.input);
if (env.verbose) {
print('Environment:');
print(' # files: ${files.length}');
print(' # workers: ${env.workers}');
print(' sdk-root: ${env.sdkRoot}');
print(' package-path: ${env.packagePath}');
print(' packages-path: ${env.packagesPath}');
print(' report-on: ${env.reportOn}');
print(' check-ignore: ${env.checkIgnore}');
}
final clock = Stopwatch()..start();
final hitmap = await HitMap.parseFiles(
files,
checkIgnoredLines: env.checkIgnore,
// ignore: deprecated_member_use_from_same_package
packagesPath: env.packagesPath,
packagePath: env.packagePath,
);
// All workers are done. Process the data.
if (env.verbose) {
print('Done creating global hitmap. Took ${clock.elapsedMilliseconds} ms.');
}
final ignoreGlobs = env.ignoreFiles?.map(Glob.new).toSet();
String output;
final resolver = env.bazel
? BazelResolver(workspacePath: env.bazelWorkspace)
: await Resolver.create(
packagesPath: env.packagesPath,
packagePath: env.packagePath,
sdkRoot: env.sdkRoot,
);
final loader = Loader();
if (env.prettyPrint) {
output = await hitmap.prettyPrint(resolver, loader,
reportOn: env.reportOn,
ignoreGlobs: ignoreGlobs,
reportFuncs: env.prettyPrintFunc,
reportBranches: env.prettyPrintBranch);
} else {
assert(env.lcov);
output = hitmap.formatLcov(resolver,
reportOn: env.reportOn,
ignoreGlobs: ignoreGlobs,
basePath: env.baseDirectory);
}
final outputSink =
env.output == null ? stdout : File(env.output!).openWrite();
outputSink.write(output);
await outputSink.flush();
if (env.verbose) {
print('Done flushing output. Took ${clock.elapsedMilliseconds} ms.');
}
if (env.verbose) {
if (resolver.failed.isNotEmpty) {
print('Failed to resolve:');
for (var error in resolver.failed.toSet()) {
print(' $error');
}
}
if (loader.failed.isNotEmpty) {
print('Failed to load:');
for (var error in loader.failed.toSet()) {
print(' $error');
}
}
}
await outputSink.close();
}
/// Checks the validity of the provided arguments. Does not initialize actual
/// processing.
Environment parseArgs(List<String> arguments, CoverageOptions defaultOptions) {
final parser = ArgParser();
parser
..addOption(
'sdk-root',
abbr: 's',
help: 'path to the SDK root',
)
..addOption(
'packages',
help: '[DEPRECATED] path to the package spec file',
)
..addOption('package',
help: 'root directory of the package',
defaultsTo: defaultOptions.packageDirectory)
..addOption(
'in',
abbr: 'i',
help: 'input(s): may be file or directory',
)
..addOption('out', abbr: 'o', help: 'output: may be file or stdout')
..addMultiOption(
'report-on',
help: 'which directories or files to report coverage on',
)
..addOption(
'workers',
abbr: 'j',
defaultsTo: '1',
help: 'number of workers',
)
..addOption('bazel-workspace',
defaultsTo: '', help: 'Bazel workspace directory')
..addOption('base-directory',
abbr: 'b',
help: 'the base directory relative to which source paths are output')
..addFlag('bazel',
defaultsTo: false, help: 'use Bazel-style path resolution')
..addFlag('pretty-print',
abbr: 'r',
negatable: false,
help: 'convert line coverage data to pretty print format')
..addFlag('pretty-print-func',
abbr: 'f',
negatable: false,
help: 'convert function coverage data to pretty print format')
..addFlag('pretty-print-branch',
negatable: false,
help: 'convert branch coverage data to pretty print format')
..addFlag('lcov',
abbr: 'l',
negatable: false,
help: 'convert coverage data to lcov format')
..addFlag('verbose', abbr: 'v', negatable: false, help: 'verbose output')
..addFlag(
'check-ignore',
abbr: 'c',
negatable: false,
help: 'check for coverage ignore comments.'
' Not supported in web coverage.',
)
..addMultiOption(
'ignore-files',
defaultsTo: [],
help: 'Ignore files by glob patterns',
)
..addFlag('help', abbr: 'h', negatable: false, help: 'show this help');
final args = parser.parse(arguments);
void printUsage() {
print('Usage: dart format_coverage.dart [OPTION...]\n');
print(parser.usage);
}
Never fail(String msg) {
print('\n$msg\n');
printUsage();
exit(1);
}
if (args['help'] as bool) {
printUsage();
exit(0);
}
var sdkRoot = args['sdk-root'] as String?;
if (sdkRoot != null) {
sdkRoot = p.normalize(p.join(p.absolute(sdkRoot), 'lib'));
if (!FileSystemEntity.isDirectorySync(sdkRoot)) {
fail('Provided SDK root "${args["sdk-root"]}" is not a valid SDK '
'top-level directory');
}
}
final packagesPath = args['packages'] as String?;
if (packagesPath != null) {
if (!FileSystemEntity.isFileSync(packagesPath)) {
fail('Package spec "${args["packages"]}" not found, or not a file.');
}
}
final packagePath = args['package'] as String;
if (!FileSystemEntity.isDirectorySync(packagePath)) {
fail('Package spec "${args["package"]}" not found, or not a directory.');
}
if (args['in'] == null && defaultOptions.outputDirectory == null) {
fail('No input files given.');
}
final input = p.normalize((args['in'] as String?) ??
p.absolute(defaultOptions.outputDirectory!, 'coverage.json'));
if (!FileSystemEntity.isDirectorySync(input) &&
!FileSystemEntity.isFileSync(input)) {
fail('Provided input "${args["in"]}" is neither a directory nor a file.');
}
String? output;
final outPath = args['out'] as String?;
if (outPath == 'stdout' ||
(outPath == null && defaultOptions.outputDirectory == null)) {
output = null;
} else {
final outFilePath = p.normalize(
outPath ?? p.absolute(defaultOptions.outputDirectory!, 'lcov.info'));
final outfile = File(outFilePath)..createSync(recursive: true);
output = outfile.path;
}
final reportOnRaw = args['report-on'] as List<String>;
final reportOn = reportOnRaw.isNotEmpty ? reportOnRaw : null;
final bazel = args['bazel'] as bool;
final bazelWorkspace = args['bazel-workspace'] as String;
if (bazelWorkspace.isNotEmpty && !bazel) {
stderr.writeln('warning: ignoring --bazel-workspace: --bazel not set');
}
String? baseDirectory;
if (args['base-directory'] != null) {
baseDirectory = p.absolute(args['base-directory'] as String);
}
final lcov = args['lcov'] as bool;
var prettyPrint = args['pretty-print'] as bool;
final prettyPrintFunc = args['pretty-print-func'] as bool;
final prettyPrintBranch = args['pretty-print-branch'] as bool;
final numModesChosen = (prettyPrint ? 1 : 0) +
(prettyPrintFunc ? 1 : 0) +
(prettyPrintBranch ? 1 : 0) +
(lcov ? 1 : 0);
if (numModesChosen > 1) {
fail('Choose one of the pretty-print modes or lcov output');
}
// The pretty printer is used by all modes other than lcov.
if (!lcov) prettyPrint = true;
int workers;
try {
workers = int.parse('${args["workers"]}');
} catch (e) {
fail('Invalid worker count: $e');
}
final checkIgnore = args['check-ignore'] as bool;
final ignoredGlobs = args['ignore-files'] as List<String>;
final verbose = args['verbose'] as bool;
return Environment(
baseDirectory: baseDirectory,
bazel: bazel,
bazelWorkspace: bazelWorkspace,
checkIgnore: checkIgnore,
input: input,
lcov: lcov,
output: output,
packagesPath: packagesPath,
packagePath: packagePath,
prettyPrint: prettyPrint,
prettyPrintFunc: prettyPrintFunc,
prettyPrintBranch: prettyPrintBranch,
reportOn: reportOn,
ignoreFiles: ignoredGlobs,
sdkRoot: sdkRoot,
verbose: verbose,
workers: workers);
}
/// Given an absolute path absPath, this function returns a [List] of files
/// are contained by it if it is a directory, or a [List] containing the file if
/// it is a file.
List<File> filesToProcess(String absPath) {
if (FileSystemEntity.isDirectorySync(absPath)) {
return Directory(absPath)
.listSync(recursive: true)
.whereType<File>()
.where((e) => e.path.endsWith('.json'))
.toList();
}
return <File>[File(absPath)];
}