forked from dart-lang/tools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcollect_coverage.dart
More file actions
164 lines (148 loc) · 5.22 KB
/
collect_coverage.dart
File metadata and controls
164 lines (148 loc) · 5.22 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
// Copyright (c) 2014, 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:async';
import 'dart:convert' show json;
import 'dart:io';
import 'package:args/args.dart';
import 'package:coverage/src/collect.dart';
import 'package:coverage/src/coverage_options.dart';
import 'package:logging/logging.dart';
import 'package:meta/meta.dart';
import 'package:path/path.dart' as p;
import 'package:stack_trace/stack_trace.dart';
Future<void> main(List<String> arguments) async {
Logger.root.level = Level.WARNING;
Logger.root.onRecord.listen((LogRecord rec) {
print('${rec.level.name}: ${rec.time}: ${rec.message}');
});
final defaultOptions = CoverageOptionsProvider().coverageOptions;
final options = parseArgs(arguments, defaultOptions);
final out = options.out == null ? stdout : File(options.out!).openWrite();
await Chain.capture(() async {
final coverage = await collect(options.serviceUri, options.resume,
options.waitPaused, options.includeDart, options.scopedOutput,
timeout: options.timeout,
functionCoverage: options.functionCoverage,
branchCoverage: options.branchCoverage);
out.write(json.encode(coverage));
await out.close();
}, onError: (dynamic error, Chain chain) {
stderr.writeln(error);
stderr.writeln(chain.terse);
// See http://www.retro11.de/ouxr/211bsd/usr/include/sysexits.h.html
// EX_SOFTWARE
exit(70);
});
}
class Options {
Options(
this.serviceUri,
this.out,
this.timeout,
this.waitPaused,
this.resume,
this.includeDart,
this.functionCoverage,
this.branchCoverage,
this.scopedOutput);
final Uri serviceUri;
final String? out;
final Duration? timeout;
final bool waitPaused;
final bool resume;
final bool includeDart;
final bool functionCoverage;
final bool branchCoverage;
final Set<String> scopedOutput;
}
@visibleForTesting
Options parseArgs(List<String> arguments, CoverageOptions defaultOptions) {
final parser = ArgParser()
..addOption('host',
abbr: 'H',
help: 'remote VM host. DEPRECATED: use --uri',
defaultsTo: '127.0.0.1')
..addOption('port',
abbr: 'p',
help: 'remote VM port. DEPRECATED: use --uri',
defaultsTo: '8181')
..addOption('uri', abbr: 'u', help: 'VM observatory service URI')
..addOption('out', abbr: 'o', help: 'output: may be file or stdout')
..addOption('connect-timeout',
abbr: 't', help: 'connect timeout in seconds')
..addMultiOption('scope-output',
defaultsTo: defaultOptions.scopeOutput,
help: 'restrict coverage results so that only scripts that start with '
'the provided package path are considered')
..addFlag('wait-paused',
abbr: 'w',
defaultsTo: false,
help: 'wait for all isolates to be paused before collecting coverage')
..addFlag('resume-isolates',
abbr: 'r', defaultsTo: false, help: 'resume all isolates on exit')
..addFlag('include-dart',
abbr: 'd', defaultsTo: false, help: 'include "dart:" libraries')
..addFlag('function-coverage',
abbr: 'f',
defaultsTo: defaultOptions.functionCoverage,
help: 'Collect function coverage info')
..addFlag('branch-coverage',
abbr: 'b',
defaultsTo: defaultOptions.branchCoverage,
help: 'Collect branch coverage info (Dart VM must also be run with '
'--branch-coverage for this to work)')
..addFlag('help', abbr: 'h', negatable: false, help: 'show this help');
final args = parser.parse(arguments);
void printUsage() {
print('Usage: dart collect_coverage.dart --uri=http://... [OPTION...]\n');
print(parser.usage);
}
Never fail(String message) {
print('Error: $message\n');
printUsage();
exit(1);
}
if (args['help'] as bool) {
printUsage();
exit(0);
}
Uri serviceUri;
if (args['uri'] == null) {
// TODO(cbracken) eliminate --host and --port support when VM defaults to
// requiring an auth token. Estimated for Dart SDK 1.22.
serviceUri = Uri.parse('http://${args['host']}:${args['port']}/');
} else {
try {
serviceUri = Uri.parse(args['uri'] as String);
} on FormatException {
fail('Invalid service URI specified: ${args['uri']}');
}
}
final scopedOutput = args['scope-output'] as List<String>;
String? out;
final outPath = args['out'] as String?;
if (outPath == 'stdout' ||
(outPath == null && defaultOptions.outputDirectory == null)) {
out = null;
} else {
final outFilePath = p.normalize(outPath ??
p.absolute(defaultOptions.outputDirectory!, 'coverage.json'));
final outFile = File(outFilePath)..createSync(recursive: true);
out = outFile.path;
}
final timeout = (args['connect-timeout'] == null)
? null
: Duration(seconds: int.parse(args['connect-timeout'] as String));
return Options(
serviceUri,
out,
timeout,
args['wait-paused'] as bool,
args['resume-isolates'] as bool,
args['include-dart'] as bool,
args['function-coverage'] as bool,
args['branch-coverage'] as bool,
scopedOutput.toSet(),
);
}