Search in sources :

Example 1 with RulesDefinitionExtractor

use of org.sonarsource.sonarlint.core.rule.extractor.RulesDefinitionExtractor in project sonarlint-core by SonarSource.

the class RuleExtractorMediumTests method extractAllRules.

@Test
void extractAllRules() throws Exception {
    Set<Language> enabledLanguages = Set.of(Language.values());
    var config = new PluginInstancesRepository.Configuration(allJars, enabledLanguages, empty());
    try (var pluginInstancesRepository = new PluginInstancesRepository(config)) {
        var allRules = new RulesDefinitionExtractor().extractRules(pluginInstancesRepository, enabledLanguages, false);
        if (COMMERCIAL_ENABLED) {
            assertThat(allJars).hasSize(19);
            assertThat(allRules).hasSize(ALL_RULES_COUNT_WITH_COMMERCIAL);
            assertThat(logTester.logs(ClientLogOutput.Level.WARN)).containsExactlyInAnyOrder("Plugin 'rpg' embeds dependencies. This will be deprecated soon. Plugin should be updated.", "Plugin 'cobol' embeds dependencies. This will be deprecated soon. Plugin should be updated.", "Plugin 'swift' embeds dependencies. This will be deprecated soon. Plugin should be updated.");
        } else {
            assertThat(allJars).hasSize(10);
            assertThat(allRules).hasSize(ALL_RULES_COUNT_WITHOUT_COMMERCIAL);
        }
        var pythonRule = allRules.stream().filter(r -> r.getKey().equals("python:S139")).findFirst();
        assertThat(pythonRule).hasValueSatisfying(rule -> {
            assertThat(rule.getKey()).isEqualTo("python:S139");
            assertThat(rule.getType()).isEqualTo("CODE_SMELL");
            assertThat(rule.getSeverity()).isEqualTo("MINOR");
            assertThat(rule.getLanguage()).isEqualTo(Language.PYTHON);
            assertThat(rule.getName()).isEqualTo("Comments should not be located at the end of lines of code");
            assertThat(rule.isActiveByDefault()).isFalse();
            assertThat(rule.getParams()).hasSize(1).hasEntrySatisfying("legalTrailingCommentPattern", param -> {
                assertThat(param.defaultValue()).isEqualTo("^#\\s*+[^\\s]++$");
                assertThat(param.description()).isNull();
                assertThat(param.key()).isEqualTo("legalTrailingCommentPattern");
                assertThat(param.multiple()).isFalse();
                assertThat(param.name()).isEqualTo("legalTrailingCommentPattern");
                assertThat(param.possibleValues()).isEmpty();
                assertThat(param.type()).isEqualTo(SonarLintRuleParamType.STRING);
            });
            assertThat(rule.getDefaultParams()).containsOnly(entry("legalTrailingCommentPattern", "^#\\s*+[^\\s]++$"));
            assertThat(rule.getDeprecatedKeys()).isEmpty();
            assertThat(rule.getHtmlDescription()).contains("<p>This rule verifies that single-line comments are not located");
            assertThat(rule.getTags()).containsOnly("convention");
        });
    }
}
Also used : ClientLogOutput(org.sonarsource.sonarlint.core.commons.log.ClientLogOutput) Optional.empty(java.util.Optional.empty) Files(java.nio.file.Files) PluginInstancesRepository(org.sonarsource.sonarlint.core.plugin.commons.PluginInstancesRepository) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Language(org.sonarsource.sonarlint.core.commons.Language) Set(java.util.Set) IOException(java.io.IOException) SonarLintRuleDefinition(org.sonarsource.sonarlint.core.rule.extractor.SonarLintRuleDefinition) Assertions.entry(org.assertj.core.api.Assertions.entry) Test(org.junit.jupiter.api.Test) SonarLintRuleParamType(org.sonarsource.sonarlint.core.rule.extractor.SonarLintRuleParamType) Paths(java.nio.file.Paths) BeforeAll(org.junit.jupiter.api.BeforeAll) RegisterExtension(org.junit.jupiter.api.extension.RegisterExtension) RulesDefinitionExtractor(org.sonarsource.sonarlint.core.rule.extractor.RulesDefinitionExtractor) SonarLintLogTester(org.sonarsource.sonarlint.core.commons.log.SonarLintLogTester) Path(java.nio.file.Path) EnumSet(java.util.EnumSet) Collectors.toSet(java.util.stream.Collectors.toSet) Language(org.sonarsource.sonarlint.core.commons.Language) PluginInstancesRepository(org.sonarsource.sonarlint.core.plugin.commons.PluginInstancesRepository) RulesDefinitionExtractor(org.sonarsource.sonarlint.core.rule.extractor.RulesDefinitionExtractor) Test(org.junit.jupiter.api.Test)

Example 2 with RulesDefinitionExtractor

use of org.sonarsource.sonarlint.core.rule.extractor.RulesDefinitionExtractor in project sonarlint-core by SonarSource.

the class RuleExtractorMediumTests method loadNoRuleIfThereIsNoPlugin.

@Test
void loadNoRuleIfThereIsNoPlugin() throws Exception {
    Set<Language> enabledLanguages = Set.of(Language.values());
    var config = new PluginInstancesRepository.Configuration(Set.of(), enabledLanguages, empty());
    try (var pluginInstancesRepository = new PluginInstancesRepository(config)) {
        var allRules = new RulesDefinitionExtractor().extractRules(pluginInstancesRepository, enabledLanguages, false);
        assertThat(allRules).isEmpty();
    }
}
Also used : Language(org.sonarsource.sonarlint.core.commons.Language) PluginInstancesRepository(org.sonarsource.sonarlint.core.plugin.commons.PluginInstancesRepository) RulesDefinitionExtractor(org.sonarsource.sonarlint.core.rule.extractor.RulesDefinitionExtractor) Test(org.junit.jupiter.api.Test)

Example 3 with RulesDefinitionExtractor

use of org.sonarsource.sonarlint.core.rule.extractor.RulesDefinitionExtractor in project sonarlint-core by SonarSource.

the class RuleExtractorMediumTests method extractAllRules_include_rule_templates.

@Test
void extractAllRules_include_rule_templates() throws Exception {
    Set<Language> enabledLanguages = Set.of(Language.values());
    var config = new PluginInstancesRepository.Configuration(allJars, enabledLanguages, empty());
    try (var pluginInstancesRepository = new PluginInstancesRepository(config)) {
        var allRules = new RulesDefinitionExtractor().extractRules(pluginInstancesRepository, enabledLanguages, true);
        if (COMMERCIAL_ENABLED) {
            assertThat(allJars).hasSize(19);
            assertThat(allRules).hasSize(ALL_RULES_COUNT_WITH_COMMERCIAL + NON_COMMERCIAL_RULE_TEMPLATES_COUNT + COMMERCIAL_RULE_TEMPLATES_COUNT);
            assertThat(logTester.logs(ClientLogOutput.Level.WARN)).containsExactlyInAnyOrder("Plugin 'rpg' embeds dependencies. This will be deprecated soon. Plugin should be updated.", "Plugin 'cobol' embeds dependencies. This will be deprecated soon. Plugin should be updated.", "Plugin 'swift' embeds dependencies. This will be deprecated soon. Plugin should be updated.");
        } else {
            assertThat(allJars).hasSize(10);
            assertThat(allRules).hasSize(ALL_RULES_COUNT_WITHOUT_COMMERCIAL + NON_COMMERCIAL_RULE_TEMPLATES_COUNT);
        }
    }
}
Also used : Language(org.sonarsource.sonarlint.core.commons.Language) PluginInstancesRepository(org.sonarsource.sonarlint.core.plugin.commons.PluginInstancesRepository) RulesDefinitionExtractor(org.sonarsource.sonarlint.core.rule.extractor.RulesDefinitionExtractor) Test(org.junit.jupiter.api.Test)

Example 4 with RulesDefinitionExtractor

use of org.sonarsource.sonarlint.core.rule.extractor.RulesDefinitionExtractor in project sonarlint-core by SonarSource.

the class RuleExtractorMediumTests method onlyLoadRulesOfEnabledLanguages.

@Test
void onlyLoadRulesOfEnabledLanguages() throws Exception {
    Set<Language> enabledLanguages = EnumSet.of(Language.JAVA, // Enable JS but not TS
    Language.JS, Language.PHP, Language.PYTHON);
    if (COMMERCIAL_ENABLED) {
        // Enable C but not C++
        enabledLanguages.add(Language.C);
    }
    var config = new PluginInstancesRepository.Configuration(allJars, enabledLanguages, empty());
    try (var pluginInstancesRepository = new PluginInstancesRepository(config)) {
        var allRules = new RulesDefinitionExtractor().extractRules(pluginInstancesRepository, enabledLanguages, false);
        assertThat(allRules.stream().map(SonarLintRuleDefinition::getLanguage)).hasSameElementsAs(enabledLanguages);
    }
}
Also used : SonarLintRuleDefinition(org.sonarsource.sonarlint.core.rule.extractor.SonarLintRuleDefinition) Language(org.sonarsource.sonarlint.core.commons.Language) PluginInstancesRepository(org.sonarsource.sonarlint.core.plugin.commons.PluginInstancesRepository) RulesDefinitionExtractor(org.sonarsource.sonarlint.core.rule.extractor.RulesDefinitionExtractor) Test(org.junit.jupiter.api.Test)

Aggregations

Test (org.junit.jupiter.api.Test)4 Language (org.sonarsource.sonarlint.core.commons.Language)4 PluginInstancesRepository (org.sonarsource.sonarlint.core.plugin.commons.PluginInstancesRepository)4 RulesDefinitionExtractor (org.sonarsource.sonarlint.core.rule.extractor.RulesDefinitionExtractor)4 SonarLintRuleDefinition (org.sonarsource.sonarlint.core.rule.extractor.SonarLintRuleDefinition)2 IOException (java.io.IOException)1 Files (java.nio.file.Files)1 Path (java.nio.file.Path)1 Paths (java.nio.file.Paths)1 EnumSet (java.util.EnumSet)1 Optional.empty (java.util.Optional.empty)1 Set (java.util.Set)1 Collectors.toSet (java.util.stream.Collectors.toSet)1 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)1 Assertions.entry (org.assertj.core.api.Assertions.entry)1 BeforeAll (org.junit.jupiter.api.BeforeAll)1 RegisterExtension (org.junit.jupiter.api.extension.RegisterExtension)1 ClientLogOutput (org.sonarsource.sonarlint.core.commons.log.ClientLogOutput)1 SonarLintLogTester (org.sonarsource.sonarlint.core.commons.log.SonarLintLogTester)1 SonarLintRuleParamType (org.sonarsource.sonarlint.core.rule.extractor.SonarLintRuleParamType)1