use of org.sonar.api.batch.fs.internal.Metadata in project sonarqube by SonarSource.
the class MetadataGenerator method setMetadata.
/**
* Sets all metadata in the file, including charset and status.
* It is an expensive computation, reading the entire file.
*/
public void setMetadata(final DefaultInputFile inputFile, Charset defaultEncoding) {
try {
Charset charset = detectCharset(inputFile.path(), defaultEncoding);
inputFile.setCharset(charset);
Metadata metadata = fileMetadata.readMetadata(inputFile.file(), charset);
inputFile.setMetadata(metadata);
inputFile.setStatus(statusDetection.status(inputModule.definition().getKeyWithBranch(), inputFile.relativePath(), metadata.hash()));
LOG.debug("'{}' generated metadata {} with charset '{}'", inputFile.relativePath(), inputFile.type() == Type.TEST ? "as test " : "", charset);
} catch (Exception e) {
throw new IllegalStateException(e);
}
}
use of org.sonar.api.batch.fs.internal.Metadata in project sonarqube by SonarSource.
the class FileMetadataTest method binary_file_with_unmappable_character.
@Test
public void binary_file_with_unmappable_character() throws Exception {
File woff = new File(this.getClass().getResource("glyphicons-halflings-regular.woff").toURI());
Metadata metadata = new FileMetadata(analysisWarnings).readMetadata(new FileInputStream(woff), StandardCharsets.UTF_8, woff.getAbsolutePath());
assertThat(metadata.lines()).isEqualTo(135);
assertThat(metadata.nonBlankLines()).isEqualTo(133);
assertThat(metadata.hash()).isNotEmpty();
assertThat(logTester.logs(LoggerLevel.WARN).get(0)).contains("Invalid character encountered in file");
verify(analysisWarnings).addUnique("There are problems with file encoding in the source code. Please check the scanner logs for more details.");
assertThat(logTester.logs(LoggerLevel.WARN).get(0)).contains("glyphicons-halflings-regular.woff at line 1 for encoding UTF-8. Please fix file content or configure the encoding to be used using property 'sonar.sourceEncoding'.");
}
use of org.sonar.api.batch.fs.internal.Metadata in project sonarqube by SonarSource.
the class FileMetadataTest method unix_without_latest_eol.
@Test
public void unix_without_latest_eol() throws Exception {
File tempFile = temp.newFile();
FileUtils.write(tempFile, "foo\nbar\nbaz", StandardCharsets.UTF_8, true);
Metadata metadata = new FileMetadata(analysisWarnings).readMetadata(new FileInputStream(tempFile), StandardCharsets.UTF_8, tempFile.getName());
assertThat(metadata.lines()).isEqualTo(3);
assertThat(metadata.nonBlankLines()).isEqualTo(3);
assertThat(metadata.hash()).isEqualTo(md5Hex("foo\nbar\nbaz"));
assertThat(metadata.originalLineStartOffsets()).containsOnly(0, 4, 8);
assertThat(metadata.originalLineEndOffsets()).containsOnly(3, 7, 11);
assertThat(metadata.isEmpty()).isFalse();
}
use of org.sonar.api.batch.fs.internal.Metadata in project sonarqube by SonarSource.
the class FileMetadataTest method mac_with_latest_eol.
@Test
public void mac_with_latest_eol() throws Exception {
File tempFile = temp.newFile();
FileUtils.write(tempFile, "foo\rbar\rbaz\r", StandardCharsets.UTF_8, true);
Metadata metadata = new FileMetadata(analysisWarnings).readMetadata(new FileInputStream(tempFile), StandardCharsets.UTF_8, tempFile.getName());
assertThat(metadata.lines()).isEqualTo(4);
assertThat(metadata.nonBlankLines()).isEqualTo(3);
assertThat(metadata.hash()).isEqualTo(md5Hex("foo\nbar\nbaz\n"));
assertThat(metadata.originalLineStartOffsets()).containsOnly(0, 4, 8, 12);
assertThat(metadata.originalLineEndOffsets()).containsOnly(3, 7, 11, 12);
}
use of org.sonar.api.batch.fs.internal.Metadata in project sonarqube by SonarSource.
the class FileMetadataTest method several_new_lines.
@Test
public void several_new_lines() throws Exception {
File tempFile = temp.newFile();
FileUtils.write(tempFile, "foo\n\n\nbar", StandardCharsets.UTF_8, true);
Metadata metadata = new FileMetadata(analysisWarnings).readMetadata(new FileInputStream(tempFile), StandardCharsets.UTF_8, tempFile.getName());
assertThat(metadata.lines()).isEqualTo(4);
assertThat(metadata.nonBlankLines()).isEqualTo(2);
assertThat(metadata.hash()).isEqualTo(md5Hex("foo\n\n\nbar"));
assertThat(metadata.originalLineStartOffsets()).containsOnly(0, 4, 5, 6);
assertThat(metadata.originalLineEndOffsets()).containsOnly(3, 4, 5, 9);
}
Aggregations