use of org.sonarsource.sonarlint.core.client.api.common.RuleKey in project sonarlint-core by SonarSource.
the class StandaloneSonarLintEngineImpl method identifyActiveRules.
private Collection<ActiveRule> identifyActiveRules(StandaloneAnalysisConfiguration configuration) {
Set<String> excludedRules = configuration.excludedRules().stream().map(RuleKey::toString).collect(toSet());
Set<String> includedRules = configuration.includedRules().stream().map(RuleKey::toString).filter(r -> !excludedRules.contains(r)).collect(toSet());
Collection<SonarLintRuleDefinition> filteredActiveRules = new ArrayList<>();
filteredActiveRules.addAll(allRulesDefinitionsByKey.values().stream().filter(SonarLintRuleDefinition::isActiveByDefault).filter(isExcludedByConfiguration(excludedRules)).collect(Collectors.toList()));
filteredActiveRules.addAll(allRulesDefinitionsByKey.values().stream().filter(r -> !r.isActiveByDefault()).filter(isIncludedByConfiguration(includedRules)).collect(Collectors.toList()));
return filteredActiveRules.stream().map(rd -> {
var activeRule = new ActiveRule(rd.getKey(), rd.getLanguage().getLanguageKey());
Map<String, String> effectiveParams = new HashMap<>(rd.getDefaultParams());
Optional.ofNullable(configuration.ruleParameters().get(RuleKey.parse(rd.getKey()))).ifPresent(effectiveParams::putAll);
activeRule.setParams(effectiveParams);
return activeRule;
}).collect(Collectors.toList());
}
use of org.sonarsource.sonarlint.core.client.api.common.RuleKey in project sonarlint-core by SonarSource.
the class StandaloneIssueMediumTests method simpleJavaWithIncludedRulesUsingDeprecatedKey.
@Test
void simpleJavaWithIncludedRulesUsingDeprecatedKey() throws Exception {
var inputFile = prepareInputFile(A_JAVA_FILE_PATH, "import java.util.Optional;\n" + "public class Foo {\n" + " public void foo(Optional<String> name) { // for squid:3553, not in Sonar Way\n" + " int x;\n" + " System.out.println(\"Foo\" + name.isPresent());\n" + " }\n" + "}", false);
final Collection<RuleKey> includedRules = singleton(new RuleKey("squid", "S3553"));
List<String> logs = new CopyOnWriteArrayList<>();
final List<Issue> issues = new ArrayList<>();
sonarlint.analyze(StandaloneAnalysisConfiguration.builder().setBaseDir(baseDir.toPath()).addInputFile(inputFile).addIncludedRules(includedRules).build(), issues::add, (msg, lvl) -> logs.add(msg), null);
assertThat(issues).extracting(Issue::getRuleKey, Issue::getStartLine, i -> i.getInputFile().relativePath(), Issue::getSeverity).containsOnly(tuple("java:S3553", 3, A_JAVA_FILE_PATH, "MAJOR"), tuple("java:S106", 5, A_JAVA_FILE_PATH, "MAJOR"), tuple("java:S1220", null, A_JAVA_FILE_PATH, "MINOR"), tuple("java:S1481", 4, A_JAVA_FILE_PATH, "MINOR"));
assertThat(logs).contains("Rule 'java:S3553' was included using its deprecated key 'squid:S3553'. Please fix your configuration.");
}
use of org.sonarsource.sonarlint.core.client.api.common.RuleKey in project sonarlint-core by SonarSource.
the class StandaloneIssueMediumTests method simpleJavaWithExcludedRules.
@Test
void simpleJavaWithExcludedRules() throws Exception {
var inputFile = prepareInputFile(A_JAVA_FILE_PATH, "public class Foo {\n" + " public void foo() {\n" + " int x;\n" + " System.out.println(\"Foo\");\n" + " }\n" + "}", false);
final Collection<RuleKey> excludedRules = singleton(new RuleKey("java", "S106"));
final List<Issue> issues = new ArrayList<>();
sonarlint.analyze(StandaloneAnalysisConfiguration.builder().setBaseDir(baseDir.toPath()).addInputFile(inputFile).addExcludedRules(excludedRules).build(), issues::add, null, null);
assertThat(issues).extracting(Issue::getRuleKey, Issue::getStartLine, i -> i.getInputFile().relativePath(), Issue::getSeverity).containsOnly(tuple("java:S1220", null, A_JAVA_FILE_PATH, "MINOR"), tuple("java:S1481", 3, A_JAVA_FILE_PATH, "MINOR"));
}
use of org.sonarsource.sonarlint.core.client.api.common.RuleKey in project sonarlint-core by SonarSource.
the class StandaloneAnalysisConfigurationTests method testToString_and_getters.
@Test
void testToString_and_getters(@TempDir Path temp) throws Exception {
Map<String, String> props = new HashMap<>();
props.put("sonar.java.libraries", "foo bar");
final var srcFile1 = createDirectory(temp.resolve("src1"));
final var srcFile2 = createDirectory(temp.resolve("src2"));
final var srcFile3 = createDirectory(temp.resolve("src3"));
ClientInputFile inputFile = new TestClientInputFile(temp, srcFile1, false, StandardCharsets.UTF_8, null);
ClientInputFile inputFileWithLanguage = new TestClientInputFile(temp, srcFile2, false, StandardCharsets.UTF_8, Language.JAVA);
ClientInputFile testInputFile = new TestClientInputFile(temp, srcFile3, true, null, Language.PHP);
var baseDir = createDirectory(temp.resolve("baseDir"));
Collection<RuleKey> excludedRules = Arrays.asList(new RuleKey("squid", "S1135"), new RuleKey("squid", "S1181"));
Collection<RuleKey> includedRules = Arrays.asList(new RuleKey("javascript", "S2424"), new RuleKey("javascript", "S1442"));
Map<String, String> squidS5Parameters = new HashMap<>();
squidS5Parameters.put("s5param1", "s5value1");
squidS5Parameters.put("s5param2", "s5value2");
Map<String, String> squidS6Parameters = new HashMap<>();
squidS6Parameters.put("s6param1", "s6value1");
squidS6Parameters.put("s6param2", "s6value2");
Map<RuleKey, Map<String, String>> ruleParameters = new HashMap<>();
ruleParameters.put(RuleKey.parse("squid:S6"), squidS6Parameters);
var config = StandaloneAnalysisConfiguration.builder().setBaseDir(baseDir).addInputFile(inputFile).addInputFiles(inputFileWithLanguage).addInputFiles(Arrays.asList(testInputFile)).putAllExtraProperties(props).putExtraProperty("sonar.foo", "bar").addExcludedRules(excludedRules).addExcludedRule(RuleKey.parse("squid:S1")).addExcludedRules(RuleKey.parse("squid:S2"), RuleKey.parse("squid:S3")).addIncludedRules(includedRules).addIncludedRule(RuleKey.parse("squid:I1")).addIncludedRules(RuleKey.parse("squid:I2"), RuleKey.parse("squid:I3")).addRuleParameter(RuleKey.parse("squid:S4"), "param1", "value1").addRuleParameters(RuleKey.parse("squid:S5"), squidS5Parameters).addRuleParameters(ruleParameters).build();
assertThat(config.toString()).isEqualTo("[\n" + " baseDir: " + baseDir.toString() + "\n" + " extraProperties: {sonar.java.libraries=foo bar, sonar.foo=bar}\n" + " moduleKey: null\n" + " excludedRules: [squid:S1135, squid:S1181, squid:S1, squid:S2, squid:S3]\n" + " includedRules: [javascript:S2424, javascript:S1442, squid:I1, squid:I2, squid:I3]\n" + " ruleParameters: {squid:S4={param1=value1}, squid:S5={s5param1=s5value1, s5param2=s5value2}, squid:S6={s6param1=s6value1, s6param2=s6value2}}\n" + " inputFiles: [\n" + " " + srcFile1.toUri().toString() + " (UTF-8)\n" + " " + srcFile2.toUri().toString() + " (UTF-8) [java]\n" + " " + srcFile3.toUri().toString() + " (default) [test] [php]\n" + " ]\n" + "]\n");
assertThat(config.baseDir()).isEqualTo(baseDir);
assertThat(config.inputFiles()).containsExactly(inputFile, inputFileWithLanguage, testInputFile);
assertThat(config.extraProperties()).containsExactly(entry("sonar.java.libraries", "foo bar"), entry("sonar.foo", "bar"));
assertThat(config.excludedRules()).extracting(RuleKey::toString).containsExactly("squid:S1135", "squid:S1181", "squid:S1", "squid:S2", "squid:S3");
assertThat(config.includedRules()).extracting(RuleKey::toString).containsExactly("javascript:S2424", "javascript:S1442", "squid:I1", "squid:I2", "squid:I3");
assertThat(config.ruleParameters()).containsKeys(RuleKey.parse("squid:S4"), RuleKey.parse("squid:S5"), RuleKey.parse("squid:S6"));
}
use of org.sonarsource.sonarlint.core.client.api.common.RuleKey in project sonarlint-core by SonarSource.
the class StandaloneIssueMediumTests method simpleJavaWithIncludedRules.
@Test
void simpleJavaWithIncludedRules() throws Exception {
var inputFile = prepareInputFile(A_JAVA_FILE_PATH, "import java.util.Optional;\n" + "public class Foo {\n" + " public void foo(Optional<String> name) { // for squid:3553, not in Sonar Way\n" + " int x;\n" + " System.out.println(\"Foo\" + name.isPresent());\n" + " }\n" + "}", false);
final Collection<RuleKey> includedRules = singleton(new RuleKey("java", "S3553"));
final List<Issue> issues = new ArrayList<>();
sonarlint.analyze(StandaloneAnalysisConfiguration.builder().setBaseDir(baseDir.toPath()).addInputFile(inputFile).addIncludedRules(includedRules).build(), issues::add, null, null);
assertThat(issues).extracting(Issue::getRuleKey, Issue::getStartLine, i -> i.getInputFile().relativePath(), Issue::getSeverity).containsOnly(tuple("java:S3553", 3, A_JAVA_FILE_PATH, "MAJOR"), tuple("java:S106", 5, A_JAVA_FILE_PATH, "MAJOR"), tuple("java:S1220", null, A_JAVA_FILE_PATH, "MINOR"), tuple("java:S1481", 4, A_JAVA_FILE_PATH, "MINOR"));
}
Aggregations