Skip to content

Commit

Permalink
Ignore intra-project dependencies in checkUnusedDependencies (#2748)
Browse files Browse the repository at this point in the history
  • Loading branch information
pkoenig10 authored Mar 21, 2024
1 parent cf49c0a commit a1a0e89
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
7 changes: 7 additions & 0 deletions changelog/@unreleased/pr-2748.v2.yml
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand Down Expand Up @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 << """
Expand All @@ -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
Expand Down Expand Up @@ -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')"
}

Expand Down

0 comments on commit a1a0e89

Please sign in to comment.