Search in sources :

Example 16 with DefaultIndexedFile

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

the class PathPatternTest method match_relative_path.

@Test
public void match_relative_path() {
    PathPattern pattern = PathPattern.create("**/*Foo.java");
    assertThat(pattern.toString()).isEqualTo("**/*Foo.java");
    IndexedFile indexedFile = new DefaultIndexedFile("ABCDE", baseDir, "src/main/java/org/MyFoo.java", null);
    assertThat(pattern.match(indexedFile.path(), Paths.get(indexedFile.relativePath()))).isTrue();
    // case sensitive by default
    indexedFile = new DefaultIndexedFile("ABCDE", baseDir, "src/main/java/org/MyFoo.JAVA", null);
    assertThat(pattern.match(indexedFile.path(), Paths.get(indexedFile.relativePath()))).isFalse();
    indexedFile = new DefaultIndexedFile("ABCDE", baseDir, "src/main/java/org/Other.java", null);
    assertThat(pattern.match(indexedFile.path(), Paths.get(indexedFile.relativePath()))).isFalse();
}
Also used : PathPattern(org.sonar.api.batch.fs.internal.PathPattern) 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 17 with DefaultIndexedFile

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

the class PathPatternTest method match_absolute_path_and_insensitive_file_extension.

@Test
public void match_absolute_path_and_insensitive_file_extension() {
    PathPattern pattern = PathPattern.create("file:**/src/main/**Foo.java");
    assertThat(pattern.toString()).isEqualTo("file:**/src/main/**Foo.java");
    IndexedFile indexedFile = new DefaultIndexedFile("ABCDE", baseDir, "src/main/java/org/MyFoo.JAVA", null);
    assertThat(pattern.match(indexedFile.path(), Paths.get(indexedFile.relativePath()), false)).isTrue();
    indexedFile = new DefaultIndexedFile("ABCDE", baseDir, "src/main/java/org/Other.JAVA", null);
    assertThat(pattern.match(indexedFile.path(), Paths.get(indexedFile.relativePath()), false)).isFalse();
}
Also used : PathPattern(org.sonar.api.batch.fs.internal.PathPattern) 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 18 with DefaultIndexedFile

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

the class FileIndexer method indexFile.

void indexFile(DefaultInputModule module, ModuleExclusionFilters moduleExclusionFilters, ModuleCoverageAndDuplicationExclusions moduleCoverageAndDuplicationExclusions, Path sourceFile, Type type, ProgressReport progressReport, ProjectFileIndexer.ExclusionCounter exclusionCounter, @Nullable IgnoreCommand ignoreCommand) throws IOException {
    // get case of real file without resolving link
    Path realAbsoluteFile = sourceFile.toRealPath(LinkOption.NOFOLLOW_LINKS).toAbsolutePath().normalize();
    Path projectRelativePath = project.getBaseDir().relativize(realAbsoluteFile);
    Path moduleRelativePath = module.getBaseDir().relativize(realAbsoluteFile);
    boolean included = evaluateInclusionsFilters(moduleExclusionFilters, realAbsoluteFile, projectRelativePath, moduleRelativePath, type);
    if (!included) {
        exclusionCounter.increaseByPatternsCount();
        return;
    }
    boolean excluded = evaluateExclusionsFilters(moduleExclusionFilters, realAbsoluteFile, projectRelativePath, moduleRelativePath, type);
    if (excluded) {
        exclusionCounter.increaseByPatternsCount();
        return;
    }
    if (!realAbsoluteFile.startsWith(project.getBaseDir())) {
        LOG.warn("File '{}' is ignored. It is not located in project basedir '{}'.", realAbsoluteFile.toAbsolutePath(), project.getBaseDir());
        return;
    }
    if (!realAbsoluteFile.startsWith(module.getBaseDir())) {
        LOG.warn("File '{}' is ignored. It is not located in module basedir '{}'.", realAbsoluteFile.toAbsolutePath(), module.getBaseDir());
        return;
    }
    Language language = langDetection.language(realAbsoluteFile, projectRelativePath);
    if (ignoreCommand != null && ignoreCommand.isIgnored(realAbsoluteFile)) {
        LOG.debug("File '{}' is excluded by the scm ignore settings.", realAbsoluteFile);
        exclusionCounter.increaseByScmCount();
        return;
    }
    DefaultIndexedFile indexedFile = new DefaultIndexedFile(realAbsoluteFile, project.key(), projectRelativePath.toString(), moduleRelativePath.toString(), type, language != null ? language.key() : null, scannerComponentIdGenerator.getAsInt(), sensorStrategy);
    DefaultInputFile inputFile = new DefaultInputFile(indexedFile, f -> metadataGenerator.setMetadata(module.key(), f, module.getEncoding()));
    if (language != null && language.isPublishAllFiles()) {
        inputFile.setPublished(true);
    }
    if (!accept(inputFile)) {
        return;
    }
    checkIfAlreadyIndexed(inputFile);
    componentStore.put(module.key(), inputFile);
    issueExclusionsLoader.addMulticriteriaPatterns(inputFile);
    String langStr = inputFile.language() != null ? format("with language '%s'", inputFile.language()) : "with no language";
    LOG.debug("'{}' indexed {}{}", projectRelativePath, type == Type.TEST ? "as test " : "", langStr);
    evaluateCoverageExclusions(moduleCoverageAndDuplicationExclusions, inputFile);
    evaluateDuplicationExclusions(moduleCoverageAndDuplicationExclusions, inputFile);
    if (properties.preloadFileMetadata()) {
        inputFile.checkMetadata();
    }
    int count = componentStore.inputFiles().size();
    progressReport.message(count + " " + pluralizeFiles(count) + " indexed...  (last one was " + inputFile.getProjectRelativePath() + ")");
}
Also used : Path(java.nio.file.Path) Language(org.sonar.scanner.repository.language.Language) DefaultIndexedFile(org.sonar.api.batch.fs.internal.DefaultIndexedFile) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile)

Example 19 with DefaultIndexedFile

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

the class DefaultInputFileTest method selectLine.

@Test
public void selectLine() {
    Metadata metadata = new FileMetadata(mock(AnalysisWarnings.class)).readMetadata(new StringReader("bla bla a\nabcde\n\nabc"));
    DefaultInputFile file = new DefaultInputFile(new DefaultIndexedFile("ABCDE", Paths.get("module"), MODULE_RELATIVE_PATH, null), f -> f.setMetadata(metadata));
    assertThat(file.selectLine(1).start().line()).isOne();
    assertThat(file.selectLine(1).start().lineOffset()).isZero();
    assertThat(file.selectLine(1).end().line()).isOne();
    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();
    try {
        file.selectLine(5);
        fail();
    } catch (Exception e) {
        assertThat(e).hasMessage("5 is not a valid line for pointer. File src/Foo.php has 4 line(s)");
    }
}
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 20 with DefaultIndexedFile

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

the class DefaultInputFileTest method checkValidPointer.

@Test
public void checkValidPointer() {
    Metadata metadata = new Metadata(2, 2, "", new int[] { 0, 10 }, new int[] { 9, 15 }, 16);
    DefaultInputFile file = new DefaultInputFile(new DefaultIndexedFile("ABCDE", Paths.get("module"), MODULE_RELATIVE_PATH, null), f -> f.setMetadata(metadata));
    assertThat(file.newPointer(1, 0).line()).isOne();
    assertThat(file.newPointer(1, 0).lineOffset()).isZero();
    // Don't fail
    file.newPointer(1, 9);
    file.newPointer(2, 0);
    file.newPointer(2, 5);
    try {
        file.newPointer(0, 1);
        fail();
    } catch (Exception e) {
        assertThat(e).hasMessage("0 is not a valid line for a file");
    }
    try {
        file.newPointer(3, 1);
        fail();
    } catch (Exception e) {
        assertThat(e).hasMessage("3 is not a valid line for pointer. File src/Foo.php has 2 line(s)");
    }
    try {
        file.newPointer(1, -1);
        fail();
    } catch (Exception e) {
        assertThat(e).hasMessage("-1 is not a valid line offset for a file");
    }
    try {
        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) IOException(java.io.IOException) 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