Skip to content

Commit

Permalink
fix: make flake8 regex more precise to avoid false positives (#185)
Browse files Browse the repository at this point in the history
* make flake8 regex more precise to avoid false positives

* fixed spotless and initializers

---------

Co-authored-by: Holger Englert <[email protected]>
  • Loading branch information
henglert and henglert authored Mar 2, 2024
1 parent 50dc3b6 commit 50ffc15
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1,878 deletions.
27 changes: 20 additions & 7 deletions src/main/java/se/bjurr/violations/lib/parsers/Flake8Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
/**
* PyLint. Format used by Flake8.<br>
* <code>
* msg-template='{path}:{line}:{column} [{msg_id}] {msg}'
* msg-template(pylint)='{path1}:{line2}: [{severity4}{code5}] {msg6}'
* msg-template(default)='{path1}:{line2}:{column7}: {severity8}{code9} {msg10}'
* </code>
*/
public class Flake8Parser implements ViolationsParser {
Expand All @@ -29,7 +30,9 @@ public Set<Violation> parseReportOutput(
final String string, final ViolationsLogger violationsLogger) throws Exception {
final Set<Violation> violations = new TreeSet<>();
final List<List<String>> partsPerLine =
getLines(string, "([^:]*):(\\d+)?:?(\\d+)?:? \\[?(\\D+)(\\d*)\\]? (.*)");
getLines(
string,
"([^:]+):(\\d+):( \\[([A-Z]{1,8})([0-9]{0,6})\\] (.+)|(\\d+): ([A-Z]{1,8})([0-9]{0,6}) (.+))");
for (final List<String> parts : partsPerLine) {
final String filename = parts.get(1);
Integer line;
Expand All @@ -39,12 +42,22 @@ public Set<Violation> parseReportOutput(
continue;
}
Integer column = null;
if (!isNullOrEmpty(parts.get(3))) {
column = parseInt(parts.get(3));
String severity;
String rule;
String message;
if (!isNullOrEmpty(parts.get(7))) {
column = parseInt(parts.get(7));
severity = parts.get(8);
rule = parts.get(9);
message = parts.get(10);
} else {
severity = parts.get(4);
rule = parts.get(5);
message = parts.get(6);
}
if (isNullOrEmpty(message)) {
continue;
}
final String severity = parts.get(4);
final String rule = parts.get(5);
final String message = parts.get(6);
violations.add( //
violationBuilder()
.setParser(FLAKE8)
Expand Down
6 changes: 3 additions & 3 deletions src/test/java/se/bjurr/violations/lib/Flake8Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -171,11 +171,11 @@ public void testThatViolationsCanBeParsedFromFileContainingNoise() {
.violations();

assertThat(actual) //
.hasSize(6);
.hasSize(1);

final Violation violation0 = new ArrayList<>(actual).get(0);
assertThat(violation0.getMessage()) //
.startsWith("HOME=/var/jenkins_home/workspace/");
.startsWith("undefined name 'FALSE'");
}

@Test
Expand All @@ -190,6 +190,6 @@ public void testFailedReportFlake8() {
.violations();

assertThat(actual) //
.hasSize(0);
.hasSize(6);
}
}
Loading

0 comments on commit 50ffc15

Please sign in to comment.