@@ -8,7 +8,10 @@ import 'dart:io';
88
99import 'package:args/args.dart' ;
1010import 'package:coverage/src/collect.dart' ;
11+ import 'package:coverage/src/coverage_options.dart' ;
1112import 'package:logging/logging.dart' ;
13+ import 'package:meta/meta.dart' ;
14+ import 'package:path/path.dart' as p;
1215import 'package:stack_trace/stack_trace.dart' ;
1316
1417Future <void > main (List <String > arguments) async {
@@ -17,15 +20,19 @@ Future<void> main(List<String> arguments) async {
1720 print ('${rec .level .name }: ${rec .time }: ${rec .message }' );
1821 });
1922
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+
2128 await Chain .capture (() async {
2229 final coverage = await collect (options.serviceUri, options.resume,
2330 options.waitPaused, options.includeDart, options.scopedOutput,
2431 timeout: options.timeout,
2532 functionCoverage: options.functionCoverage,
2633 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 ();
2936 }, onError: (dynamic error, Chain chain) {
3037 stderr.writeln (error);
3138 stderr.writeln (chain.terse);
@@ -48,7 +55,7 @@ class Options {
4855 this .scopedOutput);
4956
5057 final Uri serviceUri;
51- final IOSink out;
58+ final String ? out;
5259 final Duration ? timeout;
5360 final bool waitPaused;
5461 final bool resume;
@@ -58,7 +65,8 @@ class Options {
5865 final Set <String > scopedOutput;
5966}
6067
61- Options _parseArgs (List <String > arguments) {
68+ @visibleForTesting
69+ Options parseArgs (List <String > arguments, CoverageOptions defaultOptions) {
6270 final parser = ArgParser ()
6371 ..addOption ('host' ,
6472 abbr: 'H' ,
@@ -69,11 +77,11 @@ Options _parseArgs(List<String> arguments) {
6977 help: 'remote VM port. DEPRECATED: use --uri' ,
7078 defaultsTo: '8181' )
7179 ..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' )
7481 ..addOption ('connect-timeout' ,
7582 abbr: 't' , help: 'connect timeout in seconds' )
7683 ..addMultiOption ('scope-output' ,
84+ defaultsTo: defaultOptions.scopeOutput,
7785 help: 'restrict coverage results so that only scripts that start with '
7886 'the provided package path are considered' )
7987 ..addFlag ('wait-paused' ,
@@ -85,10 +93,12 @@ Options _parseArgs(List<String> arguments) {
8593 ..addFlag ('include-dart' ,
8694 abbr: 'd' , defaultsTo: false , help: 'include "dart:" libraries' )
8795 ..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' )
8999 ..addFlag ('branch-coverage' ,
90100 abbr: 'b' ,
91- defaultsTo: false ,
101+ defaultsTo: defaultOptions.branchCoverage ,
92102 help: 'Collect branch coverage info (Dart VM must also be run with '
93103 '--branch-coverage for this to work)' )
94104 ..addFlag ('help' , abbr: 'h' , negatable: false , help: 'show this help' );
@@ -125,13 +135,24 @@ Options _parseArgs(List<String> arguments) {
125135 }
126136
127137 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 ;
131143 } 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;
134154 }
155+
135156 final timeout = (args['connect-timeout' ] == null )
136157 ? null
137158 : Duration (seconds: int .parse (args['connect-timeout' ] as String ));
0 commit comments