@@ -7,10 +7,8 @@ import 'dart:io';
77
88import 'package:args/args.dart' ;
99import 'package:coverage/src/coverage_options.dart' ;
10- import 'package:coverage/src/util.dart'
11- show StandardOutExtension, extractVMServiceUri;
10+ import 'package:coverage/src/util.dart' ;
1211import 'package:meta/meta.dart' ;
13- import 'package:package_config/package_config.dart' ;
1412import 'package:path/path.dart' as path;
1513
1614import 'collect_coverage.dart' as collect_coverage;
@@ -19,37 +17,36 @@ import 'format_coverage.dart' as format_coverage;
1917final _allProcesses = < Process > [];
2018
2119Future <void > _dartRun (List <String > args,
22- {void Function (String )? onStdout, String ? workingDir}) async {
23- final process = await Process .start (
24- Platform .executable,
25- args,
26- workingDirectory: workingDir,
27- );
20+ {required void Function (String ) onStdout,
21+ required void Function (String ) onStderr}) async {
22+ final process = await Process .start (Platform .executable, args);
2823 _allProcesses.add (process);
29- final broadStdout = process.stdout.asBroadcastStream ();
30- broadStdout.listen (stdout.add);
31- if (onStdout != null ) {
32- broadStdout.lines ().listen (onStdout);
24+
25+ void listen (
26+ Stream <List <int >> stream, IOSink sink, void Function (String ) onLine) {
27+ final broadStream = stream.asBroadcastStream ();
28+ broadStream.listen (sink.add);
29+ broadStream.lines ().listen (onLine);
3330 }
34- process.stderr.listen (stderr.add);
31+
32+ listen (process.stdout, stdout, onStdout);
33+ listen (process.stderr, stderr, onStderr);
34+
3535 final result = await process.exitCode;
3636 if (result != 0 ) {
3737 throw ProcessException (Platform .executable, args, '' , result);
3838 }
3939}
4040
41- Future <String ?> _packageNameFromConfig (String packageDir) async {
42- final config = await findPackageConfig (Directory (packageDir));
43- return config? .packageOf (Uri .directory (packageDir))? .name;
41+ void _killSubprocessesAndExit (ProcessSignal signal) {
42+ for (final process in _allProcesses) {
43+ process.kill (signal);
44+ }
45+ exit (1 );
4446}
4547
4648void _watchExitSignal (ProcessSignal signal) {
47- signal.watch ().listen ((sig) {
48- for (final process in _allProcesses) {
49- process.kill (sig);
50- }
51- exit (1 );
52- });
49+ signal.watch ().listen (_killSubprocessesAndExit);
5350}
5451
5552ArgParser _createArgParser (CoverageOptions defaultOptions) => ArgParser ()
@@ -61,10 +58,10 @@ ArgParser _createArgParser(CoverageOptions defaultOptions) => ArgParser()
6158 ..addOption (
6259 'package-name' ,
6360 help: 'Name of the package to test. '
64- 'Deduced from --package if not provided.' ,
65- defaultsTo : defaultOptions.packageName ,
61+ 'Deduced from --package if not provided. '
62+ 'DEPRECATED: use --scope-output' ,
6663 )
67- ..addOption ('port' , help: 'VM service port.' , defaultsTo : '8181 ' )
64+ ..addOption ('port' , help: 'VM service port. Defaults to using any free port. ' )
6865 ..addOption (
6966 'out' ,
7067 defaultsTo: defaultOptions.outputDirectory,
@@ -93,13 +90,13 @@ ArgParser _createArgParser(CoverageOptions defaultOptions) => ArgParser()
9390 defaultsTo: defaultOptions.scopeOutput,
9491 help: 'restrict coverage results so that only scripts that start with '
9592 'the provided package path are considered. Defaults to the name of '
96- 'the package under test.' )
93+ 'the current package (including all subpackages, if this is a '
94+ 'workspace).' )
9795 ..addFlag ('help' , abbr: 'h' , negatable: false , help: 'Show this help.' );
9896
9997class Flags {
10098 Flags (
10199 this .packageDir,
102- this .packageName,
103100 this .outDir,
104101 this .port,
105102 this .testScript,
@@ -111,7 +108,6 @@ class Flags {
111108 });
112109
113110 final String packageDir;
114- final String packageName;
115111 final String outDir;
116112 final String port;
117113 final String testScript;
@@ -157,25 +153,23 @@ ${parser.usage}
157153 fail ('--package is not a valid directory.' );
158154 }
159155
160- final packageName = (args['package-name' ] as String ? ) ??
161- await _packageNameFromConfig (packageDir);
162- if (packageName == null ) {
156+ final pubspecPath = getPubspecPath (packageDir);
157+ if (! File (pubspecPath).existsSync ()) {
163158 fail (
164- "Couldn't figure out package name from --package . Make sure this is a "
165- 'package directory, or try passing --package-name explicitly.' ,
159+ "Couldn't find $ pubspecPath . Make sure this command is run in a "
160+ 'package directory, or pass --package to explicitly set the directory .' ,
166161 );
167162 }
168163
169164 return Flags (
170165 packageDir,
171- packageName,
172- (args['out' ] as String ? ) ?? path.join (packageDir, 'coverage' ),
173- args['port' ] as String ,
174- args['test' ] as String ,
175- args['function-coverage' ] as bool ,
176- args['branch-coverage' ] as bool ,
177- args['scope-output' ] as List <String >,
178- args['fail-under' ] as String ? ,
166+ args.option ('out' ) ?? path.join (packageDir, 'coverage' ),
167+ args.option ('port' ) ?? '0' ,
168+ args.option ('test' )! ,
169+ args.flag ('function-coverage' ),
170+ args.flag ('branch-coverage' ),
171+ args.multiOption ('scope-output' ),
172+ args.option ('fail-under' ),
179173 rest: args.rest,
180174 );
181175}
@@ -215,11 +209,19 @@ Future<void> main(List<String> arguments) async {
215209 }
216210 }
217211 },
212+ onStderr: (line) {
213+ if (! serviceUriCompleter.isCompleted) {
214+ if (line.contains ('Could not start the VM service' )) {
215+ _killSubprocessesAndExit (ProcessSignal .sigkill);
216+ }
217+ }
218+ },
218219 );
219220 final serviceUri = await serviceUriCompleter.future;
220221
221- final scopes =
222- flags.scopeOutput.isEmpty ? [flags.packageName] : flags.scopeOutput;
222+ final scopes = flags.scopeOutput.isEmpty
223+ ? getAllWorkspaceNames (flags.packageDir)
224+ : flags.scopeOutput;
223225 await collect_coverage.main ([
224226 '--wait-paused' ,
225227 '--resume-isolates' ,
0 commit comments