From a35d3f9229cab8d191a8a049688ca3413f4af588 Mon Sep 17 00:00:00 2001 From: kramdar Date: Thu, 11 Jan 2024 09:53:54 -0500 Subject: [PATCH 1/5] Add failing test for gradleLint.ignore --- .../UnusedDependencyRuleSpec.groovy | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) 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..5524a5ab 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,34 @@ 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 { + 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() From 3eeb7a5720f550071bd5808ef84309f953a8bbfa Mon Sep 17 00:00:00 2001 From: kramdar Date: Thu, 11 Jan 2024 11:32:29 -0500 Subject: [PATCH 2/5] Make test fail for the right reason --- .../nebula/lint/rule/dependency/UnusedDependencyRuleSpec.groovy | 1 + 1 file changed, 1 insertion(+) 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 5524a5ab..2351f74d 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 @@ -689,6 +689,7 @@ class UnusedDependencyRuleSpec extends BaseIntegrationTestKitSpec { repositories { mavenCentral() } dependencies { + implementation 'com.google.guava:guava:18.0' gradleLint.ignore('unused-dependency') { implementation 'org.apache.httpcomponents:httpclient:4.5.3' } From 1849da7663bc105bd52ede249c73154be65e2757 Mon Sep 17 00:00:00 2001 From: kramdar Date: Thu, 11 Jan 2024 12:00:31 -0500 Subject: [PATCH 3/5] Fix failing test --- .../lint/rule/dependency/UnusedDependencyRule.groovy | 9 +++++++++ 1 file changed, 9 insertions(+) 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..e9267a76 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,15 @@ class UnusedDependencyRule extends ModelAwareGradleLintRule { @Override void visitGradleDependency(MethodCallExpression call, String declaredConf, GradleDependency dep) { + if (ignored) { + return // short-circuit ignored dependencies + } else { + callStack.any { call2 -> + print(call2.methodAsString) + print(call2.objectExpression.text) + print(call2.arguments.expressions) + } + } String conf = dependencyService.findAndReplaceNonResolvableConfiguration(project.configurations.getByName(declaredConf)).name if(SourceSetUtils.hasSourceSets(project)) { From 120d61b8867f42545ac89f37ecde493d6148ae75 Mon Sep 17 00:00:00 2001 From: kramdar Date: Thu, 11 Jan 2024 12:13:43 -0500 Subject: [PATCH 4/5] Add sanity check test that dependencies outside of ignore closure are still processed --- .../UnusedDependencyRuleSpec.groovy | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) 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 2351f74d..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 @@ -703,6 +703,36 @@ class UnusedDependencyRuleSpec extends BaseIntegrationTestKitSpec { 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() From 87eb24ec8159a53970c787478e8c0eef08acead2 Mon Sep 17 00:00:00 2001 From: kramdar Date: Thu, 11 Jan 2024 12:21:27 -0500 Subject: [PATCH 5/5] Remove debugging print statements --- .../nebula/lint/rule/dependency/UnusedDependencyRule.groovy | 6 ------ 1 file changed, 6 deletions(-) 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 e9267a76..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 @@ -31,12 +31,6 @@ class UnusedDependencyRule extends ModelAwareGradleLintRule { void visitGradleDependency(MethodCallExpression call, String declaredConf, GradleDependency dep) { if (ignored) { return // short-circuit ignored dependencies - } else { - callStack.any { call2 -> - print(call2.methodAsString) - print(call2.objectExpression.text) - print(call2.arguments.expressions) - } } String conf = dependencyService.findAndReplaceNonResolvableConfiguration(project.configurations.getByName(declaredConf)).name