From 229c057ef81d560182251b4cb783b8f9fb08b6e3 Mon Sep 17 00:00:00 2001 From: primechord Date: Thu, 7 Dec 2023 16:58:52 +0400 Subject: [PATCH] create rules and LICENSE --- AUTHORS | 3 + CONTRIBUTING | 35 +++++ LICENSE | 2 +- README.md | 76 ++++++++++ build.gradle.kts | 15 +- .../rule/ui_tests/IsVisibleUsageRule.kt | 34 +++++ .../rule/ui_tests/LargeScreenObjectRule.kt | 38 +++++ .../rule/ui_tests/RestrictedKeywordRule.kt | 50 +++++++ .../yandex/detekt/rule/ui_tests/RuleUtils.kt | 42 ++++++ .../rule/ui_tests/TestClassNamingRule.kt | 45 ++++++ .../ui_tests/TestClassPrivateMemberRule.kt | 85 +++++++++++ .../rule/ui_tests/TestMethodNamingRule.kt | 63 ++++++++ .../rule/ui_tests/UiTestsRuleSetProvider.kt | 26 ++++ ...tlab.arturbosch.detekt.api.RuleSetProvider | 1 + src/main/resources/config/config.yml | 23 +++ .../rule/ui_tests/IsVisibleUsageTests.kt | 62 ++++++++ .../rule/ui_tests/LargeScreenObjectTests.kt | 69 +++++++++ .../rule/ui_tests/RestrictedKeywordTests.kt | 103 +++++++++++++ .../rule/ui_tests/TestClassNamingTests.kt | 57 ++++++++ .../ui_tests/TestClassPrivateMemberTests.kt | 135 ++++++++++++++++++ .../rule/ui_tests/TestMethodNamingTests.kt | 80 +++++++++++ 21 files changed, 1042 insertions(+), 2 deletions(-) create mode 100644 AUTHORS create mode 100644 CONTRIBUTING create mode 100644 README.md create mode 100644 src/main/kotlin/com/yandex/detekt/rule/ui_tests/IsVisibleUsageRule.kt create mode 100644 src/main/kotlin/com/yandex/detekt/rule/ui_tests/LargeScreenObjectRule.kt create mode 100644 src/main/kotlin/com/yandex/detekt/rule/ui_tests/RestrictedKeywordRule.kt create mode 100644 src/main/kotlin/com/yandex/detekt/rule/ui_tests/RuleUtils.kt create mode 100644 src/main/kotlin/com/yandex/detekt/rule/ui_tests/TestClassNamingRule.kt create mode 100644 src/main/kotlin/com/yandex/detekt/rule/ui_tests/TestClassPrivateMemberRule.kt create mode 100644 src/main/kotlin/com/yandex/detekt/rule/ui_tests/TestMethodNamingRule.kt create mode 100644 src/main/kotlin/com/yandex/detekt/rule/ui_tests/UiTestsRuleSetProvider.kt create mode 100644 src/main/resources/META-INF/services/io.gitlab.arturbosch.detekt.api.RuleSetProvider create mode 100644 src/main/resources/config/config.yml create mode 100644 src/test/kotlin/com/yandex/detekt/rule/ui_tests/IsVisibleUsageTests.kt create mode 100644 src/test/kotlin/com/yandex/detekt/rule/ui_tests/LargeScreenObjectTests.kt create mode 100644 src/test/kotlin/com/yandex/detekt/rule/ui_tests/RestrictedKeywordTests.kt create mode 100644 src/test/kotlin/com/yandex/detekt/rule/ui_tests/TestClassNamingTests.kt create mode 100644 src/test/kotlin/com/yandex/detekt/rule/ui_tests/TestClassPrivateMemberTests.kt create mode 100644 src/test/kotlin/com/yandex/detekt/rule/ui_tests/TestMethodNamingTests.kt diff --git a/AUTHORS b/AUTHORS new file mode 100644 index 0000000..847bc9f --- /dev/null +++ b/AUTHORS @@ -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 \ No newline at end of file diff --git a/CONTRIBUTING b/CONTRIBUTING new file mode 100644 index 0000000..3097e1e --- /dev/null +++ b/CONTRIBUTING @@ -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. \ No newline at end of file diff --git a/LICENSE b/LICENSE index aed2bbd..74ae319 100644 --- a/LICENSE +++ b/LICENSE @@ -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 diff --git a/README.md b/README.md new file mode 100644 index 0000000..1eb4651 --- /dev/null +++ b/README.md @@ -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. +``` \ No newline at end of file diff --git a/build.gradle.kts b/build.gradle.kts index b689c5a..781ade9 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -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 { @@ -19,4 +24,12 @@ tasks.test { kotlin { jvmToolchain(8) -} \ No newline at end of file +} + +gradle.taskGraph.whenReady { + allTasks + .filter { it.hasProperty("duplicatesStrategy") } + .forEach { + it.setProperty("duplicatesStrategy", "EXCLUDE") + } +} diff --git a/src/main/kotlin/com/yandex/detekt/rule/ui_tests/IsVisibleUsageRule.kt b/src/main/kotlin/com/yandex/detekt/rule/ui_tests/IsVisibleUsageRule.kt new file mode 100644 index 0000000..82e77c0 --- /dev/null +++ b/src/main/kotlin/com/yandex/detekt/rule/ui_tests/IsVisibleUsageRule.kt @@ -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'" + ) + ) + } + } +} \ No newline at end of file diff --git a/src/main/kotlin/com/yandex/detekt/rule/ui_tests/LargeScreenObjectRule.kt b/src/main/kotlin/com/yandex/detekt/rule/ui_tests/LargeScreenObjectRule.kt new file mode 100644 index 0000000..9768219 --- /dev/null +++ b/src/main/kotlin/com/yandex/detekt/rule/ui_tests/LargeScreenObjectRule.kt @@ -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" + ) + ) + } + } +} \ No newline at end of file diff --git a/src/main/kotlin/com/yandex/detekt/rule/ui_tests/RestrictedKeywordRule.kt b/src/main/kotlin/com/yandex/detekt/rule/ui_tests/RestrictedKeywordRule.kt new file mode 100644 index 0000000..7f8c112 --- /dev/null +++ b/src/main/kotlin/com/yandex/detekt/rule/ui_tests/RestrictedKeywordRule.kt @@ -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)) +} \ No newline at end of file diff --git a/src/main/kotlin/com/yandex/detekt/rule/ui_tests/RuleUtils.kt b/src/main/kotlin/com/yandex/detekt/rule/ui_tests/RuleUtils.kt new file mode 100644 index 0000000..9d0303d --- /dev/null +++ b/src/main/kotlin/com/yandex/detekt/rule/ui_tests/RuleUtils.kt @@ -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()?.isTestMethod() ?: false + +fun KtNamedFunction.isTestMethod(): Boolean = annotationEntries.any { it.shortName?.asString() == TEST_ANNOTATION } + +fun KtElement.inTestClass(baseTestClass: String): Boolean { + return getNonStrictParentOfType() + ?.getSuperNames() + ?.any { + it.nameAsSafeName().asString() == baseTestClass + } ?: false +} + +fun KtClass.isTestClass(): Boolean = body?.functions.orEmpty().any(KtNamedFunction::isTestMethod) + +fun KtElement.isOrInScreenObject(): Boolean { + return getNonStrictParentOfType() + ?.getSuperNames() + .orEmpty().any { it in screenBaseClasses } +} + +fun KtElement.inScenario(): Boolean { + return getNonStrictParentOfType() + ?.getSuperNames() + .orEmpty().any { it == SCENARIO_BASE_CLASS } +} diff --git a/src/main/kotlin/com/yandex/detekt/rule/ui_tests/TestClassNamingRule.kt b/src/main/kotlin/com/yandex/detekt/rule/ui_tests/TestClassNamingRule.kt new file mode 100644 index 0000000..a48175f --- /dev/null +++ b/src/main/kotlin/com/yandex/detekt/rule/ui_tests/TestClassNamingRule.kt @@ -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" + ) + ) + } + } +} \ No newline at end of file diff --git a/src/main/kotlin/com/yandex/detekt/rule/ui_tests/TestClassPrivateMemberRule.kt b/src/main/kotlin/com/yandex/detekt/rule/ui_tests/TestClassPrivateMemberRule.kt new file mode 100644 index 0000000..3c1d329 --- /dev/null +++ b/src/main/kotlin/com/yandex/detekt/rule/ui_tests/TestClassPrivateMemberRule.kt @@ -0,0 +1,85 @@ +/* + * 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.isConstant +import io.gitlab.arturbosch.detekt.rules.isOverride +import org.jetbrains.kotlin.com.intellij.psi.PsiElement +import org.jetbrains.kotlin.psi.KtNamedFunction +import org.jetbrains.kotlin.psi.KtObjectDeclaration +import org.jetbrains.kotlin.psi.KtProperty +import org.jetbrains.kotlin.psi.psiUtil.getNonStrictParentOfType +import org.jetbrains.kotlin.psi.psiUtil.isPrivate + +class TestClassPrivateMemberRule(config: Config) : Rule(config) { + + override val issue = Issue( + id = javaClass.simpleName, + severity = Severity.Defect, + description = "Members of test class must use private modifier", + debt = Debt(mins = 1) + ) + + private val baseTestClass: String by config(defaultValue = "TestCase") + + override fun visitObjectDeclaration(declaration: KtObjectDeclaration) { + super.visitObjectDeclaration(declaration) + + if (!declaration.inTestClass(baseTestClass)) return + + if (declaration.isCompanion() && + !declaration.isPrivate() + ) { + reportIssue(declaration, "Add private modifier to companion object") + } + } + + override fun visitProperty(property: KtProperty) { + super.visitProperty(property) + + if (!property.inTestClass(baseTestClass)) return + + val isNonLocalVariable = !property.isConstant() && !property.isLocal + if (isNonLocalVariable && + !property.isCompanionObjectProperty() && + !property.isPrivate() + ) { + reportIssue(property, "Add private modifier to non-local variable") + } + + if (!property.isConstant() && + property.isCompanionObjectProperty() && + property.isPrivate() + ) { + reportIssue(property, "Make property non-private and move it to private companion object") + } + + if (property.isConstant() && + property.isPrivate() + ) { + reportIssue(property, "Make constant non-private and move it to private companion object") + } + } + + override fun visitNamedFunction(function: KtNamedFunction) { + super.visitNamedFunction(function) + + if (!function.inTestClass(baseTestClass)) return + + if (function.annotationEntries.isEmpty() && + !function.isOverride() && + !function.isPrivate() + ) { + reportIssue(function, "Add private modifier to function") + } + } + + private fun KtProperty.isCompanionObjectProperty(): Boolean { + return getNonStrictParentOfType()?.isCompanion() ?: false + } + + private fun reportIssue(element: PsiElement, message: String) = + report(CodeSmell(issue, Entity.from(element), message)) +} \ No newline at end of file diff --git a/src/main/kotlin/com/yandex/detekt/rule/ui_tests/TestMethodNamingRule.kt b/src/main/kotlin/com/yandex/detekt/rule/ui_tests/TestMethodNamingRule.kt new file mode 100644 index 0000000..694a121 --- /dev/null +++ b/src/main/kotlin/com/yandex/detekt/rule/ui_tests/TestMethodNamingRule.kt @@ -0,0 +1,63 @@ +/* + * 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.KtNamedFunction + +class TestMethodNamingRule(config: Config) : Rule(config) { + + override val issue = Issue( + id = javaClass.simpleName, + severity = Severity.Defect, + description = "Test method name should not be long and contain unnecessary words", + debt = Debt.FIVE_MINS + ) + + private val maxFullQualifierLength: Int by config(defaultValue = 100) + private val unexpectedWords: List by config(emptyList()) + + override fun visitNamedFunction(function: KtNamedFunction) { + super.visitNamedFunction(function) + + if (!function.isTestMethod()) return + + val actualShortName = function.name.toString() + for (unexpectedWord in unexpectedWords) { + if (actualShortName.lowercase().contains(unexpectedWord)) { + report( + CodeSmell( + issue, + Entity.from(function), + "Test method name must not contain '$unexpectedWord'" + ) + ) + } + } + + val isSingleWord = function.name?.none(Char::isUpperCase) ?: false + if (isSingleWord) { + report( + CodeSmell( + issue, + Entity.from(function), + "Test method name should not consist of a single word" + ) + ) + return + } + + val actualFqName = function.fqName?.asString() ?: "" + if (actualFqName.length > maxFullQualifierLength) { + report( + ThresholdedCodeSmell( + issue, + Entity.from(function), + Metric("SIZE", actualFqName.length, maxFullQualifierLength), + "Test method name length should not be long (full qualifier)" + ) + ) + } + } +} \ No newline at end of file diff --git a/src/main/kotlin/com/yandex/detekt/rule/ui_tests/UiTestsRuleSetProvider.kt b/src/main/kotlin/com/yandex/detekt/rule/ui_tests/UiTestsRuleSetProvider.kt new file mode 100644 index 0000000..a4093e3 --- /dev/null +++ b/src/main/kotlin/com/yandex/detekt/rule/ui_tests/UiTestsRuleSetProvider.kt @@ -0,0 +1,26 @@ +/* + * 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.Config +import io.gitlab.arturbosch.detekt.api.RuleSet +import io.gitlab.arturbosch.detekt.api.RuleSetProvider + +class UiTestsRuleSetProvider : RuleSetProvider { + override val ruleSetId = "ui-tests" + + override fun instance(config: Config): RuleSet { + return RuleSet( + ruleSetId, + listOf( + TestClassNamingRule(config), + TestMethodNamingRule(config), + TestClassPrivateMemberRule(config), + IsVisibleUsageRule(config), + LargeScreenObjectRule(config), + RestrictedKeywordRule(config), + ) + ) + } +} \ No newline at end of file diff --git a/src/main/resources/META-INF/services/io.gitlab.arturbosch.detekt.api.RuleSetProvider b/src/main/resources/META-INF/services/io.gitlab.arturbosch.detekt.api.RuleSetProvider new file mode 100644 index 0000000..dd916b5 --- /dev/null +++ b/src/main/resources/META-INF/services/io.gitlab.arturbosch.detekt.api.RuleSetProvider @@ -0,0 +1 @@ +com.yandex.detekt.rule.ui_tests.UiTestsRuleSetProvider \ No newline at end of file diff --git a/src/main/resources/config/config.yml b/src/main/resources/config/config.yml new file mode 100644 index 0000000..45043bd --- /dev/null +++ b/src/main/resources/config/config.yml @@ -0,0 +1,23 @@ +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/**" \ No newline at end of file diff --git a/src/test/kotlin/com/yandex/detekt/rule/ui_tests/IsVisibleUsageTests.kt b/src/test/kotlin/com/yandex/detekt/rule/ui_tests/IsVisibleUsageTests.kt new file mode 100644 index 0000000..9a3213d --- /dev/null +++ b/src/test/kotlin/com/yandex/detekt/rule/ui_tests/IsVisibleUsageTests.kt @@ -0,0 +1,62 @@ +/* + * 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.Config +import io.gitlab.arturbosch.detekt.test.lint +import kotlin.test.Test + +class IsVisibleUsageTests { + private val rule = IsVisibleUsageRule(Config.empty) + + @Test + fun containsUnexpectedCall() { + // language="kotlin" + val case = + """ + class SomeTest : BaseTestCase() { + + @Test + fun shouldDoSomething() { + start { + step("should assert something") { + SomeScreen { + element.isVisible() + } + } + } + } + } + """.trimIndent() + + val findings = rule.lint(case) + + assert(findings.size == 1) + } + + @Test + fun notContainsUnexpectedCall() { + // language="kotlin" + val case = + """ + class SomeTest : BaseTestCase() { + + @Test + fun shouldDoSomething() { + start { + step("should assert something") { + SomeScreen { + element.isDisplayed() + } + } + } + } + } + """.trimIndent() + + val findings = rule.lint(case) + + assert(findings.isEmpty()) + } +} \ No newline at end of file diff --git a/src/test/kotlin/com/yandex/detekt/rule/ui_tests/LargeScreenObjectTests.kt b/src/test/kotlin/com/yandex/detekt/rule/ui_tests/LargeScreenObjectTests.kt new file mode 100644 index 0000000..f1816ec --- /dev/null +++ b/src/test/kotlin/com/yandex/detekt/rule/ui_tests/LargeScreenObjectTests.kt @@ -0,0 +1,69 @@ +/* + * 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.test.TestConfig +import io.gitlab.arturbosch.detekt.test.lint +import kotlin.test.Test + +class LargeScreenObjectTests { + private companion object { + const val THRESHOLD_PARAMETER_NAME = "allowedLinesOfCode" + const val THRESHOLD_PARAMETER_VALUE = 6 + } + + private val rule = LargeScreenObjectRule(TestConfig(THRESHOLD_PARAMETER_NAME to THRESHOLD_PARAMETER_VALUE)) + + @Test + fun numberOfLinesNotExceedLimit() { + // language="kotlin" + val case = + """ + object SomeScreen : KScreen() { + + override val layoutId = R.layout.fragment_some_screen + override val viewClass = SomeScreenFragment::class.java + + val subtitle = KButton { withId(R.id.subtitle) } + } + """.trimIndent() + + val findings = rule.lint(case) + + assert(findings.isEmpty()) + } + + @Test + fun numberOfLinesExceedLimit() { + // language="kotlin" + val case = + """ + object SomeScreen : KScreen() { + + override val layoutId = R.layout.fragment_some_screen + override val viewClass = SomeScreenFragment::class.java + + val subtitle = KButton { withId(R.id.subtitle) } + val title = KButton { withId(R.id.title) } + } + """.trimIndent() + + val findings = rule.lint(case) + + assert(findings.size == 1) + } + + @Test + fun notScreenObject() { + // language="kotlin" + val case = + """ + object SomeScreen + """.trimIndent() + + val findings = rule.lint(case) + + assert(findings.isEmpty()) + } +} \ No newline at end of file diff --git a/src/test/kotlin/com/yandex/detekt/rule/ui_tests/RestrictedKeywordTests.kt b/src/test/kotlin/com/yandex/detekt/rule/ui_tests/RestrictedKeywordTests.kt new file mode 100644 index 0000000..f9db14d --- /dev/null +++ b/src/test/kotlin/com/yandex/detekt/rule/ui_tests/RestrictedKeywordTests.kt @@ -0,0 +1,103 @@ +/* + * 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.Config +import io.gitlab.arturbosch.detekt.test.lint +import kotlin.test.Test + +class RestrictedKeywordTests { + private val rule = RestrictedKeywordRule(Config.empty) + + @Test + fun tryExpressionInTestMethod() { + // language="kotlin" + val case = + """ + class SomeTest: BaseTestCase() { + @Test fun shouldDoSomething() { + try { } catch (e: NoMatchingViewException) { println(e) } + } + } + """.trimIndent() + + val findings = rule.lint(case) + + assert(findings.size == 1) + } + + @Test + fun tryExpressionInScreenObject() { + // language="kotlin" + val case = + """ + object LocationDialog : UiScreen() { + override val packageName: String = "com.google.android.gms" + fun closeIfDisplayed() { + try { } catch (e: NoMatchingViewException) { println(e) } + } + } + """.trimIndent() + + val findings = rule.lint(case) + + assert(findings.size == 1) + } + + @Test + fun tryExpressionInScenario() { + // language="kotlin" + val case = + """ + class SomeScenario : BaseScenario() { + override val steps: TestContext.() -> Unit = { + try { } catch (e: NoMatchingViewException) { println(e) } + } + } + """.trimIndent() + + val findings = rule.lint(case) + + assert(findings.size == 1) + } + + @Test + fun ifExpressionInTestMethod() { + // language="kotlin" + val case = + """ + class SomeTest: BaseTestCase() { + @Test fun shouldDoSomething() { + val x = true + if (x) { println(1) } else { println(2) } + } + } + """.trimIndent() + + val findings = rule.lint(case) + + assert(findings.size == 1) + } + + @Test + fun whenExpressionInTestMethod() { + // language="kotlin" + val case = + """ + class SomeTest: BaseTestCase() { + @Test fun shouldDoSomething() { + val x = true + when { + x -> println(1) + else -> println(2) + } + } + } + """.trimIndent() + + val findings = rule.lint(case) + + assert(findings.size == 1) + } +} \ No newline at end of file diff --git a/src/test/kotlin/com/yandex/detekt/rule/ui_tests/TestClassNamingTests.kt b/src/test/kotlin/com/yandex/detekt/rule/ui_tests/TestClassNamingTests.kt new file mode 100644 index 0000000..ade3a9e --- /dev/null +++ b/src/test/kotlin/com/yandex/detekt/rule/ui_tests/TestClassNamingTests.kt @@ -0,0 +1,57 @@ +/* + * 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.Config +import io.gitlab.arturbosch.detekt.test.lint +import kotlin.test.Test + +class TestClassNamingTests { + private val rule = TestClassNamingRule(Config.empty) + + @Test + fun caseWhereIsNoSuffix() { + // language="kotlin" + val case = + """ + class WithoutSuffix: BaseTestCase() { + @Test fun shouldDoSomething() { } + } + """.trimIndent() + + val findings = rule.lint(case) + + assert(findings.size == 1) + } + + @Test + fun caseWhereIsSuffixTest() { + // language="kotlin" + val case = + """ + class SuffixTest : BaseTestCase() { + @Test fun shouldDoSomething() { } + } + """.trimIndent() + + val findings = rule.lint(case) + + assert(findings.isEmpty()) + } + + @Test + fun caseWhereIsSuffixTests() { + // language="kotlin" + val case = + """ + class SuffixTests : BaseTestCase() { + @Test fun shouldDoSomething() { } + } + """.trimIndent() + + val findings = rule.lint(case) + + assert(findings.isEmpty()) + } +} \ No newline at end of file diff --git a/src/test/kotlin/com/yandex/detekt/rule/ui_tests/TestClassPrivateMemberTests.kt b/src/test/kotlin/com/yandex/detekt/rule/ui_tests/TestClassPrivateMemberTests.kt new file mode 100644 index 0000000..a0da844 --- /dev/null +++ b/src/test/kotlin/com/yandex/detekt/rule/ui_tests/TestClassPrivateMemberTests.kt @@ -0,0 +1,135 @@ +/* + * Copyright 2023 Yandex LLC. Use of this source code is governed by the MIT license. + */ +@file:Suppress("UnusedReceiverParameter") +package com.yandex.detekt.rule.ui_tests + +import io.gitlab.arturbosch.detekt.test.TestConfig +import io.gitlab.arturbosch.detekt.test.lint +import kotlin.test.Test + +class TestClassPrivateMemberTests { + private companion object { + const val CLASS_PARAMETER_NAME = "baseTestClass" + const val CLASS_PARAMETER_VALUE = "BaseTestCase" + } + + private val rule = TestClassPrivateMemberRule(TestConfig(CLASS_PARAMETER_NAME to CLASS_PARAMETER_VALUE)) + + @Test + fun mainCaseWithoutViolations() { + // language="kotlin" + val case = + """ + class SomeTest: BaseTestCase() { + private companion object { + const val INDEX_OF_ELEMENT = 1 + } + private val queueResponses = emptyList() + private fun TestContext.checkSomething() { } + + @Test fun shouldDoSomething() { } + } + """.trimIndent() + + val findings = rule.lint(case) + + assert(findings.isEmpty()) + } + + @Test + fun privateConstInCompanionObject() { + // language="kotlin" + val case = + """ + class SomeTest: BaseTestCase() { + private companion object { + private const val INDEX_OF_ELEMENT = 1 + } + } + """.trimIndent() + + val findings = rule.lint(case) + + assert(findings.size == 1) + } + + @Test + fun privateVariableInCompanionObject() { + // language="kotlin" + val case = + """ + class SomeTest: BaseTestCase() { + private companion object { + private val variable = emptyList() + } + } + """.trimIndent() + + val findings = rule.lint(case) + + assert(findings.size == 1) + } + + @Test + fun nonPrivateCompanionObject() { + // language="kotlin" + val case = + """ + class SomeTest: BaseTestCase() { + companion object { + const val INDEX_OF_ELEMENT = 1 + } + } + """.trimIndent() + + val findings = rule.lint(case) + + assert(findings.size == 1) + } + + @Test + fun nonPrivateFunction() { + // language="kotlin" + val case = + """ + class SomeTest: BaseTestCase() { + fun TestContext.checkPhotoDisplay() { } + } + """.trimIndent() + + val findings = rule.lint(case) + + assert(findings.size == 1) + } + + @Test + fun nonPrivateNonLocalVariable() { + // language="kotlin" + val case = + """ + class SomeTest: BaseTestCase() { + val queueResponses = emptyList() + } + """.trimIndent() + + val findings = rule.lint(case) + + assert(findings.size == 1) + } + + @Test + fun unspecifiedBaseClass() { + // language="kotlin" + val case = + """ + class SomeTest: BaseTest() { + val queueResponses = emptyList() + } + """.trimIndent() + + val findings = rule.lint(case) + + assert(findings.isEmpty()) + } +} \ No newline at end of file diff --git a/src/test/kotlin/com/yandex/detekt/rule/ui_tests/TestMethodNamingTests.kt b/src/test/kotlin/com/yandex/detekt/rule/ui_tests/TestMethodNamingTests.kt new file mode 100644 index 0000000..ba8de3c --- /dev/null +++ b/src/test/kotlin/com/yandex/detekt/rule/ui_tests/TestMethodNamingTests.kt @@ -0,0 +1,80 @@ +/* + * 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.test.TestConfig +import io.gitlab.arturbosch.detekt.test.lint +import kotlin.test.Test + +class TestMethodNamingTests { + + private val rule = TestMethodNamingRule(TestConfig(WORDS_PARAMETER_NAME to wordsParameterValue)) + + private companion object { + const val WORDS_PARAMETER_NAME = "unexpectedWords" + val wordsParameterValue = listOf("test") + } + + @Test + fun containsRedundantWord() { + // language="kotlin" + val case = + """ + class SomeTest: BaseTestCase() { + @Test fun shouldDoSomethingTest() { } + } + """.trimIndent() + + val findings = rule.lint(case) + + assert(findings.size == 1) + } + + @Test + fun lengthExceedsParameterValue() { + val longPackage = ".folder".repeat(15) + // language="kotlin" + val case = + """ + package com.yandex$longPackage + class SomeTest: BaseTestCase() { + @Test fun shouldDoSomething() { } + } + """.trimIndent() + + val findings = rule.lint(case) + + assert(findings.size == 1) + } + + @Test + fun mainCaseWithoutViolations() { + val case = + """ + package com.yandex.tests + class SomeTest: BaseTestCase() { + @Test fun shouldDoSomething() { } + } + """.trimIndent() + + val findings = rule.lint(case) + + assert(findings.isEmpty()) + } + + @Test + fun consistsOfOneWord() { + val case = + """ + package com.yandex.tests + class SomeTest: BaseTestCase() { + @Test fun run() { } + } + """.trimIndent() + + val findings = rule.lint(case) + + assert(findings.size == 1) + } +} \ No newline at end of file