Search in sources :

Example 1 with Metadata

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);
    }
}
Also used : FileMetadata(org.sonar.api.batch.fs.internal.FileMetadata) Metadata(org.sonar.api.batch.fs.internal.Metadata) Charset(java.nio.charset.Charset) IOException(java.io.IOException)

Example 2 with Metadata

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'.");
}
Also used : FileMetadata(org.sonar.api.batch.fs.internal.FileMetadata) Metadata(org.sonar.api.batch.fs.internal.Metadata) FileMetadata(org.sonar.api.batch.fs.internal.FileMetadata) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) File(java.io.File) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Example 3 with Metadata

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();
}
Also used : FileMetadata(org.sonar.api.batch.fs.internal.FileMetadata) Metadata(org.sonar.api.batch.fs.internal.Metadata) FileMetadata(org.sonar.api.batch.fs.internal.FileMetadata) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) File(java.io.File) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Example 4 with Metadata

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);
}
Also used : FileMetadata(org.sonar.api.batch.fs.internal.FileMetadata) Metadata(org.sonar.api.batch.fs.internal.Metadata) FileMetadata(org.sonar.api.batch.fs.internal.FileMetadata) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) File(java.io.File) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Example 5 with Metadata

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);
}
Also used : FileMetadata(org.sonar.api.batch.fs.internal.FileMetadata) Metadata(org.sonar.api.batch.fs.internal.Metadata) FileMetadata(org.sonar.api.batch.fs.internal.FileMetadata) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) File(java.io.File) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Aggregations

Metadata (org.sonar.api.batch.fs.internal.Metadata)26 FileMetadata (org.sonar.api.batch.fs.internal.FileMetadata)25 Test (org.junit.Test)24 DefaultInputFile (org.sonar.api.batch.fs.internal.DefaultInputFile)23 File (java.io.File)15 FileInputStream (java.io.FileInputStream)14 DefaultIndexedFile (org.sonar.api.batch.fs.internal.DefaultIndexedFile)7 IOException (java.io.IOException)5 InputStream (java.io.InputStream)3 BufferedReader (java.io.BufferedReader)2 InputStreamReader (java.io.InputStreamReader)2 StringReader (java.io.StringReader)2 Charset (java.nio.charset.Charset)2 Path (java.nio.file.Path)2 BufferedWriter (java.io.BufferedWriter)1 FileOutputStream (java.io.FileOutputStream)1 OutputStreamWriter (java.io.OutputStreamWriter)1 InputFile (org.sonar.api.batch.fs.InputFile)1 TextRange (org.sonar.api.batch.fs.TextRange)1