Search in sources :

Example 6 with InputFile

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

the class JavaCpdBlockIndexer method index.

@Override
public void index(String languageKey) {
    String[] cpdExclusions = settings.getStringArray(CoreProperties.CPD_EXCLUSIONS);
    logExclusions(cpdExclusions);
    FilePredicates p = fs.predicates();
    List<InputFile> sourceFiles = Lists.newArrayList(fs.inputFiles(p.and(p.hasType(InputFile.Type.MAIN), p.hasLanguage(languageKey), p.doesNotMatchPathPatterns(cpdExclusions))));
    if (sourceFiles.isEmpty()) {
        return;
    }
    createIndex(sourceFiles);
}
Also used : FilePredicates(org.sonar.api.batch.fs.FilePredicates) InputFile(org.sonar.api.batch.fs.InputFile) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile)

Example 7 with InputFile

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

the class JavaCpdBlockIndexer method createIndex.

private void createIndex(Iterable<InputFile> sourceFiles) {
    TokenChunker tokenChunker = JavaTokenProducer.build();
    StatementChunker statementChunker = JavaStatementBuilder.build();
    BlockChunker blockChunker = new BlockChunker(BLOCK_SIZE);
    for (InputFile inputFile : sourceFiles) {
        LOG.debug("Populating index from {}", inputFile);
        String resourceEffectiveKey = ((DefaultInputFile) inputFile).key();
        List<Statement> statements;
        try (InputStream is = new FileInputStream(inputFile.file());
            Reader reader = new InputStreamReader(is, fs.encoding())) {
            statements = statementChunker.chunk(tokenChunker.chunk(reader));
        } catch (FileNotFoundException e) {
            throw new IllegalStateException("Cannot find file " + inputFile.file(), e);
        } catch (IOException e) {
            throw new IllegalStateException("Exception handling file: " + inputFile.file(), e);
        }
        List<Block> blocks = blockChunker.chunk(resourceEffectiveKey, statements);
        index.insert(inputFile, blocks);
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) TokenChunker(org.sonar.duplications.token.TokenChunker) Statement(org.sonar.duplications.statement.Statement) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileNotFoundException(java.io.FileNotFoundException) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) InputFile(org.sonar.api.batch.fs.InputFile) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) StatementChunker(org.sonar.duplications.statement.StatementChunker) Block(org.sonar.duplications.block.Block) BlockChunker(org.sonar.duplications.block.BlockChunker)

Example 8 with InputFile

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

the class IssueExclusionsLoader method execute.

/**
   * {@inheritDoc}
   */
public void execute() {
    Charset sourcesEncoding = fileSystem.encoding();
    for (InputFile inputFile : fileSystem.inputFiles(fileSystem.predicates().all())) {
        try {
            String componentEffectiveKey = ((DefaultInputFile) inputFile).key();
            if (componentEffectiveKey != null) {
                String path = inputFile.relativePath();
                inclusionPatternInitializer.initializePatternsForPath(path, componentEffectiveKey);
                exclusionPatternInitializer.initializePatternsForPath(path, componentEffectiveKey);
                if (exclusionPatternInitializer.hasFileContentPattern()) {
                    regexpScanner.scan(componentEffectiveKey, inputFile.file(), sourcesEncoding);
                }
            }
        } catch (Exception e) {
            throw new IllegalStateException("Unable to read the source file : '" + inputFile.absolutePath() + "' with the charset : '" + sourcesEncoding.name() + "'.", e);
        }
    }
}
Also used : DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) Charset(java.nio.charset.Charset) InputFile(org.sonar.api.batch.fs.InputFile) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile)

Example 9 with InputFile

use of org.sonar.api.batch.fs.InputFile 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 10 with InputFile

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

the class CoverageMediumTest method exclusions.

@Test
public void exclusions() throws IOException {
    File baseDir = temp.getRoot();
    File srcDir = new File(baseDir, "src");
    srcDir.mkdir();
    File xooFile = new File(srcDir, "sample.xoo");
    File xooUtCoverageFile = new File(srcDir, "sample.xoo.coverage");
    FileUtils.write(xooFile, "function foo() {\n  if (a && b) {\nalert('hello');\n}\n}");
    FileUtils.write(xooUtCoverageFile, "2:2:2:1\n3:1");
    TaskResult result = tester.newTask().properties(ImmutableMap.<String, String>builder().put("sonar.task", "scan").put("sonar.projectBaseDir", baseDir.getAbsolutePath()).put("sonar.projectKey", "com.foo.project").put("sonar.projectName", "Foo Project").put("sonar.projectVersion", "1.0-SNAPSHOT").put("sonar.projectDescription", "Description of Foo Project").put("sonar.sources", "src").put("sonar.coverage.exclusions", "**/sample.xoo").build()).start();
    InputFile file = result.inputFile("src/sample.xoo");
    assertThat(result.coverageFor(file, 2)).isNull();
    Map<String, List<org.sonar.scanner.protocol.output.ScannerReport.Measure>> allMeasures = result.allMeasures();
    assertThat(allMeasures.get("com.foo.project:src/sample.xoo")).extracting("metricKey").doesNotContain(CoreMetrics.LINES_TO_COVER_KEY, CoreMetrics.UNCOVERED_LINES_KEY, CoreMetrics.CONDITIONS_TO_COVER_KEY, CoreMetrics.COVERED_CONDITIONS_BY_LINE_KEY);
}
Also used : TaskResult(org.sonar.scanner.mediumtest.TaskResult) List(java.util.List) InputFile(org.sonar.api.batch.fs.InputFile) File(java.io.File) InputFile(org.sonar.api.batch.fs.InputFile) Test(org.junit.Test)

Aggregations

InputFile (org.sonar.api.batch.fs.InputFile)221 Test (org.junit.Test)120 File (java.io.File)100 TestInputFileBuilder (org.sonar.api.batch.fs.internal.TestInputFileBuilder)75 DefaultInputFile (org.sonar.api.batch.fs.internal.DefaultInputFile)58 AnalysisResult (org.sonar.scanner.mediumtest.AnalysisResult)32 DefaultFileSystem (org.sonar.api.batch.fs.internal.DefaultFileSystem)24 FileSystem (org.sonar.api.batch.fs.FileSystem)21 FilePredicates (org.sonar.api.batch.fs.FilePredicates)19 IOException (java.io.IOException)18 BlameOutput (org.sonar.api.batch.scm.BlameCommand.BlameOutput)15 List (java.util.List)14 Test (org.junit.jupiter.api.Test)12 BlameLine (org.sonar.api.batch.scm.BlameLine)11 ArrayList (java.util.ArrayList)10 Map (java.util.Map)9 Before (org.junit.Before)9 Path (java.nio.file.Path)8 HashMap (java.util.HashMap)8 ScannerReport (org.sonar.scanner.protocol.output.ScannerReport)8