Search in sources :

Example 1 with ClientInputFile

use of org.sonarsource.sonarlint.core.analysis.api.ClientInputFile in project sonarlint-core by SonarSource.

the class StandaloneIssueMediumTests method simpleJavaWithCommaInClasspath.

// SLCORE-350
@Disabled("Can be enabled when sonar-java is fixed with https://github.com/SonarSource/sonar-java/pull/3886")
@Test
void simpleJavaWithCommaInClasspath() throws Exception {
    ClientInputFile inputFile = prepareInputFile(A_JAVA_FILE_PATH, "public class Foo {\n" + "  public void foo() {\n" + "    int x;\n" + "  }\n" + "}", false);
    final List<Issue> issues = new ArrayList<>();
    sonarlint.analyze(StandaloneAnalysisConfiguration.builder().setBaseDir(baseDir.toPath()).addInputFile(inputFile).putExtraProperty("sonar.java.libraries", "\"" + Paths.get("target/lib/guava,with,comma.jar").toAbsolutePath().toString() + "\"").build(), issues::add, null, null);
    assertThat(issues).extracting(Issue::getRuleKey, Issue::getStartLine, Issue::getStartLineOffset, Issue::getEndLine, Issue::getEndLineOffset, i -> i.getInputFile().relativePath(), Issue::getSeverity).containsOnly(tuple("java:S1220", null, null, null, null, A_JAVA_FILE_PATH, "MINOR"), tuple("java:S1481", 3, 8, 3, 9, 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) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) OnDiskTestClientInputFile(testutils.OnDiskTestClientInputFile) ClientInputFile(org.sonarsource.sonarlint.core.analysis.api.ClientInputFile) Test(org.junit.jupiter.api.Test) Disabled(org.junit.jupiter.api.Disabled)

Example 2 with ClientInputFile

use of org.sonarsource.sonarlint.core.analysis.api.ClientInputFile in project sonarlint-core by SonarSource.

the class StandaloneIssueMediumTests method lazy_init_file_metadata.

@Test
void lazy_init_file_metadata() throws Exception {
    final var inputFile1 = prepareInputFile(A_JAVA_FILE_PATH, "public class Foo {\n" + "  public void foo() {\n" + "    int x;\n" + "    System.out.println(\"Foo\");\n" + "    System.out.println(\"Foo\"); //NOSONAR\n" + "  }\n" + "}", false);
    var unexistingPath = new File(baseDir, "missing.bin");
    assertThat(unexistingPath).doesNotExist();
    ClientInputFile inputFile2 = new OnDiskTestClientInputFile(unexistingPath.toPath(), "missing.bin", false, StandardCharsets.UTF_8, null);
    final List<Issue> issues = new ArrayList<>();
    final List<String> logs = new CopyOnWriteArrayList<>();
    var analysisResults = sonarlint.analyze(StandaloneAnalysisConfiguration.builder().setBaseDir(baseDir.toPath()).addInputFiles(inputFile1, inputFile2).build(), issues::add, (m, l) -> logs.add(m), null);
    assertThat(analysisResults.failedAnalysisFiles()).isEmpty();
    assertThat(analysisResults.indexedFileCount()).isEqualTo(2);
    assertThat(logs).contains("Initializing metadata of file " + inputFile1.uri()).doesNotContain("Initializing metadata of file " + inputFile2.uri());
}
Also used : Issue(org.sonarsource.sonarlint.core.client.api.common.analysis.Issue) OnDiskTestClientInputFile(testutils.OnDiskTestClientInputFile) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) OnDiskTestClientInputFile(testutils.OnDiskTestClientInputFile) ClientInputFile(org.sonarsource.sonarlint.core.analysis.api.ClientInputFile) OnDiskTestClientInputFile(testutils.OnDiskTestClientInputFile) ClientInputFile(org.sonarsource.sonarlint.core.analysis.api.ClientInputFile) File(java.io.File) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) Test(org.junit.jupiter.api.Test)

Example 3 with ClientInputFile

use of org.sonarsource.sonarlint.core.analysis.api.ClientInputFile in project sonarlint-core by SonarSource.

the class StandaloneIssueMediumTests method useRelativePathToEvaluatePathPatterns.

// SLCORE-162
@Test
void useRelativePathToEvaluatePathPatterns() throws Exception {
    // Temporary file doesn't have the correct file suffix
    final var file = new File(baseDir, "foo.tmp");
    FileUtils.write(file, "def my_function(name):\n" + "    print \"Hello\"\n" + "    print \"world!\" # NOSONAR\n" + "\n", StandardCharsets.UTF_8);
    ClientInputFile inputFile = new OnDiskTestClientInputFile(file.toPath(), "foo.py", false, StandardCharsets.UTF_8, null);
    final List<Issue> issues = new ArrayList<>();
    sonarlint.analyze(StandaloneAnalysisConfiguration.builder().setBaseDir(baseDir.toPath()).addInputFile(inputFile).build(), issues::add, null, null);
    assertThat(issues).extracting(Issue::getRuleKey, Issue::getStartLine, i -> i.getInputFile().relativePath()).containsOnly(tuple("python:PrintStatementUsage", 2, "foo.py"));
}
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) OnDiskTestClientInputFile(testutils.OnDiskTestClientInputFile) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) OnDiskTestClientInputFile(testutils.OnDiskTestClientInputFile) ClientInputFile(org.sonarsource.sonarlint.core.analysis.api.ClientInputFile) OnDiskTestClientInputFile(testutils.OnDiskTestClientInputFile) ClientInputFile(org.sonarsource.sonarlint.core.analysis.api.ClientInputFile) File(java.io.File) Test(org.junit.jupiter.api.Test)

Example 4 with ClientInputFile

use of org.sonarsource.sonarlint.core.analysis.api.ClientInputFile in project sonarlint-core by SonarSource.

the class ConnectedAnalysisConfigurationTests method testToString.

@Test
void testToString(@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"));
    ClientInputFile inputFile = new TestClientInputFile(temp, srcFile1, false, StandardCharsets.UTF_8, null);
    ClientInputFile testInputFile = new TestClientInputFile(temp, srcFile2, true, StandardCharsets.UTF_8, null);
    var baseDir = createDirectory(temp.resolve("baseDir"));
    var config = ConnectedAnalysisConfiguration.builder().setProjectKey("foo").setBaseDir(baseDir).addInputFiles(inputFile, testInputFile).putAllExtraProperties(props).build();
    assertThat(config.toString()).isEqualTo("[\n" + "  projectKey: foo\n" + "  baseDir: " + baseDir.toString() + "\n" + "  extraProperties: {sonar.java.libraries=foo bar}\n" + "  moduleKey: null\n" + "  inputFiles: [\n" + "    " + srcFile1.toUri().toString() + " (UTF-8)\n" + "    " + srcFile2.toUri().toString() + " (UTF-8) [test]\n" + "  ]\n" + "]\n");
    assertThat(config.baseDir()).isEqualTo(baseDir);
    assertThat(config.inputFiles()).containsExactly(inputFile, testInputFile);
    assertThat(config.projectKey()).isEqualTo("foo");
    assertThat(config.extraProperties()).containsExactly(entry("sonar.java.libraries", "foo bar"));
    config = ConnectedAnalysisConfiguration.builder().setBaseDir(baseDir).addInputFiles(inputFile, testInputFile).putAllExtraProperties(props).build();
    assertThat(config.toString()).isEqualTo("[\n" + "  baseDir: " + baseDir.toString() + "\n" + "  extraProperties: {sonar.java.libraries=foo bar}\n" + "  moduleKey: null\n" + "  inputFiles: [\n" + "    " + srcFile1.toUri().toString() + " (UTF-8)\n" + "    " + srcFile2.toUri().toString() + " (UTF-8) [test]\n" + "  ]\n" + "]\n");
}
Also used : HashMap(java.util.HashMap) TestClientInputFile(testutils.TestClientInputFile) ClientInputFile(org.sonarsource.sonarlint.core.analysis.api.ClientInputFile) TestClientInputFile(testutils.TestClientInputFile) Test(org.junit.jupiter.api.Test)

Example 5 with ClientInputFile

use of org.sonarsource.sonarlint.core.analysis.api.ClientInputFile 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"));
}
Also used : HashMap(java.util.HashMap) RuleKey(org.sonarsource.sonarlint.core.client.api.common.RuleKey) ClientInputFile(org.sonarsource.sonarlint.core.analysis.api.ClientInputFile) TestClientInputFile(testutils.TestClientInputFile) TestClientInputFile(testutils.TestClientInputFile) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.jupiter.api.Test)

Aggregations

ClientInputFile (org.sonarsource.sonarlint.core.analysis.api.ClientInputFile)17 Test (org.junit.jupiter.api.Test)16 OnDiskTestClientInputFile (testutils.OnDiskTestClientInputFile)14 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)4 InputFile (org.sonar.api.batch.fs.InputFile)4 InMemoryTestClientInputFile (testutils.InMemoryTestClientInputFile)4 File (java.io.File)3 Map (java.util.Map)3 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)3 RuleKey (org.sonarsource.sonarlint.core.client.api.common.RuleKey)3 Issue (org.sonarsource.sonarlint.core.client.api.common.analysis.Issue)3 IOException (java.io.IOException)2 Charset (java.nio.charset.Charset)2 StandardCharsets (java.nio.charset.StandardCharsets)2 Files (java.nio.file.Files)2 Path (java.nio.file.Path)2 Paths (java.nio.file.Paths)2 Collection (java.util.Collection)2 Collections (java.util.Collections)2