use of org.sonar.commonruleengine.Engine in project sonar-go by SonarSource.
the class TestUtils method analyze.
private static SingleFileVerifier analyze(Check check, Path testFile, UastNode uast) throws IOException {
TestInputFileBuilder inputFile = TestInputFileBuilder.create("test", testFile.getParent().toFile(), testFile.toFile());
inputFile.setContents(new String(Files.readAllBytes(testFile), StandardCharsets.UTF_8));
inputFile.setCharset(StandardCharsets.UTF_8);
Engine engine = new Engine(Collections.singletonList(check));
List<Issue> issues = engine.scan(uast, inputFile.build()).issues;
SingleFileVerifier verifier = SingleFileVerifier.create(testFile, StandardCharsets.UTF_8);
uast.getDescendants(UastNode.Kind.COMMENT, comment -> verifier.addComment(comment.token.line, comment.token.column, comment.token.value, 2, 0));
issues.forEach(issue -> reportIssueTo(verifier, issue));
return verifier;
}
use of org.sonar.commonruleengine.Engine in project sonar-go by SonarSource.
the class GoSensor method execute.
@Override
public void execute(SensorContext context) {
Engine ruleEngine = new Engine(checks.all());
UastGeneratorWrapper uastGenerator;
try {
uastGenerator = new UastGeneratorWrapper(context);
} catch (Exception e) {
LOG.error("Error initializing UAST generator", e);
return;
}
List<InputFile> failedFiles = new ArrayList<>();
for (InputFile inputFile : getInputFiles(context)) {
try {
UastNode uast = uastGenerator.createUast(inputFile.inputStream());
// FIXME currently *_test.go files are MAIN and not TEST, see issue #140
if (inputFile.type() == InputFile.Type.MAIN) {
Engine.ScanResult scanResult = ruleEngine.scan(uast, inputFile);
scanResult.issues.forEach(issue -> reportIssue(issue, context, inputFile));
saveMetrics(scanResult.metrics, context, inputFile);
saveCpdTokens(uast, context, inputFile);
}
saveHighlighting(uast, context, inputFile);
} catch (Exception e) {
failedFiles.add(inputFile);
LOG.debug("Error analyzing file " + inputFile.toString(), e);
}
}
if (!failedFiles.isEmpty()) {
String failedFilesAsString = failedFiles.stream().map(InputFile::toString).collect(Collectors.joining("\n"));
LOG.error("Failed to analyze {} file(s). Turn on debug message to see the details. Failed files:\n{}", failedFiles.size(), failedFilesAsString);
}
saveCoverageReports(context, GoCoverageReport.GoContext.DEFAULT);
}
Aggregations