-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
04be590
commit f2720de
Showing
7 changed files
with
212 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
src/main/java/se/bjurr/violations/lib/parsers/GoogleErrorProneParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Violation> parseReportOutput(final String reportContent) throws Exception { | ||
final List<Violation> 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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
src/test/java/se/bjurr/violations/lib/GoogleErrorProneTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Violation> 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<Violation> 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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<String> 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 |
11 changes: 11 additions & 0 deletions
11
src/test/resources/googleErrorProne/googleErrorProneMaven.log
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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] ------------------------------------------------------------------------ |