Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make flake8 regex more precise to avoid false positives #185

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
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 @@
continue;
}
Integer column = null;
if (!isNullOrEmpty(parts.get(3))) {
column = parseInt(parts.get(3));
String severity = null;
String rule = null;
String message = null;
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
Loading