Skip to content

Commit 1ba1e78

Browse files
committed
add new Gradle rule for detecting usage of DependendencySubstitutions.all()
1 parent 5f44112 commit 1ba1e78

3 files changed

Lines changed: 50 additions & 0 deletions

File tree

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.netflix.nebula.archrules.gradleplugins;
2+
3+
import com.tngtech.archunit.lang.ArchRule;
4+
import com.tngtech.archunit.lang.Priority;
5+
import com.tngtech.archunit.lang.syntax.ArchRuleDefinition;
6+
7+
public class DependencySubstitutionsAllMethodRule {
8+
9+
public static final ArchRule DEPENDENCYSUBSTITUTIONS_ALL = ArchRuleDefinition.priority(Priority.MEDIUM)
10+
.noClasses()
11+
.should().callMethod(
12+
"org.gradle.api.artifacts.DependencySubstitutions",
13+
"all",
14+
"org.gradle.api.Action")
15+
.allowEmptyShould(true)
16+
.because(
17+
"Calling DependencySubstitutions.all causes the configuration to be resolved " +
18+
"at task graph calculation time due to the possibility of project substitutions there. " +
19+
"Instead, use DefaultDependencySubstitutions.addSubstitution or ResolutionStrategy.eachDependency."
20+
);
21+
}

archrules-gradle-plugin-development/src/archRules/java/com/netflix/nebula/archrules/gradleplugins/GradlePluginBestPractices.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import java.util.HashMap;
88
import java.util.Map;
99

10+
import static com.netflix.nebula.archrules.gradleplugins.DependencySubstitutionsAllMethodRule.DEPENDENCYSUBSTITUTIONS_ALL;
1011
import static com.netflix.nebula.archrules.gradleplugins.GradleDeprecatedApiRule.pluginsShouldNotUseDeprecatedGradleApis;
1112
import static com.netflix.nebula.archrules.gradleplugins.GradleDeprecatedApiRule.tasksShouldNotUseDeprecatedGradleApis;
1213
import static com.netflix.nebula.archrules.gradleplugins.GradleInternalApiRule.PLUGIN_INTERNAL;
@@ -52,6 +53,7 @@ public Map<String, ArchRule> getRules() {
5253
rules.put("Extension abstract getters", EXTENSION_ABSTRACT_GETTERS);
5354
rules.put("Cacheable Task input field path sensitivity", FIELDS_PATH_SENSITIVITY);
5455
rules.put("Cacheable Task input method path sensitivity", METHODS_PATH_SENSITIVITY);
56+
rules.put("Don't call DependendencySubstitutions.all()", DEPENDENCYSUBSTITUTIONS_ALL);
5557
rules.put("Apply plugins by ID", APPLY_BY_ID);
5658
return rules;
5759
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.netflix.nebula.archrules.gradleplugins;
2+
3+
import com.netflix.nebula.archrules.core.Runner;
4+
import com.tngtech.archunit.lang.EvaluationResult;
5+
import org.gradle.api.artifacts.DependencySubstitutions;
6+
import org.junit.jupiter.api.Test;
7+
8+
import static org.assertj.core.api.Assertions.assertThat;
9+
10+
public class DependencySubstitutionsAllMethodRuleTest {
11+
@Test
12+
public void pluginNotUsingDeprecatedApis_should_fail() {
13+
final EvaluationResult result = Runner.check(
14+
DependencySubstitutionsAllMethodRule.DEPENDENCYSUBSTITUTIONS_ALL,
15+
DependencySubstitutionsAllMethodRuleTest.CodeUsingAll.class
16+
);
17+
assertThat(result.hasViolation()).isTrue();
18+
}
19+
20+
static class CodeUsingAll {
21+
public void bad(DependencySubstitutions substitutions) {
22+
substitutions.all((substitution) -> {
23+
24+
});
25+
}
26+
}
27+
}

0 commit comments

Comments
 (0)