Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import spock.lang.Unroll
*/
@CompileDynamic
class ProtobufJavaPluginTest extends Specification {
// Current supported version is Gradle 5+.
private static final List<String> GRADLE_VERSIONS = ["5.6", "6.0", "6.7.1"]
private static final List<String> KOTLIN_VERSIONS = ["1.3.20", "1.3.30"]

Expand Down Expand Up @@ -284,23 +283,23 @@ class ProtobufJavaPluginTest extends Specification {
}

@Unroll
void "testProjectDependentApp should be successfully executed [gradle #gradleVersion]"() {
given: "project from testProject & testProjectDependent"
File testProjectStaging = ProtobufPluginTestHelper.projectBuilder('testProjectJavaLibrary')
void "testProjectLibraryDependent should be successfully executed [gradle #gradleVersion]"() {
given: "project from testProjectJavaLibrary & testProjectDependent"
File testProjectStaging = ProtobufPluginTestHelper.projectBuilder('testProject')
.copyDirs('testProjectBase', 'testProjectJavaLibrary')
.build()
File testProjectDependentStaging = ProtobufPluginTestHelper.projectBuilder('testProjectDependentApp')
.copyDirs('testProjectDependentApp')
File testProjectDependentStaging = ProtobufPluginTestHelper.projectBuilder('testProjectDependent')
.copyDirs('testProjectDependent')
.build()

File mainProjectDir = ProtobufPluginTestHelper.projectBuilder('testProjectDependentAppMain')
File mainProjectDir = ProtobufPluginTestHelper.projectBuilder('testProjectLibraryDependentMain')
.copySubProjects(testProjectStaging, testProjectDependentStaging)
.build()

when: "build is invoked"
BuildResult result = GradleRunner.create()
.withProjectDir(mainProjectDir)
.withArguments('testProjectDependentApp:build', '--stacktrace')
.withArguments('testProjectDependent:build', '--stacktrace')
.withPluginClasspath()
.withGradleVersion(gradleVersion)
.forwardStdOutput(new OutputStreamWriter(System.out))
Expand All @@ -309,7 +308,7 @@ class ProtobufJavaPluginTest extends Specification {
.build()

then: "it succeed"
result.task(":testProjectDependentApp:build").outcome == TaskOutcome.SUCCESS
result.task(":testProjectDependent:build").outcome == TaskOutcome.SUCCESS

where:
gradleVersion << GRADLE_VERSIONS
Expand Down
2 changes: 1 addition & 1 deletion testProjectAndroid/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// See: ProtobufPluginTestHelper.groovy

plugins {
id 'com.android.application'
id 'com.google.protobuf'
}
apply plugin: 'com.android.application'
apply from: 'build_base.gradle'
33 changes: 0 additions & 33 deletions testProjectDependentApp/build.gradle

This file was deleted.

1 change: 0 additions & 1 deletion testProjectDependentApp/settings.gradle

This file was deleted.

14 changes: 0 additions & 14 deletions testProjectDependentApp/src/main/proto/dependent.proto

This file was deleted.

19 changes: 0 additions & 19 deletions testProjectDependentApp/src/test/java/DependentTest.java

This file was deleted.

8 changes: 0 additions & 8 deletions testProjectDependentApp/src/test/proto/dependent2.proto

This file was deleted.

188 changes: 93 additions & 95 deletions testProjectKotlinDslBase/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,123 +1,121 @@
import com.google.protobuf.gradle.*
import org.gradle.api.internal.HasConvention
import org.gradle.kotlin.dsl.provider.gradleKotlinDslOf

plugins {
java
idea
id("com.google.protobuf")
java
idea
id("com.google.protobuf")
}

// This extension is not auto generated when we apply the plugin using
// apply(plugin = "com.google.protobuf")
val Project.protobuf: ProtobufConvention get() =
this.convention.getPlugin(ProtobufConvention::class)

repositories {
maven("https://plugins.gradle.org/m2/")
maven("https://plugins.gradle.org/m2/")
}

java {
sourceCompatibility = JavaVersion.VERSION_1_7
targetCompatibility = JavaVersion.VERSION_1_7
sourceCompatibility = JavaVersion.VERSION_1_7
targetCompatibility = JavaVersion.VERSION_1_7
}

val grpcCompile by configurations.creating

the<JavaPluginConvention>().sourceSets {

val grpc by creating {
compileClasspath += grpcCompile
}
val grpc by creating {
compileClasspath += grpcCompile
}

"test"{
compileClasspath += grpc.output
runtimeClasspath += grpc.output
}
"test"{
compileClasspath += grpc.output
runtimeClasspath += grpc.output
}
}

val protobufDep = "com.google.protobuf:protobuf-java:3.0.0"

dependencies {
protobuf(files("lib/protos.tar.gz"))
protobuf(files("ext/"))
testProtobuf(files("lib/protos-test.tar.gz"))

compile(protobufDep)
testCompile("junit:junit:4.12")
// KotlinFooTest.kt requires reflection utilities
testCompile("org.jetbrains.kotlin:kotlin-reflect:1.2.0")
grpcCompile(protobufDep)
grpcCompile("io.grpc:grpc-stub:1.0.0-pre2")
grpcCompile("io.grpc:grpc-protobuf:1.0.0-pre2")
protobuf(files("lib/protos.tar.gz"))
protobuf(files("ext/"))
testProtobuf(files("lib/protos-test.tar.gz"))

compile(protobufDep)
testCompile("junit:junit:4.12")
// KotlinFooTest.kt requires reflection utilities
testCompile("org.jetbrains.kotlin:kotlin-reflect:1.2.0")
grpcCompile(protobufDep)
grpcCompile("io.grpc:grpc-stub:1.0.0-pre2")
grpcCompile("io.grpc:grpc-protobuf:1.0.0-pre2")
}

protobuf {
protoc {
artifact = "com.google.protobuf:protoc:3.0.0"
protoc {
artifact = "com.google.protobuf:protoc:3.0.0"
}
plugins {
id("grpc") {
artifact = "io.grpc:protoc-gen-grpc-java:1.0.0-pre2"
}
plugins {
}
generateProtoTasks {
ofSourceSet("grpc").forEach { task ->
task.plugins {
id("grpc") {
artifact = "io.grpc:protoc-gen-grpc-java:1.0.0-pre2"
}
}
generateProtoTasks {
ofSourceSet("grpc").forEach { task ->
task.plugins {
id("grpc") {
outputSubDir = "grpc_output"
}
}
task.generateDescriptorSet = true
outputSubDir = "grpc_output"
}
}
task.generateDescriptorSet = true
}
}
}

tasks {

"jar"(Jar::class) {
sourceSets.forEach { sourceSet ->
from(sourceSet.output)
"jar"(Jar::class) {
sourceSets.forEach { sourceSet ->
from(sourceSet.output)

val compileTaskName = sourceSet.getCompileTaskName("java")
dependsOn(project.tasks.getByName(compileTaskName))
}
val compileTaskName = sourceSet.getCompileTaskName("java")
dependsOn(project.tasks.getByName(compileTaskName))
}
}

"test"{
"test"{

doLast{
val generateProtoTasks = project.protobuf.protobuf.generateProtoTasks
doLast {
val generateProtoTasks = project.protobuf.protobuf.generateProtoTasks

val generateProtoTaskNames = generateProtoTasks.all().map { it.name }.toSet()
val generateProtoTaskNamesMain = generateProtoTasks.ofSourceSet("main").map { it.name }.toSet()
val generateProtoTaskNames = generateProtoTasks.all().map { it.name }.toSet()
val generateProtoTaskNamesMain =
generateProtoTasks.ofSourceSet("main").map { it.name }.toSet()

assert(setOf("generateProto", "generateGrpcProto", "generateTestProto") == generateProtoTaskNames)
assert(setOf("generateProto") == generateProtoTaskNamesMain)
assert(setOf("generateProto",
"generateGrpcProto",
"generateTestProto") == generateProtoTaskNames)
assert(setOf("generateProto") == generateProtoTaskNamesMain)

assertJavaCompileHasProtoGeneratedDir("main", listOf("java"))
assertJavaCompileHasProtoGeneratedDir("test", listOf("java"))
assertJavaCompileHasProtoGeneratedDir("grpc", listOf("java", "grpc_output"))
assertJavaCompileHasProtoGeneratedDir("main", listOf("java"))
assertJavaCompileHasProtoGeneratedDir("test", listOf("java"))
assertJavaCompileHasProtoGeneratedDir("grpc", listOf("java", "grpc_output"))

listOf("main", "test").forEach { sourceSet ->
assertFileExists(false, "$buildDir/generated/source/proto/$sourceSet/descriptor_set.desc")
}
assertFileExists(true, "$buildDir/generated/source/proto/grpc/descriptor_set.desc")
}
listOf("main", "test").forEach { sourceSet ->
assertFileExists(false,
"$buildDir/generated/source/proto/$sourceSet/descriptor_set.desc")
}
assertFileExists(true, "$buildDir/generated/source/proto/grpc/descriptor_set.desc")
}
}
}

fun assertJavaCompileHasProtoGeneratedDir(sourceSet: String, codegenPlugins: Collection<String>) {
val compileJavaTask = tasks.getByName(sourceSets.getByName(sourceSet).getCompileTaskName("java")) as JavaCompile
assertJavaCompileHasProtoGeneratedDir(project, sourceSet, compileJavaTask, codegenPlugins)
val compileJavaTask =
tasks.getByName(sourceSets.getByName(sourceSet).getCompileTaskName("java")) as JavaCompile
assertJavaCompileHasProtoGeneratedDir(project, sourceSet, compileJavaTask, codegenPlugins)
}

fun assertFileExists(exists: Boolean, path: String) {
if (exists) {
assert(File(path).exists())
} else {
assert(!File(path).exists())
}
if (exists) {
assert(File(path).exists())
} else {
assert(!File(path).exists())
}
}

fun assertJavaCompileHasProtoGeneratedDir(
Expand All @@ -126,28 +124,28 @@ fun assertJavaCompileHasProtoGeneratedDir(
compileJavaTask: JavaCompile,
codegenPlugins: Collection<String>
) {
val baseDir = File("${project.buildDir}/generated/source/proto/$sourceSet")
// The expected direct subdirectories under baseDir
val expectedDirs = codegenPlugins.map { codegenPlugin ->
File("${project.buildDir}/generated/source/proto/$sourceSet/$codegenPlugin")
}.toSet()

val actualDirs = mutableSetOf<File>()
compileJavaTask.source.visit {

// If the visited file is or is under a direct subdirectory of baseDir, add
// that subdirectory to actualDirs.
var file = this@visit.file
while (true) {
if (file.parentFile == baseDir) {
actualDirs.add(file)
}
if (file.parentFile == null) {
break
}
file = file.parentFile
}
val baseDir = File("${project.buildDir}/generated/source/proto/$sourceSet")
// The expected direct subdirectories under baseDir
val expectedDirs = codegenPlugins.map { codegenPlugin ->
File("${project.buildDir}/generated/source/proto/$sourceSet/$codegenPlugin")
}.toSet()

val actualDirs = mutableSetOf<File>()
compileJavaTask.source.visit {

// If the visited file is or is under a direct subdirectory of baseDir, add
// that subdirectory to actualDirs.
var file = this@visit.file
while (true) {
if (file.parentFile == baseDir) {
actualDirs.add(file)
}
if (file.parentFile == null) {
break
}
file = file.parentFile
}
assert(expectedDirs == actualDirs)
}
assert(expectedDirs == actualDirs)
}