-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
30ba7fd
commit 319eaab
Showing
7 changed files
with
165 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
src/main/java/se/bjurr/violations/lib/parsers/CppLintParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package se.bjurr.violations.lib.parsers; | ||
|
||
import static com.google.common.base.Charsets.UTF_8; | ||
import static com.google.common.collect.Lists.newArrayList; | ||
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.reports.Reporter.CPPLINT; | ||
|
||
import java.io.File; | ||
import java.util.List; | ||
|
||
import se.bjurr.violations.lib.model.SEVERITY; | ||
import se.bjurr.violations.lib.model.Violation; | ||
|
||
import com.google.common.io.Files; | ||
|
||
/** | ||
* PyLint. Format used by Flake8. | ||
*/ | ||
public class CppLintParser extends ViolationsParser { | ||
|
||
@Override | ||
public List<Violation> parseFile(File file) throws Exception { | ||
String string = Files.toString(file, UTF_8); | ||
List<Violation> violations = newArrayList(); | ||
List<String> lines = getLines(string); | ||
for (String line : lines) { | ||
List<String> parts = getParts(line, "\\[([^\\]]*)\\]$", "\\[([^\\]]*)\\]$", "^([^:]*):", "^([^:]*):", "(.*)"); | ||
if (parts.isEmpty()) { | ||
continue; // Happens for the line "Done processing cpp/test.cpp" | ||
} | ||
Integer severity = parseInt(parts.get(0)); | ||
String rule = parts.get(1); | ||
String filename = parts.get(2); | ||
Integer lineNumber = parseInt(parts.get(3)); | ||
String message = parts.get(4); | ||
violations.add(// | ||
violationBuilder()// | ||
.setReporter(CPPLINT)// | ||
.setStartLine(lineNumber)// | ||
.setFile(filename)// | ||
.setRule(rule)// | ||
.setSeverity(toSeverity(severity))// | ||
.setMessage(message)// | ||
.build()// | ||
); | ||
} | ||
return violations; | ||
} | ||
|
||
public SEVERITY toSeverity(Integer severity) { | ||
if (severity >= 5) { | ||
return ERROR; | ||
} | ||
if (severity >= 3) { | ||
return WARN; | ||
} | ||
return INFO; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
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.ERROR; | ||
import static se.bjurr.violations.lib.reports.Reporter.CPPLINT; | ||
|
||
import java.util.List; | ||
|
||
import org.junit.Test; | ||
|
||
import se.bjurr.violations.lib.model.Violation; | ||
|
||
public class CppLintTest { | ||
|
||
@Test | ||
public void testThatViolationsCanBeParsed() { | ||
String rootFolder = getRootFolder(); | ||
|
||
List<Violation> actual = violationsReporterApi() // | ||
.withPattern(".*/cpplint/.*\\.txt$") // | ||
.inFolder(rootFolder) // | ||
.findAll(CPPLINT) // | ||
.violations(); | ||
|
||
assertThat(actual)// | ||
.hasSize(3); | ||
|
||
assertThat(actual.get(0).getMessage())// | ||
.isEqualTo("No copyright message found. You should have a line: \"Copyright [year] <Copyright Owner>\""); | ||
assertThat(actual.get(0).getFile())// | ||
.isEqualTo("cpp/test.cpp"); | ||
assertThat(actual.get(0).getSeverity())// | ||
.isEqualTo(ERROR); | ||
assertThat(actual.get(0).getRule().get())// | ||
.isEqualTo("legal/copyright"); | ||
assertThat(actual.get(0).getStartLine())// | ||
.isEqualTo(0); | ||
assertThat(actual.get(0).getEndLine())// | ||
.isEqualTo(0); | ||
|
||
assertThat(actual.get(2).getMessage())// | ||
.isEqualTo("Missing space before ( in while("); | ||
assertThat(actual.get(2).getFile())// | ||
.isEqualTo("cpp/test.cpp"); | ||
assertThat(actual.get(2).getSeverity())// | ||
.isEqualTo(ERROR); | ||
assertThat(actual.get(2).getRule().get())// | ||
.isEqualTo("whitespace/parens"); | ||
assertThat(actual.get(2).getStartLine())// | ||
.isEqualTo(11); | ||
assertThat(actual.get(2).getEndLine())// | ||
.isEqualTo(11); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
cpp/test.cpp:0: No copyright message found. You should have a line: "Copyright [year] <Copyright Owner>" [legal/copyright] [5] | ||
cpp/test.cpp:5: Do not use namespace using-directives. Use using-declarations instead. [build/namespaces] [5] | ||
cpp/test.cpp:11: Missing space before ( in while( [whitespace/parens] [5] | ||
Done processing cpp/test.cpp | ||
Total errors found: 3 |