From 87cc92e5df35d02a7c9bc50b229c513563dc1689 Mon Sep 17 00:00:00 2001 From: Klaus Aehlig Date: Tue, 12 Sep 2017 14:42:27 +0200 Subject: [PATCH] BEP: always close stream, even if aborted due to missing tests If testing is requested, but no tests are found, the build is aborted. Ensure that even in this case the steam of build events is internally closed (i.e., all announced events are reported, the last event is marked as such). Change-Id: I88763ed6ccd7793deedbcb3428df7e8d289efa23 PiperOrigin-RevId: 168364127 --- .../build/lib/runtime/BuildEventStreamer.java | 6 ++++++ .../lib/runtime/commands/NoTestsFound.java | 18 ++++++++++++++++++ .../lib/runtime/commands/TestCommand.java | 1 + .../integration/build_event_stream_test.sh | 13 +++++++++++++ 4 files changed, 38 insertions(+) create mode 100644 src/main/java/com/google/devtools/build/lib/runtime/commands/NoTestsFound.java diff --git a/src/main/java/com/google/devtools/build/lib/runtime/BuildEventStreamer.java b/src/main/java/com/google/devtools/build/lib/runtime/BuildEventStreamer.java index 069044cbe43b47..189ce3fb069f8d 100644 --- a/src/main/java/com/google/devtools/build/lib/runtime/BuildEventStreamer.java +++ b/src/main/java/com/google/devtools/build/lib/runtime/BuildEventStreamer.java @@ -57,6 +57,7 @@ import com.google.devtools.build.lib.events.Event; import com.google.devtools.build.lib.events.EventHandler; import com.google.devtools.build.lib.events.Reporter; +import com.google.devtools.build.lib.runtime.commands.NoTestsFound; import com.google.devtools.build.lib.util.Pair; import java.util.ArrayList; import java.util.Collection; @@ -373,6 +374,11 @@ public void buildInterrupted(BuildInterruptedEvent event) { abortReason = AbortReason.USER_INTERRUPTED; } + @Subscribe + public void noTestsFound(NoTestsFound event) { + buildComplete(); + } + @Subscribe public void buildEvent(BuildEvent event) { if (isActionWithoutError(event) diff --git a/src/main/java/com/google/devtools/build/lib/runtime/commands/NoTestsFound.java b/src/main/java/com/google/devtools/build/lib/runtime/commands/NoTestsFound.java new file mode 100644 index 00000000000000..ba2d3b98efc9b4 --- /dev/null +++ b/src/main/java/com/google/devtools/build/lib/runtime/commands/NoTestsFound.java @@ -0,0 +1,18 @@ +// Copyright 2014 The Bazel Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package com.google.devtools.build.lib.runtime.commands; + +/** This event is posted by the {@link TestCommand} if no tests were found. */ +public class NoTestsFound {} diff --git a/src/main/java/com/google/devtools/build/lib/runtime/commands/TestCommand.java b/src/main/java/com/google/devtools/build/lib/runtime/commands/TestCommand.java index 2de82d76390942..db2797d3cdb50c 100644 --- a/src/main/java/com/google/devtools/build/lib/runtime/commands/TestCommand.java +++ b/src/main/java/com/google/devtools/build/lib/runtime/commands/TestCommand.java @@ -137,6 +137,7 @@ private ExitCode doTest(CommandEnvironment env, if (testTargets.isEmpty()) { env.getReporter().handle(Event.error( null, "No test targets were found, yet testing was requested")); + env.getEventBus().post(new NoTestsFound()); return buildResult.getSuccess() ? ExitCode.NO_TESTS_FOUND : buildResult.getExitCondition(); } diff --git a/src/test/shell/integration/build_event_stream_test.sh b/src/test/shell/integration/build_event_stream_test.sh index 1bc721da6fcd2f..0a96bc805848da 100755 --- a/src/test/shell/integration/build_event_stream_test.sh +++ b/src/test/shell/integration/build_event_stream_test.sh @@ -101,6 +101,11 @@ filegroup( name = "outergroup", srcs = ["sourcefileC", ":innergroup"], ) +genrule( + name = "not_a_test", + outs = ["not_a_test.txt"], + cmd = "touch $@", +) EOF cat > simpleaspect.bzl <