From a1a0e89e4dbb03d2cdfa154eba0c782fa243dd4d Mon Sep 17 00:00:00 2001 From: Patrick Koenig Date: Thu, 21 Mar 2024 12:42:51 -0400 Subject: [PATCH] Ignore intra-project dependencies in checkUnusedDependencies (#2748) --- changelog/@unreleased/pr-2748.v2.yml | 7 +++++++ .../plugins/BaselineExactDependencies.java | 6 +++++- .../BaselineExactDependenciesTest.groovy | 18 ++++++++++++++++-- 3 files changed, 28 insertions(+), 3 deletions(-) create mode 100644 changelog/@unreleased/pr-2748.v2.yml diff --git a/changelog/@unreleased/pr-2748.v2.yml b/changelog/@unreleased/pr-2748.v2.yml new file mode 100644 index 000000000..7b420def0 --- /dev/null +++ b/changelog/@unreleased/pr-2748.v2.yml @@ -0,0 +1,7 @@ +type: improvement +improvement: + description: Ignore intra-project dependencies in `checkUnusedDependencies`. This + allows `checkUnusedDependencies` to be used with Gradle test fixtures even when + the test fixtures are not used by the tests. + links: + - https://github.com/palantir/gradle-baseline/pull/2748 diff --git a/gradle-baseline-java/src/main/groovy/com/palantir/baseline/plugins/BaselineExactDependencies.java b/gradle-baseline-java/src/main/groovy/com/palantir/baseline/plugins/BaselineExactDependencies.java index 03b5f2e95..01cc0c410 100644 --- a/gradle-baseline-java/src/main/groovy/com/palantir/baseline/plugins/BaselineExactDependencies.java +++ b/gradle-baseline-java/src/main/groovy/com/palantir/baseline/plugins/BaselineExactDependencies.java @@ -182,6 +182,10 @@ private static void configureSourceSet( task.setSourceClasses(sourceSet.getOutput().getClassesDirs()); task.getDependenciesConfigurations().add(explicitCompile); + // ignore intra-project dependencies, which are typically added automatically for things + // like test fixtures + task.ignore(project.getGroup().toString(), project.getName()); + // this is liberally applied to ease the Java8 -> 11 transition task.ignore("javax.annotation", "javax.annotation-api"); @@ -278,7 +282,7 @@ private static String asDependencyString(ResolvedArtifact artifact, boolean with .append(projectComponentId.getProjectPath()) .append("')"); if (withName) { - builder.append(" (").append(artifact.getId().getDisplayName()).append(")"); + builder.append(" <-- ").append(artifact.getName()); } return builder.toString(); } diff --git a/gradle-baseline-java/src/test/groovy/com/palantir/baseline/BaselineExactDependenciesTest.groovy b/gradle-baseline-java/src/test/groovy/com/palantir/baseline/BaselineExactDependenciesTest.groovy index e92d698e5..1093ea4d5 100644 --- a/gradle-baseline-java/src/test/groovy/com/palantir/baseline/BaselineExactDependenciesTest.groovy +++ b/gradle-baseline-java/src/test/groovy/com/palantir/baseline/BaselineExactDependenciesTest.groovy @@ -163,7 +163,7 @@ class BaselineExactDependenciesTest extends AbstractPluginTest { task << ['checkUnusedDependencies', 'checkImplicitDependencies'] } - def 'checkUnusedDependenciesTest passes if dependency from main source set is not referenced in test'() { + def 'checkUnusedDependenciesTest passes if main source set is not referenced in test'() { when: buildFile << standardBuildFile buildFile << """ @@ -188,6 +188,20 @@ class BaselineExactDependenciesTest extends AbstractPluginTest { result.task(':checkUnusedDependenciesTest').getOutcome() == TaskOutcome.SUCCESS } + def 'checkUnusedDependenciesTest passes if test fixture source set is not referenced in test'() { + when: + buildFile << standardBuildFile + buildFile << """ + plugins { + id 'java-test-fixtures' + } + """ + + then: + def result = with('checkUnusedDependencies', '--stacktrace').build() + result.task(':checkUnusedDependenciesTest').getOutcome() == TaskOutcome.SUCCESS + } + def 'checkImplicitDependencies fails when a class is imported without being declared as a dependency'() { when: buildFile << standardBuildFile @@ -263,7 +277,7 @@ class BaselineExactDependenciesTest extends AbstractPluginTest { then: BuildResult result = with(':checkUnusedDependencies', '--stacktrace').withDebug(true).buildAndFail() - result.output.contains "project(':sub-project-with-deps') (main (project :sub-project-with-deps))" + result.output.contains "project(':sub-project-with-deps') <-- main" result.output.contains "project(':sub-project-no-deps')" }