@@ -8,7 +8,10 @@ import 'dart:io';
8
8
9
9
import 'package:args/args.dart' ;
10
10
import 'package:coverage/src/collect.dart' ;
11
+ import 'package:coverage/src/coverage_options.dart' ;
11
12
import 'package:logging/logging.dart' ;
13
+ import 'package:meta/meta.dart' ;
14
+ import 'package:path/path.dart' as p;
12
15
import 'package:stack_trace/stack_trace.dart' ;
13
16
14
17
Future <void > main (List <String > arguments) async {
@@ -17,15 +20,19 @@ Future<void> main(List<String> arguments) async {
17
20
print ('${rec .level .name }: ${rec .time }: ${rec .message }' );
18
21
});
19
22
20
- final options = _parseArgs (arguments);
23
+ final defaultOptions = CoverageOptionsProvider ().coverageOptions;
24
+ final options = parseArgs (arguments, defaultOptions);
25
+
26
+ final out = options.out == null ? stdout : File (options.out! ).openWrite ();
27
+
21
28
await Chain .capture (() async {
22
29
final coverage = await collect (options.serviceUri, options.resume,
23
30
options.waitPaused, options.includeDart, options.scopedOutput,
24
31
timeout: options.timeout,
25
32
functionCoverage: options.functionCoverage,
26
33
branchCoverage: options.branchCoverage);
27
- options. out.write (json.encode (coverage));
28
- await options. out.close ();
34
+ out.write (json.encode (coverage));
35
+ await out.close ();
29
36
}, onError: (dynamic error, Chain chain) {
30
37
stderr.writeln (error);
31
38
stderr.writeln (chain.terse);
@@ -48,7 +55,7 @@ class Options {
48
55
this .scopedOutput);
49
56
50
57
final Uri serviceUri;
51
- final IOSink out;
58
+ final String ? out;
52
59
final Duration ? timeout;
53
60
final bool waitPaused;
54
61
final bool resume;
@@ -58,7 +65,8 @@ class Options {
58
65
final Set <String > scopedOutput;
59
66
}
60
67
61
- Options _parseArgs (List <String > arguments) {
68
+ @visibleForTesting
69
+ Options parseArgs (List <String > arguments, CoverageOptions defaultOptions) {
62
70
final parser = ArgParser ()
63
71
..addOption ('host' ,
64
72
abbr: 'H' ,
@@ -69,11 +77,11 @@ Options _parseArgs(List<String> arguments) {
69
77
help: 'remote VM port. DEPRECATED: use --uri' ,
70
78
defaultsTo: '8181' )
71
79
..addOption ('uri' , abbr: 'u' , help: 'VM observatory service URI' )
72
- ..addOption ('out' ,
73
- abbr: 'o' , defaultsTo: 'stdout' , help: 'output: may be file or stdout' )
80
+ ..addOption ('out' , abbr: 'o' , help: 'output: may be file or stdout' )
74
81
..addOption ('connect-timeout' ,
75
82
abbr: 't' , help: 'connect timeout in seconds' )
76
83
..addMultiOption ('scope-output' ,
84
+ defaultsTo: defaultOptions.scopeOutput,
77
85
help: 'restrict coverage results so that only scripts that start with '
78
86
'the provided package path are considered' )
79
87
..addFlag ('wait-paused' ,
@@ -85,10 +93,12 @@ Options _parseArgs(List<String> arguments) {
85
93
..addFlag ('include-dart' ,
86
94
abbr: 'd' , defaultsTo: false , help: 'include "dart:" libraries' )
87
95
..addFlag ('function-coverage' ,
88
- abbr: 'f' , defaultsTo: false , help: 'Collect function coverage info' )
96
+ abbr: 'f' ,
97
+ defaultsTo: defaultOptions.functionCoverage,
98
+ help: 'Collect function coverage info' )
89
99
..addFlag ('branch-coverage' ,
90
100
abbr: 'b' ,
91
- defaultsTo: false ,
101
+ defaultsTo: defaultOptions.branchCoverage ,
92
102
help: 'Collect branch coverage info (Dart VM must also be run with '
93
103
'--branch-coverage for this to work)' )
94
104
..addFlag ('help' , abbr: 'h' , negatable: false , help: 'show this help' );
@@ -125,13 +135,24 @@ Options _parseArgs(List<String> arguments) {
125
135
}
126
136
127
137
final scopedOutput = args['scope-output' ] as List <String >;
128
- IOSink out;
129
- if (args['out' ] == 'stdout' ) {
130
- out = stdout;
138
+ String ? out;
139
+ final outPath = args['out' ] as String ? ;
140
+ if (outPath == 'stdout' ||
141
+ (outPath == null && defaultOptions.outputDirectory == null )) {
142
+ out = null ;
131
143
} else {
132
- final outfile = File (args['out' ] as String )..createSync (recursive: true );
133
- out = outfile.openWrite ();
144
+ final outFilePath = p.normalize (outPath ??
145
+ p.absolute (defaultOptions.outputDirectory! , 'coverage.json' ));
146
+
147
+ final outFile = File (outFilePath);
148
+ if (! FileSystemEntity .isDirectorySync (outFilePath) &&
149
+ ! FileSystemEntity .isFileSync (outFilePath)) {
150
+ outFile.createSync (recursive: true );
151
+ }
152
+
153
+ out = outFile.path;
134
154
}
155
+
135
156
final timeout = (args['connect-timeout' ] == null )
136
157
? null
137
158
: Duration (seconds: int .parse (args['connect-timeout' ] as String ));
0 commit comments