Revisit plugin test target detection

The way it is currently implemented to detect test targets is error
prone. In case of find-owners plugin there are two test targets:

$ grep -2 junit_tests BUILD | grep -o 'name = "[^"]*"' | cut -d '"' -f 2
findowners_junit_tests
findowners_IT_tests

But the code assumes that there are only one single target:

if [ "$TEST_TARGET" != "" ]
then
  bazelisk test --test_env DOCKER_HOST=$DOCKER_HOST plugins/{name}:$TEST_TARGET
fi

And this is why the second target findowners_IT_tests gets orphaned and
is not prefixed with "plugins/{name}" prefix and thus not found.

Moreover, this approach to grep for targets can also lead to false
negative results, because the test rules might be located in nested
plugin packages but not in the root package, like it is the case for
checks plugin. For such plugins no test are executed at all.

To rectify, avoid grep and just use Bazel's native wildcard "/..."
target feature, that should work in all three possible cases:

1. no test rule exist
2. one single test rule exsits in plugin's root package
3. many test rules exist in root and/or nested packages

This approach has problem with exit code of bazel in case that there are
no tests even thoufg test run was requested.

As documented in the user guide: [1], it is expected that Bazel
unconditionally returns exit code 4 in this situation. Check for that
exit code explicitly and warn CI users to contact plugin maintainers if
there were no tests found. Let's face it: the code is inherently broken
if there are no tests at all. Still we would like to avoid the
boilerplate to be needed and wrote this feature request: [2].

[1] https://docs.bazel.build/versions/master/guide.html#what-exit-code-will-i-get
[2] https://github.com/bazelbuild/bazel/issues/11465

Bug: Issue 12512
Change-Id: Id641cfd2300939d3eac5adb4cdc4a14b12d5f7c2
diff --git a/jenkins/gerrit-bazel-build-account-plugin.sh b/jenkins/gerrit-bazel-build-account-plugin.sh
index a2fd971..3cbf149 100644
--- a/jenkins/gerrit-bazel-build-account-plugin.sh
+++ b/jenkins/gerrit-bazel-build-account-plugin.sh
@@ -11,7 +11,6 @@
 fi
 
 TARGETS=$(echo "plugins/account:account" | sed -e 's/account/account/g')
-TEST_TARGET=$(grep -2 junit_tests plugins/account/BUILD | grep -o 'name = "[^"]*"' | cut -d '"' -f 2)
 
 . set-java.sh 8
 
@@ -30,9 +29,18 @@
 bazelisk version
 bazelisk build --spawn_strategy=standalone --genrule_strategy=standalone $TARGETS
 
-if [ "$TEST_TARGET" != "" ]
+echo 'Running tests...'
+set +e
+bazelisk test --test_env DOCKER_HOST=$DOCKER_HOST plugins/account/...
+TEST_RES=$?
+set -e
+if [ $TEST_RES -eq 4 ]
 then
-    bazelisk test --test_env DOCKER_HOST=$DOCKER_HOST plugins/account:$TEST_TARGET
+    echo 'No tests found for account plugin (tell this to the plugin maintainers?).'
+elif [ ! $TEST_RES -eq 0 ]
+then
+    echo 'Tests failed'
+    exit 1
 fi
 
 for JAR in $(find bazel-bin/plugins/account -name account*.jar)
diff --git a/jenkins/gerrit-bazel-build-its-plugin-branch.sh b/jenkins/gerrit-bazel-build-its-plugin-branch.sh
index 9556622..0f8bae0 100644
--- a/jenkins/gerrit-bazel-build-its-plugin-branch.sh
+++ b/jenkins/gerrit-bazel-build-its-plugin-branch.sh
@@ -20,7 +20,6 @@
 fi
 
 TARGETS=$(echo "{targets}" | sed -e 's/its-{{name}}/its-{name}/g')
-TEST_TARGET=$(grep -2 junit_tests plugins/its-{name}/BUILD | grep -o 'name = "[^"]*"' | cut -d '"' -f 2)
 
 . set-java.sh 8
 
@@ -28,9 +27,18 @@
 bazelisk version
 bazelisk build --spawn_strategy=standalone --genrule_strategy=standalone $TARGETS
 
-if [ "$TEST_TARGET" != "" ]
+echo 'Running tests...'
+set +e
+bazelisk test --test_env DOCKER_HOST=$DOCKER_HOST plugins/its-{name}/...
+TEST_RES=$?
+set -e
+if [ $TEST_RES -eq 4 ]
 then
-    bazelisk test --test_env DOCKER_HOST=$DOCKER_HOST plugins/its-{name}:$TEST_TARGET
+    echo 'No tests found for its plugin (tell this to the plugin maintainers?).'
+elif [ ! $TEST_RES -eq 0 ]
+then
+    echo 'Tests failed'
+    exit 1
 fi
 
 for JAR in $(find bazel-bin/plugins/its-{name} -name its-{name}*.jar)
diff --git a/jenkins/gerrit-bazel-build-its-plugin.sh b/jenkins/gerrit-bazel-build-its-plugin.sh
index 92dca88..39fc2a4 100644
--- a/jenkins/gerrit-bazel-build-its-plugin.sh
+++ b/jenkins/gerrit-bazel-build-its-plugin.sh
@@ -16,7 +16,6 @@
 fi
 
 TARGETS=$(echo "{targets}" | sed -e 's/its-{{name}}/its-{name}/g')
-TEST_TARGET=$(grep -2 junit_tests plugins/its-{name}/BUILD | grep -o 'name = "[^"]*"' | cut -d '"' -f 2)
 
 . set-java.sh 8
 
@@ -24,9 +23,18 @@
 bazelisk version
 bazelisk build --spawn_strategy=standalone --genrule_strategy=standalone $TARGETS
 
-if [ "$TEST_TARGET" != "" ]
+echo 'Running tests...'
+set +e
+bazelisk test --test_env DOCKER_HOST=$DOCKER_HOST plugins/its-{name}/...
+TEST_RES=$?
+set -e
+if [ $TEST_RES -eq 4 ]
 then
-    bazelisk test --test_env DOCKER_HOST=$DOCKER_HOST plugins/its-{name}:$TEST_TARGET
+    echo 'No tests found for its plugin (tell this to the plugin maintainers?).'
+elif [ ! $TEST_RES -eq 0 ]
+then
+    echo 'Tests failed'
+    exit 1
 fi
 
 for JAR in $(find bazel-bin/plugins/its-{name} -name its-{name}*.jar)
diff --git a/jenkins/gerrit-bazel-build-plugin-branch.sh b/jenkins/gerrit-bazel-build-plugin-branch.sh
index c51e794..7892d5e 100644
--- a/jenkins/gerrit-bazel-build-plugin-branch.sh
+++ b/jenkins/gerrit-bazel-build-plugin-branch.sh
@@ -17,22 +17,30 @@
 fi
 
 TARGETS=$(echo "{targets}" | sed -e 's/{{name}}/{name}/g')
-BUILD_TARGETS=$(echo "$TARGETS" | tr ' ' '\n' | grep -v test)
-
 java -fullversion
 bazelisk version
-bazelisk build $BAZEL_OPTS --spawn_strategy=standalone --genrule_strategy=standalone $BUILD_TARGETS
+bazelisk build $BAZEL_OPTS --spawn_strategy=standalone --genrule_strategy=standalone $TARGETS
 
-if TEST_TARGETS=$(echo "$TARGETS" | tr ' ' '\n' | grep test)
-then
-    BAZEL_OPTS="$BAZEL_OPTS --spawn_strategy=standalone --genrule_strategy=standalone \
+BAZEL_OPTS="$BAZEL_OPTS --spawn_strategy=standalone --genrule_strategy=standalone \
                    --test_output errors \
                    --test_summary detailed --flaky_test_attempts 3 \
                    --test_verbose_timeout_warnings --build_tests_only \
                    --test_timeout 3600 \
                    --test_tag_filters=-flaky \
                    --test_env DOCKER_HOST=$DOCKER_HOST"
-    bazelisk test $BAZEL_OPTS $TEST_TARGETS
+
+echo 'Running tests...'
+set +e
+bazelisk test $BAZEL_OPTS plugins/{name}/...
+TEST_RES=$?
+set -e
+if [ $TEST_RES -eq 4 ]
+then
+    echo 'No tests found for this plugin (tell this to the plugin maintainers?).'
+elif [ ! $TEST_RES -eq 0 ]
+then
+    echo 'Tests failed'
+    exit 1
 fi
 
 for JAR in $(find bazel-bin/plugins/{name} -name {name}*.jar)
diff --git a/jenkins/gerrit-bazel-build-plugin.sh b/jenkins/gerrit-bazel-build-plugin.sh
index f68b48b..f470616 100644
--- a/jenkins/gerrit-bazel-build-plugin.sh
+++ b/jenkins/gerrit-bazel-build-plugin.sh
@@ -12,17 +12,24 @@
 fi
 
 TARGETS=$(echo "{targets}" | sed -e 's/{{name}}/{name}/g')
-TEST_TARGET=$(grep -2 junit_tests plugins/{name}/BUILD | grep -o 'name = "[^"]*"' | cut -d '"' -f 2)
-
 . set-java.sh 8
 
 java -fullversion
 bazelisk version
 bazelisk build --spawn_strategy=standalone --genrule_strategy=standalone $TARGETS
 
-if [ "$TEST_TARGET" != "" ]
+echo 'Running tests...'
+set +e
+bazelisk test --test_env DOCKER_HOST=$DOCKER_HOST plugins/{name}/...
+TEST_RES=$?
+set -e
+if [ $TEST_RES -eq 4 ]
 then
-    bazelisk test --test_env DOCKER_HOST=$DOCKER_HOST plugins/{name}:$TEST_TARGET
+    echo 'No tests found for this plugin (tell this to the plugin maintainers?).'
+elif [ ! $TEST_RES -eq 0 ]
+then
+    echo 'Tests failed'
+    exit 1
 fi
 
 for JAR in $(find bazel-bin/plugins/{name} -name {name}*.jar)