diff options
2 files changed, 30 insertions, 7 deletions
diff --git a/packages/SystemUI/checks/src/com/android/internal/systemui/lint/DemotingTestWithoutBugDetector.kt b/packages/SystemUI/checks/src/com/android/internal/systemui/lint/DemotingTestWithoutBugDetector.kt index 459a38e9318c..09762b04e6a9 100644 --- a/packages/SystemUI/checks/src/com/android/internal/systemui/lint/DemotingTestWithoutBugDetector.kt +++ b/packages/SystemUI/checks/src/com/android/internal/systemui/lint/DemotingTestWithoutBugDetector.kt @@ -25,10 +25,12 @@ import com.android.tools.lint.detector.api.JavaContext import com.android.tools.lint.detector.api.Scope import com.android.tools.lint.detector.api.Severity import com.android.tools.lint.detector.api.SourceCodeScanner +import java.util.EnumSet import java.util.regex.Pattern import org.jetbrains.uast.UAnnotation import org.jetbrains.uast.UElement +@Suppress("UnstableApiUsage") // For linter api class DemotingTestWithoutBugDetector : Detector(), SourceCodeScanner { override fun getApplicableUastTypes(): List<Class<out UElement>> { return listOf(UAnnotation::class.java) @@ -39,18 +41,15 @@ class DemotingTestWithoutBugDetector : Detector(), SourceCodeScanner { override fun visitAnnotation(node: UAnnotation) { // Annotations having int bugId field if (node.qualifiedName in DEMOTING_ANNOTATION_BUG_ID) { - val bugId = node.findAttributeValue("bugId")!!.evaluate() as Int - if (bugId <= 0) { + if (!containsBugId(node)) { val location = context.getLocation(node) val message = "Please attach a bug id to track demoted test" context.report(ISSUE, node, location, message) } } - // @Ignore has a String field for reason + // @Ignore has a String field for specifying reasons if (node.qualifiedName == DEMOTING_ANNOTATION_IGNORE) { - val reason = node.findAttributeValue("value")!!.evaluate() as String - val bugPattern = Pattern.compile("b/\\d+") - if (!bugPattern.matcher(reason).find()) { + if (!containsBugString(node)) { val location = context.getLocation(node) val message = "Please attach a bug (e.g. b/123) to track demoted test" context.report(ISSUE, node, location, message) @@ -60,6 +59,17 @@ class DemotingTestWithoutBugDetector : Detector(), SourceCodeScanner { } } + private fun containsBugId(node: UAnnotation): Boolean { + val bugId = node.findAttributeValue("bugId")?.evaluate() as Int? + return bugId != null && bugId > 0 + } + + private fun containsBugString(node: UAnnotation): Boolean { + val reason = node.findAttributeValue("value")?.evaluate() as String? + val bugPattern = Pattern.compile("b/\\d+") + return reason != null && bugPattern.matcher(reason).find() + } + companion object { val DEMOTING_ANNOTATION_BUG_ID = listOf( @@ -87,7 +97,7 @@ class DemotingTestWithoutBugDetector : Detector(), SourceCodeScanner { implementation = Implementation( DemotingTestWithoutBugDetector::class.java, - Scope.JAVA_FILE_SCOPE + EnumSet.of(Scope.JAVA_FILE, Scope.TEST_SOURCES) ) ) } diff --git a/packages/SystemUI/checks/tests/com/android/internal/systemui/lint/DemotingTestWithoutBugDetectorTest.kt b/packages/SystemUI/checks/tests/com/android/internal/systemui/lint/DemotingTestWithoutBugDetectorTest.kt index 63eb2632979c..a1e6f92a7218 100644 --- a/packages/SystemUI/checks/tests/com/android/internal/systemui/lint/DemotingTestWithoutBugDetectorTest.kt +++ b/packages/SystemUI/checks/tests/com/android/internal/systemui/lint/DemotingTestWithoutBugDetectorTest.kt @@ -20,8 +20,11 @@ import com.android.tools.lint.checks.infrastructure.TestFile import com.android.tools.lint.checks.infrastructure.TestFiles import com.android.tools.lint.detector.api.Detector import com.android.tools.lint.detector.api.Issue +import com.android.tools.lint.detector.api.Scope +import java.util.EnumSet import org.junit.Test +@Suppress("UnstableApiUsage") class DemotingTestWithoutBugDetectorTest : SystemUILintDetectorTest() { override fun getDetector(): Detector = DemotingTestWithoutBugDetector() @@ -45,6 +48,7 @@ class DemotingTestWithoutBugDetectorTest : SystemUILintDetectorTest() { .indented(), *stubs ) + .customScope(testScope) .issues(DemotingTestWithoutBugDetector.ISSUE) .run() .expectClean() @@ -65,6 +69,7 @@ class DemotingTestWithoutBugDetectorTest : SystemUILintDetectorTest() { .indented(), *stubs ) + .customScope(testScope) .issues(DemotingTestWithoutBugDetector.ISSUE) .run() .expectClean() @@ -88,6 +93,7 @@ class DemotingTestWithoutBugDetectorTest : SystemUILintDetectorTest() { .indented(), *stubs ) + .customScope(testScope) .issues(DemotingTestWithoutBugDetector.ISSUE) .run() .expect( @@ -115,6 +121,7 @@ class DemotingTestWithoutBugDetectorTest : SystemUILintDetectorTest() { .indented(), *stubs ) + .customScope(testScope) .issues(DemotingTestWithoutBugDetector.ISSUE) .run() .expect( @@ -145,6 +152,7 @@ class DemotingTestWithoutBugDetectorTest : SystemUILintDetectorTest() { .indented(), *stubs ) + .customScope(testScope) .issues(DemotingTestWithoutBugDetector.ISSUE) .run() .expectClean() @@ -168,6 +176,7 @@ class DemotingTestWithoutBugDetectorTest : SystemUILintDetectorTest() { .indented(), *stubs ) + .customScope(testScope) .issues(DemotingTestWithoutBugDetector.ISSUE) .run() .expect( @@ -198,6 +207,7 @@ class DemotingTestWithoutBugDetectorTest : SystemUILintDetectorTest() { .indented(), *stubs ) + .customScope(testScope) .issues(DemotingTestWithoutBugDetector.ISSUE) .run() .expectClean() @@ -221,6 +231,7 @@ class DemotingTestWithoutBugDetectorTest : SystemUILintDetectorTest() { .indented(), *stubs ) + .customScope(testScope) .issues(DemotingTestWithoutBugDetector.ISSUE) .run() .expect( @@ -248,6 +259,7 @@ class DemotingTestWithoutBugDetectorTest : SystemUILintDetectorTest() { .indented(), *stubs ) + .customScope(testScope) .issues(DemotingTestWithoutBugDetector.ISSUE) .run() .expect( @@ -260,6 +272,7 @@ class DemotingTestWithoutBugDetectorTest : SystemUILintDetectorTest() { ) } + private val testScope = EnumSet.of(Scope.JAVA_FILE, Scope.TEST_SOURCES) private val filtersFlakyTestStub: TestFile = java( """ |