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);
}
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);
}
}
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);
}
}
}
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");
}
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);
}
Aggregations