From d98815d5471cdc06ba8b57a8b8541808789721aa Mon Sep 17 00:00:00 2001 From: Andrew Parmet Date: Fri, 20 Sep 2024 13:50:03 -0400 Subject: [PATCH 1/2] improve error message when task with custom error handler fails with no stdout --- .../kotlin/build/buf/gradle/BufSupport.kt | 35 +++++++++++------ .../kotlin/build/buf/gradle/BufSupportTest.kt | 38 +++++++++++++++++++ 2 files changed, 62 insertions(+), 11 deletions(-) create mode 100644 src/test/kotlin/build/buf/gradle/BufSupportTest.kt diff --git a/src/main/kotlin/build/buf/gradle/BufSupport.kt b/src/main/kotlin/build/buf/gradle/BufSupport.kt index a4da9ea1..ef408e61 100644 --- a/src/main/kotlin/build/buf/gradle/BufSupport.kt +++ b/src/main/kotlin/build/buf/gradle/BufSupport.kt @@ -16,6 +16,7 @@ package build.buf.gradle import org.gradle.api.Project import org.gradle.kotlin.dsl.dependencies +import org.jetbrains.annotations.VisibleForTesting import java.nio.charset.StandardCharsets const val BUF_BINARY_CONFIGURATION_NAME = "bufTool" @@ -80,24 +81,36 @@ internal fun AbstractBufExecTask.execBuf( logger.info("Running buf from $workingDirValue: `buf ${args.joinToString(" ")}`") val result = ProcessRunner().use { it.shell(workingDirValue, processArgs) } + handleResult(result, customErrorMessage) +} + +internal fun AbstractBufExecTask.obtainDefaultProtoFileSet() = + project.fileTree(workingDir.get()) { + include("**/*.proto") + // not to interfere with random plugins producing output to build dir + exclude("build") + } + +@VisibleForTesting +internal fun handleResult( + result: ProcessRunner.Result, + customErrorMessage: ((String) -> String)?, +) { if (result.exitCode != 0) { if (customErrorMessage != null) { val stdOut = result.stdOut.toString(StandardCharsets.UTF_8) val stdErr = result.stdErr.toString(StandardCharsets.UTF_8) - val ex = IllegalStateException(customErrorMessage(stdOut)) - if (stdErr.isNotEmpty()) { - ex.addSuppressed(IllegalStateException(result.toString())) + if (stdOut.isEmpty()) { + error(result.toString()) + } else { + val ex = IllegalStateException(customErrorMessage(stdOut)) + if (stdErr.isNotEmpty()) { + ex.addSuppressed(IllegalStateException(result.toString())) + } + throw ex } - throw ex } else { error(result.toString()) } } } - -internal fun AbstractBufExecTask.obtainDefaultProtoFileSet() = - project.fileTree(workingDir.get()) { - include("**/*.proto") - // not to interfere with random plugins producing output to build dir - exclude("build") - } diff --git a/src/test/kotlin/build/buf/gradle/BufSupportTest.kt b/src/test/kotlin/build/buf/gradle/BufSupportTest.kt new file mode 100644 index 00000000..df10e4f6 --- /dev/null +++ b/src/test/kotlin/build/buf/gradle/BufSupportTest.kt @@ -0,0 +1,38 @@ +// Copyright 2024 Buf Technologies, Inc. +// +// 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 build.buf.gradle + +import com.google.common.truth.Truth.assertThat +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.assertThrows + +class BufSupportTest { + @Test + fun `handleResult includes stderr when stdout is empty and custom error message is specified`() { + val thrown = + assertThrows { + handleResult( + ProcessRunner.Result( + emptyList(), + 1, + "".toByteArray(), + "error that would be hidden".toByteArray(), + ), + ) { "foo" } + } + + assertThat(thrown).hasMessageThat().contains("error that would be hidden") + } +} From 1e613e61ff02bae2818ae5927fe684c6c2da7e6f Mon Sep 17 00:00:00 2001 From: Andrew Parmet Date: Fri, 20 Sep 2024 13:52:39 -0400 Subject: [PATCH 2/2] fix git --- src/main/kotlin/build/buf/gradle/BufSupport.kt | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/main/kotlin/build/buf/gradle/BufSupport.kt b/src/main/kotlin/build/buf/gradle/BufSupport.kt index ef408e61..14287ff5 100644 --- a/src/main/kotlin/build/buf/gradle/BufSupport.kt +++ b/src/main/kotlin/build/buf/gradle/BufSupport.kt @@ -84,13 +84,6 @@ internal fun AbstractBufExecTask.execBuf( handleResult(result, customErrorMessage) } -internal fun AbstractBufExecTask.obtainDefaultProtoFileSet() = - project.fileTree(workingDir.get()) { - include("**/*.proto") - // not to interfere with random plugins producing output to build dir - exclude("build") - } - @VisibleForTesting internal fun handleResult( result: ProcessRunner.Result, @@ -114,3 +107,10 @@ internal fun handleResult( } } } + +internal fun AbstractBufExecTask.obtainDefaultProtoFileSet() = + project.fileTree(workingDir.get()) { + include("**/*.proto") + // not to interfere with random plugins producing output to build dir + exclude("build") + }