From ae6f00ed481502f7abfa074001eeb10af2254a39 Mon Sep 17 00:00:00 2001 From: rougsig Date: Thu, 27 Oct 2022 16:45:27 +0300 Subject: [PATCH 1/3] Do not use compileClasspath as source of proto files --- .../protobuf/gradle/ProtobufPlugin.groovy | 22 ++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/src/main/groovy/com/google/protobuf/gradle/ProtobufPlugin.groovy b/src/main/groovy/com/google/protobuf/gradle/ProtobufPlugin.groovy index 160f8013..bab71b7c 100644 --- a/src/main/groovy/com/google/protobuf/gradle/ProtobufPlugin.groovy +++ b/src/main/groovy/com/google/protobuf/gradle/ProtobufPlugin.groovy @@ -52,6 +52,8 @@ import org.gradle.api.file.SourceDirectorySet import org.gradle.api.plugins.AppliedPlugin import org.gradle.api.provider.Provider import org.gradle.api.tasks.SourceSet +import org.gradle.api.tasks.SourceSetContainer +import org.gradle.api.tasks.TaskProvider import org.gradle.language.jvm.tasks.ProcessResources import org.gradle.util.GradleVersion @@ -237,12 +239,22 @@ class ProtobufPlugin implements Plugin { Configuration compileProtoPath, Collection postConfigure) { Provider extractProtosTask = setupExtractProtosTask(sourceSet.name, protobufConfig) - // In Java projects, the compileClasspath of the 'test' sourceSet includes all the - // 'resources' of the output of 'main', in which the source protos are placed. This is - // nicer than the ad-hoc solution that Android has, because it works for any extended - // configuration, not just 'testCompile'. + + // Pass include proto files from main to test. + // Process resource task contains all source proto files from a proto source set. + FileCollection testClassPathConfig = project.objects.fileCollection() + if (Utils.isTest(sourceSet.name)) { + TaskProvider mainProcessResources = project.tasks.named( + project.extensions.getByType(SourceSetContainer) + .getByName(SourceSet.MAIN_SOURCE_SET_NAME) + .processResourcesTaskName, + ProcessResources + ) + testClassPathConfig.from(mainProcessResources) + } + Provider extractIncludeProtosTask = setupExtractIncludeProtosTask( - sourceSet.name, compileProtoPath, sourceSet.compileClasspath) + sourceSet.name, compileProtoPath, testClassPathConfig) Provider generateProtoTask = addGenerateProtoTask( sourceSet.name, protoSrcDirSet, project.files(extractProtosTask), extractIncludeProtosTask) { From 28fd3fd90af531b173251db1243070d255e43602 Mon Sep 17 00:00:00 2001 From: rougsig Date: Thu, 27 Oct 2022 22:28:07 +0300 Subject: [PATCH 2/3] Add failing test case for #624 --- .../gradle/ProtobufJavaPluginTest.groovy | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/test/groovy/com/google/protobuf/gradle/ProtobufJavaPluginTest.groovy b/src/test/groovy/com/google/protobuf/gradle/ProtobufJavaPluginTest.groovy index de7e40ed..c8603a1c 100644 --- a/src/test/groovy/com/google/protobuf/gradle/ProtobufJavaPluginTest.groovy +++ b/src/test/groovy/com/google/protobuf/gradle/ProtobufJavaPluginTest.groovy @@ -91,6 +91,29 @@ class ProtobufJavaPluginTest extends Specification { gradleVersion << GRADLE_VERSIONS } + @Unroll + void "generateTestProto should not execute :compileJava task (java-only project) [gradle #gradleVersion]"() { + given: "project from testProject" + File projectDir = ProtobufPluginTestHelper.projectBuilder('testProject') + .copyDirs('testProjectBase', 'testProject') + .build() + + when: "build is invoked" + BuildResult result = ProtobufPluginTestHelper.getGradleRunner( + projectDir, + gradleVersion, + "generateTestProto" + ).build() + + then: "it succeed" + result.task(":generateTestProto").outcome == TaskOutcome.SUCCESS + assert !result.output.contains("Task :classes") + assert !result.output.contains("Task :compileJava") + + where: + gradleVersion << GRADLE_VERSIONS + } + @Unroll void "testProject should be successfully executed (configuration cache) [gradle #gradleVersion]"() { given: "project from testProject" From e432f3e6caddfbcb8f6f714938d15a705936267b Mon Sep 17 00:00:00 2001 From: rougsig Date: Fri, 28 Oct 2022 14:39:06 +0300 Subject: [PATCH 3/3] Fix codenarc problems --- .../com/google/protobuf/gradle/ProtobufJavaPluginTest.groovy | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/groovy/com/google/protobuf/gradle/ProtobufJavaPluginTest.groovy b/src/test/groovy/com/google/protobuf/gradle/ProtobufJavaPluginTest.groovy index c8603a1c..e8f84709 100644 --- a/src/test/groovy/com/google/protobuf/gradle/ProtobufJavaPluginTest.groovy +++ b/src/test/groovy/com/google/protobuf/gradle/ProtobufJavaPluginTest.groovy @@ -92,7 +92,7 @@ class ProtobufJavaPluginTest extends Specification { } @Unroll - void "generateTestProto should not execute :compileJava task (java-only project) [gradle #gradleVersion]"() { + void "test generateTestProto should not execute :compileJava task (java-only project) [gradle #gradleVersion]"() { given: "project from testProject" File projectDir = ProtobufPluginTestHelper.projectBuilder('testProject') .copyDirs('testProjectBase', 'testProject')