Search in sources :

Example 6 with RuleKey

use of org.sonarsource.sonarlint.core.client.api.common.RuleKey in project sonarlint-core by SonarSource.

the class StandaloneIssueMediumTests method simpleJavaWithIssueOnDir.

@Test
void simpleJavaWithIssueOnDir() throws Exception {
    var inputFile = prepareInputFile("foo/Foo.java", "package foo;\n" + "public class Foo {\n" + "}", false);
    final Collection<RuleKey> includedRules = singleton(new RuleKey("java", "S1228"));
    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() != null ? i.getInputFile().relativePath() : null, Issue::getSeverity).containsOnly(tuple("java:S2094", 2, "foo/Foo.java", "MINOR"), tuple("java:S1228", null, null, "MINOR"));
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Language(org.sonarsource.sonarlint.core.commons.Language) Disabled(org.junit.jupiter.api.Disabled) StandaloneSonarLintEngineImpl(org.sonarsource.sonarlint.core.StandaloneSonarLintEngineImpl) AfterAll(org.junit.jupiter.api.AfterAll) Future(java.util.concurrent.Future) Collections.singleton(java.util.Collections.singleton) BeforeAll(org.junit.jupiter.api.BeforeAll) Map(java.util.Map) Path(java.nio.file.Path) EnumSet(java.util.EnumSet) Collection(java.util.Collection) Set(java.util.Set) StandardCharsets(java.nio.charset.StandardCharsets) Executors(java.util.concurrent.Executors) StandaloneAnalysisConfiguration(org.sonarsource.sonarlint.core.client.api.standalone.StandaloneAnalysisConfiguration) Test(org.junit.jupiter.api.Test) List(java.util.List) TempDir(org.junit.jupiter.api.io.TempDir) Issue(org.sonarsource.sonarlint.core.client.api.common.analysis.Issue) ComponentContainer(org.sonarsource.sonarlint.core.plugin.commons.pico.ComponentContainer) PluginLocator(org.sonarsource.sonarlint.core.util.PluginLocator) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) Assertions.fail(org.junit.jupiter.api.Assertions.fail) HashMap(java.util.HashMap) TestUtils(testutils.TestUtils) ArrayList(java.util.ArrayList) SonarLintModuleFileSystem(org.sonarsource.sonarlint.core.analysis.sonarapi.SonarLintModuleFileSystem) Charset(java.nio.charset.Charset) StandaloneGlobalConfiguration(org.sonarsource.sonarlint.core.client.api.standalone.StandaloneGlobalConfiguration) OnDiskTestClientInputFile(testutils.OnDiskTestClientInputFile) Assumptions.assumeTrue(org.junit.jupiter.api.Assumptions.assumeTrue) Nullable(javax.annotation.Nullable) ClientModuleInfo(org.sonarsource.sonarlint.core.analysis.api.ClientModuleInfo) Files(java.nio.file.Files) RuleDetails(org.sonarsource.sonarlint.core.client.api.common.RuleDetails) Assertions.tuple(org.assertj.core.api.Assertions.tuple) SystemUtils(org.apache.commons.lang3.SystemUtils) ClientInputFile(org.sonarsource.sonarlint.core.analysis.api.ClientInputFile) ClientFileSystemFixtures.aClientFileSystemWith(org.sonarsource.sonarlint.core.client.api.common.ClientFileSystemFixtures.aClientFileSystemWith) IOException(java.io.IOException) FileUtils(org.apache.commons.io.FileUtils) Assertions.entry(org.assertj.core.api.Assertions.entry) File(java.io.File) ExecutionException(java.util.concurrent.ExecutionException) Paths(java.nio.file.Paths) RuleKey(org.sonarsource.sonarlint.core.client.api.common.RuleKey) Collections(java.util.Collections) NodeJsHelper(org.sonarsource.sonarlint.core.NodeJsHelper) Issue(org.sonarsource.sonarlint.core.client.api.common.analysis.Issue) RuleKey(org.sonarsource.sonarlint.core.client.api.common.RuleKey) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) Test(org.junit.jupiter.api.Test)

Example 7 with RuleKey

use of org.sonarsource.sonarlint.core.client.api.common.RuleKey in project sonarlint-core by SonarSource.

the class StandaloneIssueMediumTests method simpleJavaNoHotspots.

@Test
void simpleJavaNoHotspots() throws Exception {
    assertThat(sonarlint.getAllRuleDetails()).extracting(RuleDetails::getKey).doesNotContain("java:S1313");
    assertThat(sonarlint.getRuleDetails("java:S1313")).isEmpty();
    var inputFile = prepareInputFile("foo/Foo.java", "package foo;\n" + "public class Foo {\n" + "  String ip = \"192.168.12.42\"; // Hotspots should not be reported in SonarLint\n" + "}", false);
    final List<Issue> issues = new ArrayList<>();
    sonarlint.analyze(StandaloneAnalysisConfiguration.builder().setBaseDir(baseDir.toPath()).addInputFile(inputFile).addIncludedRule(new RuleKey("java", "S1313")).build(), issues::add, null, null);
    assertThat(issues).isEmpty();
}
Also used : Issue(org.sonarsource.sonarlint.core.client.api.common.analysis.Issue) RuleKey(org.sonarsource.sonarlint.core.client.api.common.RuleKey) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) Test(org.junit.jupiter.api.Test)

Example 8 with RuleKey

use of org.sonarsource.sonarlint.core.client.api.common.RuleKey in project sonarlint-core by SonarSource.

the class StandaloneIssueMediumTests method simpleJavaWithIncludedAndExcludedRules.

@Test
void simpleJavaWithIncludedAndExcludedRules() 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:S3553, not in Sonar Way\n" + "    int x;\n" + "    System.out.println(\"Foo\" + name.isPresent());\n" + "  }\n" + "}", false);
    // exclusion wins
    final Collection<RuleKey> excludedRules = Collections.singleton(new RuleKey("squid", "S3553"));
    final Collection<RuleKey> includedRules = Collections.singleton(new RuleKey("squid", "S3553"));
    final List<Issue> issues = new ArrayList<>();
    sonarlint.analyze(StandaloneAnalysisConfiguration.builder().setBaseDir(baseDir.toPath()).addInputFile(inputFile).addExcludedRules(excludedRules).addIncludedRules(includedRules).build(), issues::add, null, null);
    assertThat(issues).extracting(Issue::getRuleKey, Issue::getStartLine, i -> i.getInputFile().relativePath(), Issue::getSeverity).containsOnly(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"));
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Language(org.sonarsource.sonarlint.core.commons.Language) Disabled(org.junit.jupiter.api.Disabled) StandaloneSonarLintEngineImpl(org.sonarsource.sonarlint.core.StandaloneSonarLintEngineImpl) AfterAll(org.junit.jupiter.api.AfterAll) Future(java.util.concurrent.Future) Collections.singleton(java.util.Collections.singleton) BeforeAll(org.junit.jupiter.api.BeforeAll) Map(java.util.Map) Path(java.nio.file.Path) EnumSet(java.util.EnumSet) Collection(java.util.Collection) Set(java.util.Set) StandardCharsets(java.nio.charset.StandardCharsets) Executors(java.util.concurrent.Executors) StandaloneAnalysisConfiguration(org.sonarsource.sonarlint.core.client.api.standalone.StandaloneAnalysisConfiguration) Test(org.junit.jupiter.api.Test) List(java.util.List) TempDir(org.junit.jupiter.api.io.TempDir) Issue(org.sonarsource.sonarlint.core.client.api.common.analysis.Issue) ComponentContainer(org.sonarsource.sonarlint.core.plugin.commons.pico.ComponentContainer) PluginLocator(org.sonarsource.sonarlint.core.util.PluginLocator) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) Assertions.fail(org.junit.jupiter.api.Assertions.fail) HashMap(java.util.HashMap) TestUtils(testutils.TestUtils) ArrayList(java.util.ArrayList) SonarLintModuleFileSystem(org.sonarsource.sonarlint.core.analysis.sonarapi.SonarLintModuleFileSystem) Charset(java.nio.charset.Charset) StandaloneGlobalConfiguration(org.sonarsource.sonarlint.core.client.api.standalone.StandaloneGlobalConfiguration) OnDiskTestClientInputFile(testutils.OnDiskTestClientInputFile) Assumptions.assumeTrue(org.junit.jupiter.api.Assumptions.assumeTrue) Nullable(javax.annotation.Nullable) ClientModuleInfo(org.sonarsource.sonarlint.core.analysis.api.ClientModuleInfo) Files(java.nio.file.Files) RuleDetails(org.sonarsource.sonarlint.core.client.api.common.RuleDetails) Assertions.tuple(org.assertj.core.api.Assertions.tuple) SystemUtils(org.apache.commons.lang3.SystemUtils) ClientInputFile(org.sonarsource.sonarlint.core.analysis.api.ClientInputFile) ClientFileSystemFixtures.aClientFileSystemWith(org.sonarsource.sonarlint.core.client.api.common.ClientFileSystemFixtures.aClientFileSystemWith) IOException(java.io.IOException) FileUtils(org.apache.commons.io.FileUtils) Assertions.entry(org.assertj.core.api.Assertions.entry) File(java.io.File) ExecutionException(java.util.concurrent.ExecutionException) Paths(java.nio.file.Paths) RuleKey(org.sonarsource.sonarlint.core.client.api.common.RuleKey) Collections(java.util.Collections) NodeJsHelper(org.sonarsource.sonarlint.core.NodeJsHelper) Issue(org.sonarsource.sonarlint.core.client.api.common.analysis.Issue) RuleKey(org.sonarsource.sonarlint.core.client.api.common.RuleKey) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) Test(org.junit.jupiter.api.Test)

Example 9 with RuleKey

use of org.sonarsource.sonarlint.core.client.api.common.RuleKey in project sonarlint-core by SonarSource.

the class StandaloneIssueMediumTests method simpleJavaWithExcludedRulesUsingDeprecatedKey.

@Test
void simpleJavaWithExcludedRulesUsingDeprecatedKey() 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("squid", "S106"));
    List<String> logs = new ArrayList<>();
    final List<Issue> issues = new ArrayList<>();
    sonarlint.analyze(StandaloneAnalysisConfiguration.builder().setBaseDir(baseDir.toPath()).addInputFile(inputFile).addExcludedRules(excludedRules).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:S1220", null, A_JAVA_FILE_PATH, "MINOR"), tuple("java:S1481", 3, A_JAVA_FILE_PATH, "MINOR"));
    assertThat(logs).contains("Rule 'java:S106' was excluded using its deprecated key 'squid:S106'. Please fix your configuration.");
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Language(org.sonarsource.sonarlint.core.commons.Language) Disabled(org.junit.jupiter.api.Disabled) StandaloneSonarLintEngineImpl(org.sonarsource.sonarlint.core.StandaloneSonarLintEngineImpl) AfterAll(org.junit.jupiter.api.AfterAll) Future(java.util.concurrent.Future) Collections.singleton(java.util.Collections.singleton) BeforeAll(org.junit.jupiter.api.BeforeAll) Map(java.util.Map) Path(java.nio.file.Path) EnumSet(java.util.EnumSet) Collection(java.util.Collection) Set(java.util.Set) StandardCharsets(java.nio.charset.StandardCharsets) Executors(java.util.concurrent.Executors) StandaloneAnalysisConfiguration(org.sonarsource.sonarlint.core.client.api.standalone.StandaloneAnalysisConfiguration) Test(org.junit.jupiter.api.Test) List(java.util.List) TempDir(org.junit.jupiter.api.io.TempDir) Issue(org.sonarsource.sonarlint.core.client.api.common.analysis.Issue) ComponentContainer(org.sonarsource.sonarlint.core.plugin.commons.pico.ComponentContainer) PluginLocator(org.sonarsource.sonarlint.core.util.PluginLocator) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) Assertions.fail(org.junit.jupiter.api.Assertions.fail) HashMap(java.util.HashMap) TestUtils(testutils.TestUtils) ArrayList(java.util.ArrayList) SonarLintModuleFileSystem(org.sonarsource.sonarlint.core.analysis.sonarapi.SonarLintModuleFileSystem) Charset(java.nio.charset.Charset) StandaloneGlobalConfiguration(org.sonarsource.sonarlint.core.client.api.standalone.StandaloneGlobalConfiguration) OnDiskTestClientInputFile(testutils.OnDiskTestClientInputFile) Assumptions.assumeTrue(org.junit.jupiter.api.Assumptions.assumeTrue) Nullable(javax.annotation.Nullable) ClientModuleInfo(org.sonarsource.sonarlint.core.analysis.api.ClientModuleInfo) Files(java.nio.file.Files) RuleDetails(org.sonarsource.sonarlint.core.client.api.common.RuleDetails) Assertions.tuple(org.assertj.core.api.Assertions.tuple) SystemUtils(org.apache.commons.lang3.SystemUtils) ClientInputFile(org.sonarsource.sonarlint.core.analysis.api.ClientInputFile) ClientFileSystemFixtures.aClientFileSystemWith(org.sonarsource.sonarlint.core.client.api.common.ClientFileSystemFixtures.aClientFileSystemWith) IOException(java.io.IOException) FileUtils(org.apache.commons.io.FileUtils) Assertions.entry(org.assertj.core.api.Assertions.entry) File(java.io.File) ExecutionException(java.util.concurrent.ExecutionException) Paths(java.nio.file.Paths) RuleKey(org.sonarsource.sonarlint.core.client.api.common.RuleKey) Collections(java.util.Collections) NodeJsHelper(org.sonarsource.sonarlint.core.NodeJsHelper) Issue(org.sonarsource.sonarlint.core.client.api.common.analysis.Issue) RuleKey(org.sonarsource.sonarlint.core.client.api.common.RuleKey) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) Test(org.junit.jupiter.api.Test)

Aggregations

RuleKey (org.sonarsource.sonarlint.core.client.api.common.RuleKey)9 ArrayList (java.util.ArrayList)8 HashMap (java.util.HashMap)8 Map (java.util.Map)8 Test (org.junit.jupiter.api.Test)8 Collection (java.util.Collection)7 Set (java.util.Set)7 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)7 Nullable (javax.annotation.Nullable)7 ClientInputFile (org.sonarsource.sonarlint.core.analysis.api.ClientInputFile)7 File (java.io.File)6 IOException (java.io.IOException)6 Charset (java.nio.charset.Charset)6 StandardCharsets (java.nio.charset.StandardCharsets)6 Files (java.nio.file.Files)6 Path (java.nio.file.Path)6 Paths (java.nio.file.Paths)6 Collections (java.util.Collections)6 Collections.singleton (java.util.Collections.singleton)6 EnumSet (java.util.EnumSet)6