@@ -963,17 +963,37 @@ class TestCommand extends ProductCommand {
963963 Future <int > doit () async {
964964 final javaHome = Platform .environment['JAVA_HOME' ];
965965 if (javaHome == null ) {
966- log ('JAVA_HOME environment variable not set - this is needed by gradle.' );
966+ log ('ERROR: JAVA_HOME environment variable not set - this is needed by gradle.' );
967967 return 1 ;
968968 }
969969
970970 log ('JAVA_HOME=$javaHome ' );
971971
972- final spec = specs.firstWhere ((s) => s.isUnitTestTarget);
973- if (! argResults! .flag ('skip' )) {
974- return await _runUnitTests (spec);
972+ // Case 1: Handle skipping tests
973+ if (argResults != null && argResults! .flag ('skip' )) {
974+ log ('Skipping unit tests as requested.' );
975+ return 0 ;
975976 }
976- return 0 ;
977+
978+ // Filter for all unit test targets
979+ final unitTestTargets = specs.where ((s) => s.isUnitTestTarget).toList ();
980+
981+ // Case 2: Zero unit test targets
982+ if (unitTestTargets.isEmpty) {
983+ log ('ERROR: No unit test target found in the specifications. Cannot run tests.' );
984+ return 1 ;
985+ }
986+
987+ // Case 3: More than one unit test target
988+ if (unitTestTargets.length > 1 ) {
989+ final targetNames = unitTestTargets.map ((s) => s.name).join (', ' );
990+ log ('ERROR: More than one unit test target found: $targetNames . Please specify which one to run, or ensure only one exists.' );
991+ return 1 ;
992+ }
993+
994+ // Happy Case: Exactly one unit test target
995+ final spec = unitTestTargets.first;
996+ return await _runUnitTests (spec);
977997 }
978998
979999 Future <int > _runUnitTests (BuildSpec spec) async {
0 commit comments