Skip to content

Commit

Permalink
Merge pull request #1040 from Jobinquest/coverity_parser_test
Browse files Browse the repository at this point in the history
[JENKINS-72887] Add coverity parser
  • Loading branch information
uhafner committed Apr 21, 2024
2 parents 433eb68 + 2b927cc commit 2c94e6a
Show file tree
Hide file tree
Showing 6 changed files with 148 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package edu.hm.hafner.analysis.parser.violations;

import se.bjurr.violations.lib.parsers.CoverityParser;

/**
* Parses Coverity JSON V7 report files.
*
* @author Jobin Jose
*/
public class CoverityAdapter extends AbstractViolationAdapter {
private static final long serialVersionUID = -8210423965588732109L;

@Override
CoverityParser createParser() {
return new CoverityParser();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package edu.hm.hafner.analysis.registry;

import edu.hm.hafner.analysis.IssueParser;
import edu.hm.hafner.analysis.parser.violations.CoverityAdapter;

/**
* A descriptor for Coverity.
*
* @author Ullrich Hafner
*/
class CoverityDescriptor extends ParserDescriptor {
private static final String ID = "coverity";
private static final String NAME = "Coverity Scan";

CoverityDescriptor() {
super(ID, NAME);
}

@Override
public IssueParser createParser(final Option... options) {
return new CoverityAdapter();
}

@Override
public String getUrl() {
return "https://scan.coverity.com/";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public class ParserRegistry {
new CodeGeneratorDescriptor(),
new CodeNarcDescriptor(),
new CoolfluxChessccDescriptor(),
new CoverityDescriptor(),
new CpdDescriptor(),
new CppCheckDescriptor(),
new CppLintDescriptor(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package edu.hm.hafner.analysis.parser.violations;

import edu.hm.hafner.analysis.Report;
import edu.hm.hafner.analysis.Severity;
import edu.hm.hafner.analysis.assertions.SoftAssertions;
import edu.hm.hafner.analysis.registry.AbstractParserTest;

class CoverityAdapterTest extends AbstractParserTest {
CoverityAdapterTest() {
super("coverity.json");
}

@Override
protected void assertThatIssuesArePresent(final Report report, final SoftAssertions softly) {
softly.assertThat(report).hasSize(1);
var issue = report.get(0);
softly.assertThat(issue)
.hasFileName("C:/Workspace/workspace/Build_jenkins_development/somefile.cs")
.hasCategory("Integer handling issues")
.hasType("constant_expression_result/bit_and_with_zero")
.hasLineStart(79)
.hasSeverity(Severity.WARNING_NORMAL);
softly.assertThat(issue.getMessage()).startsWith("Bitwise-and ('&') operation applied to zero always produces zero.");
}

@Override
protected CoverityAdapter createParser() {
return new CoverityAdapter();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,15 @@ void shouldParseAllIssues() {
void shouldRegisterParser() {
assumeThat(createParser().getClass().getPackageName()).startsWith("edu.hm.hafner.analysis"); // only test our own parsers

Set<Class<?>> parsers = new ParserRegistry().getAllDescriptors()
var parserRegistry = new ParserRegistry();

Set<Class<?>> parsers = parserRegistry.getAllDescriptors()
.stream()
.map(ParserDescriptor::createParser)
.map(IssueParser::getClass)
.collect(Collectors.toSet());

var compositeParsers = new ParserRegistry().getAllDescriptors().stream()
var compositeParsers = parserRegistry.getAllDescriptors().stream()
.filter(descriptor -> descriptor instanceof CompositeParserDescriptor)
.map(descriptor -> (CompositeParserDescriptor) descriptor)
.map(CompositeParserDescriptor::createParsers)
Expand All @@ -99,6 +101,12 @@ void shouldRegisterParser() {
assertThat(parsers)
.as("Every parser should be registered in the ParserRegistry")
.contains(createParser().getClass());

parserRegistry.getAllDescriptors()
.stream()
.map(ParserDescriptor::getUrl)
.forEach(url ->
assertThat(url).matches("https?://.*|^$"));
}

protected void assertThatReportHasSeverities(final Report report, final int expectedSizeError,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
{
"type": "Coverity issues",
"formatVersion": 7,
"suppressedIssueCount": 0,
"issues": [
{
"mergeKey": "884ed7531feed32eb916d9038a3b9bd6",
"occurrenceCountForMK": 1,
"occurrenceNumberInMK": 1,
"referenceOccurrenceCountForMK": null,
"checkerName": "CONSTANT_EXPRESSION_RESULT",
"subcategory": "bit_and_with_zero",
"type": "constant_expression_result",
"subtype": "bit_and_with_zero",
"code-language": "c#",
"extra": "status",
"domain": "STATIC_CS",
"language": "C#",
"mainEventFilePathname": "C:\\Workspace\\workspace\\Build_jenkins_development\\somefile.cs",
"strippedMainEventFilePathname": "\\workspace\\Build_jenkins_development\\Architecture\\somefile.cs",
"mainEventLineNumber": 79,
"properties": {},
"functionDisplayName": "somename",
"functionMangledName": "somename",
"localStatus": null,
"ordered": false,
"events": [
{
"covLStrEventDescription": "{CovLStrv2{{t{{0} is always 0.}{{code{status & System.Printing.PrintJobStatus.None}}}}{t{ This occurs as a value.}}}}",
"eventDescription": "\"status & System.Printing.PrintJobStatus.None\" is always 0. This occurs as a value.",
"eventNumber": 1,
"eventTreePosition": "1",
"eventSet": 0,
"eventTag": "bit_and_with_zero",
"filePathname": "C:\\Workspace\\workspace\\Build_jenkins_development\\somefile.cs",
"strippedFilePathname": "\\workspace\\Build_jenkins_development\\Architecture\\somefile.cs",
"lineNumber": 79,
"main": true,
"moreInformationId": null,
"remediation": false,
"events": null
}
],
"stateOnServer": null,
"checkerProperties": {
"category": "Integer handling issues",
"categoryDescription": "Integer handling issues",
"cweCategory": "569",
"issueKinds": ["QUALITY"],
"eventSetCaptions": [],
"impact": "Medium",
"impactDescription": "Medium",
"subcategoryLocalEffect": "The expression's value is always zero; construct may indicate an inadvertent logic error.",
"subcategoryShortDescription": "Bitwise-and with zero",
"subcategoryLongDescription": "Bitwise-and ('&amp;') operation applied to zero always produces zero"
}
}
],
"desktopAnalysisSettings": null,
"error": null,
"warnings": []
}

0 comments on commit 2c94e6a

Please sign in to comment.