Skip to content

Commit

Permalink
Support GoLint #17
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasbjerre committed Feb 18, 2017
1 parent f721255 commit c404a4a
Show file tree
Hide file tree
Showing 7 changed files with 141 additions and 3 deletions.
10 changes: 9 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,15 @@ Changelog of Git Changelog.
**PHPMD and PHPCS**


[7a14ff2c22834d4](https://github.com/tomasbjerre/violations-lib/commit/7a14ff2c22834d4) Tomas Bjerre *2017-02-18 19:57:29*
[f7212555f7a986b](https://github.com/tomasbjerre/violations-lib/commit/f7212555f7a986b) Tomas Bjerre *2017-02-18 20:01:36*


### GitHub [#17](https://github.com/tomasbjerre/violations-lib/issues/17) Support golint

**Support GoLint**


[37123c1e2e4d37f](https://github.com/tomasbjerre/violations-lib/commit/37123c1e2e4d37f) Tomas Bjerre *2017-02-18 20:47:01*


## 1.22
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ It supports:
* [_PyFlakes_](https://pypi.python.org/pypi/pyflakes)
* [_FxCop_](https://en.wikipedia.org/wiki/FxCop)
* [_Gendarme_](http://www.mono-project.com/docs/tools+libraries/tools/gendarme/)
* [_GoLint_](https://github.com/golang/lint)
* [_JSHint_](http://jshint.com/)
* _Lint_ A common XML format, used by different linters.
* [_JCReport_](https://github.com/jCoderZ/fawkez/wiki/JcReport)
Expand Down
62 changes: 62 additions & 0 deletions src/main/java/se/bjurr/violations/lib/parsers/GoLintParser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package se.bjurr.violations.lib.parsers;

import static java.lang.Integer.parseInt;
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.parsers.ViolationParserUtils.getLines;
import static se.bjurr.violations.lib.reports.Reporter.GOLINT;
import static se.bjurr.violations.lib.util.Utils.isNullOrEmpty;

import java.util.ArrayList;
import java.util.List;

import se.bjurr.violations.lib.model.SEVERITY;
import se.bjurr.violations.lib.model.Violation;

public class GoLintParser implements ViolationsParser {

@Override
public List<Violation> parseReportOutput(String reportContent) throws Exception {
List<Violation> violations = new ArrayList<>();
List<List<String>> partsPerLine = getLines(reportContent, "^([^:]+?):(\\d*):?(\\d*?):?([^:]*?)?:? (.*)$");
for (List<String> parts : partsPerLine) {
String fileName = parts.get(1);
Integer lineNumber = 0;
if (!parts.get(2).isEmpty()) {
lineNumber = parseInt(parts.get(2));
}
Integer columnNumber = 0;
if (!parts.get(3).isEmpty()) {
columnNumber = parseInt(parts.get(3));
}
String severity = parts.get(4);
String message = parts.get(5);
violations.add(//
violationBuilder()//
.setReporter(GOLINT)//
.setStartLine(lineNumber)//
.setColumn(columnNumber)//
.setFile(fileName)//
.setSeverity(toSeverity(severity))//
.setMessage(message)//
.build()//
);
}
return violations;
}

public SEVERITY toSeverity(String severity) {
if (isNullOrEmpty(severity)) {
return INFO;
}
if (severity.equalsIgnoreCase("error")) {
return ERROR;
}
if (severity.equalsIgnoreCase("warning")) {
return WARN;
}
return INFO;
}
}
2 changes: 2 additions & 0 deletions src/main/java/se/bjurr/violations/lib/reports/Reporter.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import se.bjurr.violations.lib.parsers.Flake8Parser;
import se.bjurr.violations.lib.parsers.FxCopParser;
import se.bjurr.violations.lib.parsers.GendarmeParser;
import se.bjurr.violations.lib.parsers.GoLintParser;
import se.bjurr.violations.lib.parsers.JCReportParser;
import se.bjurr.violations.lib.parsers.JSHintParser;
import se.bjurr.violations.lib.parsers.LintParser;
Expand Down Expand Up @@ -53,6 +54,7 @@ public enum Reporter {
JSHINT(new JSHintParser()), //
LINT(new LintParser()), //
MYPY(new MyPyParser()), //
GOLINT(new GoLintParser()), //
PERLCRITIC(new PerlCriticParser()), //
PITEST(new PiTestParser()), //
PMD(new PMDParser()), //
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/se/bjurr/violations/lib/util/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ public static InputStream getResource(String filename) {
return Thread.currentThread().getContextClassLoader().getSystemResourceAsStream(filename);
}

public static boolean isNullOrEmpty(String findbugsMessagesXml) {
return findbugsMessagesXml == null || findbugsMessagesXml.isEmpty();
public static boolean isNullOrEmpty(String str) {
return str == null || str.isEmpty();
}

@SuppressWarnings("resource")
Expand Down
58 changes: 58 additions & 0 deletions src/test/java/se/bjurr/violations/lib/GoLintTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
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.ViolationsReporterApi.violationsReporterApi;
import static se.bjurr.violations.lib.model.SEVERITY.INFO;
import static se.bjurr.violations.lib.model.SEVERITY.WARN;
import static se.bjurr.violations.lib.reports.Reporter.GOLINT;

import java.util.List;

import org.junit.Test;

import se.bjurr.violations.lib.model.Violation;

public class GoLintTest {

@Test
public void testThatGoLintViolationsCanBeParsed() {
String rootFolder = getRootFolder();

List<Violation> actual = violationsReporterApi() //
.withPattern(".*/golint/golint\\.txt$") //
.inFolder(rootFolder) //
.findAll(GOLINT) //
.violations();

assertThat(actual)//
.hasSize(7);

assertThat(actual.get(0).getMessage())//
.isEqualTo(
"comment on exported type RestDataSource should be of the form \"RestDataSource ...\" (with optional leading article)");
assertThat(actual.get(0).getFile())//
.isEqualTo("src/bla/bla/bla/dataSource.go");
assertThat(actual.get(0).getSeverity())//
.isEqualTo(INFO);
assertThat(actual.get(0).getRule().orNull())//
.isEqualTo(null);
assertThat(actual.get(0).getStartLine())//
.isEqualTo(28);
assertThat(actual.get(0).getEndLine())//
.isEqualTo(28);

assertThat(actual.get(2).getMessage())//
.isEqualTo("declaration of err shadows declaration at journalevent.go:165: (vet shadow) ");
assertThat(actual.get(2).getFile())//
.isEqualTo("journalevent.go");
assertThat(actual.get(2).getSeverity())//
.isEqualTo(WARN);
assertThat(actual.get(2).getRule().orNull())//
.isEqualTo(null);
assertThat(actual.get(2).getStartLine())//
.isEqualTo(182);
assertThat(actual.get(2).getEndLine())//
.isEqualTo(182);
}
}
7 changes: 7 additions & 0 deletions src/test/resources/golint/golint.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
src/bla/bla/bla/dataSource.go:28:1: comment on exported type RestDataSource should be of the form "RestDataSource ..." (with optional leading article)
journalevent.go:308:1:warning: exported function NewSendJournalEventJob should have comment or be unexported (golint)
journalevent.go:182::warning: declaration of err shadows declaration at journalevent.go:165: (vet shadow)
journalevent.go:::: testing1
journalevent.go::: testing2
journalevent.go:: testing3
journalevent.go: testing4

0 comments on commit c404a4a

Please sign in to comment.