Search in sources :

Example 1 with InputFile

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

the class ComponentKeysTest method create_effective_key.

@Test
public void create_effective_key() {
    Project project = new Project(ProjectDefinition.create().setKey("my_project"));
    assertThat(ComponentKeys.createEffectiveKey("my_project", project)).isEqualTo("my_project");
    Directory dir = Directory.create("src/org/foo");
    assertThat(ComponentKeys.createEffectiveKey("my_project", dir)).isEqualTo("my_project:src/org/foo");
    InputFile file = mock(InputFile.class);
    when(file.relativePath()).thenReturn("foo/Bar.php");
    assertThat(ComponentKeys.createEffectiveKey("my_project", file)).isEqualTo("my_project:foo/Bar.php");
}
Also used : Project(org.sonar.api.resources.Project) Directory(org.sonar.api.resources.Directory) InputFile(org.sonar.api.batch.fs.InputFile) Test(org.junit.Test)

Example 2 with InputFile

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

the class DefaultInputFileTest method test.

@Test
public void test() throws Exception {
    Path baseDir = temp.newFolder().toPath();
    Metadata metadata = new Metadata(42, 42, "", new int[0], 0);
    DefaultIndexedFile indexedFile = new DefaultIndexedFile("ABCDE", baseDir, "src/Foo.php", InputFile.Type.TEST, 0).setLanguage("php");
    DefaultInputFile inputFile = new DefaultInputFile(indexedFile, (f) -> f.setMetadata(metadata)).setStatus(InputFile.Status.ADDED).setCharset(StandardCharsets.ISO_8859_1);
    assertThat(inputFile.relativePath()).isEqualTo("src/Foo.php");
    assertThat(new File(inputFile.relativePath())).isRelative();
    assertThat(inputFile.absolutePath()).endsWith("Foo.php");
    assertThat(new File(inputFile.absolutePath())).isAbsolute();
    assertThat(inputFile.language()).isEqualTo("php");
    assertThat(inputFile.status()).isEqualTo(InputFile.Status.ADDED);
    assertThat(inputFile.type()).isEqualTo(InputFile.Type.TEST);
    assertThat(inputFile.lines()).isEqualTo(42);
    assertThat(inputFile.charset()).isEqualTo(StandardCharsets.ISO_8859_1);
}
Also used : Path(java.nio.file.Path) InputFile(org.sonar.api.batch.fs.InputFile) File(java.io.File) Test(org.junit.Test)

Example 3 with InputFile

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

the class FilenamePredicateTest method should_find_matching_file_in_index.

@Test
public void should_find_matching_file_in_index() throws IOException {
    String filename = "some name";
    InputFile inputFile = mock(InputFile.class);
    when(inputFile.file()).thenReturn(newDummyFile(filename));
    FileSystem.Index index = mock(FileSystem.Index.class);
    when(index.getFilesByName(filename)).thenReturn(Collections.singleton(inputFile));
    assertThat(new FilenamePredicate(filename).get(index)).containsOnly(inputFile);
}
Also used : FileSystem(org.sonar.api.batch.fs.FileSystem) InputFile(org.sonar.api.batch.fs.InputFile)

Example 4 with InputFile

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

the class DefaultCpdBlockIndexer method populateIndex.

private void populateIndex(String languageKey, List<InputFile> sourceFiles, CpdMapping mapping) {
    TokenizerBridge bridge = new TokenizerBridge(mapping.getTokenizer(), fs.encoding().name(), getBlockSize(languageKey));
    for (InputFile inputFile : sourceFiles) {
        if (!index.isIndexed(inputFile)) {
            LOG.debug("Populating index from {}", inputFile.absolutePath());
            String resourceEffectiveKey = ((DefaultInputFile) inputFile).key();
            List<Block> blocks = bridge.chunk(resourceEffectiveKey, inputFile.file());
            index.insert(inputFile, blocks);
        }
    }
}
Also used : DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) TokenizerBridge(org.sonar.duplications.internal.pmd.TokenizerBridge) Block(org.sonar.duplications.block.Block) InputFile(org.sonar.api.batch.fs.InputFile) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile)

Example 5 with InputFile

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

the class DefaultCpdBlockIndexer method index.

@Override
public void index(String languageKey) {
    CpdMapping mapping = mappings.getMapping(languageKey);
    if (mapping == null) {
        LOG.debug("No CpdMapping for language {}", languageKey);
        return;
    }
    String[] cpdExclusions = settings.getStringArray(CoreProperties.CPD_EXCLUSIONS);
    logExclusions(cpdExclusions);
    FilePredicates p = fs.predicates();
    List<InputFile> sourceFiles = Lists.newArrayList(fs.inputFiles(p.and(p.hasType(InputFile.Type.MAIN), p.hasLanguage(languageKey), p.doesNotMatchPathPatterns(cpdExclusions))));
    if (sourceFiles.isEmpty()) {
        return;
    }
    // Create index
    populateIndex(languageKey, sourceFiles, mapping);
}
Also used : CpdMapping(org.sonar.api.batch.CpdMapping) FilePredicates(org.sonar.api.batch.fs.FilePredicates) InputFile(org.sonar.api.batch.fs.InputFile) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile)

Aggregations

InputFile (org.sonar.api.batch.fs.InputFile)221 Test (org.junit.Test)120 File (java.io.File)100 TestInputFileBuilder (org.sonar.api.batch.fs.internal.TestInputFileBuilder)75 DefaultInputFile (org.sonar.api.batch.fs.internal.DefaultInputFile)58 AnalysisResult (org.sonar.scanner.mediumtest.AnalysisResult)32 DefaultFileSystem (org.sonar.api.batch.fs.internal.DefaultFileSystem)24 FileSystem (org.sonar.api.batch.fs.FileSystem)21 FilePredicates (org.sonar.api.batch.fs.FilePredicates)19 IOException (java.io.IOException)18 BlameOutput (org.sonar.api.batch.scm.BlameCommand.BlameOutput)15 List (java.util.List)14 Test (org.junit.jupiter.api.Test)12 BlameLine (org.sonar.api.batch.scm.BlameLine)11 ArrayList (java.util.ArrayList)10 Map (java.util.Map)9 Before (org.junit.Before)9 Path (java.nio.file.Path)8 HashMap (java.util.HashMap)8 ScannerReport (org.sonar.scanner.protocol.output.ScannerReport)8