-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
791ac27
commit 229c057
Showing
21 changed files
with
1,042 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
The following authors have created the source code of "detekt-rules-ui-tests" | ||
published and distributed by YANDEX LLC as the owner: | ||
Nikolay Nedoseykin nedoseykin@yandex-team.ru |
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,35 @@ | ||
# Notice to external contributors | ||
|
||
|
||
## General info | ||
|
||
Hello! In order for us (YANDEX LLC) to accept patches and other contributions from you, you will have to adopt our Yandex Contributor License Agreement (the “**CLA**â€). The current version of the CLA can be found here: | ||
1) https://yandex.ru/legal/cla/?lang=en (in English) and | ||
2) https://yandex.ru/legal/cla/?lang=ru (in Russian). | ||
|
||
By adopting the CLA, you state the following: | ||
|
||
* You obviously wish and are willingly licensing your contributions to us for our open source projects under the terms of the CLA, | ||
* You have read the terms and conditions of the CLA and agree with them in full, | ||
* You are legally able to provide and license your contributions as stated, | ||
* We may use your contributions for our open source projects and for any other our project too, | ||
* We rely on your assurances concerning the rights of third parties in relation to your contributions. | ||
|
||
If you agree with these principles, please read and adopt our CLA. By providing us your contributions, you hereby declare that you have already read and adopt our CLA, and we may freely merge your contributions with our corresponding open source project and use it in further in accordance with terms and conditions of the CLA. | ||
|
||
## Provide contributions | ||
|
||
If you have already adopted terms and conditions of the CLA, you are able to provide your contributions. When you submit your pull request, please add the following information into it: | ||
|
||
``` | ||
I hereby agree to the terms of the CLA available at: [link]. | ||
``` | ||
|
||
Replace the bracketed text as follows: | ||
* [link] is the link to the current version of the CLA: https://yandex.ru/legal/cla/?lang=en (in English) or https://yandex.ru/legal/cla/?lang=ru (in Russian). | ||
|
||
It is enough to provide us such notification once. | ||
|
||
## Other questions | ||
|
||
If you have any questions, please mail us at opensource@yandex-team.ru. |
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 |
---|---|---|
@@ -0,0 +1,76 @@ | ||
# Detekt rules for UI-tests | ||
|
||
A set of [Detekt](https://detekt.dev) rules to help prevent common errors in UI-tests. | ||
|
||
# Rules description | ||
|
||
[[RU] Detekt: How static analysis helps to improve autotest code](https://habr.com/p/779152) | ||
|
||
TestClassNamingRule: | ||
A test class name should fit the naming pattern; | ||
|
||
TestMethodNamingRule: | ||
Test method name should not be long and contain unnecessary words; | ||
|
||
TestClassPrivateMemberRule: | ||
Members of test class must use private modifier; | ||
|
||
IsVisibleUsageRule: | ||
In general, 'Espresso isVisible' should not be used -> use 'isDisplayed'; | ||
|
||
LargeScreenObjectRule: | ||
Split a large ScreenObject into PageElement's and combine them on this SO; | ||
|
||
RestrictedKeywordRule: | ||
Restricted keyword for test method, ScreenObject and Scenario. | ||
|
||
# Installation and configuration | ||
|
||
Add detekt rules in your `build.gradle.kts` | ||
|
||
``` | ||
dependencies { | ||
implementation(files("detekt-rules-ui-tests-0.1.0-SNAPSHOT.jar")) | ||
} | ||
``` | ||
|
||
and then add this configuration section to your `detekt-config.yml` to activate the rules: | ||
``` | ||
ui-tests: | ||
TestClassNamingRule: | ||
active: true | ||
includes: "**/androidTest/**" | ||
TestMethodNamingRule: | ||
active: true | ||
unexpectedWords: [ 'test' ] | ||
maxFullQualifierLength: 135 | ||
includes: "**/androidTest/**" | ||
TestClassPrivateMemberRule: | ||
active: true | ||
baseTestClass: "BaseTestCase" | ||
includes: "**/androidTest/**" | ||
IsVisibleUsageRule: | ||
active: true | ||
includes: "**/androidTest/**" | ||
LargeScreenObjectRule: | ||
active: true | ||
allowedLinesOfCode: 110 | ||
includes: "**/androidTest/**" | ||
RestrictedKeywordRule: | ||
active: true | ||
includes: "**/androidTest/**" | ||
``` | ||
|
||
# Contributors | ||
[primechord](https://github.com/primechord/) | ||
|
||
# License | ||
``` | ||
Copyright 2023 Yandex LLC | ||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | ||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | ||
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
``` |
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
34 changes: 34 additions & 0 deletions
34
src/main/kotlin/com/yandex/detekt/rule/ui_tests/IsVisibleUsageRule.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,34 @@ | ||
/* | ||
* Copyright 2023 Yandex LLC. Use of this source code is governed by the MIT license. | ||
*/ | ||
package com.yandex.detekt.rule.ui_tests | ||
|
||
import io.gitlab.arturbosch.detekt.api.* | ||
import org.jetbrains.kotlin.psi.KtCallExpression | ||
import org.jetbrains.kotlin.resolve.calls.util.getCalleeExpressionIfAny | ||
|
||
class IsVisibleUsageRule(config: Config) : Rule(config) { | ||
|
||
override val issue = Issue( | ||
id = javaClass.simpleName, | ||
severity = Severity.Defect, | ||
description = "In general, 'isVisible' should not be used. " + | ||
"Use 'isDisplayed', 'isCompletelyDisplayed', 'isNotCompletelyDisplayed'", | ||
debt = Debt.FIVE_MINS, | ||
) | ||
|
||
override fun visitCallExpression(expression: KtCallExpression) { | ||
super.visitCallExpression(expression) | ||
|
||
val isVisibleAssertion = expression.getCalleeExpressionIfAny()?.text?.equals("isVisible") ?: false | ||
if (isVisibleAssertion) { | ||
report( | ||
CodeSmell( | ||
issue, | ||
Entity.from(expression), | ||
"In general, 'Espresso isVisible' should not be used -> use 'isDisplayed'" | ||
) | ||
) | ||
} | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
src/main/kotlin/com/yandex/detekt/rule/ui_tests/LargeScreenObjectRule.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,38 @@ | ||
/* | ||
* Copyright 2023 Yandex LLC. Use of this source code is governed by the MIT license. | ||
*/ | ||
package com.yandex.detekt.rule.ui_tests | ||
|
||
import io.github.detekt.metrics.linesOfCode | ||
import io.gitlab.arturbosch.detekt.api.* | ||
import org.jetbrains.kotlin.psi.KtClassOrObject | ||
|
||
class LargeScreenObjectRule(config: Config) : Rule(config) { | ||
|
||
override val issue = Issue( | ||
id = javaClass.simpleName, | ||
severity = Severity.Defect, | ||
description = "Split a large ScreenObject into PageElement's and combine them on this ScreenObject", | ||
debt = Debt(hours = 4) | ||
) | ||
|
||
private val allowedLinesOfCode: Int by config(defaultValue = 100) | ||
|
||
override fun visitClassOrObject(classOrObject: KtClassOrObject) { | ||
super.visitClassOrObject(classOrObject) | ||
|
||
if (!classOrObject.isOrInScreenObject()) return | ||
|
||
val lines = classOrObject.linesOfCode() | ||
if (lines >= allowedLinesOfCode) { | ||
report( | ||
ThresholdedCodeSmell( | ||
issue, | ||
Entity.atName(classOrObject), | ||
Metric("SIZE", lines, allowedLinesOfCode), | ||
"Split a large ScreenObject into PageElement's and combine them on this SO" | ||
) | ||
) | ||
} | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
src/main/kotlin/com/yandex/detekt/rule/ui_tests/RestrictedKeywordRule.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,50 @@ | ||
/* | ||
* Copyright 2023 Yandex LLC. Use of this source code is governed by the MIT license. | ||
*/ | ||
package com.yandex.detekt.rule.ui_tests | ||
|
||
import io.gitlab.arturbosch.detekt.api.* | ||
import org.jetbrains.kotlin.com.intellij.psi.PsiElement | ||
import org.jetbrains.kotlin.psi.KtIfExpression | ||
import org.jetbrains.kotlin.psi.KtTryExpression | ||
import org.jetbrains.kotlin.psi.KtWhenExpression | ||
|
||
class RestrictedKeywordRule(config: Config) : Rule(config) { | ||
|
||
override val issue = Issue( | ||
id = javaClass.simpleName, | ||
severity = Severity.Defect, | ||
description = "Restricted keyword for test method and ScreenObject", | ||
debt = Debt.FIVE_MINS, | ||
) | ||
|
||
override fun visitTryExpression(expression: KtTryExpression) { | ||
super.visitTryExpression(expression) | ||
if (expression.inTestMethod()) { | ||
reportIssue(expression, "Test method must not contain 'try' expression") | ||
return | ||
} | ||
|
||
if (expression.inScenario()) { | ||
reportIssue(expression, "Scenario must not contain 'try' expression") | ||
return | ||
} | ||
|
||
if (expression.isOrInScreenObject()) { | ||
reportIssue(expression, "ScreenObject must not contain 'try' expression") | ||
} | ||
} | ||
|
||
override fun visitIfExpression(expression: KtIfExpression) { | ||
super.visitIfExpression(expression) | ||
if (expression.inTestMethod()) reportIssue(expression, "Test method must not contain 'if' expression") | ||
} | ||
|
||
override fun visitWhenExpression(expression: KtWhenExpression) { | ||
super.visitWhenExpression(expression) | ||
if (expression.inTestMethod()) reportIssue(expression, "Test method must not contain 'when' expression") | ||
} | ||
|
||
private fun reportIssue(element: PsiElement, message: String) = | ||
report(CodeSmell(issue, Entity.from(element), message)) | ||
} |
42 changes: 42 additions & 0 deletions
42
src/main/kotlin/com/yandex/detekt/rule/ui_tests/RuleUtils.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,42 @@ | ||
/* | ||
* Copyright 2023 Yandex LLC. Use of this source code is governed by the MIT license. | ||
*/ | ||
package com.yandex.detekt.rule.ui_tests | ||
|
||
import org.jetbrains.kotlin.fir.lightTree.converter.nameAsSafeName | ||
import org.jetbrains.kotlin.psi.KtClass | ||
import org.jetbrains.kotlin.psi.KtClassOrObject | ||
import org.jetbrains.kotlin.psi.KtElement | ||
import org.jetbrains.kotlin.psi.KtNamedFunction | ||
import org.jetbrains.kotlin.psi.psiUtil.getNonStrictParentOfType | ||
import org.jetbrains.kotlin.psi.psiUtil.getSuperNames | ||
|
||
private const val TEST_ANNOTATION = "Test" | ||
private const val SCENARIO_BASE_CLASS = "BaseScenario" | ||
private val screenBaseClasses = listOf("KScreen", "UiScreen") | ||
|
||
fun KtElement.inTestMethod(): Boolean = getNonStrictParentOfType<KtNamedFunction>()?.isTestMethod() ?: false | ||
|
||
fun KtNamedFunction.isTestMethod(): Boolean = annotationEntries.any { it.shortName?.asString() == TEST_ANNOTATION } | ||
|
||
fun KtElement.inTestClass(baseTestClass: String): Boolean { | ||
return getNonStrictParentOfType<KtClass>() | ||
?.getSuperNames() | ||
?.any { | ||
it.nameAsSafeName().asString() == baseTestClass | ||
} ?: false | ||
} | ||
|
||
fun KtClass.isTestClass(): Boolean = body?.functions.orEmpty().any(KtNamedFunction::isTestMethod) | ||
|
||
fun KtElement.isOrInScreenObject(): Boolean { | ||
return getNonStrictParentOfType<KtClassOrObject>() | ||
?.getSuperNames() | ||
.orEmpty().any { it in screenBaseClasses } | ||
} | ||
|
||
fun KtElement.inScenario(): Boolean { | ||
return getNonStrictParentOfType<KtClassOrObject>() | ||
?.getSuperNames() | ||
.orEmpty().any { it == SCENARIO_BASE_CLASS } | ||
} |
45 changes: 45 additions & 0 deletions
45
src/main/kotlin/com/yandex/detekt/rule/ui_tests/TestClassNamingRule.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,45 @@ | ||
/* | ||
* Copyright 2023 Yandex LLC. Use of this source code is governed by the MIT license. | ||
*/ | ||
package com.yandex.detekt.rule.ui_tests | ||
|
||
import io.gitlab.arturbosch.detekt.api.* | ||
import io.gitlab.arturbosch.detekt.rules.identifierName | ||
import org.jetbrains.kotlin.psi.KtClass | ||
|
||
class TestClassNamingRule(config: Config) : Rule(config) { | ||
|
||
private companion object { | ||
const val REGULAR_EXPRESSION = "[A-Z][a-zA-Z0-9]*(Test|Tests)" | ||
} | ||
|
||
override val issue = Issue( | ||
id = javaClass.simpleName, | ||
severity = Severity.Defect, | ||
description = "A test class name should fit the naming pattern $REGULAR_EXPRESSION", | ||
debt = Debt(mins = 1) | ||
) | ||
|
||
private val classPattern = REGULAR_EXPRESSION.toRegex() | ||
|
||
override fun visitClass(ktClass: KtClass) { | ||
super.visitClass(ktClass) | ||
|
||
/** copy-paste optimization */ | ||
if (ktClass.nameAsSafeName.isSpecial || ktClass.nameIdentifier?.parent?.javaClass == null) { | ||
return | ||
} | ||
|
||
if (!ktClass.isTestClass()) return | ||
|
||
if (!ktClass.identifierName().removeSurrounding("`").matches(classPattern)) { | ||
report( | ||
CodeSmell( | ||
issue, | ||
Entity.atName(ktClass), | ||
message = "Test class names should match the pattern: $classPattern" | ||
) | ||
) | ||
} | ||
} | ||
} |
Oops, something went wrong.