Search in sources :

Example 71 with DefaultInputFile

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

the class MetadataGeneratorTest method createInputFileWithMetadata.

private DefaultInputFile createInputFileWithMetadata(Path baseDir, String relativePath) {
    DefaultInputFile inputFile = new TestInputFileBuilder("struts", relativePath).setModuleBaseDir(baseDir).build();
    generator.setMetadata("module", inputFile, StandardCharsets.US_ASCII);
    return inputFile;
}
Also used : TestInputFileBuilder(org.sonar.api.batch.fs.internal.TestInputFileBuilder) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile)

Example 72 with DefaultInputFile

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

the class MetadataGeneratorTest method complete_input_file.

@Test
public void complete_input_file() throws Exception {
    // file system
    Path baseDir = temp.newFolder().toPath();
    Path srcFile = baseDir.resolve("src/main/java/foo/Bar.java");
    FileUtils.touch(srcFile.toFile());
    FileUtils.write(srcFile.toFile(), "single line");
    // status
    DefaultInputFile inputFile = createInputFileWithMetadata(baseDir, "src/main/java/foo/Bar.java");
    when(statusDetection.status("foo", inputFile, "6c1d64c0b3555892fe7273e954f6fb5a")).thenReturn(InputFile.Status.ADDED);
    assertThat(inputFile.type()).isEqualTo(InputFile.Type.MAIN);
    assertThat(inputFile.file()).isEqualTo(srcFile.toFile());
    assertThat(inputFile.absolutePath()).isEqualTo(PathUtils.sanitize(srcFile.toAbsolutePath().toString()));
    assertThat(inputFile.key()).isEqualTo("struts:src/main/java/foo/Bar.java");
    assertThat(inputFile.relativePath()).isEqualTo("src/main/java/foo/Bar.java");
    assertThat(inputFile.lines()).isOne();
}
Also used : Path(java.nio.file.Path) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) Test(org.junit.Test)

Example 73 with DefaultInputFile

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

the class ChangedLinesPublisherTest method write_last_line_as_changed_if_all_other_lines_are_changed_and_last_line_is_empty.

@Test
public void write_last_line_as_changed_if_all_other_lines_are_changed_and_last_line_is_empty() {
    DefaultInputFile fileWithChangedLines = createInputFile("path1", "l1\nl2\nl3\n");
    DefaultInputFile fileWithoutChangedLines = createInputFile("path2", "l1\nl2\nl3\n");
    Set<Path> paths = new HashSet<>(Arrays.asList(BASE_DIR.resolve("path1"), BASE_DIR.resolve("path2")));
    Set<Integer> lines = new HashSet<>(Arrays.asList(1, 2, 3));
    when(provider.branchChangedLines(TARGET_BRANCH, BASE_DIR, paths)).thenReturn(Collections.singletonMap(BASE_DIR.resolve("path1"), lines));
    when(inputComponentStore.allChangedFilesToPublish()).thenReturn(Arrays.asList(fileWithChangedLines, fileWithoutChangedLines));
    publisher.publish(writer);
    assertPublished(fileWithChangedLines, new HashSet<>(Arrays.asList(1, 2, 3, 4)));
    assertPublished(fileWithoutChangedLines, Collections.emptySet());
}
Also used : Path(java.nio.file.Path) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 74 with DefaultInputFile

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

the class ChangedLinesPublisherTest method assertPublished.

private void assertPublished(DefaultInputFile file, Set<Integer> lines) {
    assertThat(new File(temp.getRoot(), "changed-lines-" + file.scannerId() + ".pb")).exists();
    ScannerReportReader reader = new ScannerReportReader(temp.getRoot());
    assertThat(reader.readComponentChangedLines(file.scannerId()).getLineList()).containsExactlyElementsOf(lines);
}
Also used : ScannerReportReader(org.sonar.scanner.protocol.output.ScannerReportReader) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) File(java.io.File)

Example 75 with DefaultInputFile

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

the class SonarCpdBlockIndex method insert.

public void insert(InputFile inputFile, Collection<Block> blocks) {
    if (settings.isCrossProjectDuplicationEnabled()) {
        int id = ((DefaultInputFile) inputFile).scannerId();
        if (publisher.getWriter().hasComponentData(FileStructure.Domain.CPD_TEXT_BLOCKS, id)) {
            throw new UnsupportedOperationException("Trying to save CPD tokens twice for the same file is not supported: " + inputFile.absolutePath());
        }
        final ScannerReport.CpdTextBlock.Builder builder = ScannerReport.CpdTextBlock.newBuilder();
        publisher.getWriter().writeCpdTextBlocks(id, blocks.stream().map(block -> {
            builder.clear();
            builder.setStartLine(block.getStartLine());
            builder.setEndLine(block.getEndLine());
            builder.setStartTokenIndex(block.getStartUnit());
            builder.setEndTokenIndex(block.getEndUnit());
            builder.setHash(block.getBlockHash().toHexString());
            return builder.build();
        }).collect(Collectors.toList()));
    }
    for (Block block : blocks) {
        mem.insert(block);
    }
    if (blocks.isEmpty()) {
        LOG.debug("Not enough content in '{}' to have CPD blocks, it will not be part of the duplication detection", inputFile.relativePath());
    }
    indexedFiles.add(inputFile);
}
Also used : DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) Block(org.sonar.duplications.block.Block)

Aggregations

DefaultInputFile (org.sonar.api.batch.fs.internal.DefaultInputFile)173 Test (org.junit.Test)117 TestInputFileBuilder (org.sonar.api.batch.fs.internal.TestInputFileBuilder)75 File (java.io.File)63 InputFile (org.sonar.api.batch.fs.InputFile)46 Path (java.nio.file.Path)25 DefaultFileSystem (org.sonar.api.batch.fs.internal.DefaultFileSystem)18 SensorContextTester (org.sonar.api.batch.sensor.internal.SensorContextTester)17 BlameOutput (org.sonar.api.batch.scm.BlameCommand.BlameOutput)16 FileMetadata (org.sonar.api.batch.fs.internal.FileMetadata)14 IOException (java.io.IOException)13 DefaultIndexedFile (org.sonar.api.batch.fs.internal.DefaultIndexedFile)11 Metadata (org.sonar.api.batch.fs.internal.Metadata)11 Before (org.junit.Before)10 DefaultInputModule (org.sonar.api.batch.fs.internal.DefaultInputModule)10 BlameLine (org.sonar.api.batch.scm.BlameLine)9 List (java.util.List)8 ScannerReport (org.sonar.scanner.protocol.output.ScannerReport)8 ProjectDefinition (org.sonar.api.batch.bootstrap.ProjectDefinition)7 TextRange (org.sonar.api.batch.fs.TextRange)7