-
Notifications
You must be signed in to change notification settings - Fork 931
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new lint rule to avoid robolectric runner (#2784)
- Loading branch information
1 parent
de6c5a6
commit adf1d62
Showing
23 changed files
with
214 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,7 +32,7 @@ android { | |
|
||
lint { | ||
abortOnError true | ||
ignoreTestSources true | ||
ignoreTestSources false | ||
} | ||
|
||
buildTypes { | ||
|
71 changes: 71 additions & 0 deletions
71
lint-rules/src/main/java/com/duckduckgo/lint/NoRobolectricTestRunnerDetector.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* | ||
* Copyright (c) 2023 DuckDuckGo | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.duckduckgo.lint | ||
|
||
import com.android.tools.lint.client.api.UElementHandler | ||
import com.android.tools.lint.detector.api.Category | ||
import com.android.tools.lint.detector.api.Detector | ||
import com.android.tools.lint.detector.api.Implementation | ||
import com.android.tools.lint.detector.api.Issue | ||
import com.android.tools.lint.detector.api.JavaContext | ||
import com.android.tools.lint.detector.api.Scope.JAVA_FILE | ||
import com.android.tools.lint.detector.api.Scope.TEST_SOURCES | ||
import com.android.tools.lint.detector.api.Severity.ERROR | ||
import com.android.tools.lint.detector.api.SourceCodeScanner | ||
import org.jetbrains.uast.UAnnotation | ||
import org.jetbrains.uast.skipParenthesizedExprDown | ||
import java.util.* | ||
|
||
@Suppress("UnstableApiUsage") | ||
class NoRobolectricTestRunnerDetector : Detector(), SourceCodeScanner { | ||
override fun getApplicableUastTypes() = listOf(UAnnotation::class.java) | ||
|
||
override fun createUastHandler(context: JavaContext): UElementHandler = NoInternalImportHandler(context) | ||
|
||
internal class NoInternalImportHandler(private val context: JavaContext) : UElementHandler() { | ||
override fun visitAnnotation(node: UAnnotation) { | ||
if (node.qualifiedName?.contains("RunWith") == true) { | ||
val attributes = node.attributeValues | ||
if (attributes.size == 1) { | ||
val attribute = attributes[0] | ||
val value = attribute.expression.skipParenthesizedExprDown() | ||
val klass = value?.asSourceString() ?: return | ||
if (!klass.contains("Parameterized") && klass.contains("RobolectricTestRunner")) { | ||
context.report( | ||
NO_ROBOLECTRIC_TEST_RUNNER_ISSUE, | ||
node, | ||
context.getNameLocation(node), | ||
"The RobolectricTestRunner parameter must not be used in the RunWith annotation." | ||
) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
companion object { | ||
val NO_ROBOLECTRIC_TEST_RUNNER_ISSUE = Issue.create("NoRobolectricTestRunnerAnnotation", | ||
"The RobolectricTestRunner parameter must not be used in the RunWith annotation", | ||
""" | ||
The @RunWith(RobolectricTestRunner::class) annotation must not be used. | ||
Use @RunWith(AndroidJUnit4::class) instead. | ||
""".trimIndent(), | ||
Category.CORRECTNESS, 10, ERROR, | ||
Implementation(NoRobolectricTestRunnerDetector::class.java, EnumSet.of(JAVA_FILE, TEST_SOURCES)) | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.