From 5012fd634dd301656d13faf3166a5516d4ae1816 Mon Sep 17 00:00:00 2001 From: Tony Robalik Date: Thu, 23 May 2024 15:44:35 -0700 Subject: [PATCH 1/2] test: reproduces issue 947 --- .../jvm/CustomSourceSetSpec.groovy | 18 +++- .../projects/MultiDepSourceSetProject.groovy | 87 +++++++++++++++++++ 2 files changed, 104 insertions(+), 1 deletion(-) create mode 100644 src/functionalTest/groovy/com/autonomousapps/jvm/projects/MultiDepSourceSetProject.groovy diff --git a/src/functionalTest/groovy/com/autonomousapps/jvm/CustomSourceSetSpec.groovy b/src/functionalTest/groovy/com/autonomousapps/jvm/CustomSourceSetSpec.groovy index fa1b238d4..a4bd37401 100644 --- a/src/functionalTest/groovy/com/autonomousapps/jvm/CustomSourceSetSpec.groovy +++ b/src/functionalTest/groovy/com/autonomousapps/jvm/CustomSourceSetSpec.groovy @@ -191,7 +191,7 @@ final class CustomSourceSetSpec extends AbstractJvmSpec { ) } - def "don't suggest moving a dependency from one feature variant to another"() { + def "don't suggest moving a dependency from one feature variant to another (#gradleVersion)"() { given: def project = new FeatureVariantInConsumerTestProject() gradleProject = project.gradleProject @@ -205,4 +205,20 @@ final class CustomSourceSetSpec extends AbstractJvmSpec { where: gradleVersion << gradleVersions() } + + // https://github.com/autonomousapps/dependency-analysis-gradle-plugin/issues/947 + def "different versions of same dependency in different source sets are analyzed individually (#gradleVersion)"() { + given: + def project = new MultiDepSourceSetProject() + gradleProject = project.gradleProject + + when: + build(gradleVersion, gradleProject.rootDir, 'buildHealth') + + then: + assertThat(project.actualBuildHealth()).containsExactlyElementsIn(project.expectedBuildHealth) + + where: + gradleVersion << gradleVersions() + } } diff --git a/src/functionalTest/groovy/com/autonomousapps/jvm/projects/MultiDepSourceSetProject.groovy b/src/functionalTest/groovy/com/autonomousapps/jvm/projects/MultiDepSourceSetProject.groovy new file mode 100644 index 000000000..8ebdee697 --- /dev/null +++ b/src/functionalTest/groovy/com/autonomousapps/jvm/projects/MultiDepSourceSetProject.groovy @@ -0,0 +1,87 @@ +package com.autonomousapps.jvm.projects + +import com.autonomousapps.AbstractProject +import com.autonomousapps.kit.GradleProject +import com.autonomousapps.kit.Source +import com.autonomousapps.kit.gradle.Dependency +import com.autonomousapps.kit.gradle.Feature +import com.autonomousapps.kit.gradle.Java +import com.autonomousapps.model.Advice +import com.autonomousapps.model.ProjectAdvice + +import static com.autonomousapps.AdviceHelper.* +import static com.autonomousapps.kit.gradle.Dependency.implementation + +/** + * @see Issue 947 + */ +final class MultiDepSourceSetProject extends AbstractProject { + + private final Dependency okio380 = implementation('com.squareup.okio:okio:3.8.0') + private final Dependency okio390 = new Dependency('extraApi', 'com.squareup.okio:okio:3.9.0') + + final GradleProject gradleProject + + MultiDepSourceSetProject() { + this.gradleProject = build() + } + + private GradleProject build() { + return newGradleProjectBuilder() + .withSubproject('proj') { s -> + s.sources = consumerSources + s.withBuildScript { bs -> + bs.plugins = javaLibrary + bs.java = Java.ofFeatures(Feature.ofName('extra')) + bs.dependencies = [okio380, okio390] + } + } + .write() + } + + private consumerSources = [ + Source + .java( + ''' + package com.example; + + import okio.Buffer; + + public class Project { + // implementation but should be api (bc public) + public Buffer buffer; + } + '''.stripIndent() + ) + .withPath('com.example', 'Project') + .build(), + Source + .java( + ''' + package com.example.extra; + + import okio.Buffer; + + public class Extra { + // extraApi but should be extraImplementation (bc private) + private Buffer buffer; + } + '''.stripIndent() + ) + .withPath('com.example.extra', 'Extra') + .build(), + ] + + Set actualBuildHealth() { + return actualProjectAdvice(gradleProject) + } + + private final Set expectedAdvice = [ + Advice.ofChange(moduleCoordinates(okio380), 'implementation', 'api'), + Advice.ofChange(moduleCoordinates(okio390), 'extraApi', 'extraImplementation'), + ] + + final Set expectedBuildHealth = [ + projectAdviceForDependencies(':proj', expectedAdvice), + ] +} From f234730272b30d979a884f50175cf79fcad85789 Mon Sep 17 00:00:00 2001 From: Jendrik Johannes Date: Fri, 24 May 2024 07:58:09 +0200 Subject: [PATCH 2/2] Add missing 'withSourceSet("extra")' --- .../autonomousapps/jvm/projects/MultiDepSourceSetProject.groovy | 1 + 1 file changed, 1 insertion(+) diff --git a/src/functionalTest/groovy/com/autonomousapps/jvm/projects/MultiDepSourceSetProject.groovy b/src/functionalTest/groovy/com/autonomousapps/jvm/projects/MultiDepSourceSetProject.groovy index 8ebdee697..3fd3bf717 100644 --- a/src/functionalTest/groovy/com/autonomousapps/jvm/projects/MultiDepSourceSetProject.groovy +++ b/src/functionalTest/groovy/com/autonomousapps/jvm/projects/MultiDepSourceSetProject.groovy @@ -68,6 +68,7 @@ final class MultiDepSourceSetProject extends AbstractProject { } '''.stripIndent() ) + .withSourceSet("extra") .withPath('com.example.extra', 'Extra') .build(), ]