Search in sources :

Example 1 with Engine

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;
}
Also used : TestInputFileBuilder(org.sonar.api.batch.fs.internal.TestInputFileBuilder) Issue(org.sonar.commonruleengine.Issue) SingleFileVerifier(com.sonarsource.checks.verifier.SingleFileVerifier) Engine(org.sonar.commonruleengine.Engine)

Example 2 with Engine

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);
}
Also used : ArrayList(java.util.ArrayList) UastNode(org.sonar.uast.UastNode) Engine(org.sonar.commonruleengine.Engine) InputFile(org.sonar.api.batch.fs.InputFile)

Aggregations

Engine (org.sonar.commonruleengine.Engine)2 SingleFileVerifier (com.sonarsource.checks.verifier.SingleFileVerifier)1 ArrayList (java.util.ArrayList)1 InputFile (org.sonar.api.batch.fs.InputFile)1 TestInputFileBuilder (org.sonar.api.batch.fs.internal.TestInputFileBuilder)1 Issue (org.sonar.commonruleengine.Issue)1 UastNode (org.sonar.uast.UastNode)1