diff --git a/src/main/groovy/com/netflix/nebula/lint/rule/dependency/UnusedDependencyRule.groovy b/src/main/groovy/com/netflix/nebula/lint/rule/dependency/UnusedDependencyRule.groovy index f8623e81..a2e7f679 100644 --- a/src/main/groovy/com/netflix/nebula/lint/rule/dependency/UnusedDependencyRule.groovy +++ b/src/main/groovy/com/netflix/nebula/lint/rule/dependency/UnusedDependencyRule.groovy @@ -29,6 +29,9 @@ class UnusedDependencyRule extends ModelAwareGradleLintRule { @Override void visitGradleDependency(MethodCallExpression call, String declaredConf, GradleDependency dep) { + if (ignored) { + return // short-circuit ignored dependencies + } String conf = dependencyService.findAndReplaceNonResolvableConfiguration(project.configurations.getByName(declaredConf)).name if(SourceSetUtils.hasSourceSets(project)) { diff --git a/src/test/groovy/com/netflix/nebula/lint/rule/dependency/UnusedDependencyRuleSpec.groovy b/src/test/groovy/com/netflix/nebula/lint/rule/dependency/UnusedDependencyRuleSpec.groovy index a3fa6710..ec8cedf0 100644 --- a/src/test/groovy/com/netflix/nebula/lint/rule/dependency/UnusedDependencyRuleSpec.groovy +++ b/src/test/groovy/com/netflix/nebula/lint/rule/dependency/UnusedDependencyRuleSpec.groovy @@ -674,6 +674,65 @@ class UnusedDependencyRuleSpec extends BaseIntegrationTestKitSpec { assert result.output.contains(it) } } + + @Issue('#380') + def 'ignore dependencies in closures when rule is ignored'() { + setup: + buildFile.text = """ + plugins { + id 'java' + id 'nebula.lint' + } + + gradleLint.rules = ['all-dependency'] + + repositories { mavenCentral() } + + dependencies { + implementation 'com.google.guava:guava:18.0' + gradleLint.ignore('unused-dependency') { + implementation 'org.apache.httpcomponents:httpclient:4.5.3' + } + } + """ + + when: + writeJavaSourceFile(main) + def result = runTasks('compileJava') + + then: + !result.output.contains('warning unused-dependency this dependency is unused and can be removed') + } + + @Issue('#380') + def 'dont ignore dependencies not in closures when rule is ignored'() { + setup: + buildFile.text = """ + plugins { + id 'java' + id 'nebula.lint' + } + + gradleLint.rules = ['all-dependency'] + + repositories { mavenCentral() } + + dependencies { + implementation 'com.google.guava:guava:18.0' + implementation 'org.apache.poi:poi:5.2.5' + gradleLint.ignore('unused-dependency') { + implementation 'org.apache.httpcomponents:httpclient:4.5.3' + } + } + """ + + when: + writeJavaSourceFile(main) + def result = runTasks('compileJava') + + then: + result.output.contains('warning unused-dependency this dependency is unused and can be removed') + } def dependencies(File _buildFile, String... confs = ['compile', 'testCompile', 'implementation', 'testImplementation', 'api']) { _buildFile.text.readLines()