Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update RulesConfigYamlTest #307

Merged
merged 9 commits into from
Sep 23, 2020
3 changes: 3 additions & 0 deletions diktat-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,9 @@
- name: WRONG_MULTIPLE_MODIFIERS_ORDER
enabled: true
configuration: {}
- name: CONFUSING_IDENTIFIER_NAMING
enabled: true
configuration: {}
- name: LOCAL_VARIABLE_EARLY_DECLARATION
enabled: true
configuration: {}
Expand Down
3 changes: 3 additions & 0 deletions diktat-rules/src/main/resources/diktat-analysis-huawei.yml
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,9 @@
- name: WRONG_MULTIPLE_MODIFIERS_ORDER
enabled: true
configuration: {}
- name: CONFUSING_IDENTIFIER_NAMING
enabled: true
configuration: {}
- name: LOCAL_VARIABLE_EARLY_DECLARATION
enabled: true
configuration: {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,36 @@ import org.cqfn.diktat.common.config.rules.getRuleConfig
import org.cqfn.diktat.ruleset.constants.Warnings
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Test
import java.io.File

/**
* Special test that checks that developer has not forgotten to add his warning to a diktat-analysis.yml
* This file is needed to be in tact with latest changes in Warnings.kt
*/
class RulesConfigJsonTest {
class RulesConfigYamlTest {
@Test
fun `read rules config json`() {
val allRulesFromConfig = readAllRulesFromConfig()
fun `read rules config yml`() {
compareRulesAndConfig("diktat-analysis.yml")
compareRulesAndConfig("diktat-analysis-huawei.yml")
val thirdConfig = "${System.getProperty("user.dir")}${File.separator}..${File.separator}diktat-analysis.yml${File.separator}"
compareRulesAndConfig(thirdConfig, "diktat-analysis.yml")
}

private fun compareRulesAndConfig(nameConfig: String, nameConfigToText: String? = null) {
val allRulesFromConfig = readAllRulesFromConfig(nameConfig)
val allRulesFromCode = readAllRulesFromCode()

allRulesFromCode.forEach { rule ->
val foundRule = allRulesFromConfig.getRuleConfig(rule)
val jsonCodeSnippet = RulesConfig(rule.ruleName(), true, mapOf())
val ymlCodeSnippet = RulesConfig(rule.ruleName(), true, mapOf())
val jacksonMapper = jacksonObjectMapper()

val ruleJson = jacksonMapper.writeValueAsString(jsonCodeSnippet)
val ruleYaml = jacksonMapper.writeValueAsString(ymlCodeSnippet)
Assertions.assertTrue(foundRule != null) {
"""
Cannot find warning ${rule.ruleName()} in rules-config.json.
You can fix it by adding the following code below to rules-config.json:
$ruleJson
Cannot find warning ${rule.ruleName()} in ${nameConfigToText ?: nameConfig}.
You can fix it by adding the following code below to ${nameConfigToText ?: nameConfig}:
$ruleYaml
"""
}
}
Expand All @@ -38,15 +46,15 @@ class RulesConfigJsonTest {
val ruleFound = allRulesFromCode.find { it.ruleName() == warningName || warningName == "DIKTAT_COMMON" } != null
Assertions.assertTrue(ruleFound) {
"""
Found rule (warning) in rules-config.json: <$warningName> that does not exist in the code. Misprint or configuration was renamed?
Found rule (warning) in ${nameConfigToText ?: nameConfig}: <$warningName> that does not exist in the code. Misprint or configuration was renamed?
""".trimIndent()
}
}
}

private fun readAllRulesFromConfig() =
private fun readAllRulesFromConfig(nameConfig: String) =
RulesConfigReader(javaClass.classLoader)
.readResource("diktat-analysis.yml") ?: listOf()
.readResource(nameConfig) ?: listOf()

private fun readAllRulesFromCode() =
Warnings.values()
Expand Down