Skip to content

Commit

Permalink
make flake8 regex more precise to avoid false positives
Browse files Browse the repository at this point in the history
  • Loading branch information
henglert committed Feb 25, 2024
1 parent 50dc3b6 commit a88ff1a
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1,878 deletions.
28 changes: 21 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,10 @@ 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*)\\]? (.*)");
/**
* group 1 2 4 5 6 7 8 9 10
* field path line severity code msg column severity code msg */
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 +43,22 @@ public Set<Violation> parseReportOutput(
continue;
}
Integer column = null;
if (!isNullOrEmpty(parts.get(3))) {
column = parseInt(parts.get(3));
String severity = null;

Check warning

Code scanning / Violations Lib

The initializer for variable 'severity' is never used (overwritten on lines 51 and 55) Best Practices https://pmd.github.io/pmd-6.55.0/pmd_rules_java_bestpractices.html#unusedassignment Warning

The initializer for variable 'severity' is never used (overwritten on lines 51 and 55) Best Practices https://pmd.github.io/pmd-6.55.0/pmd\_rules\_java\_bestpractices.html#unusedassignment
String rule = null;

Check warning

Code scanning / Violations Lib

The initializer for variable 'rule' is never used (overwritten on lines 52 and 56) Best Practices https://pmd.github.io/pmd-6.55.0/pmd_rules_java_bestpractices.html#unusedassignment Warning

The initializer for variable 'rule' is never used (overwritten on lines 52 and 56) Best Practices https://pmd.github.io/pmd-6.55.0/pmd\_rules\_java\_bestpractices.html#unusedassignment
String message = null;

Check warning

Code scanning / Violations Lib

The initializer for variable 'message' is never used (overwritten on lines 53 and 57) Best Practices https://pmd.github.io/pmd-6.55.0/pmd_rules_java_bestpractices.html#unusedassignment Warning

The initializer for variable 'message' is never used (overwritten on lines 53 and 57) Best Practices https://pmd.github.io/pmd-6.55.0/pmd\_rules\_java\_bestpractices.html#unusedassignment
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 a88ff1a

Please sign in to comment.