Search in sources :

Example 11 with ClientInputFile

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

the class SonarLintInputFileTests method testGetters.

@Test
void testGetters(@TempDir Path path) throws IOException {
    var filePath = path.resolve("foo.php");
    Files.write(filePath, "test string".getBytes(StandardCharsets.UTF_8));
    ClientInputFile inputFile = new OnDiskTestClientInputFile(filePath, "file", false, StandardCharsets.UTF_8);
    var file = new SonarLintInputFile(inputFile, f -> new FileMetadata().readMetadata(filePath.toFile(), StandardCharsets.UTF_8));
    assertThat(file.contents()).isEqualTo("test string");
    assertThat(file.charset()).isEqualByComparingTo(StandardCharsets.UTF_8);
    assertThat(file.absolutePath()).isEqualTo(FileUtils.toSonarQubePath(inputFile.getPath()));
    assertThat(file.file()).isEqualTo(filePath.toFile());
    assertThat(file.path()).isEqualTo(filePath);
    assertThat(file.getClientInputFile()).isEqualTo(inputFile);
    assertThat(file.status()).isEqualTo(Status.ADDED);
    assertThat(file).isEqualTo(file).isNotEqualTo(mock(SonarLintInputFile.class));
    var stream = file.inputStream();
    try (var reader = new BufferedReader(new InputStreamReader(stream))) {
        assertThat(reader.lines().collect(Collectors.joining())).isEqualTo("test string");
    }
}
Also used : OnDiskTestClientInputFile(testutils.OnDiskTestClientInputFile) InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) ClientInputFile(org.sonarsource.sonarlint.core.analysis.api.ClientInputFile) InMemoryTestClientInputFile(testutils.InMemoryTestClientInputFile) OnDiskTestClientInputFile(testutils.OnDiskTestClientInputFile) Test(org.junit.jupiter.api.Test)

Example 12 with ClientInputFile

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

the class SonarLintInputFileTests method checkValidPointer.

@Test
void checkValidPointer() {
    ClientInputFile inputFile = new InMemoryTestClientInputFile("foo", "src/Foo.php", null, false, null);
    var metadata = new FileMetadata.Metadata(2, new int[] { 0, 10 }, 16);
    var file = new SonarLintInputFile(inputFile, f -> metadata);
    assertThat(file.newPointer(1, 0).line()).isEqualTo(1);
    assertThat(file.newPointer(1, 0).lineOffset()).isZero();
    // Don't fail
    file.newPointer(1, 9);
    file.newPointer(2, 0);
    file.newPointer(2, 5);
}
Also used : ClientInputFile(org.sonarsource.sonarlint.core.analysis.api.ClientInputFile) InMemoryTestClientInputFile(testutils.InMemoryTestClientInputFile) OnDiskTestClientInputFile(testutils.OnDiskTestClientInputFile) InMemoryTestClientInputFile(testutils.InMemoryTestClientInputFile) Test(org.junit.jupiter.api.Test)

Example 13 with ClientInputFile

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

the class SonarLintInputFileTests method selectLine.

@Test
void selectLine() {
    ClientInputFile inputFile = new InMemoryTestClientInputFile("foo", "src/Foo.php", null, false, null);
    var metadata = new FileMetadata().readMetadata(new ByteArrayInputStream("bla bla a\nabcde\n\nabc".getBytes(StandardCharsets.UTF_8)), StandardCharsets.UTF_8, URI.create("file://foo.php"), null);
    var file = new SonarLintInputFile(inputFile, f -> metadata);
    assertThat(file.selectLine(1).start().line()).isEqualTo(1);
    assertThat(file.selectLine(1).start().lineOffset()).isZero();
    assertThat(file.selectLine(1).end().line()).isEqualTo(1);
    assertThat(file.selectLine(1).end().lineOffset()).isEqualTo(9);
    // Don't fail when selecting empty line
    assertThat(file.selectLine(3).start().line()).isEqualTo(3);
    assertThat(file.selectLine(3).start().lineOffset()).isZero();
    assertThat(file.selectLine(3).end().line()).isEqualTo(3);
    assertThat(file.selectLine(3).end().lineOffset()).isZero();
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ClientInputFile(org.sonarsource.sonarlint.core.analysis.api.ClientInputFile) InMemoryTestClientInputFile(testutils.InMemoryTestClientInputFile) OnDiskTestClientInputFile(testutils.OnDiskTestClientInputFile) InMemoryTestClientInputFile(testutils.InMemoryTestClientInputFile) Test(org.junit.jupiter.api.Test)

Example 14 with ClientInputFile

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

the class SonarLintInputFileTests method testRangeOverlap.

@Test
void testRangeOverlap() {
    ClientInputFile inputFile = new InMemoryTestClientInputFile("foo", "src/Foo.php", null, false, null);
    var metadata = new FileMetadata.Metadata(2, new int[] { 0, 10 }, 16);
    var file = new SonarLintInputFile(inputFile, f -> metadata);
    // Don't fail
    assertThat(file.newRange(file.newPointer(1, 0), file.newPointer(1, 1)).overlap(file.newRange(file.newPointer(1, 0), file.newPointer(1, 1)))).isTrue();
    assertThat(file.newRange(file.newPointer(1, 0), file.newPointer(1, 1)).overlap(file.newRange(file.newPointer(1, 0), file.newPointer(1, 2)))).isTrue();
    assertThat(file.newRange(file.newPointer(1, 0), file.newPointer(1, 1)).overlap(file.newRange(file.newPointer(1, 1), file.newPointer(1, 2)))).isFalse();
    assertThat(file.newRange(file.newPointer(1, 2), file.newPointer(1, 3)).overlap(file.newRange(file.newPointer(1, 0), file.newPointer(1, 2)))).isFalse();
}
Also used : ClientInputFile(org.sonarsource.sonarlint.core.analysis.api.ClientInputFile) InMemoryTestClientInputFile(testutils.InMemoryTestClientInputFile) OnDiskTestClientInputFile(testutils.OnDiskTestClientInputFile) InMemoryTestClientInputFile(testutils.InMemoryTestClientInputFile) Test(org.junit.jupiter.api.Test)

Example 15 with ClientInputFile

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

the class InputFileBuilderTests method testCreateWithLanguageSet.

@Test
void testCreateWithLanguageSet() throws IOException {
    var path = tempDir.resolve("file");
    Files.write(path, "test".getBytes(StandardCharsets.ISO_8859_1));
    ClientInputFile file = new OnDiskTestClientInputFile(path, "file", true, StandardCharsets.ISO_8859_1, Language.CPP);
    var builder = new InputFileBuilder(langDetection, metadata, issueExclusionsLoader);
    var inputFile = builder.create(file);
    assertThat(inputFile.language()).isEqualTo("cpp");
    verifyNoInteractions(langDetection);
}
Also used : OnDiskTestClientInputFile(testutils.OnDiskTestClientInputFile) ClientInputFile(org.sonarsource.sonarlint.core.analysis.api.ClientInputFile) OnDiskTestClientInputFile(testutils.OnDiskTestClientInputFile) 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