From 626514cdf18ff6a32bcea41852b2041168aa132d Mon Sep 17 00:00:00 2001 From: Andrew Parmet Date: Mon, 31 Jul 2023 13:10:59 -0400 Subject: [PATCH] Delay configuration of buf binary configuration until after the tool version is resolvable from the extension (#148) See the (formerly) failing test - `configureBufDependency` grabs the `toolVersion` from the extension at plugin application time today, but this needs to be done after project evaluation in order to get the configured version. --- src/main/kotlin/build/buf/gradle/BufPlugin.kt | 2 +- .../DirectorySpecificBufExecutionSupport.kt | 2 +- .../buf/gradle/AbstractBufIntegrationTest.kt | 3 -- .../kotlin/build/buf/gradle/BufVersionTest.kt | 29 +++++++++++++++++++ .../build.gradle | 16 ++++++++++ 5 files changed, 47 insertions(+), 5 deletions(-) create mode 100644 src/test/kotlin/build/buf/gradle/BufVersionTest.kt create mode 100644 src/test/resources/BufVersionTest/buf_version_can_be_specified_by_the_extension/build.gradle diff --git a/src/main/kotlin/build/buf/gradle/BufPlugin.kt b/src/main/kotlin/build/buf/gradle/BufPlugin.kt index dfe9c127..9a63b182 100644 --- a/src/main/kotlin/build/buf/gradle/BufPlugin.kt +++ b/src/main/kotlin/build/buf/gradle/BufPlugin.kt @@ -35,13 +35,13 @@ class BufPlugin : Plugin { } private fun Project.configureBuf() { - configureBufDependency() configureLint() configureFormat() configureBuild() configureGenerate() afterEvaluate { + configureBufDependency() getArtifactDetails()?.let { if (publishSchema()) { configureImagePublication(it) diff --git a/src/main/kotlin/build/buf/gradle/DirectorySpecificBufExecutionSupport.kt b/src/main/kotlin/build/buf/gradle/DirectorySpecificBufExecutionSupport.kt index ae52d4b9..e4f7c3d3 100644 --- a/src/main/kotlin/build/buf/gradle/DirectorySpecificBufExecutionSupport.kt +++ b/src/main/kotlin/build/buf/gradle/DirectorySpecificBufExecutionSupport.kt @@ -27,7 +27,7 @@ internal fun Task.execBufInSpecificDirectory( internal fun Task.execBufInSpecificDirectory( bufCommand: String, extraArgs: Iterable, - customErrorMessage: ((String) -> String), + customErrorMessage: (String) -> String, ) { execBufInSpecificDirectory(listOf(bufCommand), extraArgs, customErrorMessage) } diff --git a/src/test/kotlin/build/buf/gradle/AbstractBufIntegrationTest.kt b/src/test/kotlin/build/buf/gradle/AbstractBufIntegrationTest.kt index 160834ee..0e56dfc5 100644 --- a/src/test/kotlin/build/buf/gradle/AbstractBufIntegrationTest.kt +++ b/src/test/kotlin/build/buf/gradle/AbstractBufIntegrationTest.kt @@ -39,9 +39,6 @@ abstract class AbstractBufIntegrationTest : IntegrationTest { File(projectDir, "settings.gradle").writeText("rootProject.name = 'testing'") File(projectDir, "gradle.properties").writeText("org.gradle.jvmargs=-Xmx5g") - // TODO: The test name is dependent on the directory name. - // There is a change to sanitize the directory names to be compatible - // with Buf's license-header tool. val testName = testInfo.testMethod.get().name .replace(",", "") .replace("--", "") diff --git a/src/test/kotlin/build/buf/gradle/BufVersionTest.kt b/src/test/kotlin/build/buf/gradle/BufVersionTest.kt new file mode 100644 index 00000000..54987568 --- /dev/null +++ b/src/test/kotlin/build/buf/gradle/BufVersionTest.kt @@ -0,0 +1,29 @@ +// Copyright 2023 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 + +class BufVersionTest : AbstractBufIntegrationTest() { + @Test + fun `buf version can be specified by the extension`() { + val result = gradleRunner().withArguments("printBufVersion", "-PbufVersion=asdf").build() + + val versionLine = result.output.lines().single { it.startsWith("Resolved") } + + assertThat(versionLine).isEqualTo("Resolved Buf tool version: asdf") + } +} diff --git a/src/test/resources/BufVersionTest/buf_version_can_be_specified_by_the_extension/build.gradle b/src/test/resources/BufVersionTest/buf_version_can_be_specified_by_the_extension/build.gradle new file mode 100644 index 00000000..33f12f37 --- /dev/null +++ b/src/test/resources/BufVersionTest/buf_version_can_be_specified_by_the_extension/build.gradle @@ -0,0 +1,16 @@ +import static build.buf.gradle.BufSupportKt.BUF_BINARY_CONFIGURATION_NAME + +plugins { + id "build.buf" +} + +buf { + toolVersion = "$bufVersion" +} + +tasks.register("printBufVersion") { + def bufConfiguration = project.configurations.named(BUF_BINARY_CONFIGURATION_NAME).get() + bufConfiguration.dependencies.forEach { + logger.quiet("Resolved Buf tool version: ${it.version}") + } +}