Search in sources :

Example 41 with DefaultInputFile

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

the class FileSystemMediumTest method language_without_publishAllFiles_should_not_auto_publish_files.

@Test
public void language_without_publishAllFiles_should_not_auto_publish_files() throws IOException {
    File srcDir = new File(baseDir, "src");
    srcDir.mkdir();
    writeFile(srcDir, "sample.xoo3", "Sample xoo\ncontent");
    writeFile(srcDir, "sample2.xoo3", "Sample xoo 2\ncontent");
    AnalysisResult result = tester.newAnalysis().properties(builder.put("sonar.sources", "src").build()).execute();
    assertThat(result.inputFiles()).extracting(InputFile::filename, InputFile::language, f -> ((DefaultInputFile) f).isPublished()).containsOnly(tuple("sample.xoo3", "xoo3", false), tuple("sample2.xoo3", "xoo3", false));
    assertThat(result.getReportReader().readComponent(result.getReportReader().readMetadata().getRootComponentRef()).getChildRefCount()).isZero();
}
Also used : InputFile(org.sonar.api.batch.fs.InputFile) XooPlugin(org.sonar.xoo.XooPlugin) StringUtils(org.apache.commons.lang.StringUtils) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Random(java.util.Random) GlobalProjectSensor(org.sonar.xoo.global.GlobalProjectSensor) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) MessageException(org.sonar.api.utils.MessageException) PathUtils(org.sonar.api.utils.PathUtils) AnalysisBuilder(org.sonar.scanner.mediumtest.ScannerMediumTester.AnalysisBuilder) Path(java.nio.file.Path) Before(org.junit.Before) ImmutableMap(com.google.common.collect.ImmutableMap) Files(java.nio.file.Files) System2(org.sonar.api.utils.System2) Assertions.tuple(org.assertj.core.api.Assertions.tuple) CoreProperties(org.sonar.api.CoreProperties) IOException(java.io.IOException) FileUtils(org.apache.commons.io.FileUtils) Test(org.junit.Test) File(java.io.File) StandardCharsets(java.nio.charset.StandardCharsets) Rule(org.junit.Rule) LogTester(org.sonar.api.utils.log.LogTester) XooRulesDefinition(org.sonar.xoo.rule.XooRulesDefinition) SonarEdition(org.sonar.api.SonarEdition) ScannerMediumTester(org.sonar.scanner.mediumtest.ScannerMediumTester) DeprecatedGlobalSensor(org.sonar.xoo.global.DeprecatedGlobalSensor) Assume.assumeTrue(org.junit.Assume.assumeTrue) AnalysisResult(org.sonar.scanner.mediumtest.AnalysisResult) TemporaryFolder(org.junit.rules.TemporaryFolder) LoggerLevel(org.sonar.api.utils.log.LoggerLevel) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) InputFile(org.sonar.api.batch.fs.InputFile) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) File(java.io.File) AnalysisResult(org.sonar.scanner.mediumtest.AnalysisResult) Test(org.junit.Test)

Example 42 with DefaultInputFile

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

the class FileSystemMediumTest method publishFilesWithIssues.

@Test
public void publishFilesWithIssues() throws IOException {
    tester.addRules(new XooRulesDefinition()).addActiveRule("xoo", "OneIssueOnDirPerFile", null, "OneIssueOnDirPerFile", "MAJOR", null, "xoo");
    File srcDir = new File(baseDir, "src");
    srcDir.mkdir();
    writeFile(srcDir, "sample.xoo", "Sample xoo\ncontent");
    AnalysisResult result = tester.newAnalysis().properties(builder.put("sonar.sources", "src").build()).execute();
    DefaultInputFile file = (DefaultInputFile) result.inputFile("src/sample.xoo");
    assertThat(file.isPublished()).isTrue();
    assertThat(result.getReportComponent(file)).isNotNull();
}
Also used : DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) XooRulesDefinition(org.sonar.xoo.rule.XooRulesDefinition) InputFile(org.sonar.api.batch.fs.InputFile) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) File(java.io.File) AnalysisResult(org.sonar.scanner.mediumtest.AnalysisResult) Test(org.junit.Test)

Example 43 with DefaultInputFile

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

the class FileSystemMediumTest method create_issue_on_any_file.

@Test
public void create_issue_on_any_file() throws IOException {
    tester.addRules(new XooRulesDefinition()).addActiveRule("xoo", "OneIssuePerUnknownFile", null, "OneIssuePerUnknownFile", "MAJOR", null, "xoo");
    File srcDir = new File(baseDir, "src");
    srcDir.mkdir();
    writeFile(srcDir, "sample.unknown", "Sample xoo\ncontent");
    logTester.setLevel(LoggerLevel.DEBUG);
    AnalysisResult result = tester.newAnalysis().properties(builder.put("sonar.sources", "src").build()).execute();
    assertThat(logTester.logs()).contains("1 file indexed");
    assertThat(logTester.logs()).contains("'src" + File.separator + "sample.unknown' indexed with no language");
    assertThat(logTester.logs()).contains("'src/sample.unknown' generated metadata with charset 'UTF-8'");
    DefaultInputFile inputFile = (DefaultInputFile) result.inputFile("src/sample.unknown");
    assertThat(result.getReportComponent(inputFile)).isNotNull();
}
Also used : DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) XooRulesDefinition(org.sonar.xoo.rule.XooRulesDefinition) InputFile(org.sonar.api.batch.fs.InputFile) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) File(java.io.File) AnalysisResult(org.sonar.scanner.mediumtest.AnalysisResult) Test(org.junit.Test)

Example 44 with DefaultInputFile

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

the class FileMetadataTest method ignore_whitespace_when_computing_line_hashes.

@Test
public void ignore_whitespace_when_computing_line_hashes() throws Exception {
    File tempFile = temp.newFile();
    FileUtils.write(tempFile, " foo\nb ar\r\nbaz \t", StandardCharsets.UTF_8, true);
    DefaultInputFile f = new TestInputFileBuilder("foo", tempFile.getName()).setModuleBaseDir(tempFile.getParentFile().toPath()).setCharset(StandardCharsets.UTF_8).build();
    FileMetadata.computeLineHashesForIssueTracking(f, new FileMetadata.LineHashConsumer() {

        @Override
        public void consume(int lineIdx, @Nullable byte[] hash) {
            switch(lineIdx) {
                case 1:
                    assertThat(Hex.encodeHexString(hash)).isEqualTo(md5Hex("foo"));
                    break;
                case 2:
                    assertThat(Hex.encodeHexString(hash)).isEqualTo(md5Hex("bar"));
                    break;
                case 3:
                    assertThat(Hex.encodeHexString(hash)).isEqualTo(md5Hex("baz"));
                    break;
                default:
                    fail("Invalid line");
            }
        }
    });
}
Also used : TestInputFileBuilder(org.sonar.api.batch.fs.internal.TestInputFileBuilder) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) FileMetadata(org.sonar.api.batch.fs.internal.FileMetadata) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) File(java.io.File) Test(org.junit.Test)

Example 45 with DefaultInputFile

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

the class FileMetadataTest method dont_fail_on_empty_file.

@Test
public void dont_fail_on_empty_file() throws Exception {
    File tempFile = temp.newFile();
    FileUtils.write(tempFile, "", StandardCharsets.UTF_8, true);
    DefaultInputFile f = new TestInputFileBuilder("foo", tempFile.getName()).setModuleBaseDir(tempFile.getParentFile().toPath()).setCharset(StandardCharsets.UTF_8).build();
    FileMetadata.computeLineHashesForIssueTracking(f, new FileMetadata.LineHashConsumer() {

        @Override
        public void consume(int lineIdx, @Nullable byte[] hash) {
            switch(lineIdx) {
                case 1:
                    assertThat(hash).isNull();
                    break;
                default:
                    fail("Invalid line");
            }
        }
    });
}
Also used : TestInputFileBuilder(org.sonar.api.batch.fs.internal.TestInputFileBuilder) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) FileMetadata(org.sonar.api.batch.fs.internal.FileMetadata) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) File(java.io.File) Test(org.junit.Test)

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