Skip to content

Commit

Permalink
Throw explicit exception if experimental_split_coverage_postprocessin…
Browse files Browse the repository at this point in the history
…g is used without experimental_fetch_all_coverage_outputs

This closes bazelbuild#13185
According to bazelbuild@a445cda,
The flag --experimental_split_coverage_postprocessing depends on the flag
--experimental_fetch_all_coverage_outputs being enabled, but if the former one
is set without the latter one, Bazel throws a quite confusing NullPointerException.
Now we throw an explicit exception with proper error message.
  • Loading branch information
blindpirate committed Oct 5, 2022
1 parent c3425fe commit 73a8d6c
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -102,14 +102,10 @@ public TestRunnerSpawn createTestRunnerSpawn(
TestRunnerAction action, ActionExecutionContext actionExecutionContext)
throws ExecException, InterruptedException {
if (action.getExecutionSettings().getInputManifest() == null) {
String errorMessage = "cannot run local tests with --nobuild_runfile_manifests";
throw new TestExecException(
errorMessage,
FailureDetail.newBuilder()
.setTestAction(
TestAction.newBuilder().setCode(TestAction.Code.LOCAL_TEST_PREREQ_UNMET))
.setMessage(errorMessage)
.build());
throw createTestExecException(
TestAction.Code.LOCAL_TEST_PREREQ_UNMET,
"cannot run local tests with --nobuild_runfile_manifests"
);
}
Map<String, String> testEnvironment =
createEnvironment(
Expand Down Expand Up @@ -641,6 +637,16 @@ public void finalizeCancelledTest(List<FailedAttemptResult> failedAttempts) thro
}
}

private static TestExecException createTestExecException(TestAction.Code errorCode, String errorMessage) {
return new TestExecException(
errorMessage,
FailureDetail.newBuilder()
.setTestAction(TestAction.newBuilder().setCode(errorCode))
.setMessage(errorMessage)
.build()
);
}

private final class BazelTestAttemptContinuation extends TestAttemptContinuation {
private final TestRunnerAction testAction;
private final ActionExecutionContext actionExecutionContext;
Expand Down Expand Up @@ -760,6 +766,16 @@ public TestAttemptContinuation execute()
.setHasCoverage(testAction.isCoverageMode());

if (testAction.isCoverageMode() && testAction.getSplitCoveragePostProcessing()) {
if (testAction.getCoverageDirectoryTreeArtifact() == null) {
// Otherwise we'll get a NPE https://github.com/bazelbuild/bazel/issues/13185
TestExecException e = createTestExecException(
TestAction.Code.LOCAL_TEST_PREREQ_UNMET,
"coverageDirectoryTreeArtifact is null: --experimental_split_coverage_postprocessing depends on --experimental_fetch_all_coverage_outputs being enabled"
);
closeSuppressed(e, streamed);
closeSuppressed(e, fileOutErr);
throw e;
}
actionExecutionContext
.getMetadataHandler()
.getMetadata(testAction.getCoverageDirectoryTreeArtifact());
Expand Down
26 changes: 26 additions & 0 deletions src/test/shell/bazel/bazel_coverage_cc_test_gcc.sh
Original file line number Diff line number Diff line change
Expand Up @@ -875,6 +875,32 @@ EOF
assert_contains 'name: "test.lcov"' bep.txt
}

function test_coverage_with_experimental_split_coverage_postprocessing_only() {
local -r LCOV=$(which lcov)
if [[ ! -x ${LCOV:-/usr/bin/lcov} ]]; then
echo "lcov not installed. Skipping test."
return
fi

cat << EOF > BUILD
cc_test(
name = "hello-test",
srcs = ["hello-test.cc"],
)
EOF


cat << EOF > hello-test.cc
int main() {
return 0;
}
EOF
bazel coverage --test_output=all --experimental_split_coverage_postprocessing //:hello-test \
&>$TEST_log && fail "Expected test failure" || :

assert_contains '--experimental_split_coverage_postprocessing depends on --experimental_fetch_all_coverage_outputs being enabled' $TEST_log
}

function test_coverage_doesnt_fail_on_empty_output() {
if is_gcov_missing_or_wrong_version; then
echo "Skipping test." && return
Expand Down

0 comments on commit 73a8d6c

Please sign in to comment.