Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 648f9363 authored by Jeff Chen's avatar Jeff Chen
Browse files

Enable DemotingTestWithoutBugDetector linter on test files.

Java and Kt tests are all working on sysui-studio. Send other CLs (see Change-Id: Ia60fd89191beb0eb17ccedbb3e05d0e2b2c3920e) to add lint check to Soong system.

Test: Manually in sysui-studio
Bug: 270628073
Change-Id: I17605b931095197ab354cbb6f4c2fb5165a07324
parent 97cef3f4
Loading
Loading
Loading
Loading
+17 −7
Original line number Diff line number Diff line
@@ -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)
                    )
            )
    }
+13 −0
Original line number Diff line number Diff line
@@ -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(
            """