Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add v2 buf.yaml Generate and Breaking test cases #214

Merged
merged 9 commits into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ This plugin supports straightforward usage of `buf lint`, `buf format`, and `buf

## Usage

This plugin assumes that Buf is configured for the project root with a configured `buf.work.yaml`, and instructions for setting up a Buf workspace can be found in the [Buf docs][buf-docs].
This plugin assumes that Buf is configured for the project root with a configured `buf.yaml`, and instructions for setting up a Buf workspace can be found in the [Buf docs][buf-docs].

The plugin can also be used without specifying a `buf.work.yaml`, in which case the plugin will scan all top-level directories for Protobuf sources.
The plugin can also be used without specifying a `buf.yaml`, in which case the plugin will scan all top-level directories for Protobuf sources.

If the project includes the `protobuf-gradle-plugin`, this plugin will use an implicit Buf workspace that includes the following:
- All specified Protobuf source directories
Expand Down Expand Up @@ -60,7 +60,7 @@ For a basic Buf project or one that uses the `protobuf-gradle-plugin`, you can c
``` yaml
# buf.yaml

version: v1
version: v2
lint:
ignore:
- path/to/dir/to/ignore
Expand Down Expand Up @@ -125,7 +125,7 @@ If your `buf.yaml` declares any dependencies using the `deps` key, you must run
An example, for Java code generation using the remote plugin:

``` yaml
version: v1
version: v2
plugins:
- plugin: buf.build/protocolbuffers/java:<version>
out: java
Expand Down
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ dependencies {

implementation(libs.jacksonDataformatYaml)
implementation(libs.jacksonModuleKotlin)
implementation(libs.versioncompare)

testImplementation(libs.junit)
testImplementation(libs.truth)
Expand Down
2 changes: 2 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ buildConfig = "5.4.0"
# runtime
bufbuild = "1.35.0"
jackson = "2.17.2"
versioncompare = "1.5.0"

# test
junit = "5.10.3"
Expand All @@ -26,6 +27,7 @@ spotless = { id = "com.diffplug.spotless", version.ref = "spotless" }
bufbuild = { module = "build.buf:buf", version.ref = "bufbuild" }
jacksonDataformatYaml = { module = "com.fasterxml.jackson.dataformat:jackson-dataformat-yaml", version.ref = "jackson" }
jacksonModuleKotlin = { module = "com.fasterxml.jackson.module:jackson-module-kotlin", version.ref = "jackson" }
versioncompare = { module = "io.github.g00fy2:versioncompare", version.ref = "versioncompare" }

# test
junit = { module = "org.junit.jupiter:junit-jupiter", version.ref = "junit" }
Expand Down
14 changes: 8 additions & 6 deletions src/main/kotlin/build/buf/gradle/BreakingTask.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@ import org.gradle.api.tasks.TaskAction
abstract class BreakingTask : DefaultTask() {
@TaskAction
fun bufBreaking() {
execBuf(
"breaking",
bufBuildPublicationFile,
"--against",
singleFileFromConfiguration(BUF_BREAKING_CONFIGURATION_NAME),
) {
val args = mutableListOf<Any>()
args.add("breaking")
if (project.bufV1SyntaxOnly()) {
args.add(bufBuildPublicationFile)
}
args.add("--against")
args.add(singleFileFromConfiguration(BUF_BREAKING_CONFIGURATION_NAME))
execBuf(*args.toTypedArray()) {
"""
|Some Protobuf files had breaking changes:
|$it
Expand Down
30 changes: 26 additions & 4 deletions src/main/kotlin/build/buf/gradle/ProtobufGradlePluginSupport.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

package build.buf.gradle

import io.github.g00fy2.versioncompare.Version
import org.gradle.api.DefaultTask
import org.gradle.api.Project
import org.gradle.api.Task
Expand All @@ -36,6 +37,8 @@ import kotlin.reflect.full.declaredMemberProperties
const val CREATE_SYM_LINKS_TO_MODULES_TASK_NAME = "createSymLinksToModules"
const val WRITE_WORKSPACE_YAML_TASK_NAME = "writeWorkspaceYaml"

internal const val BUF_CLI_V2_INITIAL_VERSION = "1.32.0"

private val BUILD_EXTRACTED_INCLUDE_PROTOS_MAIN =
listOf("build", "extracted-include-protos", "main").joinToString(File.separator)

Expand All @@ -44,6 +47,8 @@ private val BUILD_EXTRACTED_PROTOS_MAIN =

internal fun Project.hasProtobufGradlePlugin() = pluginManager.hasPlugin("com.google.protobuf")

internal fun Project.bufV1SyntaxOnly() = Version(getExtension().toolVersion) < Version(BUF_CLI_V2_INITIAL_VERSION)

internal fun Project.withProtobufGradlePlugin(action: (AppliedPlugin) -> Unit) = pluginManager.withPlugin("com.google.protobuf", action)

internal fun Project.configureCreateSymLinksToModules() {
Expand Down Expand Up @@ -79,10 +84,22 @@ abstract class WriteWorkspaceYamlTask : DefaultTask() {

@TaskAction
fun writeWorkspaceYaml() {
val protoDirs = allProtoDirs().map { project.makeMangledRelativizedPathStr(it) }
val bufYaml = bufYamlGenerator.generate(project.bufConfigFile(), protoDirs)
logger.info("Writing generated buf.yaml:{}\n", bufYaml)
File(bufbuildDir, "buf.yaml").writeText(bufYaml)
if (project.bufV1SyntaxOnly()) {
val bufWork =
"""
|version: v1
|directories:
${workspaceSymLinkEntries()}
""".trimMargin()

logger.info("Writing generated buf.work.yaml:\n$bufWork")
File(bufbuildDir, "buf.work.yaml").writeText(bufWork)
} else {
val protoDirs = allProtoDirs().map { project.makeMangledRelativizedPathStr(it) }
val bufYaml = bufYamlGenerator.generate(project.bufConfigFile(), protoDirs)
logger.info("Writing generated buf.yaml:{}\n", bufYaml)
File(bufbuildDir, "buf.yaml").writeText(bufYaml)
}
}
}

Expand All @@ -95,6 +112,11 @@ private fun Task.workspaceCommonConfig() {
createsOutput()
}

private fun Task.workspaceSymLinkEntries() =
allProtoDirs()
.map { project.makeMangledRelativizedPathStr(it) }
.joinToString("\n") { "| - $it" }

// Returns all directories that have may have proto files relevant to processing the project's proto files. This
// includes any proto files that are simply references (includes) as well as those that will be processed (code
// generation or validation).
Expand Down
32 changes: 32 additions & 0 deletions src/test/kotlin/build/buf/gradle/BreakingWithWorkspaceTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,40 @@

package build.buf.gradle

import org.junit.jupiter.api.Test
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.MethodSource
import java.nio.file.Paths

class BreakingWithWorkspaceTest : AbstractBreakingTest() {
override fun protoFile() = Paths.get(projectDir.path, "workspace", "buf", "test", "v1", "test.proto")

@Test
fun `breaking schema v2`() {
super.`breaking schema`()
}

@Test
fun `breaking schema fails with latest-release and previousVersion v2`() {
super.`breaking schema fails with latest-release and previousVersion`()
}

@Test
fun `breaking schema with latest-release as version v2`() {
super.`breaking schema with latest-release as version`()
}

@ParameterizedTest
@MethodSource("build.buf.gradle.ImageGenerationSupport#publicationFileExtensionTestCase")
fun `breaking schema with specified publication file extension v2`(
format: String,
compression: String?,
) {
super.`breaking schema with specified publication file extension`(format, compression)
}

@Test
fun `normally breaking schema with an ignore v2`() {
super.`normally breaking schema with an ignore`()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ publishing {
}

buf {
toolVersion = "1.31.0"
publishSchema = true
//checkSchemaAgainstLatestRelease = true

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
version: v2
modules:
- path: workspace
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
plugins {
id 'java'
id 'build.buf'
id 'maven-publish'
}

repositories {
mavenCentral()
maven { url 'build/repos/test' }
}

buf {
publishSchema = true
checkSchemaAgainstLatestRelease = true
previousVersion = '2319'

imageArtifact {
groupId = 'foo'
artifactId = 'bar'
version = '2319'
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
version: v2
modules:
- path: workspace
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
plugins {
id 'java'
id 'build.buf'
id 'maven-publish'
}

repositories {
mavenCentral()
maven { url 'build/repos/test' }
}

publishing {
repositories {
maven { url 'build/repos/test' }
}
}

buf {
publishSchema = true
//previousVersion = '2319'

imageArtifact {
groupId = 'foo'
artifactId = 'bar'
version = '2319'
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// 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.

syntax = "proto3";

package buf.test.v1;

message BasicMessage {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
version: v2
modules:
- path: workspace
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
plugins {
id 'java'
id 'build.buf'
id 'maven-publish'
}

repositories {
mavenCentral()
maven { url 'build/repos/test' }
}

publishing {
repositories {
maven { url 'build/repos/test' }
}
}

buf {
publishSchema = true
//checkSchemaAgainstLatestRelease = true

imageArtifact {
groupId = 'foo'
artifactId = 'bar'
version = '2319'
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// 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.

syntax = "proto3";

package buf.test.v1;

message BasicMessage {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
version: v2
modules:
- path: workspace
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
plugins {
id 'java'
id 'build.buf'
id 'maven-publish'
}

repositories {
mavenCentral()
maven { url 'build/repos/test' }
}

publishing {
repositories {
maven { url 'build/repos/test' }
}
}

buf {
publishSchema = true
//previousVersion = '2319'

build {
imageFormat = REPLACEME
compressionFormat = REPLACEME
}

imageArtifact {
groupId = 'foo'
artifactId = 'bar'
version = '2319'
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// 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.

syntax = "proto3";

package buf.test.v1;

message BasicMessage {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
version: v2
modules:
- path: workspace
breaking:
ignore:
- workspace/buf
Loading