From 5b0e3c1180bee633b8ad7d8de73c837a94e8658a Mon Sep 17 00:00:00 2001 From: kentr0w Date: Tue, 11 May 2021 18:03:41 +0300 Subject: [PATCH 1/2] Disable Magic Number in test ### What's done: Added option Added test --- diktat-analysis.yml | 2 ++ .../ruleset/rules/chapter3/MagicNumberRule.kt | 13 +++++++++- .../main/resources/diktat-analysis-huawei.yml | 2 ++ .../src/main/resources/diktat-analysis.yml | 2 ++ .../chapter3/MagicNumberRuleWarnTest.kt | 26 +++++++++++++++++++ 5 files changed, 44 insertions(+), 1 deletion(-) diff --git a/diktat-analysis.yml b/diktat-analysis.yml index 14bfcb612c..1f107ac579 100644 --- a/diktat-analysis.yml +++ b/diktat-analysis.yml @@ -304,6 +304,8 @@ - name: MAGIC_NUMBER enabled: true configuration: + # Ignore numbers from test + ignoreTest: "true" # Ignore numbers ignoreNumbers: "-1, 1, 0, 2" # Is ignore override hashCode function diff --git a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/MagicNumberRule.kt b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/MagicNumberRule.kt index 643362eb52..6075cbd386 100644 --- a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/MagicNumberRule.kt +++ b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/MagicNumberRule.kt @@ -18,6 +18,7 @@ import com.pinterest.ktlint.core.ast.ElementType.PROPERTY import com.pinterest.ktlint.core.ast.ElementType.RANGE import com.pinterest.ktlint.core.ast.ElementType.VALUE_PARAMETER import com.pinterest.ktlint.core.ast.parent +import org.cqfn.diktat.common.config.rules.getCommonConfiguration import org.jetbrains.kotlin.com.intellij.lang.ASTNode import org.jetbrains.kotlin.psi.KtFunction import org.jetbrains.kotlin.psi.KtProperty @@ -38,8 +39,12 @@ class MagicNumberRule(configRules: List) : DiktatRule( ) } override fun logic(node: ASTNode) { + val filePath = node.getRootNode().getFilePath() + val config = configRules.getCommonConfiguration() if (node.elementType == INTEGER_CONSTANT || node.elementType == FLOAT_CONSTANT) { - checkNumber(node, configuration) + if (!isLocatedInTest(filePath.splitPathToDirs(), config.testAnchors) || !configuration.isIgnoreTest) { + checkNumber(node, configuration) + } } } @@ -76,6 +81,11 @@ class MagicNumberRule(configRules: List) : DiktatRule( * [RuleConfiguration] for configuration */ class MagicNumberConfiguration(config: Map) : RuleConfiguration(config) { + /** + * Flag to ignore numbers from test + */ + val isIgnoreTest = config["ignoreTest"]?.toBoolean() ?: ignoreTest + /** * List of ignored numbers */ @@ -95,6 +105,7 @@ class MagicNumberRule(configRules: List) : DiktatRule( } companion object { + const val ignoreTest = true val ignoreNumbersList = listOf("-1", "1", "0", "2") val mapConfiguration = mapOf( "ignoreHashCodeFunction" to true, diff --git a/diktat-rules/src/main/resources/diktat-analysis-huawei.yml b/diktat-rules/src/main/resources/diktat-analysis-huawei.yml index 9354b65640..7ea3341699 100644 --- a/diktat-rules/src/main/resources/diktat-analysis-huawei.yml +++ b/diktat-rules/src/main/resources/diktat-analysis-huawei.yml @@ -304,6 +304,8 @@ - name: MAGIC_NUMBER enabled: true configuration: + # Ignore numbers from test + ignoreTest: "true" # Ignore numbers ignoreNumbers: "-1, 1, 0, 2" # Is ignore override hashCode function diff --git a/diktat-rules/src/main/resources/diktat-analysis.yml b/diktat-rules/src/main/resources/diktat-analysis.yml index 3f953a53ed..b6a1f62d55 100644 --- a/diktat-rules/src/main/resources/diktat-analysis.yml +++ b/diktat-rules/src/main/resources/diktat-analysis.yml @@ -300,6 +300,8 @@ - name: MAGIC_NUMBER enabled: true configuration: + # Ignore numbers from test + ignoreTest: "true" # Ignore numbers ignoreNumbers: "-1, 1, 0, 2" # Is ignore override hashCode function diff --git a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter3/MagicNumberRuleWarnTest.kt b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter3/MagicNumberRuleWarnTest.kt index baffac256c..91ee368201 100644 --- a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter3/MagicNumberRuleWarnTest.kt +++ b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter3/MagicNumberRuleWarnTest.kt @@ -181,4 +181,30 @@ class MagicNumberRuleWarnTest : LintTestBase(::MagicNumberRule) { rulesConfigList = rulesConfigMagicNumber ) } + + @Test + @Tag(WarningNames.MAGIC_NUMBER) + fun `check ignore numbers in test`() { + lintMethod( + """ + |fun f1oo() { + | val m = -1 + | val a: Byte = 4 + | val b = 0xff + |} + | + |enum class A(b:Int) { + | A(-240), + | B(50), + | C(3), + |} + |@Override + |fun hashCode(): Int { + | return 32 + |} + """.trimMargin(), + fileName = "src/test/kotlin/org/cqfn/diktat/test/hehe/MagicNumberTest.kt", + rulesConfigList = rulesConfigIgnoreNumbersMagicNumber, + ) + } } From 3135799f7f5d0ebe8b39b6cd9413bdd448920399 Mon Sep 17 00:00:00 2001 From: kentr0w Date: Wed, 12 May 2021 10:44:09 +0300 Subject: [PATCH 2/2] Disable Magic Number in test ### What's done: Fixed according to our code style --- .../cqfn/diktat/ruleset/rules/chapter3/MagicNumberRule.kt | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/MagicNumberRule.kt b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/MagicNumberRule.kt index 6075cbd386..549fd4d8e4 100644 --- a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/MagicNumberRule.kt +++ b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/MagicNumberRule.kt @@ -2,6 +2,7 @@ package org.cqfn.diktat.ruleset.rules.chapter3 import org.cqfn.diktat.common.config.rules.RuleConfiguration import org.cqfn.diktat.common.config.rules.RulesConfig +import org.cqfn.diktat.common.config.rules.getCommonConfiguration import org.cqfn.diktat.common.config.rules.getRuleConfig import org.cqfn.diktat.ruleset.constants.Warnings.MAGIC_NUMBER import org.cqfn.diktat.ruleset.rules.DiktatRule @@ -18,7 +19,6 @@ import com.pinterest.ktlint.core.ast.ElementType.PROPERTY import com.pinterest.ktlint.core.ast.ElementType.RANGE import com.pinterest.ktlint.core.ast.ElementType.VALUE_PARAMETER import com.pinterest.ktlint.core.ast.parent -import org.cqfn.diktat.common.config.rules.getCommonConfiguration import org.jetbrains.kotlin.com.intellij.lang.ASTNode import org.jetbrains.kotlin.psi.KtFunction import org.jetbrains.kotlin.psi.KtProperty @@ -38,6 +38,7 @@ class MagicNumberRule(configRules: List) : DiktatRule( configRules.getRuleConfig(MAGIC_NUMBER)?.configuration ?: emptyMap() ) } + @Suppress("COLLAPSE_IF_STATEMENTS") override fun logic(node: ASTNode) { val filePath = node.getRootNode().getFilePath() val config = configRules.getCommonConfiguration() @@ -84,7 +85,7 @@ class MagicNumberRule(configRules: List) : DiktatRule( /** * Flag to ignore numbers from test */ - val isIgnoreTest = config["ignoreTest"]?.toBoolean() ?: ignoreTest + val isIgnoreTest = config["ignoreTest"]?.toBoolean() ?: IGNORE_TEST /** * List of ignored numbers @@ -105,7 +106,7 @@ class MagicNumberRule(configRules: List) : DiktatRule( } companion object { - const val ignoreTest = true + const val IGNORE_TEST = true val ignoreNumbersList = listOf("-1", "1", "0", "2") val mapConfiguration = mapOf( "ignoreHashCodeFunction" to true,