Skip to content

Commit

Permalink
Add support for package and module in Pylint
Browse files Browse the repository at this point in the history
  • Loading branch information
JeanChristopheMorinPerso committed Mar 2, 2019
1 parent 88803fe commit 1845834
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
10 changes: 9 additions & 1 deletion src/main/java/edu/hm/hafner/analysis/parser/PyLintParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class PyLintParser extends RegexpLineParser {
private static final long serialVersionUID = 4464053085862883240L;

// the default pattern matches "--output-format=parseable" output.
private static final String PYLINT_PATTERN = "(?<path>.*):(?<line>\\d+): \\[(?<category>\\D\\d*)(?:\\((?<symbol>.*)\\), )?.*?\\] (?<message>.*)";
private static final String PYLINT_PATTERN = "(?<path>[^:]*)(?:\\:(?<module>.*))?:(?<line>\\d+): \\[(?<category>\\D\\d*)(?:\\((?<symbol>.*)\\), )?.*?\\] (?<message>.*)";

private static final String UNKNOWN_CAT = "pylint-unknown";

Expand All @@ -41,6 +41,14 @@ protected Optional<Issue> createIssue(final Matcher matcher, final IssueBuilder
builder.setSeverity(mapPriority(category));
builder.setCategory(StringUtils.firstNonBlank(matcher.group("symbol"), category, UNKNOWN_CAT));

final String moduleName = matcher.group("module");
if (moduleName != null) {
if (moduleName.contains(".")) {
builder.setPackageName(moduleName.substring(0, moduleName.lastIndexOf(".")));
}
builder.setModuleName(moduleName);
}

return builder.setFileName(matcher.group("path"))
.setLineStart(matcher.group("line"))
.setMessage(matcher.group("message"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ protected void assertThatIssuesArePresent(final Report report, final SoftAsserti
void shouldParseReportWithoutSymbol() {
Report report = parse("pyLint.txt");

assertThat(report).hasSize(6);
assertThat(report).hasSize(8);

Iterator<Issue> iterator = report.iterator();
SoftAssertions.assertSoftly(softly -> {
Expand Down Expand Up @@ -219,6 +219,24 @@ void shouldParseReportWithoutSymbol() {
.hasFileName("trunk/src/python/tv.py")
.hasCategory("W0102")
.hasSeverity(Severity.WARNING_NORMAL);
softly.assertThat(iterator.next())
.hasLineStart(1)
.hasLineEnd(1)
.hasMessage("Unused import os (unused-import)")
.hasFileName("trunk/src/python_package/module_name.py")
.hasCategory("W0611")
.hasSeverity(Severity.WARNING_NORMAL)
.hasModuleName("python_package.module_name")
.hasPackageName("python_package");
softly.assertThat(iterator.next())
.hasLineStart(1)
.hasLineEnd(1)
.hasMessage("Unused import os (unused-import)")
.hasFileName("trunk/src/module_name_no_package.py")
.hasCategory("W0611")
.hasSeverity(Severity.WARNING_NORMAL)
.hasModuleName("module_name_no_package")
.hasPackageName(null);
});
}

Expand Down
2 changes: 2 additions & 0 deletions src/test/resources/edu/hm/hafner/analysis/parser/pyLint.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ trunk/src/python/tv.py:35: [C0111, Episode] Missing docstring
trunk/src/python/tv.py:39: [E0213, Episode.__init__] Method should have "self" as first argument
trunk/src/python/tv.py:5: [F0401, ] Unable to import 'deadbeef'
trunk/src/python/tv.py:39: [W0102, Episode.__init__] Dangerous default value "[]" as argument
trunk/src/python_package/module_name.py:python_package.module_name:1: [W0611, ] Unused import os (unused-import)
trunk/src/module_name_no_package.py:module_name_no_package:1: [W0611, ] Unused import os (unused-import)

0 comments on commit 1845834

Please sign in to comment.