Skip to content

Commit

Permalink
Delay configuration of buf binary configuration until after the tool …
Browse files Browse the repository at this point in the history
…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.
  • Loading branch information
andrewparmet authored Jul 31, 2023
1 parent 8dbb4ac commit 626514c
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/main/kotlin/build/buf/gradle/BufPlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ class BufPlugin : Plugin<Project> {
}

private fun Project.configureBuf() {
configureBufDependency()
configureLint()
configureFormat()
configureBuild()
configureGenerate()

afterEvaluate {
configureBufDependency()
getArtifactDetails()?.let {
if (publishSchema()) {
configureImagePublication(it)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ internal fun Task.execBufInSpecificDirectory(
internal fun Task.execBufInSpecificDirectory(
bufCommand: String,
extraArgs: Iterable<String>,
customErrorMessage: ((String) -> String),
customErrorMessage: (String) -> String,
) {
execBufInSpecificDirectory(listOf(bufCommand), extraArgs, customErrorMessage)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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("--", "")
Expand Down
29 changes: 29 additions & 0 deletions src/test/kotlin/build/buf/gradle/BufVersionTest.kt
Original file line number Diff line number Diff line change
@@ -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")
}
}
Original file line number Diff line number Diff line change
@@ -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}")
}
}

0 comments on commit 626514c

Please sign in to comment.