From 0521fe707ccedee7a0b4ce0fb88409eefb04e59d Mon Sep 17 00:00:00 2001 From: Andrey Stepankov Date: Fri, 4 Nov 2022 19:42:37 +0300 Subject: [PATCH] Do not use compileClasspath as source of proto files (#631) * Do not use compileClasspath as source of proto files * Add failing test case for #624 * Fix codenarc problems --- .../protobuf/gradle/ProtobufPlugin.groovy | 22 ++++++++++++++---- .../gradle/ProtobufJavaPluginTest.groovy | 23 +++++++++++++++++++ 2 files changed, 40 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) { diff --git a/src/test/groovy/com/google/protobuf/gradle/ProtobufJavaPluginTest.groovy b/src/test/groovy/com/google/protobuf/gradle/ProtobufJavaPluginTest.groovy index de7e40ed..e8f84709 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 "test 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"