Search in sources :

Example 21 with DefaultIndexedFile

use of org.sonar.api.batch.fs.internal.DefaultIndexedFile in project sonarqube by SonarSource.

the class DefaultInputFileTest method checkValidPointerUsingGlobalOffset.

@Test
public void checkValidPointerUsingGlobalOffset() {
    Metadata metadata = new Metadata(2, 2, "", new int[] { 0, 10 }, new int[] { 8, 15 }, 16);
    DefaultInputFile file = new DefaultInputFile(new DefaultIndexedFile("ABCDE", Paths.get("module"), MODULE_RELATIVE_PATH, null), f -> f.setMetadata(metadata));
    assertThat(file.newPointer(0).line()).isOne();
    assertThat(file.newPointer(0).lineOffset()).isZero();
    assertThat(file.newPointer(9).line()).isOne();
    // Ignore eol characters
    assertThat(file.newPointer(9).lineOffset()).isEqualTo(8);
    assertThat(file.newPointer(10).line()).isEqualTo(2);
    assertThat(file.newPointer(10).lineOffset()).isZero();
    assertThat(file.newPointer(15).line()).isEqualTo(2);
    assertThat(file.newPointer(15).lineOffset()).isEqualTo(5);
    assertThat(file.newPointer(16).line()).isEqualTo(2);
    // Ignore eol characters
    assertThat(file.newPointer(16).lineOffset()).isEqualTo(5);
    try {
        file.newPointer(-1);
        fail();
    } catch (Exception e) {
        assertThat(e).hasMessage("-1 is not a valid offset for a file");
    }
    try {
        file.newPointer(17);
        fail();
    } catch (Exception e) {
        assertThat(e).hasMessage("17 is not a valid offset for file src/Foo.php. Max offset is 16");
    }
}
Also used : DefaultIndexedFile(org.sonar.api.batch.fs.internal.DefaultIndexedFile) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) FileMetadata(org.sonar.api.batch.fs.internal.FileMetadata) Metadata(org.sonar.api.batch.fs.internal.Metadata) IOException(java.io.IOException) Test(org.junit.Test)

Example 22 with DefaultIndexedFile

use of org.sonar.api.batch.fs.internal.DefaultIndexedFile in project sonarqube by SonarSource.

the class DefaultInputFileTest method checkValidRange.

@Test
public void checkValidRange() {
    Metadata metadata = new FileMetadata(mock(AnalysisWarnings.class)).readMetadata(new StringReader("bla bla a\nabcde"));
    DefaultInputFile file = new DefaultInputFile(new DefaultIndexedFile("ABCDE", Paths.get("module"), MODULE_RELATIVE_PATH, null), f -> f.setMetadata(metadata));
    assertThat(file.newRange(file.newPointer(1, 0), file.newPointer(2, 1)).start().line()).isOne();
    // Don't fail
    file.newRange(file.newPointer(1, 0), file.newPointer(1, 1));
    file.newRange(file.newPointer(1, 0), file.newPointer(1, 9));
    file.newRange(file.newPointer(1, 0), file.newPointer(2, 0));
    assertThat(file.newRange(file.newPointer(1, 0), file.newPointer(2, 5))).isEqualTo(file.newRange(0, 15));
    try {
        file.newRange(file.newPointer(1, 0), file.newPointer(1, 0));
        fail();
    } catch (Exception e) {
        assertThat(e).hasMessage("Start pointer [line=1, lineOffset=0] should be before end pointer [line=1, lineOffset=0]");
    }
    try {
        file.newRange(file.newPointer(1, 0), file.newPointer(1, 10));
        fail();
    } catch (Exception e) {
        assertThat(e).hasMessage("10 is not a valid line offset for pointer. File src/Foo.php has 9 character(s) at line 1");
    }
}
Also used : DefaultIndexedFile(org.sonar.api.batch.fs.internal.DefaultIndexedFile) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) FileMetadata(org.sonar.api.batch.fs.internal.FileMetadata) Metadata(org.sonar.api.batch.fs.internal.Metadata) FileMetadata(org.sonar.api.batch.fs.internal.FileMetadata) StringReader(java.io.StringReader) IOException(java.io.IOException) Test(org.junit.Test)

Example 23 with DefaultIndexedFile

use of org.sonar.api.batch.fs.internal.DefaultIndexedFile in project sonarqube by SonarSource.

the class ProjectExclusionFiltersTest method no_inclusions_nor_exclusions.

@Test
public void no_inclusions_nor_exclusions() {
    ProjectExclusionFilters filter = new ProjectExclusionFilters(settings.asConfig());
    IndexedFile indexedFile = new DefaultIndexedFile("foo", moduleBaseDir, "src/main/java/com/mycompany/FooDao.java", null);
    assertThat(filter.isExcluded(indexedFile.path(), Paths.get(indexedFile.relativePath()), InputFile.Type.MAIN)).isFalse();
    assertThat(filter.isExcluded(indexedFile.path(), Paths.get(indexedFile.relativePath()), InputFile.Type.TEST)).isFalse();
    assertThat(filter.isIncluded(indexedFile.path(), Paths.get(indexedFile.relativePath()), InputFile.Type.MAIN)).isTrue();
    assertThat(filter.isIncluded(indexedFile.path(), Paths.get(indexedFile.relativePath()), InputFile.Type.TEST)).isTrue();
}
Also used : IndexedFile(org.sonar.api.batch.fs.IndexedFile) DefaultIndexedFile(org.sonar.api.batch.fs.internal.DefaultIndexedFile) DefaultIndexedFile(org.sonar.api.batch.fs.internal.DefaultIndexedFile) Test(org.junit.Test)

Example 24 with DefaultIndexedFile

use of org.sonar.api.batch.fs.internal.DefaultIndexedFile in project sonarqube by SonarSource.

the class ProjectExclusionFiltersTest method match_inclusion.

@Test
public void match_inclusion() {
    settings.setProperty(CoreProperties.PROJECT_INCLUSIONS_PROPERTY, "**/*Dao.java");
    ProjectExclusionFilters filter = new ProjectExclusionFilters(settings.asConfig());
    IndexedFile indexedFile = new DefaultIndexedFile("foo", moduleBaseDir, "src/main/java/com/mycompany/FooDao.java", null);
    assertThat(filter.isIncluded(indexedFile.path(), Paths.get(indexedFile.relativePath()), InputFile.Type.MAIN)).isTrue();
    indexedFile = new DefaultIndexedFile("foo", moduleBaseDir, "src/main/java/com/mycompany/Foo.java", null);
    assertThat(filter.isIncluded(indexedFile.path(), Paths.get(indexedFile.relativePath()), InputFile.Type.MAIN)).isFalse();
}
Also used : IndexedFile(org.sonar.api.batch.fs.IndexedFile) DefaultIndexedFile(org.sonar.api.batch.fs.internal.DefaultIndexedFile) DefaultIndexedFile(org.sonar.api.batch.fs.internal.DefaultIndexedFile) Test(org.junit.Test)

Example 25 with DefaultIndexedFile

use of org.sonar.api.batch.fs.internal.DefaultIndexedFile in project sonarqube by SonarSource.

the class ProjectExclusionFiltersTest method match_exclusion_by_absolute_path.

@Test
public void match_exclusion_by_absolute_path() {
    File excludedFile = new File(moduleBaseDir.toString(), "src/main/java/org/bar/Bar.java");
    settings.setProperty(CoreProperties.PROJECT_INCLUSIONS_PROPERTY, "src/main/java/**/*");
    settings.setProperty(CoreProperties.PROJECT_EXCLUSIONS_PROPERTY, "file:" + excludedFile.getAbsolutePath());
    ProjectExclusionFilters filter = new ProjectExclusionFilters(settings.asConfig());
    IndexedFile indexedFile = new DefaultIndexedFile("foo", moduleBaseDir, "src/main/java/org/bar/Foo.java", null);
    assertThat(filter.isExcluded(indexedFile.path(), Paths.get(indexedFile.relativePath()), InputFile.Type.MAIN)).isFalse();
    indexedFile = new DefaultIndexedFile("foo", moduleBaseDir, "src/main/java/org/bar/Bar.java", null);
    assertThat(filter.isExcluded(indexedFile.path(), Paths.get(indexedFile.relativePath()), InputFile.Type.MAIN)).isTrue();
}
Also used : IndexedFile(org.sonar.api.batch.fs.IndexedFile) DefaultIndexedFile(org.sonar.api.batch.fs.internal.DefaultIndexedFile) DefaultIndexedFile(org.sonar.api.batch.fs.internal.DefaultIndexedFile) InputFile(org.sonar.api.batch.fs.InputFile) IndexedFile(org.sonar.api.batch.fs.IndexedFile) File(java.io.File) DefaultIndexedFile(org.sonar.api.batch.fs.internal.DefaultIndexedFile) Test(org.junit.Test)

Aggregations

DefaultIndexedFile (org.sonar.api.batch.fs.internal.DefaultIndexedFile)25 Test (org.junit.Test)22 IndexedFile (org.sonar.api.batch.fs.IndexedFile)14 DefaultInputFile (org.sonar.api.batch.fs.internal.DefaultInputFile)10 FileMetadata (org.sonar.api.batch.fs.internal.FileMetadata)8 Metadata (org.sonar.api.batch.fs.internal.Metadata)8 MapSettings (org.sonar.api.config.MapSettings)5 FileExclusions (org.sonar.api.scan.filesystem.FileExclusions)5 IOException (java.io.IOException)4 PathPattern (org.sonar.api.batch.fs.internal.PathPattern)4 Settings (org.sonar.api.config.Settings)4 File (java.io.File)2 StringReader (java.io.StringReader)2 InputFile (org.sonar.api.batch.fs.InputFile)2 Path (java.nio.file.Path)1 CheckForNull (javax.annotation.CheckForNull)1 Before (org.junit.Before)1 TextRange (org.sonar.api.batch.fs.TextRange)1 SensorStrategy (org.sonar.api.batch.fs.internal.SensorStrategy)1 Language (org.sonar.scanner.repository.language.Language)1