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

Commit adf49496 authored by Jeff Chen's avatar Jeff Chen Committed by Automerger Merge Worker
Browse files

Merge "Enable DemotingTestWithoutBugDetector linter on test files. Java and Kt...

Merge "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." into tm-qpr-dev am: b9b82584 am: 9aeb6be5 am: f7a1ad0c

Original change: https://googleplex-android-review.googlesource.com/c/platform/frameworks/base/+/21913302



Change-Id: I08b355b04da71a0edc6c7964b6f6318e9a016734
Signed-off-by: default avatarAutomerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
parents 17104cbe f7a1ad0c
Loading
Loading
Loading
Loading
+17 −7
Original line number Original line 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.Scope
import com.android.tools.lint.detector.api.Severity
import com.android.tools.lint.detector.api.Severity
import com.android.tools.lint.detector.api.SourceCodeScanner
import com.android.tools.lint.detector.api.SourceCodeScanner
import java.util.EnumSet
import java.util.regex.Pattern
import java.util.regex.Pattern
import org.jetbrains.uast.UAnnotation
import org.jetbrains.uast.UAnnotation
import org.jetbrains.uast.UElement
import org.jetbrains.uast.UElement


@Suppress("UnstableApiUsage") // For linter api
class DemotingTestWithoutBugDetector : Detector(), SourceCodeScanner {
class DemotingTestWithoutBugDetector : Detector(), SourceCodeScanner {
    override fun getApplicableUastTypes(): List<Class<out UElement>> {
    override fun getApplicableUastTypes(): List<Class<out UElement>> {
        return listOf(UAnnotation::class.java)
        return listOf(UAnnotation::class.java)
@@ -39,18 +41,15 @@ class DemotingTestWithoutBugDetector : Detector(), SourceCodeScanner {
            override fun visitAnnotation(node: UAnnotation) {
            override fun visitAnnotation(node: UAnnotation) {
                // Annotations having int bugId field
                // Annotations having int bugId field
                if (node.qualifiedName in DEMOTING_ANNOTATION_BUG_ID) {
                if (node.qualifiedName in DEMOTING_ANNOTATION_BUG_ID) {
                    val bugId = node.findAttributeValue("bugId")!!.evaluate() as Int
                    if (!containsBugId(node)) {
                    if (bugId <= 0) {
                        val location = context.getLocation(node)
                        val location = context.getLocation(node)
                        val message = "Please attach a bug id to track demoted test"
                        val message = "Please attach a bug id to track demoted test"
                        context.report(ISSUE, node, location, message)
                        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) {
                if (node.qualifiedName == DEMOTING_ANNOTATION_IGNORE) {
                    val reason = node.findAttributeValue("value")!!.evaluate() as String
                    if (!containsBugString(node)) {
                    val bugPattern = Pattern.compile("b/\\d+")
                    if (!bugPattern.matcher(reason).find()) {
                        val location = context.getLocation(node)
                        val location = context.getLocation(node)
                        val message = "Please attach a bug (e.g. b/123) to track demoted test"
                        val message = "Please attach a bug (e.g. b/123) to track demoted test"
                        context.report(ISSUE, node, location, message)
                        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 {
    companion object {
        val DEMOTING_ANNOTATION_BUG_ID =
        val DEMOTING_ANNOTATION_BUG_ID =
            listOf(
            listOf(
@@ -87,7 +97,7 @@ class DemotingTestWithoutBugDetector : Detector(), SourceCodeScanner {
                implementation =
                implementation =
                    Implementation(
                    Implementation(
                        DemotingTestWithoutBugDetector::class.java,
                        DemotingTestWithoutBugDetector::class.java,
                        Scope.JAVA_FILE_SCOPE
                        EnumSet.of(Scope.JAVA_FILE, Scope.TEST_SOURCES)
                    )
                    )
            )
            )
    }
    }
+13 −0
Original line number Original line 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.checks.infrastructure.TestFiles
import com.android.tools.lint.detector.api.Detector
import com.android.tools.lint.detector.api.Detector
import com.android.tools.lint.detector.api.Issue
import com.android.tools.lint.detector.api.Issue
import com.android.tools.lint.detector.api.Scope
import java.util.EnumSet
import org.junit.Test
import org.junit.Test


@Suppress("UnstableApiUsage")
class DemotingTestWithoutBugDetectorTest : SystemUILintDetectorTest() {
class DemotingTestWithoutBugDetectorTest : SystemUILintDetectorTest() {


    override fun getDetector(): Detector = DemotingTestWithoutBugDetector()
    override fun getDetector(): Detector = DemotingTestWithoutBugDetector()
@@ -45,6 +48,7 @@ class DemotingTestWithoutBugDetectorTest : SystemUILintDetectorTest() {
                    .indented(),
                    .indented(),
                *stubs
                *stubs
            )
            )
            .customScope(testScope)
            .issues(DemotingTestWithoutBugDetector.ISSUE)
            .issues(DemotingTestWithoutBugDetector.ISSUE)
            .run()
            .run()
            .expectClean()
            .expectClean()
@@ -65,6 +69,7 @@ class DemotingTestWithoutBugDetectorTest : SystemUILintDetectorTest() {
                    .indented(),
                    .indented(),
                *stubs
                *stubs
            )
            )
            .customScope(testScope)
            .issues(DemotingTestWithoutBugDetector.ISSUE)
            .issues(DemotingTestWithoutBugDetector.ISSUE)
            .run()
            .run()
            .expectClean()
            .expectClean()
@@ -88,6 +93,7 @@ class DemotingTestWithoutBugDetectorTest : SystemUILintDetectorTest() {
                    .indented(),
                    .indented(),
                *stubs
                *stubs
            )
            )
            .customScope(testScope)
            .issues(DemotingTestWithoutBugDetector.ISSUE)
            .issues(DemotingTestWithoutBugDetector.ISSUE)
            .run()
            .run()
            .expect(
            .expect(
@@ -115,6 +121,7 @@ class DemotingTestWithoutBugDetectorTest : SystemUILintDetectorTest() {
                    .indented(),
                    .indented(),
                *stubs
                *stubs
            )
            )
            .customScope(testScope)
            .issues(DemotingTestWithoutBugDetector.ISSUE)
            .issues(DemotingTestWithoutBugDetector.ISSUE)
            .run()
            .run()
            .expect(
            .expect(
@@ -145,6 +152,7 @@ class DemotingTestWithoutBugDetectorTest : SystemUILintDetectorTest() {
                    .indented(),
                    .indented(),
                *stubs
                *stubs
            )
            )
            .customScope(testScope)
            .issues(DemotingTestWithoutBugDetector.ISSUE)
            .issues(DemotingTestWithoutBugDetector.ISSUE)
            .run()
            .run()
            .expectClean()
            .expectClean()
@@ -168,6 +176,7 @@ class DemotingTestWithoutBugDetectorTest : SystemUILintDetectorTest() {
                    .indented(),
                    .indented(),
                *stubs
                *stubs
            )
            )
            .customScope(testScope)
            .issues(DemotingTestWithoutBugDetector.ISSUE)
            .issues(DemotingTestWithoutBugDetector.ISSUE)
            .run()
            .run()
            .expect(
            .expect(
@@ -198,6 +207,7 @@ class DemotingTestWithoutBugDetectorTest : SystemUILintDetectorTest() {
                    .indented(),
                    .indented(),
                *stubs
                *stubs
            )
            )
            .customScope(testScope)
            .issues(DemotingTestWithoutBugDetector.ISSUE)
            .issues(DemotingTestWithoutBugDetector.ISSUE)
            .run()
            .run()
            .expectClean()
            .expectClean()
@@ -221,6 +231,7 @@ class DemotingTestWithoutBugDetectorTest : SystemUILintDetectorTest() {
                    .indented(),
                    .indented(),
                *stubs
                *stubs
            )
            )
            .customScope(testScope)
            .issues(DemotingTestWithoutBugDetector.ISSUE)
            .issues(DemotingTestWithoutBugDetector.ISSUE)
            .run()
            .run()
            .expect(
            .expect(
@@ -248,6 +259,7 @@ class DemotingTestWithoutBugDetectorTest : SystemUILintDetectorTest() {
                    .indented(),
                    .indented(),
                *stubs
                *stubs
            )
            )
            .customScope(testScope)
            .issues(DemotingTestWithoutBugDetector.ISSUE)
            .issues(DemotingTestWithoutBugDetector.ISSUE)
            .run()
            .run()
            .expect(
            .expect(
@@ -260,6 +272,7 @@ class DemotingTestWithoutBugDetectorTest : SystemUILintDetectorTest() {
            )
            )
    }
    }


    private val testScope = EnumSet.of(Scope.JAVA_FILE, Scope.TEST_SOURCES)
    private val filtersFlakyTestStub: TestFile =
    private val filtersFlakyTestStub: TestFile =
        java(
        java(
            """
            """