-
Notifications
You must be signed in to change notification settings - Fork 4k
Fix #1984 where TestMethodNameDetector does not scan some tests #1985
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,6 +18,7 @@ package com.google.samples.apps.nowinandroid.lint | |
|
|
||
| import com.android.tools.lint.detector.api.AnnotationInfo | ||
| import com.android.tools.lint.detector.api.AnnotationUsageInfo | ||
| import com.android.tools.lint.detector.api.AnnotationUsageType | ||
| import com.android.tools.lint.detector.api.Category.Companion.TESTING | ||
| import com.android.tools.lint.detector.api.Detector | ||
| import com.android.tools.lint.detector.api.Implementation | ||
|
|
@@ -31,6 +32,7 @@ import com.android.tools.lint.detector.api.SourceCodeScanner | |
| import com.android.tools.lint.detector.api.TextFormat.RAW | ||
| import com.intellij.psi.PsiMethod | ||
| import org.jetbrains.uast.UElement | ||
| import org.jetbrains.uast.UMethod | ||
| import java.util.EnumSet | ||
| import kotlin.io.path.Path | ||
|
|
||
|
|
@@ -43,13 +45,18 @@ class TestMethodNameDetector : Detector(), SourceCodeScanner { | |
|
|
||
| override fun applicableAnnotations() = listOf("org.junit.Test") | ||
|
|
||
| // Restrict this detector to `AnnotationUsageType.DEFINITION` following | ||
| // similar examples in AOSP like `IgnoreWithoutReasonDetector`. | ||
| override fun isApplicableAnnotationUsage(type: AnnotationUsageType): Boolean = | ||
| type == AnnotationUsageType.DEFINITION | ||
|
|
||
| override fun visitAnnotationUsage( | ||
| context: JavaContext, | ||
| element: UElement, | ||
| annotationInfo: AnnotationInfo, | ||
| usageInfo: AnnotationUsageInfo, | ||
| ) { | ||
| val method = usageInfo.referenced as? PsiMethod ?: return | ||
| val method = annotationInfo.annotation.uastParent as? UMethod ?: return | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I had to change this line because for some annotations I based the new interpretation on similar logic in |
||
|
|
||
| method.detectPrefix(context, usageInfo) | ||
| method.detectFormat(context, usageInfo) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -40,6 +40,11 @@ class TestMethodNameDetectorTest { | |
| fun test_foo() = Unit | ||
| @Test | ||
| fun `test foo`() = Unit | ||
|
|
||
| @Test | ||
| fun test_foo2() { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The previous version of the TestMethodNameDetector would not have reported on this method. In other words, a false negative. |
||
| // Blank body. | ||
| } | ||
| } | ||
| """, | ||
| ).indented(), | ||
|
|
@@ -53,19 +58,26 @@ class TestMethodNameDetectorTest { | |
| src/Test.kt:8: Warning: Test method starts with test [TestMethodPrefix] | ||
| fun `test foo`() = Unit | ||
| ~~~~~~~~~~ | ||
| 0 errors, 2 warnings | ||
| src/Test.kt:11: Warning: Test method starts with test [TestMethodPrefix] | ||
| fun test_foo2() { | ||
| ~~~~~~~~~ | ||
| 0 errors, 3 warnings | ||
| """.trimIndent(), | ||
| ) | ||
| .expectFixDiffs( | ||
| """ | ||
| Autofix for src/Test.kt line 6: Remove prefix: | ||
| @@ -6 +6 | ||
| - fun test_foo() = Unit | ||
| + fun foo() = Unit | ||
| @@ -6 +6 @@ | ||
| - fun test_foo() = Unit | ||
| + fun foo() = Unit | ||
| Autofix for src/Test.kt line 8: Remove prefix: | ||
| @@ -8 +8 | ||
| - fun `test foo`() = Unit | ||
| + fun `foo`() = Unit | ||
| @@ -8 +8 @@ | ||
| - fun `test foo`() = Unit | ||
| + fun `foo`() = Unit | ||
| Autofix for src/Test.kt line 11: Remove prefix: | ||
| @@ -11 +11 @@ | ||
| - fun test_foo2() { | ||
| + fun foo2() { | ||
| """.trimIndent(), | ||
| ) | ||
| } | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we don't override this function from the superclass, we get the following implementation which skips
AnnotationUsageType.DEFINITION:Hence the problem where test functions with an annotation that is not
AnnotationUsageType.METHOD_RETURNaren't scanned.Docs for
AnnotationUsageType.DEFINITIONsuggest opting in like this:but if I do that, I get duplicate reports. In other words, the same problem is reported twice. So I have based the solution on this one that restricts annotations to just
AnnotationUsageType.DEFINITION