From 60fa8a9b6091ddd495238e5d3bb2de534a31a057 Mon Sep 17 00:00:00 2001 From: Tomas Bjerre Date: Sat, 5 Nov 2016 23:38:58 +0100 Subject: [PATCH] Simian, ZPTLint, JCReport --- CHANGELOG.md | 63 +++++- README.md | 3 + .../lib/parsers/JCReportParser.java | 86 ++++++++ .../violations/lib/parsers/SimianParser.java | 75 +++++++ .../violations/lib/parsers/ZPTLintParser.java | 38 ++++ .../violations/lib/reports/Reporter.java | 8 +- .../se/bjurr/violations/lib/JCReportTest.java | 46 +++++ .../se/bjurr/violations/lib/SimianTest.java | 45 +++++ .../se/bjurr/violations/lib/ZPTLintTest.java | 46 +++++ .../resources/jcreport/jcoderz-report.xml | 186 ++++++++++++++++++ src/test/resources/simian/fourfile.xml | 24 +++ src/test/resources/simian/onefile.xml | 20 ++ src/test/resources/simian/twofile.xml | 20 ++ src/test/resources/simian/twosets.xml | 26 +++ src/test/resources/zptlint/zplint-report.log | 2 + 15 files changed, 686 insertions(+), 2 deletions(-) create mode 100644 src/main/java/se/bjurr/violations/lib/parsers/JCReportParser.java create mode 100644 src/main/java/se/bjurr/violations/lib/parsers/SimianParser.java create mode 100644 src/main/java/se/bjurr/violations/lib/parsers/ZPTLintParser.java create mode 100644 src/test/java/se/bjurr/violations/lib/JCReportTest.java create mode 100644 src/test/java/se/bjurr/violations/lib/SimianTest.java create mode 100644 src/test/java/se/bjurr/violations/lib/ZPTLintTest.java create mode 100644 src/test/resources/jcreport/jcoderz-report.xml create mode 100644 src/test/resources/simian/fourfile.xml create mode 100644 src/test/resources/simian/onefile.xml create mode 100644 src/test/resources/simian/twofile.xml create mode 100644 src/test/resources/simian/twosets.xml create mode 100644 src/test/resources/zptlint/zplint-report.log diff --git a/CHANGELOG.md b/CHANGELOG.md index da5ffeaf..8b60cb72 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,10 +5,71 @@ Changelog of Git Changelog. ## Unreleased ### No issue +**Simian, ZPTLint, JCReport** + + +[3128473c31455e8](https://github.com/tomasbjerre/git-changelog-lib/commit/3128473c31455e8) Tomas Bjerre *2016-11-05 22:38:58* + +**Gendarme** + + +[3ac58ccc82b8360](https://github.com/tomasbjerre/git-changelog-lib/commit/3ac58ccc82b8360) Tomas Bjerre *2016-11-05 21:28:33* + +**CPD** + + +[7a4e651c53ab3b2](https://github.com/tomasbjerre/git-changelog-lib/commit/7a4e651c53ab3b2) Tomas Bjerre *2016-11-05 19:16:50* + +**CodeNarc** + + +[23fa562feee2bb2](https://github.com/tomasbjerre/git-changelog-lib/commit/23fa562feee2bb2) Tomas Bjerre *2016-11-05 18:58:32* + + +## 1.15 +### No issue + +**Exposing parser in reporter** + + +[2e216ee38cb2fdb](https://github.com/tomasbjerre/git-changelog-lib/commit/2e216ee38cb2fdb) Tomas Bjerre *2016-11-03 18:03:08* + + +## 1.14 +### No issue + +**Letting the reporters parse strings, not files** + + +[375706d8e579bfc](https://github.com/tomasbjerre/git-changelog-lib/commit/375706d8e579bfc) Tomas Bjerre *2016-11-03 17:54:20* + + +## 1.13 +### GitHub [#11](https://github.com/tomasbjerre/violations-lib/issues/11) + +**Handling css-lint reports where there are not line or evidence** + + * Also setting severity level for PyLint. + +[35d5d33b447b37a](https://github.com/tomasbjerre/git-changelog-lib/commit/35d5d33b447b37a) Tomas Bjerre *2016-10-26 15:39:37* + + +## 1.12 +### No issue + +**Changing rule format in PyLint to CODE(codeName)** + + +[24618707a88497c](https://github.com/tomasbjerre/git-changelog-lib/commit/24618707a88497c) Tomas Bjerre *2016-10-25 17:44:33* + + +## 1.11 +### No issue + **PyLint parser** -[10929695672b31f](https://github.com/tomasbjerre/git-changelog-lib/commit/10929695672b31f) Tomas Bjerre *2016-10-24 17:16:22* +[ec90aa741fdeb67](https://github.com/tomasbjerre/git-changelog-lib/commit/ec90aa741fdeb67) Tomas Bjerre *2016-10-24 17:16:46* ## 1.10 diff --git a/README.md b/README.md index 99f38c6d..bd8d42c5 100644 --- a/README.md +++ b/README.md @@ -16,13 +16,16 @@ It supports: * [_Gendarme_](http://www.mono-project.com/docs/tools+libraries/tools/gendarme/) * [_JSHint_](http://jshint.com/) * _Lint_ A common XML format, used by different linters. + * [_JCReport_](https://github.com/jCoderZ/fawkez/wiki/JcReport) * [_PerlCritic_](https://github.com/Perl-Critic) * [_PiTest_](http://pitest.org/) * [_PyLint_](https://www.pylint.org/) * [_PMD_](https://pmd.github.io/) * [_ReSharper_](https://www.jetbrains.com/resharper/) + * [_Simian_](http://www.harukizaemon.com/simian/) * [_StyleCop_](https://stylecop.codeplex.com/) * [_XMLLint_](http://xmlsoft.org/xmllint.html) + * [_ZPTLint_](https://pypi.python.org/pypi/zptlint) Example reports are available [in the test resources](https://github.com/tomasbjerre/violations-lib/tree/master/src/test/resources), examples of how to generate them are available [here](https://github.com/tomasbjerre/violations-test/blob/master/build.sh). diff --git a/src/main/java/se/bjurr/violations/lib/parsers/JCReportParser.java b/src/main/java/se/bjurr/violations/lib/parsers/JCReportParser.java new file mode 100644 index 00000000..8393f30f --- /dev/null +++ b/src/main/java/se/bjurr/violations/lib/parsers/JCReportParser.java @@ -0,0 +1,86 @@ +package se.bjurr.violations.lib.parsers; + +import static com.google.common.collect.Lists.newArrayList; +import static javax.xml.stream.XMLStreamConstants.START_ELEMENT; +import static se.bjurr.violations.lib.model.SEVERITY.ERROR; +import static se.bjurr.violations.lib.model.SEVERITY.INFO; +import static se.bjurr.violations.lib.model.SEVERITY.WARN; +import static se.bjurr.violations.lib.model.Violation.violationBuilder; +import static se.bjurr.violations.lib.parsers.ViolationParserUtils.getAttribute; +import static se.bjurr.violations.lib.parsers.ViolationParserUtils.getIntegerAttribute; +import static se.bjurr.violations.lib.reports.Reporter.JCREPORT; + +import java.io.ByteArrayInputStream; +import java.io.InputStream; +import java.util.List; + +import javax.xml.stream.XMLInputFactory; +import javax.xml.stream.XMLStreamReader; + +import se.bjurr.violations.lib.model.SEVERITY; +import se.bjurr.violations.lib.model.Violation; + +public class JCReportParser implements ViolationsParser { + + @Override + public List parseFile(String string) throws Exception { + List violations = newArrayList(); + + try (InputStream input = new ByteArrayInputStream(string.getBytes())) { + + XMLInputFactory factory = XMLInputFactory.newInstance(); + XMLStreamReader xmlr = factory.createXMLStreamReader(input); + + String name = null; + String findingType = null; + Integer line = null; + String message = null; + String origin = null; + String severity = null; + while (xmlr.hasNext()) { + int eventType = xmlr.next(); + if (eventType == START_ELEMENT) { + if (xmlr.getLocalName().equals("file")) { + name = getAttribute(xmlr, "name"); + } + if (xmlr.getLocalName().equals("item")) { + findingType = getAttribute(xmlr, "finding-type"); + line = getIntegerAttribute(xmlr, "line"); + message = getAttribute(xmlr, "message"); + origin = getAttribute(xmlr, "origin"); + severity = getAttribute(xmlr, "severity"); + Violation violation = violationBuilder()// + .setReporter(JCREPORT)// + .setFile(name)// + .setMessage(message)// + .setRule(findingType + "(" + origin + ")")// + .setSeverity(toSeverity(severity))// + .setStartLine(line)// + .build(); + violations.add(violation); + } + } + } + } + return violations; + } + + private SEVERITY toSeverity(String severity) { + if (severity.equals("error")) { + return ERROR; + } + if (severity.equals("cpd")) { + return ERROR; + } + if (severity.equals("warning")) { + return WARN; + } + if (severity.equals("design")) { + return WARN; + } + if (severity.equals("code-style")) { + return INFO; + } + return INFO; + } +} diff --git a/src/main/java/se/bjurr/violations/lib/parsers/SimianParser.java b/src/main/java/se/bjurr/violations/lib/parsers/SimianParser.java new file mode 100644 index 00000000..a2dd0b23 --- /dev/null +++ b/src/main/java/se/bjurr/violations/lib/parsers/SimianParser.java @@ -0,0 +1,75 @@ +package se.bjurr.violations.lib.parsers; + +import static com.google.common.collect.Lists.newArrayList; +import static javax.xml.stream.XMLStreamConstants.START_ELEMENT; +import static se.bjurr.violations.lib.model.SEVERITY.ERROR; +import static se.bjurr.violations.lib.model.SEVERITY.INFO; +import static se.bjurr.violations.lib.model.SEVERITY.WARN; +import static se.bjurr.violations.lib.model.Violation.violationBuilder; +import static se.bjurr.violations.lib.parsers.ViolationParserUtils.getAttribute; +import static se.bjurr.violations.lib.parsers.ViolationParserUtils.getIntegerAttribute; +import static se.bjurr.violations.lib.reports.Reporter.SIMIAN; + +import java.io.ByteArrayInputStream; +import java.io.InputStream; +import java.util.List; + +import javax.xml.stream.XMLInputFactory; +import javax.xml.stream.XMLStreamReader; + +import se.bjurr.violations.lib.model.SEVERITY; +import se.bjurr.violations.lib.model.Violation; + +public class SimianParser implements ViolationsParser { + + @Override + public List parseFile(String string) throws Exception { + List violations = newArrayList(); + + try (InputStream input = new ByteArrayInputStream(string.getBytes())) { + + XMLInputFactory factory = XMLInputFactory.newInstance(); + XMLStreamReader xmlr = factory.createXMLStreamReader(input); + + String sourceFile = null; + Integer lineCount = null; + Integer startLineNumber = null; + Integer endLineNumber = null; + while (xmlr.hasNext()) { + int eventType = xmlr.next(); + if (eventType == START_ELEMENT) { + if (xmlr.getLocalName().equals("set")) { + lineCount = getIntegerAttribute(xmlr, "lineCount"); + } + if (xmlr.getLocalName().equals("block")) { + sourceFile = getAttribute(xmlr, "sourceFile"); + startLineNumber = getIntegerAttribute(xmlr, "startLineNumber"); + endLineNumber = getIntegerAttribute(xmlr, "endLineNumber"); + + Violation violation = violationBuilder()// + .setReporter(SIMIAN)// + .setFile(sourceFile)// + .setMessage("Duplication")// + .setRule("DUPLICATION")// + .setSeverity(toSeverity(lineCount))// + .setStartLine(startLineNumber)// + .setEndLine(endLineNumber)// + .build(); + violations.add(violation); + } + } + } + } + return violations; + } + + private SEVERITY toSeverity(Integer lineCount) { + if (lineCount < 10) { + return INFO; + } + if (lineCount < 50) { + return WARN; + } + return ERROR; + } +} diff --git a/src/main/java/se/bjurr/violations/lib/parsers/ZPTLintParser.java b/src/main/java/se/bjurr/violations/lib/parsers/ZPTLintParser.java new file mode 100644 index 00000000..46fed187 --- /dev/null +++ b/src/main/java/se/bjurr/violations/lib/parsers/ZPTLintParser.java @@ -0,0 +1,38 @@ +package se.bjurr.violations.lib.parsers; + +import static com.google.common.collect.Lists.newArrayList; +import static se.bjurr.violations.lib.model.SEVERITY.ERROR; +import static se.bjurr.violations.lib.model.Violation.violationBuilder; +import static se.bjurr.violations.lib.parsers.ViolationParserUtils.getLines; +import static se.bjurr.violations.lib.reports.Reporter.ZPTLINT; + +import java.util.List; + +import se.bjurr.violations.lib.model.Violation; + +public class ZPTLintParser implements ViolationsParser { + + @Override + public List parseFile(String string) throws Exception { + List violations = newArrayList(); + for (List parts : getLines(string, "[ ]+Error in: (.*) (.*) , at line (\\d+).*")) { + if (parts.size() < 3) { + continue; + } + Integer lineInFile = Integer.parseInt(parts.get(3)); + String message = parts.get(2); + String fileName = parts.get(1); + Violation violation = violationBuilder()// + .setReporter(ZPTLINT)// + .setFile(fileName)// + .setMessage(message)// + .setRule("ZPT")// + .setSeverity(ERROR)// + .setStartLine(lineInFile)// + .build(); + violations.add(violation); + } + return violations; + } + +} diff --git a/src/main/java/se/bjurr/violations/lib/reports/Reporter.java b/src/main/java/se/bjurr/violations/lib/reports/Reporter.java index f983d991..a7a5c1f1 100644 --- a/src/main/java/se/bjurr/violations/lib/reports/Reporter.java +++ b/src/main/java/se/bjurr/violations/lib/reports/Reporter.java @@ -22,6 +22,7 @@ import se.bjurr.violations.lib.parsers.Flake8Parser; import se.bjurr.violations.lib.parsers.FxCopParser; import se.bjurr.violations.lib.parsers.GendarmeParser; +import se.bjurr.violations.lib.parsers.JCReportParser; import se.bjurr.violations.lib.parsers.JSHintParser; import se.bjurr.violations.lib.parsers.LintParser; import se.bjurr.violations.lib.parsers.PMDParser; @@ -29,9 +30,11 @@ import se.bjurr.violations.lib.parsers.PiTestParser; import se.bjurr.violations.lib.parsers.PyLintParser; import se.bjurr.violations.lib.parsers.ResharperParser; +import se.bjurr.violations.lib.parsers.SimianParser; import se.bjurr.violations.lib.parsers.StyleCopParser; import se.bjurr.violations.lib.parsers.ViolationsParser; import se.bjurr.violations.lib.parsers.XMLLintParser; +import se.bjurr.violations.lib.parsers.ZPTLintParser; public enum Reporter { ANDROIDLINT(new AndroidLintParser()), // @@ -45,6 +48,7 @@ public enum Reporter { FLAKE8(new Flake8Parser()), // FXCOP(new FxCopParser()), // GENDARME(new GendarmeParser()), // + JCREPORT(new JCReportParser()), // JSHINT(new JSHintParser()), // LINT(new LintParser()), // PERLCRITIC(new PerlCriticParser()), // @@ -52,8 +56,10 @@ public enum Reporter { PMD(new PMDParser()), // PYLINT(new PyLintParser()), // RESHARPER(new ResharperParser()), // + SIMIAN(new SimianParser()), // STYLECOP(new StyleCopParser()), // - XMLLINT(new XMLLintParser()); + XMLLINT(new XMLLintParser()), // + ZPTLINT(new ZPTLintParser()); private static Logger LOG = Logger.getLogger(Reporter.class.getSimpleName()); private ViolationsParser violationsParser; diff --git a/src/test/java/se/bjurr/violations/lib/JCReportTest.java b/src/test/java/se/bjurr/violations/lib/JCReportTest.java new file mode 100644 index 00000000..eac1dce4 --- /dev/null +++ b/src/test/java/se/bjurr/violations/lib/JCReportTest.java @@ -0,0 +1,46 @@ +package se.bjurr.violations.lib; + +import static org.assertj.core.api.Assertions.assertThat; +import static se.bjurr.violations.lib.TestUtils.getRootFolder; +import static se.bjurr.violations.lib.ViolationsReporterApi.violationsReporterApi; +import static se.bjurr.violations.lib.model.SEVERITY.INFO; +import static se.bjurr.violations.lib.reports.Reporter.JCREPORT; + +import java.util.List; + +import org.junit.Test; + +import se.bjurr.violations.lib.model.Violation; + +public class JCReportTest { + @Test + public void testThatViolationsCanBeParsed() { + String rootFolder = getRootFolder(); + + List actual = violationsReporterApi() // + .withPattern(".*/jcreport/.*\\.xml$") // + .inFolder(rootFolder) // + .findAll(JCREPORT) // + .violations(); + + assertThat(actual)// + .hasSize(54); + + assertThat(actual.get(0).getMessage())// + .isEqualTo("Type Javadoc comment is missing an @author tag."); + assertThat(actual.get(0).getFile())// + .isEqualTo("D:/projects/fawkez/test/java/org/jcoderz/commons/logging/XmlPrinterTest.java"); + assertThat(actual.get(0).getSeverity())// + .isEqualTo(INFO); + assertThat(actual.get(0).getRule().get())// + .isEqualTo("CS_MISSING_TAG(Checkstyle)"); + assertThat(actual.get(0).getStartLine())// + .isEqualTo(50); + assertThat(actual.get(0).getEndLine())// + .isEqualTo(50); + + assertThat(actual.get(1).getMessage())// + .isEqualTo("Class LogElementHandler should be declared as final."); + } + +} diff --git a/src/test/java/se/bjurr/violations/lib/SimianTest.java b/src/test/java/se/bjurr/violations/lib/SimianTest.java new file mode 100644 index 00000000..1624598b --- /dev/null +++ b/src/test/java/se/bjurr/violations/lib/SimianTest.java @@ -0,0 +1,45 @@ +package se.bjurr.violations.lib; + +import static org.assertj.core.api.Assertions.assertThat; +import static se.bjurr.violations.lib.TestUtils.getRootFolder; +import static se.bjurr.violations.lib.ViolationsReporterApi.violationsReporterApi; +import static se.bjurr.violations.lib.model.SEVERITY.INFO; +import static se.bjurr.violations.lib.reports.Reporter.SIMIAN; + +import java.util.List; + +import org.junit.Test; + +import se.bjurr.violations.lib.model.Violation; + +public class SimianTest { + @Test + public void testThatViolationsCanBeParsed() { + String rootFolder = getRootFolder(); + + List actual = violationsReporterApi() // + .withPattern(".*/simian/.*\\.xml$") // + .inFolder(rootFolder) // + .findAll(SIMIAN) // + .violations(); + + assertThat(actual)// + .hasSize(12); + + assertThat(actual.get(0).getMessage())// + .startsWith("Duplication"); + assertThat(actual.get(0).getFile())// + .isEqualTo("c:/java/foo1.java"); + assertThat(actual.get(0).getSeverity())// + .isEqualTo(INFO); + assertThat(actual.get(0).getRule().get())// + .isEqualTo("DUPLICATION"); + assertThat(actual.get(0).getStartLine())// + .isEqualTo(11); + assertThat(actual.get(0).getEndLine())// + .isEqualTo(16); + + assertThat(actual.get(5).getMessage())// + .startsWith("Duplication"); + } +} diff --git a/src/test/java/se/bjurr/violations/lib/ZPTLintTest.java b/src/test/java/se/bjurr/violations/lib/ZPTLintTest.java new file mode 100644 index 00000000..d0fde7ba --- /dev/null +++ b/src/test/java/se/bjurr/violations/lib/ZPTLintTest.java @@ -0,0 +1,46 @@ +package se.bjurr.violations.lib; + +import static org.assertj.core.api.Assertions.assertThat; +import static se.bjurr.violations.lib.TestUtils.getRootFolder; +import static se.bjurr.violations.lib.ViolationsReporterApi.violationsReporterApi; +import static se.bjurr.violations.lib.model.SEVERITY.ERROR; +import static se.bjurr.violations.lib.reports.Reporter.ZPTLINT; + +import java.util.List; + +import org.junit.Test; + +import se.bjurr.violations.lib.model.Violation; + +public class ZPTLintTest { + @Test + public void testThatViolationsCanBeParsed() { + String rootFolder = getRootFolder(); + + List actual = violationsReporterApi() // + .withPattern(".*/zptlint/.*\\.log$") // + .inFolder(rootFolder) // + .findAll(ZPTLINT) // + .violations(); + + assertThat(actual)// + .hasSize(2); + + assertThat(actual.get(0).getMessage())// + .isEqualTo("abc def ghe '\" 123"); + assertThat(actual.get(0).getFile())// + .isEqualTo("cpplint.py"); + assertThat(actual.get(0).getSeverity())// + .isEqualTo(ERROR); + assertThat(actual.get(0).getRule().get())// + .isEqualTo("ZPT"); + assertThat(actual.get(0).getStartLine())// + .isEqualTo(4796); + assertThat(actual.get(0).getEndLine())// + .isEqualTo(4796); + + assertThat(actual.get(1).getMessage())// + .isEqualTo("abc '\" 123 def ghe"); + } + +} diff --git a/src/test/resources/jcreport/jcoderz-report.xml b/src/test/resources/jcreport/jcoderz-report.xml new file mode 100644 index 00000000..7520a5c5 --- /dev/null +++ b/src/test/resources/jcreport/jcoderz-report.xml @@ -0,0 +1,186 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/test/resources/simian/fourfile.xml b/src/test/resources/simian/fourfile.xml new file mode 100644 index 00000000..0cf47be6 --- /dev/null +++ b/src/test/resources/simian/fourfile.xml @@ -0,0 +1,24 @@ + + + + + + + + + + + + + + diff --git a/src/test/resources/simian/onefile.xml b/src/test/resources/simian/onefile.xml new file mode 100644 index 00000000..2ea9693e --- /dev/null +++ b/src/test/resources/simian/onefile.xml @@ -0,0 +1,20 @@ + + + + + + + + + + + + diff --git a/src/test/resources/simian/twofile.xml b/src/test/resources/simian/twofile.xml new file mode 100644 index 00000000..bfd8b8d7 --- /dev/null +++ b/src/test/resources/simian/twofile.xml @@ -0,0 +1,20 @@ + + + + + + + + + + + + diff --git a/src/test/resources/simian/twosets.xml b/src/test/resources/simian/twosets.xml new file mode 100644 index 00000000..0c28da75 --- /dev/null +++ b/src/test/resources/simian/twosets.xml @@ -0,0 +1,26 @@ + + + + + + + + + + + + + + + + diff --git a/src/test/resources/zptlint/zplint-report.log b/src/test/resources/zptlint/zplint-report.log new file mode 100644 index 00000000..c1485846 --- /dev/null +++ b/src/test/resources/zptlint/zplint-report.log @@ -0,0 +1,2 @@ + Error in: cpplint.py abc def ghe '" 123 , at line 4796 + Error in: cpplint.py abc '" 123 def ghe , at line 4797