From f2720de0ec7d36c319106bd1da19c30a7b64c906 Mon Sep 17 00:00:00 2001 From: Tomas Bjerre Date: Sun, 14 Jan 2018 12:18:56 +0100 Subject: [PATCH] Google error-prone #10 --- CHANGELOG.md | 11 ++- README.md | 1 + .../lib/parsers/GoogleErrorProneParser.java | 71 ++++++++++++++++ .../bjurr/violations/lib/reports/Parser.java | 6 +- .../violations/lib/GoogleErrorProneTest.java | 83 +++++++++++++++++++ .../googleErrorProne/googleErrorProne.log | 32 +++++++ .../googleErrorProneMaven.log | 11 +++ 7 files changed, 212 insertions(+), 3 deletions(-) create mode 100644 src/main/java/se/bjurr/violations/lib/parsers/GoogleErrorProneParser.java create mode 100644 src/test/java/se/bjurr/violations/lib/GoogleErrorProneTest.java create mode 100644 src/test/resources/googleErrorProne/googleErrorProne.log create mode 100644 src/test/resources/googleErrorProne/googleErrorProneMaven.log diff --git a/CHANGELOG.md b/CHANGELOG.md index 8c9f5c9f..53319173 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,12 +4,21 @@ Changelog of Violations lib. ## Unreleased +### GitHub [#10](https://github.com/tomasbjerre/violations-lib/issues/10) Google error-prone + +**Google error-prone** + + +[80b0e9e99d20eee](https://github.com/tomasbjerre/violations-lib/commit/80b0e9e99d20eee) Tomas Bjerre *2018-01-14 11:19:04* + + +## 1.51 ### GitHub [#30](https://github.com/tomasbjerre/violations-lib/pull/30) Parameterize the max line length for the report table. **Parameterize max width of reporter table** -[0be6456df10d23b](https://github.com/tomasbjerre/violations-lib/commit/0be6456df10d23b) Tomas Bjerre *2018-01-13 19:03:00* +[87c5ab7cb5f4ae7](https://github.com/tomasbjerre/violations-lib/commit/87c5ab7cb5f4ae7) Tomas Bjerre *2018-01-13 19:06:03* ## 1.50 diff --git a/README.md b/README.md index 51bedeb8..b478742a 100644 --- a/README.md +++ b/README.md @@ -28,6 +28,7 @@ It supports: * [_Gendarme_](http://www.mono-project.com/docs/tools+libraries/tools/gendarme/) * [_GoLint_](https://github.com/golang/lint) * [_GoVet_](https://golang.org/cmd/vet/) Same format as GoLint. + * [_GoogleErrorProne_](https://github.com/google/error-prone) * [_JSHint_](http://jshint.com/) * _Lint_ A common XML format, used by different linters. * [_JCReport_](https://github.com/jCoderZ/fawkez/wiki/JcReport) diff --git a/src/main/java/se/bjurr/violations/lib/parsers/GoogleErrorProneParser.java b/src/main/java/se/bjurr/violations/lib/parsers/GoogleErrorProneParser.java new file mode 100644 index 00000000..0f9ef90d --- /dev/null +++ b/src/main/java/se/bjurr/violations/lib/parsers/GoogleErrorProneParser.java @@ -0,0 +1,71 @@ +package se.bjurr.violations.lib.parsers; + +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.reports.Parser.GOOGLEERRORPRONE; + +import java.util.ArrayList; +import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import se.bjurr.violations.lib.model.SEVERITY; +import se.bjurr.violations.lib.model.Violation; + +public class GoogleErrorProneParser implements ViolationsParser { + private static Pattern NEW_VIOLATION = + Pattern.compile("^((:[^/]*)|([^:]))([^:]+?):([^:]+?):([^:]+?):[^\\[]*\\[([^\\]]+?)](.*)"); + + @Override + public List parseReportOutput(final String reportContent) throws Exception { + final List found = new ArrayList<>(); + String currentFilename = null; + Integer currentLine = null; + SEVERITY currentSeverity = null; + String currentRule = null; + String currentRuleMessage = null; + StringBuilder currentMessage = null; + final String[] lines = reportContent.split("\n"); + for (int i = 0; i < lines.length; i++) { + String line = lines[i]; + final Matcher matcher = NEW_VIOLATION.matcher(line); + if (matcher.find()) { + currentFilename = matcher.group(4).trim(); + currentLine = Integer.parseInt(matcher.group(5)); + currentSeverity = toSeverity(matcher.group(6)); + currentRule = matcher.group(7).trim(); + currentRuleMessage = matcher.group(8).trim(); + currentMessage = new StringBuilder(); + for (int j = i + 1; j < lines.length; j++) { + line = lines[j]; + if (!line.startsWith(" ")) { + found.add( + violationBuilder() // + .setFile(currentFilename) // + .setMessage(currentRuleMessage + "\n\n" + currentMessage.toString()) // + .setParser(GOOGLEERRORPRONE) // + .setRule(currentRule) // + .setSeverity(currentSeverity) // + .setStartLine(currentLine) // + .build()); + break; + } + i++; + currentMessage.append(line.trim()); + } + } + } + return found; + } + + private SEVERITY toSeverity(final String from) { + if (from.trim().equalsIgnoreCase("error")) { + return ERROR; + } + if (from.trim().equalsIgnoreCase("warning")) { + return WARN; + } + return INFO; + } +} diff --git a/src/main/java/se/bjurr/violations/lib/reports/Parser.java b/src/main/java/se/bjurr/violations/lib/reports/Parser.java index 8956200c..6d638e17 100644 --- a/src/main/java/se/bjurr/violations/lib/reports/Parser.java +++ b/src/main/java/se/bjurr/violations/lib/reports/Parser.java @@ -22,6 +22,7 @@ import se.bjurr.violations.lib.parsers.FxCopParser; import se.bjurr.violations.lib.parsers.GendarmeParser; import se.bjurr.violations.lib.parsers.GoLintParser; +import se.bjurr.violations.lib.parsers.GoogleErrorProneParser; import se.bjurr.violations.lib.parsers.JCReportParser; import se.bjurr.violations.lib.parsers.JSHintParser; import se.bjurr.violations.lib.parsers.KlocworkParser; @@ -60,6 +61,7 @@ public enum Parser { KLOCWORK(new KlocworkParser()), // MYPY(new MyPyParser()), // GOLINT(new GoLintParser()), // + GOOGLEERRORPRONE(new GoogleErrorProneParser()), // PERLCRITIC(new PerlCriticParser()), // PITEST(new PiTestParser()), // PMD(new PMDParser()), // @@ -76,11 +78,11 @@ public enum Parser { private static Logger LOG = Logger.getLogger(Parser.class.getSimpleName()); private transient ViolationsParser violationsParser; - private Parser(ViolationsParser violationsParser) { + private Parser(final ViolationsParser violationsParser) { this.violationsParser = violationsParser; } - public List findViolations(List includedFiles) { + public List findViolations(final List includedFiles) { final List violations = new ArrayList<>(); for (final File file : includedFiles) { try { diff --git a/src/test/java/se/bjurr/violations/lib/GoogleErrorProneTest.java b/src/test/java/se/bjurr/violations/lib/GoogleErrorProneTest.java new file mode 100644 index 00000000..4dbeffe5 --- /dev/null +++ b/src/test/java/se/bjurr/violations/lib/GoogleErrorProneTest.java @@ -0,0 +1,83 @@ +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.ViolationsApi.violationsApi; +import static se.bjurr.violations.lib.model.SEVERITY.ERROR; +import static se.bjurr.violations.lib.model.SEVERITY.WARN; +import static se.bjurr.violations.lib.reports.Parser.GOOGLEERRORPRONE; + +import java.util.List; +import org.junit.Test; +import se.bjurr.violations.lib.model.Violation; + +public class GoogleErrorProneTest { + + @Test + public void testThatViolationsCanBeParsedGradle() { + final String rootFolder = getRootFolder(); + + final List actual = + violationsApi() // + .withPattern(".*/googleErrorProne/googleErrorProne\\.log$") // + .inFolder(rootFolder) // + .findAll(GOOGLEERRORPRONE) // + .violations(); + + assertThat(actual) // + .hasSize(5); + + final Violation violation0 = actual.get(0); + assertThat(violation0.getMessage()) // + .endsWith("Splitter.on(\",\").split(link)) {'?"); + assertThat(violation0.getFile()) // + .isEqualTo( + "/home/bjerre/workspace/git-changelog/git-changelog-lib/src/main/java/se/bjurr/gitchangelog/internal/integrations/github/GitHubHelper.java"); + assertThat(violation0.getSeverity()) // + .isEqualTo(WARN); + assertThat(violation0.getRule().get()) // + .isEqualTo("StringSplitter"); + assertThat(violation0.getStartLine()) // + .isEqualTo(51); + + final Violation violation4 = actual.get(4); + assertThat(violation4.getMessage()) // + .endsWith(", otherCommitTime);'?"); + assertThat(violation4.getFile()) // + .isEqualTo( + "home/bjerre/workspace/git-changelog/git-changelog-lib/src/main/java/se/bjurr/gitchangelog/internal/git/TraversalWork.java"); + assertThat(violation4.getSeverity()) // + .isEqualTo(WARN); + assertThat(violation4.getRule().get()) // + .isEqualTo("BoxedPrimitiveConstructor"); + assertThat(violation4.getStartLine()) // + .isEqualTo(73); + } + + @Test + public void testThatViolationsCanBeParsedMaven() { + final String rootFolder = getRootFolder(); + + final List actual = + violationsApi() // + .withPattern(".*/googleErrorProne/googleErrorProneMaven\\.log$") // + .inFolder(rootFolder) // + .findAll(GOOGLEERRORPRONE) // + .violations(); + + assertThat(actual) // + .hasSize(1); + + final Violation violation0 = actual.get(0); + assertThat(violation0.getMessage()) // + .endsWith("row new Exception();'?"); + assertThat(violation0.getFile()) // + .isEqualTo("../examples/maven/error_prone_should_flag/src/main/java/Main.java"); + assertThat(violation0.getSeverity()) // + .isEqualTo(ERROR); + assertThat(violation0.getRule().get()) // + .isEqualTo("DeadException"); + assertThat(violation0.getStartLine()) // + .isEqualTo(20); + } +} diff --git a/src/test/resources/googleErrorProne/googleErrorProne.log b/src/test/resources/googleErrorProne/googleErrorProne.log new file mode 100644 index 00000000..2fed38db --- /dev/null +++ b/src/test/resources/googleErrorProne/googleErrorProne.log @@ -0,0 +1,32 @@ +:googleErrorProne UP-TO-DATE +:googleJavaFormat NO-SOURCE +:compileJava/home/bjerre/workspace/git-changelog/git-changelog-lib/src/main/java/se/bjurr/gitchangelog/internal/integrations/github/GitHubHelper.java:51: warning: [StringSplitter] Prefer Splitter to String.split + for (final String part : link.split(",")) { + ^ + (see http://errorprone.info/bugpattern/StringSplitter) + Did you mean 'for (final String part : Splitter.on(",").split(link)) {'? +/home/bjerre/workspace/git-changelog/git-changelog-lib/src/main/java/se/bjurr/gitchangelog/internal/integrations/github/GitHubHelper.java:52: warning: [StringSplitter] Prefer Splitter to String.split + for (final String piece : part.split(";")) { + ^ + (see http://errorprone.info/bugpattern/StringSplitter) + Did you mean 'for (final String piece : Splitter.on(";").split(part)) {'? +/home/bjerre/workspace/git-changelog/git-changelog-lib/src/main/java/se/bjurr/gitchangelog/api/model/Commit.java:24: warning: [StringSplitter] Prefer Splitter to String.split + for (String part : message.split("\n")) { + ^ + (see http://errorprone.info/bugpattern/StringSplitter) + Did you mean 'for (String part : Splitter.on("\n").split(message)) {'? +/home/bjerre/workspace/git-changelog/git-changelog-lib/src/main/java/se/bjurr/gitchangelog/internal/git/GitRepoData.java:76: warning: [StringSplitter] Prefer Splitter to String.split + .split("[/:\\.]"); + ^ + (see http://errorprone.info/bugpattern/StringSplitter) + Did you mean 'List parts ='? +/home/bjerre/workspace/git-changelog/git-changelog-lib/src/main/java/se/bjurr/gitchangelog/internal/git/TraversalWork.java:73: warning: [BoxedPrimitiveConstructor] valueOf or autoboxing provides better time and space performance + return new Integer(selfCommitTime) // + ^ + (see http://errorprone.info/bugpattern/BoxedPrimitiveConstructor) + Did you mean 'return Integer.compare(selfCommitTime, otherCommitTime);'? +5 warnings + + +BUILD SUCCESSFUL in 5s +1 actionable task: 1 executed diff --git a/src/test/resources/googleErrorProne/googleErrorProneMaven.log b/src/test/resources/googleErrorProne/googleErrorProneMaven.log new file mode 100644 index 00000000..7760a91b --- /dev/null +++ b/src/test/resources/googleErrorProne/googleErrorProneMaven.log @@ -0,0 +1,11 @@ +../examples/maven/error_prone_should_flag$ mvn compile +[INFO] Compiling 1 source file to .../examples/maven/error_prone_should_flag/target/classes +.../examples/maven/error_prone_should_flag/src/main/java/Main.java:20: error: [DeadException] Exception created but not thrown + new Exception(); + ^ + (see http://errorprone.info/bugpattern/DeadException) + Did you mean 'throw new Exception();'? +1 error +[INFO] ------------------------------------------------------------------------ +[INFO] BUILD FAILURE +[INFO] ------------------------------------------------------------------------ \ No newline at end of file