@@ -963,17 +963,37 @@ class TestCommand extends ProductCommand {
963
963
Future <int > doit () async {
964
964
final javaHome = Platform .environment['JAVA_HOME' ];
965
965
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.' );
967
967
return 1 ;
968
968
}
969
969
970
970
log ('JAVA_HOME=$javaHome ' );
971
971
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 ;
975
976
}
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);
977
997
}
978
998
979
999
Future <int > _runUnitTests (BuildSpec spec) async {
0 commit comments