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