use of org.sonar.api.batch.fs.InputFile in project sonarqube by SonarSource.
the class TestExecutionSensor method processTestFile.
private void processTestFile(InputFile inputFile, SensorContext context) {
File testExecutionFile = new File(inputFile.file().getParentFile(), inputFile.file().getName() + TEST_EXTENSION);
if (testExecutionFile.exists()) {
LOG.debug("Processing " + testExecutionFile.getAbsolutePath());
try {
List<String> lines = FileUtils.readLines(testExecutionFile, fs.encoding().name());
int lineNumber = 0;
MutableTestPlan testPlan = perspectives.as(MutableTestPlan.class, inputFile);
for (String line : lines) {
lineNumber++;
if (StringUtils.isBlank(line)) {
continue;
}
if (line.startsWith("#")) {
continue;
}
try {
Iterator<String> split = Splitter.on(":").split(line).iterator();
String name = split.next();
String durationStr = split.next();
Long duration = StringUtils.isNotBlank(durationStr) ? Long.parseLong(durationStr) : null;
String msg = split.next();
String stack = split.next();
String status = split.next();
String type = split.next();
MutableTestCase testCase = testPlan.addTestCase(name);
testCase.setDurationInMs(duration);
testCase.setMessage(msg);
testCase.setStackTrace(stack);
testCase.setStatus(Status.valueOf(status));
testCase.setType(type);
} catch (Exception e) {
throw new IllegalStateException("Error processing line " + lineNumber + " of file " + testExecutionFile.getAbsolutePath(), e);
}
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
use of org.sonar.api.batch.fs.InputFile in project sonarqube by SonarSource.
the class AbstractDeprecatedXooRuleSensor method doAnalyse.
private void doAnalyse(SensorContext context, String languageKey) {
RuleKey ruleKey = RuleKey.of(XooRulesDefinition.XOO_REPOSITORY, getRuleKey());
if (activeRules.find(ruleKey) == null) {
return;
}
for (InputFile inputFile : fs.inputFiles(fs.predicates().hasLanguage(languageKey))) {
File sonarFile = File.create(inputFile.relativePath());
sonarFile = context.getResource(sonarFile);
processFile(inputFile, sonarFile, context, ruleKey, languageKey);
}
}
use of org.sonar.api.batch.fs.InputFile in project sonarlint-core by SonarSource.
the class InputPathCacheTest method testFiles.
@Test
public void testFiles() {
InputFile file1 = mock(InputFile.class);
when(file1.filename()).thenReturn("file1.java");
when(file1.language()).thenReturn("lang1");
InputFile file2 = mock(InputFile.class);
when(file2.filename()).thenReturn("file2");
when(file2.language()).thenReturn("lang2");
cache.doAdd(file1);
cache.doAdd(file2);
assertThat(cache.inputFiles()).containsOnly(file1, file2);
// always null
assertThat(cache.inputFile("file1.java")).isNull();
assertThat(cache.getFilesByExtension("java")).containsOnly(file1);
assertThat(cache.getFilesByExtension("")).containsOnly(file2);
assertThat(cache.getFilesByName("file1.java")).containsOnly(file1);
assertThat(cache.languages()).containsExactly("lang1", "lang2");
}
use of org.sonar.api.batch.fs.InputFile in project sonar-web by SonarSource.
the class WebSensor method saveMetrics.
private static void saveMetrics(SensorContext context, WebSourceCode sourceCode) {
InputFile inputFile = sourceCode.inputFile();
saveComplexityDistribution(context, sourceCode);
for (Map.Entry<Metric<Integer>, Integer> entry : sourceCode.getMeasures().entrySet()) {
context.<Integer>newMeasure().on(inputFile).forMetric(entry.getKey()).withValue(entry.getValue()).save();
}
for (WebIssue issue : sourceCode.getIssues()) {
NewIssue newIssue = context.newIssue().forRule(issue.ruleKey()).gap(issue.cost());
Integer line = issue.line();
NewIssueLocation location = newIssue.newLocation().on(inputFile).message(issue.message());
if (line != null) {
location.at(inputFile.selectLine(line));
}
newIssue.at(location);
newIssue.save();
}
}
use of org.sonar.api.batch.fs.InputFile in project sonar-java by SonarSource.
the class UnitTestAnalyzer method readExecutionData.
private void readExecutionData(@Nullable File jacocoExecutionData, SensorContext context) {
File newJacocoExecutionData = jacocoExecutionData;
if (newJacocoExecutionData == null || !newJacocoExecutionData.isFile()) {
JaCoCoExtensions.LOG.info("Project coverage is set to 0% as no JaCoCo execution data has been dumped: {}", newJacocoExecutionData);
newJacocoExecutionData = null;
}
ExecutionDataVisitor executionDataVisitor = new ExecutionDataVisitor();
jacocoReportReader = new JacocoReportReader(newJacocoExecutionData).readJacocoReport(executionDataVisitor, executionDataVisitor);
boolean collectedCoveragePerTest = readCoveragePerTests(executionDataVisitor);
CoverageBuilder coverageBuilder = jacocoReportReader.analyzeFiles(executionDataVisitor.getMerged(), classFilesCache.values());
int analyzedResources = 0;
for (ISourceFileCoverage coverage : coverageBuilder.getSourceFiles()) {
InputFile inputFile = getResource(coverage);
if (inputFile != null) {
NewCoverage newCoverage = context.newCoverage().onFile(inputFile);
analyzeFile(newCoverage, inputFile, coverage);
newCoverage.save();
analyzedResources++;
}
}
if (analyzedResources == 0) {
JaCoCoExtensions.LOG.warn("Coverage information was not collected. Perhaps you forget to include debug information into compiled classes?");
} else if (collectedCoveragePerTest) {
JaCoCoExtensions.LOG.info("Information about coverage per test has been collected.");
} else if (newJacocoExecutionData != null) {
JaCoCoExtensions.LOG.info("No information about coverage per test.");
}
}
Aggregations