Search in sources :

Example 31 with InputFile

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

the class GoCoverageReport method saveFileCoverage.

private static void saveFileCoverage(SensorContext sensorContext, FileCoverage fileCoverage) {
    String absolutePath = fileCoverage.absolutePath.toString();
    FileSystem fileSystem = sensorContext.fileSystem();
    InputFile inputFile = fileSystem.inputFile(fileSystem.predicates().hasAbsolutePath(absolutePath));
    if (inputFile != null) {
        LOG.debug("Saving coverage measures for file '{}'", absolutePath);
        NewCoverage newCoverage = sensorContext.newCoverage().onFile(inputFile);
        for (Map.Entry<Integer, LineCoverage> entry : fileCoverage.lineMap.entrySet()) {
            newCoverage.lineHits(entry.getKey(), entry.getValue().hits);
        }
        newCoverage.save();
    } else {
        LOG.warn("File '{}' is not included in the project, ignoring coverage", absolutePath);
    }
}
Also used : NewCoverage(org.sonar.api.batch.sensor.coverage.NewCoverage) FileSystem(org.sonar.api.batch.fs.FileSystem) HashMap(java.util.HashMap) Map(java.util.Map) InputFile(org.sonar.api.batch.fs.InputFile)

Example 32 with InputFile

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

the class CpdVisitorTest method test.

@Test
void test() throws IOException {
    String filename = "lets.go";
    String code = readTestResource(getClass(), filename);
    String codeJson = readTestResource(getClass(), filename + ".uast.json");
    InputFile inputFile = createInputFile("lets.go", code);
    sensorContext.fileSystem().add(inputFile);
    CpdVisitor cpdVisitor = new CpdVisitor(sensorContext, inputFile);
    UastNode node = Uast.from(new StringReader(codeJson));
    cpdVisitor.scan(node);
    cpdVisitor.save();
    List<TokensLine> tokensLines = sensorContext.cpdTokens("module:" + inputFile.filename());
    assertThat(tokensLines).isNotNull().hasSize(5);
    assertThat(tokensLines).extracting("value").isEqualTo(Arrays.asList("packagemain", "funcfun()string{", "a:=LITERAL", "returna", "}"));
    assertThat(tokensLines).extracting("startLine").isEqualTo(Arrays.asList(1, 3, 4, 5, 6));
}
Also used : StringReader(java.io.StringReader) TokensLine(org.sonar.duplications.internal.pmd.TokensLine) UastNode(org.sonar.uast.UastNode) InputFile(org.sonar.api.batch.fs.InputFile) Test(org.junit.jupiter.api.Test)

Example 33 with InputFile

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

the class EngineTest method visit_should_visit_all_nodes.

@Test
void visit_should_visit_all_nodes() throws Exception {
    NodeCounter nodeCounter = new NodeCounter();
    Engine engine = new Engine(Collections.singletonList(nodeCounter));
    InputFile inputFile = TestInputFileBuilder.create(".", "foo.go").setType(InputFile.Type.MAIN).build();
    List<Issue> issues = engine.scan(uast, inputFile).issues;
    assertEquals(4, issues.size());
    assertTrue(issues.stream().map(Issue::getCheck).allMatch(rule -> rule == nodeCounter));
}
Also used : Test(org.junit.jupiter.api.Test) BeforeEach(org.junit.jupiter.api.BeforeEach) InputFile(org.sonar.api.batch.fs.InputFile) List(java.util.List) Uast(org.sonar.uast.Uast) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) UastNode(org.sonar.uast.UastNode) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) InputStreamReader(java.io.InputStreamReader) Collections(java.util.Collections) TestInputFileBuilder(org.sonar.api.batch.fs.internal.TestInputFileBuilder) Check(org.sonar.commonruleengine.checks.Check) InputFile(org.sonar.api.batch.fs.InputFile) Test(org.junit.jupiter.api.Test)

Example 34 with InputFile

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

the class GoSensorTest method test_failure_empty_file.

@Test
void test_failure_empty_file() throws Exception {
    InputFile failingFile = createInputFile("lets.go", InputFile.Type.MAIN, "");
    sensorContext.fileSystem().add(failingFile);
    GoSensor goSensor = getSensor("S2068");
    goSensor.execute(sensorContext);
    assertThat(logTester.logs(LoggerLevel.ERROR)).contains("Failed to analyze 1 file(s). Turn on debug message to see the details. Failed files:\nlets.go");
    assertThat(logTester.logs(LoggerLevel.DEBUG)).contains("Error analyzing file lets.go");
    // test log from external process asynchronously
    await().until(() -> logTester.logs(LoggerLevel.DEBUG).contains("panic: -:1:1: expected 'package', found 'EOF'"));
}
Also used : InputFile(org.sonar.api.batch.fs.InputFile) Test(org.junit.jupiter.api.Test)

Example 35 with InputFile

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

the class GoSensorTest method test_failure.

@Test
void test_failure() throws Exception {
    InputFile failingFile = createInputFile("lets.go", InputFile.Type.MAIN, "package main \n" + "\n" + "func test() {\n" + " pwd := \"secret\"\n" + "}");
    failingFile = spy(failingFile);
    IOException ioException = new IOException();
    when(failingFile.inputStream()).thenThrow(ioException);
    sensorContext.fileSystem().add(failingFile);
    sensorContext.settings().setProperty("sonar.go.coverage.reportPaths", "invalid-coverage-path.out");
    GoSensor goSensor = getSensor("S2068");
    goSensor.execute(sensorContext);
    assertThat(logTester.logs(LoggerLevel.ERROR).stream().collect(Collectors.joining("\n"))).contains("Failed to analyze 1 file(s). Turn on debug message to see the details. Failed files:\nlets.go").contains("Coverage report can't be loaded, file not found:").contains("invalid-coverage-path.out");
}
Also used : IOException(java.io.IOException) InputFile(org.sonar.api.batch.fs.InputFile) Test(org.junit.jupiter.api.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