Skip to content

Commit

Permalink
create rules and LICENSE
Browse files Browse the repository at this point in the history
  • Loading branch information
primechord committed Dec 11, 2023
1 parent 791ac27 commit 229c057
Show file tree
Hide file tree
Showing 21 changed files with 1,042 additions and 2 deletions.
3 changes: 3 additions & 0 deletions AUTHORS
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
35 changes: 35 additions & 0 deletions CONTRIBUTING
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.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2023 yandexmobile
Copyright (c) 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
Expand Down
76 changes: 76 additions & 0 deletions README.md
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.
```
15 changes: 14 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,13 @@ repositories {
mavenCentral()
}

val detektVersion = "1.23.4"

dependencies {
implementation("io.gitlab.arturbosch.detekt:detekt-api:$detektVersion")
implementation("io.gitlab.arturbosch.detekt:detekt-metrics:$detektVersion")
testImplementation(kotlin("test"))
testImplementation("io.gitlab.arturbosch.detekt:detekt-test:$detektVersion")
}

tasks.test {
Expand All @@ -19,4 +24,12 @@ tasks.test {

kotlin {
jvmToolchain(8)
}
}

gradle.taskGraph.whenReady {
allTasks
.filter { it.hasProperty("duplicatesStrategy") }
.forEach {
it.setProperty("duplicatesStrategy", "EXCLUDE")
}
}
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'"
)
)
}
}
}
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"
)
)
}
}
}
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 src/main/kotlin/com/yandex/detekt/rule/ui_tests/RuleUtils.kt
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 }
}
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"
)
)
}
}
}
Loading

0 comments on commit 229c057

Please sign in to comment.