Skip to content

Commit

Permalink
fix: support CFN with JUnit format (refs #131)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasbjerre committed Aug 9, 2021
1 parent 7836eb5 commit 56175a4
Show file tree
Hide file tree
Showing 5 changed files with 227 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ A number of **parsers** have been implemented. Some **parsers** can parse output
| [_CPPLint_](https://github.com/theandrewdavis/cpplint) | `CPPLINT` |
| [_CSSLint_](https://github.com/CSSLint/csslint) | `CSSLINT` |
| [_Checkstyle_](http://checkstyle.sourceforge.net/) | `CHECKSTYLE` |
| [_CloudFormation Linter_](https://github.com/aws-cloudformation/cfn-lint) | `JUNIT` | `cfn-lint . -f junit --output-file report-junit.xml`
| [_CodeClimate_](https://codeclimate.com/) | `CODECLIMATE` |
| [_CodeNarc_](http://codenarc.sourceforge.net/) | `CODENARC` |
| [_Detekt_](https://github.com/arturbosch/detekt) | `CHECKSTYLE` | With `--output-format xml`.
Expand Down
11 changes: 7 additions & 4 deletions src/main/java/se/bjurr/violations/lib/parsers/JUnitParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import static se.bjurr.violations.lib.model.SEVERITY.ERROR;
import static se.bjurr.violations.lib.model.Violation.violationBuilder;
import static se.bjurr.violations.lib.reports.Parser.JUNIT;
import static se.bjurr.violations.lib.util.ViolationParserUtils.findAttribute;
import static se.bjurr.violations.lib.util.ViolationParserUtils.getAttribute;

import java.io.ByteArrayInputStream;
Expand Down Expand Up @@ -36,7 +37,7 @@ public Set<Violation> parseReportOutput(
final int eventType = xmlr.next();
if (eventType == XMLStreamConstants.START_ELEMENT) {
if (xmlr.getLocalName().equalsIgnoreCase("testcase")) {
className = getAttribute(xmlr, "classname");
className = findAttribute(xmlr, "classname").orElse("");
name = getAttribute(xmlr, "name");
} else if (xmlr.getLocalName().equalsIgnoreCase("failure")
|| xmlr.getLocalName().equalsIgnoreCase("error")) {
Expand Down Expand Up @@ -141,9 +142,11 @@ private FileAndLine findFilePathInContent(final String failureContent, final Str
if (found.size() == 1) {
return found.get(0);
}
for (final FileAndLine candidate : found) {
if (candidate.file.startsWith(className.replace(".", "/"))) {
return candidate;
if (!className.isEmpty()) {
for (final FileAndLine candidate : found) {
if (candidate.file.startsWith(className.replace(".", "/"))) {
return candidate;
}
}
}
return found.get(0);
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/se/bjurr/violations/lib/reports/Reporter.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ public enum Reporter {
"https://github.com/PyCQA/bandit",
"With `bandit -r examples/ -f custom -o bandit.out --msg-template \"{abspath}:{line}: {severity}: {test_id}: {msg}\"`"),
CLANG("CLang", Parser.CLANG, "https://clang-analyzer.llvm.org/", ""),
CFN(
"CloudFormation Linter",
Parser.JUNIT,
"https://github.com/aws-cloudformation/cfn-lint",
"`cfn-lint . -f junit --output-file report-junit.xml`"),
CPD("CPD", Parser.CPD, "http://pmd.sourceforge.net/pmd-4.3.0/cpd.html", ""),
CPPCHECK(
"CPPCheck",
Expand Down
27 changes: 27 additions & 0 deletions src/test/java/se/bjurr/violations/lib/JUnitTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -333,4 +333,31 @@ public void testThatViolationsCanBeParsedFromPlain() {
"shouldParseEmptyFile : org.json.JSONException: Missing value at 0 [character 1 line 1]");
assertThat(violation0.getSeverity()).isEqualTo(ERROR);
}

@Test
public void testThatViolationsCanBeParsedFromCfn() {
final String rootFolder = getRootFolder();

final Set<Violation> actual =
violationsApi() //
.withPattern(".*/junit/cfn-lint\\.xml$") //
.inFolder(rootFolder) //
.findAll(JUNIT) //
.violations();

assertThat(actual) //
.hasSize(2);

final Violation violation0 = new ArrayList<>(actual).get(0);
assertThat(violation0.getSource()) //
.isEqualTo("");
assertThat(violation0.getFile()) //
.isEqualTo("-");
assertThat(violation0.getMessage()) //
.startsWith("E3012") //
.contains(
"E3012 Check resource properties values : Property Resources/ActivitiesTable/Properties/ProvisionedThroughput/ReadCapacityUnits should be of type Long at sam-python-crud-sample/template.yaml:142:9");
assertThat(violation0.getSeverity()) //
.isEqualTo(ERROR);
}
}
Loading

0 comments on commit 56175a4

Please sign in to comment.