Search in sources :

Example 1 with FileInfo

use of com.github._1c_syntax.bsl.languageserver.reporters.data.FileInfo in project bsl-language-server by 1c-syntax.

the class JUnitReporterTest method report.

@Test
void report() throws IOException {
    // given
    List<Diagnostic> diagnostics = new ArrayList<>();
    diagnostics.add(new Diagnostic(Ranges.create(0, 1, 2, 3), "message", DiagnosticSeverity.Error, "test-source", "test"));
    diagnostics.add(new Diagnostic(Ranges.create(0, 1, 2, 4), "message4", DiagnosticSeverity.Error, "test-source2", "test3"));
    diagnostics.add(new Diagnostic(Ranges.create(3, 1, 4, 4), "message4", DiagnosticSeverity.Error, "test-source2", "test3"));
    DocumentContext documentContext = TestUtils.getDocumentContext(Paths.get("./src/test/java/diagnostics/CanonicalSpellingKeywordsDiagnostic.bsl").toUri(), "");
    String sourceDir = ".";
    FileInfo fileInfo = new FileInfo(sourceDir, documentContext, diagnostics);
    AnalysisInfo analysisInfo = new AnalysisInfo(LocalDateTime.now(), Collections.singletonList(fileInfo), sourceDir);
    DiagnosticReporter reporter = new JUnitReporter();
    // when
    reporter.report(analysisInfo, Path.of(sourceDir));
    // then
    ObjectMapper mapper = new XmlMapper();
    JUnitTestSuites report = mapper.readValue(file, JUnitTestSuites.class);
    assertThat(report).isNotNull();
}
Also used : FileInfo(com.github._1c_syntax.bsl.languageserver.reporters.data.FileInfo) ArrayList(java.util.ArrayList) Diagnostic(org.eclipse.lsp4j.Diagnostic) AnalysisInfo(com.github._1c_syntax.bsl.languageserver.reporters.data.AnalysisInfo) DocumentContext(com.github._1c_syntax.bsl.languageserver.context.DocumentContext) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) XmlMapper(com.fasterxml.jackson.dataformat.xml.XmlMapper) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 2 with FileInfo

use of com.github._1c_syntax.bsl.languageserver.reporters.data.FileInfo in project bsl-language-server by 1c-syntax.

the class JsonReporterTest method report.

@Test
void report() throws IOException {
    // given
    Diagnostic diagnostic = new Diagnostic(Ranges.create(0, 1, 2, 3), "message", DiagnosticSeverity.Error, "test-source", "test");
    DocumentContext documentContext = TestUtils.getDocumentContext("");
    String sourceDir = ".";
    FileInfo fileInfo = new FileInfo(sourceDir, documentContext, Collections.singletonList(diagnostic));
    AnalysisInfo analysisInfo = new AnalysisInfo(LocalDateTime.now(), Collections.singletonList(fileInfo), sourceDir);
    JsonReporter reporter = new JsonReporter();
    // when
    reporter.report(analysisInfo, Path.of(sourceDir));
    // then
    ObjectMapper mapper = new AnalysisInfoObjectMapper();
    mapper.findAndRegisterModules();
    AnalysisInfo report = mapper.readValue(file, AnalysisInfo.class);
    Assertions.assertThat(report.getFileinfos()).hasSize(1);
}
Also used : FileInfo(com.github._1c_syntax.bsl.languageserver.reporters.data.FileInfo) AnalysisInfoObjectMapper(com.github._1c_syntax.bsl.languageserver.reporters.databind.AnalysisInfoObjectMapper) Diagnostic(org.eclipse.lsp4j.Diagnostic) AnalysisInfo(com.github._1c_syntax.bsl.languageserver.reporters.data.AnalysisInfo) DocumentContext(com.github._1c_syntax.bsl.languageserver.context.DocumentContext) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) AnalysisInfoObjectMapper(com.github._1c_syntax.bsl.languageserver.reporters.databind.AnalysisInfoObjectMapper) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 3 with FileInfo

use of com.github._1c_syntax.bsl.languageserver.reporters.data.FileInfo in project bsl-language-server by 1c-syntax.

the class TSLintReporterTest method report.

@Test
void report() throws IOException {
    // given
    Diagnostic diagnostic = new Diagnostic(Ranges.create(0, 1, 2, 3), "message", DiagnosticSeverity.Error, "test-source", "test");
    DocumentContext documentContext = TestUtils.getDocumentContext("");
    String sourceDir = ".";
    FileInfo fileInfo = new FileInfo(sourceDir, documentContext, Collections.singletonList(diagnostic));
    AnalysisInfo analysisInfo = new AnalysisInfo(LocalDateTime.now(), Collections.singletonList(fileInfo), sourceDir);
    TSLintReporter reporter = new TSLintReporter();
    // when
    reporter.report(analysisInfo, Path.of(sourceDir));
    // then
    ObjectMapper mapper = new ObjectMapper();
    List<TSLintReportEntry> report = mapper.readValue(file, new TypeReference<ArrayList<TSLintReportEntry>>() {
    });
    assertThat(report).hasSize(1);
}
Also used : FileInfo(com.github._1c_syntax.bsl.languageserver.reporters.data.FileInfo) ArrayList(java.util.ArrayList) Diagnostic(org.eclipse.lsp4j.Diagnostic) AnalysisInfo(com.github._1c_syntax.bsl.languageserver.reporters.data.AnalysisInfo) DocumentContext(com.github._1c_syntax.bsl.languageserver.context.DocumentContext) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 4 with FileInfo

use of com.github._1c_syntax.bsl.languageserver.reporters.data.FileInfo in project bsl-language-server by 1c-syntax.

the class SarifReporter method createResult.

private static Result createResult(FileInfo fileInfo, Diagnostic diagnostic) {
    var uri = Absolute.uri(fileInfo.getPath().toUri()).toString();
    var message = new Message().withText(diagnostic.getMessage());
    var ruleId = DiagnosticCode.getStringValue(diagnostic.getCode());
    var level = severityToResultLevel.get(diagnostic.getSeverity());
    var analysisTarget = new ArtifactLocation().withUri(uri);
    var locations = List.of(createLocation(diagnostic.getMessage(), uri, diagnostic.getRange()));
    var relatedLocations = Optional.ofNullable(diagnostic.getRelatedInformation()).stream().flatMap(Collection::stream).skip(1).map(relatedInformation -> createLocation(relatedInformation.getMessage(), relatedInformation.getLocation().getUri(), relatedInformation.getLocation().getRange())).collect(Collectors.toSet());
    return new Result().withMessage(message).withRuleId(ruleId).withLevel(level).withAnalysisTarget(analysisTarget).withLocations(locations).withRelatedLocations(relatedLocations);
}
Also used : ArtifactLocation(com.contrastsecurity.sarif.ArtifactLocation) ToolComponent(com.contrastsecurity.sarif.ToolComponent) SneakyThrows(lombok.SneakyThrows) ServerInfo(org.eclipse.lsp4j.ServerInfo) FileInfo(com.github._1c_syntax.bsl.languageserver.reporters.data.FileInfo) RequiredArgsConstructor(lombok.RequiredArgsConstructor) Diagnostic(org.eclipse.lsp4j.Diagnostic) Range(org.eclipse.lsp4j.Range) ReportingDescriptor(com.contrastsecurity.sarif.ReportingDescriptor) Message(com.contrastsecurity.sarif.Message) Absolute(com.github._1c_syntax.utils.Absolute) ArrayList(java.util.ArrayList) Region(com.contrastsecurity.sarif.Region) HashSet(java.util.HashSet) AnalysisInfo(com.github._1c_syntax.bsl.languageserver.reporters.data.AnalysisInfo) LanguageServerConfiguration(com.github._1c_syntax.bsl.languageserver.configuration.LanguageServerConfiguration) Map(java.util.Map) Either(org.eclipse.lsp4j.jsonrpc.messages.Either) ConfigurationOverride(com.contrastsecurity.sarif.ConfigurationOverride) Run(com.contrastsecurity.sarif.Run) URI(java.net.URI) Invocation(com.contrastsecurity.sarif.Invocation) Path(java.nio.file.Path) DiagnosticInfo(com.github._1c_syntax.bsl.languageserver.diagnostics.metadata.DiagnosticInfo) DiagnosticSeverity(org.eclipse.lsp4j.DiagnosticSeverity) Location(com.contrastsecurity.sarif.Location) Collection(java.util.Collection) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Set(java.util.Set) Collectors(java.util.stream.Collectors) File(java.io.File) Slf4j(lombok.extern.slf4j.Slf4j) Component(org.springframework.stereotype.Component) List(java.util.List) ReportingDescriptorReference(com.contrastsecurity.sarif.ReportingDescriptorReference) PropertyBag(com.contrastsecurity.sarif.PropertyBag) Tool(com.contrastsecurity.sarif.Tool) Optional(java.util.Optional) ReportingConfiguration(com.contrastsecurity.sarif.ReportingConfiguration) SerializationFeature(com.fasterxml.jackson.databind.SerializationFeature) MultiformatMessageString(com.contrastsecurity.sarif.MultiformatMessageString) PhysicalLocation(com.contrastsecurity.sarif.PhysicalLocation) DiagnosticCode(com.github._1c_syntax.bsl.languageserver.diagnostics.metadata.DiagnosticCode) Result(com.contrastsecurity.sarif.Result) SarifSchema210(com.contrastsecurity.sarif.SarifSchema210) Message(com.contrastsecurity.sarif.Message) ArtifactLocation(com.contrastsecurity.sarif.ArtifactLocation) Result(com.contrastsecurity.sarif.Result)

Example 5 with FileInfo

use of com.github._1c_syntax.bsl.languageserver.reporters.data.FileInfo in project bsl-language-server by 1c-syntax.

the class AnalyzeCommand method getFileInfoFromFile.

private FileInfo getFileInfoFromFile(Path srcDir, File file) {
    String textDocumentContent;
    try {
        textDocumentContent = FileUtils.readFileToString(file, StandardCharsets.UTF_8);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    DocumentContext documentContext = context.addDocument(file.toURI(), textDocumentContent, 1);
    Path filePath = srcDir.relativize(Absolute.path(file));
    List<Diagnostic> diagnostics = documentContext.getDiagnostics();
    MetricStorage metrics = documentContext.getMetrics();
    String mdoRef = "";
    Optional<AbstractMDObjectBase> mdObjectBase = documentContext.getMdObject();
    if (mdObjectBase.isPresent()) {
        mdoRef = mdObjectBase.get().getMdoReference().getMdoRef();
    }
    FileInfo fileInfo = new FileInfo(filePath, mdoRef, diagnostics, metrics);
    // clean up AST after diagnostic computing to free up RAM.
    documentContext.clearSecondaryData();
    return fileInfo;
}
Also used : Path(java.nio.file.Path) FileInfo(com.github._1c_syntax.bsl.languageserver.reporters.data.FileInfo) AbstractMDObjectBase(com.github._1c_syntax.mdclasses.mdo.AbstractMDObjectBase) MetricStorage(com.github._1c_syntax.bsl.languageserver.context.MetricStorage) Diagnostic(org.eclipse.lsp4j.Diagnostic) IOException(java.io.IOException) DocumentContext(com.github._1c_syntax.bsl.languageserver.context.DocumentContext)

Aggregations

FileInfo (com.github._1c_syntax.bsl.languageserver.reporters.data.FileInfo)12 AnalysisInfo (com.github._1c_syntax.bsl.languageserver.reporters.data.AnalysisInfo)10 Diagnostic (org.eclipse.lsp4j.Diagnostic)10 DocumentContext (com.github._1c_syntax.bsl.languageserver.context.DocumentContext)8 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)7 Test (org.junit.jupiter.api.Test)7 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)7 ArrayList (java.util.ArrayList)5 File (java.io.File)4 Path (java.nio.file.Path)4 Location (com.contrastsecurity.sarif.Location)2 PhysicalLocation (com.contrastsecurity.sarif.PhysicalLocation)2 Result (com.contrastsecurity.sarif.Result)2 SarifSchema210 (com.contrastsecurity.sarif.SarifSchema210)2 LanguageServerConfiguration (com.github._1c_syntax.bsl.languageserver.configuration.LanguageServerConfiguration)2 DiagnosticInfo (com.github._1c_syntax.bsl.languageserver.diagnostics.metadata.DiagnosticInfo)2 IOException (java.io.IOException)2 Collection (java.util.Collection)2 ArtifactLocation (com.contrastsecurity.sarif.ArtifactLocation)1 ConfigurationOverride (com.contrastsecurity.sarif.ConfigurationOverride)1