Search in sources :

Example 11 with DefaultInputFile

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

the class CpdExecutorTest method createComponent.

private DefaultInputFile createComponent(String relativePath, int lines) {
    DefaultInputFile file = new TestInputFileBuilder("foo", relativePath).setModuleBaseDir(baseDir.toPath()).setLines(lines).build();
    componentStore.put(file);
    return file;
}
Also used : TestInputFileBuilder(org.sonar.api.batch.fs.internal.TestInputFileBuilder) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile)

Example 12 with DefaultInputFile

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

the class CpdExecutorTest method timeout.

@Test
public void timeout() {
    for (int i = 1; i <= 2; i++) {
        DefaultInputFile component = createComponent("src/Foo" + i + ".php", 100);
        List<Block> blocks = new ArrayList<>();
        for (int j = 1; j <= 10000; j++) {
            blocks.add(Block.builder().setResourceId(component.key()).setIndexInFile(j).setLines(j, j + 1).setUnit(j, j + 1).setBlockHash(new ByteArray("abcd1234".getBytes())).build());
        }
        index.insert((InputFile) component, blocks);
    }
    executor.execute(1);
    readDuplications(0);
    assertThat(logTester.logs(LoggerLevel.WARN)).usingElementComparator((l, r) -> l.matches(r) ? 0 : 1).containsOnly("Timeout during detection of duplications for .*Foo1.php", "Timeout during detection of duplications for .*Foo2.php");
}
Also used : InputFile(org.sonar.api.batch.fs.InputFile) Duplication(org.sonar.scanner.protocol.output.ScannerReport.Duplication) Arrays(java.util.Arrays) InputComponentStore(org.sonar.scanner.scan.filesystem.InputComponentStore) CloneGroup(org.sonar.duplications.index.CloneGroup) SonarCpdBlockIndex(org.sonar.scanner.cpd.index.SonarCpdBlockIndex) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) Block(org.sonar.duplications.block.Block) ScannerReportWriter(org.sonar.scanner.protocol.output.ScannerReportWriter) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ArrayList(java.util.ArrayList) Duplicate(org.sonar.scanner.protocol.output.ScannerReport.Duplicate) ClonePart(org.sonar.duplications.index.ClonePart) ScannerReportReader(org.sonar.scanner.protocol.output.ScannerReportReader) ExpectedException(org.junit.rules.ExpectedException) Before(org.junit.Before) ByteArray(org.sonar.duplications.block.ByteArray) PathResolver(org.sonar.api.scan.filesystem.PathResolver) CloseableIterator(org.sonar.core.util.CloseableIterator) IOException(java.io.IOException) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) File(java.io.File) TestInputFileBuilder(org.sonar.api.batch.fs.internal.TestInputFileBuilder) List(java.util.List) ReportPublisher(org.sonar.scanner.report.ReportPublisher) Rule(org.junit.Rule) LogTester(org.sonar.api.utils.log.LogTester) Settings(org.sonar.api.config.Settings) MapSettings(org.sonar.api.config.MapSettings) Collections(java.util.Collections) TemporaryFolder(org.junit.rules.TemporaryFolder) LoggerLevel(org.sonar.api.utils.log.LoggerLevel) Mockito.mock(org.mockito.Mockito.mock) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) ArrayList(java.util.ArrayList) Block(org.sonar.duplications.block.Block) ByteArray(org.sonar.duplications.block.ByteArray) Test(org.junit.Test)

Example 13 with DefaultInputFile

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

the class BranchMediumTest method scanMultiModuleWithBranch.

@Test
public void scanMultiModuleWithBranch() throws IOException {
    Path srcDir = baseDir.toPath().resolve("moduleA").resolve("src");
    Files.createDirectories(srcDir);
    File xooFile = new File(srcDir.toFile(), "sample.xoo");
    FileUtils.write(xooFile, "Sample xoo\ncontent");
    TaskResult result = tester.newTask().properties(ImmutableMap.<String, String>builder().putAll(commonProps).put("sonar.branch", "branch").put("sonar.modules", "moduleA").build()).start();
    assertThat(result.inputFiles()).hasSize(1);
    assertThat(result.inputFile("src/sample.xoo").key()).isEqualTo("com.foo.project:moduleA:src/sample.xoo");
    // no branch in the report
    DefaultInputFile inputfile = (DefaultInputFile) result.inputFile("src/sample.xoo");
    assertThat(result.getReportReader().readComponent(inputfile.batchId()).getPath()).isEqualTo("src/sample.xoo");
    // no branch in InputModule's key or in report
    assertThat(result.getReportComponent("com.foo.project:moduleA").getKey()).isEqualTo("com.foo.project:moduleA");
    assertThat(result.getReportReader().readMetadata().getBranch()).isEqualTo("branch");
    result = tester.newTask().properties(ImmutableMap.<String, String>builder().putAll(commonProps).put("sonar.branch", "").put("sonar.modules", "moduleA").build()).start();
    assertThat(result.inputFiles()).hasSize(1);
    assertThat(result.inputFile("src/sample.xoo").key()).isEqualTo("com.foo.project:moduleA:src/sample.xoo");
}
Also used : Path(java.nio.file.Path) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) TaskResult(org.sonar.scanner.mediumtest.TaskResult) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) File(java.io.File) Test(org.junit.Test)

Example 14 with DefaultInputFile

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

the class FileSystemMediumTest method dontPublishFilesWithoutDetectedLanguage.

@Test
public void dontPublishFilesWithoutDetectedLanguage() throws IOException {
    builder = createBuilder();
    Path mainDir = baseDir.toPath().resolve("src").resolve("main");
    Files.createDirectories(mainDir);
    Path testDir = baseDir.toPath().resolve("src").resolve("test");
    Files.createDirectories(testDir);
    Path testXooFile = testDir.resolve("sample.java");
    Files.write(testXooFile, "Sample xoo\ncontent".getBytes(StandardCharsets.UTF_8));
    Path xooFile = mainDir.resolve("sample.xoo");
    Files.write(xooFile, "Sample xoo\ncontent".getBytes(StandardCharsets.UTF_8));
    Path javaFile = mainDir.resolve("sample.java");
    Files.write(javaFile, "Sample xoo\ncontent".getBytes(StandardCharsets.UTF_8));
    TaskResult result = tester.newTask().properties(builder.put("sonar.sources", "src/main").put("sonar.tests", "src/test").build()).start();
    assertThat(logs.getAllAsString()).contains("3 files indexed");
    assertThat(logs.getAllAsString()).contains("'src/main/sample.xoo' generated metadata");
    assertThat(logs.getAllAsString()).doesNotContain("'src/main/sample.java' generated metadata");
    assertThat(logs.getAllAsString()).doesNotContain("'src/test/sample.java' generated metadata");
    DefaultInputFile javaInputFile = (DefaultInputFile) result.inputFile("src/main/sample.java");
    assertThat(result.getReportComponent(javaInputFile.key())).isNull();
}
Also used : Path(java.nio.file.Path) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) TaskResult(org.sonar.scanner.mediumtest.TaskResult) Test(org.junit.Test)

Example 15 with DefaultInputFile

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

the class FileSystemMediumTest method scanProjectWithoutProjectName.

@Test
public void scanProjectWithoutProjectName() throws IOException {
    builder = createBuilder();
    File srcDir = new File(baseDir, "src");
    srcDir.mkdir();
    File xooFile = new File(srcDir, "sample.xoo");
    FileUtils.write(xooFile, "Sample xoo\ncontent");
    TaskResult result = tester.newTask().properties(builder.put("sonar.sources", "src").build()).start();
    int ref = result.getReportReader().readMetadata().getRootComponentRef();
    assertThat(result.getReportReader().readComponent(ref).getName()).isEmpty();
    assertThat(result.inputFiles()).hasSize(1);
    assertThat(result.inputDirs()).hasSize(1);
    DefaultInputFile file = (DefaultInputFile) result.inputFile("src/sample.xoo");
    InputDir dir = result.inputDir("src");
    assertThat(file.type()).isEqualTo(InputFile.Type.MAIN);
    assertThat(file.relativePath()).isEqualTo("src/sample.xoo");
    assertThat(file.language()).isEqualTo("xoo");
    assertThat(dir.relativePath()).isEqualTo("src");
    // file and dirs were published, since language matched xoo
    assertThat(file.publish()).isTrue();
    assertThat(result.getReportComponent(dir.key())).isNotNull();
    assertThat(result.getReportComponent(file.key())).isNotNull();
}
Also used : DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) TaskResult(org.sonar.scanner.mediumtest.TaskResult) InputDir(org.sonar.api.batch.fs.InputDir) InputFile(org.sonar.api.batch.fs.InputFile) 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