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;
}
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();
}
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());
}
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);
}
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);
}
Aggregations