-
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
e1a7ad7
commit 5bfc5d3
Showing
7 changed files
with
207 additions
and
2 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
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
70 changes: 70 additions & 0 deletions
70
src/main/java/se/bjurr/violations/lib/parsers/ResharperParser.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,70 @@ | ||
package se.bjurr.violations.lib.parsers; | ||
|
||
import static com.google.common.collect.Lists.newArrayList; | ||
import static com.google.common.collect.Maps.newHashMap; | ||
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.RESHARPER; | ||
|
||
import java.io.File; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import se.bjurr.violations.lib.model.SEVERITY; | ||
import se.bjurr.violations.lib.model.Violation; | ||
|
||
import com.google.common.base.Charsets; | ||
import com.google.common.io.Files; | ||
|
||
public class ResharperParser extends ViolationsParser { | ||
|
||
@Override | ||
public List<Violation> parseFile(File file) throws Exception { | ||
String string = Files.toString(file, Charsets.UTF_8); | ||
List<Violation> violations = newArrayList(); | ||
List<String> issueTypeChunks = getChunks(string, "<IssueType ", "/>"); | ||
Map<String, Map<String, String>> issueTypesPerTypeId = newHashMap(); | ||
for (String issueTypesChunk : issueTypeChunks) { | ||
Map<String, String> issueType = newHashMap(); | ||
String id = getAttribute(issueTypesChunk, "Id"); | ||
issueType.put("category", getAttribute(issueTypesChunk, "Category")); | ||
issueType.put("description", getAttribute(issueTypesChunk, "Description")); | ||
issueType.put("severity", getAttribute(issueTypesChunk, "Severity")); | ||
issueTypesPerTypeId.put(id, issueType); | ||
} | ||
|
||
List<String> issueChunks = getChunks(string, "<Issue ", "/>"); | ||
for (String issueChunk : issueChunks) { | ||
String typeId = getAttribute(issueChunk, "TypeId"); | ||
String filename = getAttribute(issueChunk, "File"); | ||
String message = getAttribute(issueChunk, "Message") + ". " + issueTypesPerTypeId.get(typeId).get("category") + ". " | ||
+ issueTypesPerTypeId.get(typeId).get("description"); | ||
Integer line = findIntegerAttribute(issueChunk, "Line").or(0); | ||
String severity = issueTypesPerTypeId.get(typeId).get("severity"); | ||
violations.add(// | ||
violationBuilder()// | ||
.setReporter(RESHARPER)// | ||
.setStartLine(line)// | ||
.setFile(filename)// | ||
.setSeverity(toSeverity(severity))// | ||
.setMessage(message)// | ||
.setRule(typeId)// | ||
.build()// | ||
); | ||
} | ||
return violations; | ||
} | ||
|
||
public SEVERITY toSeverity(String severity) { | ||
if (severity.equalsIgnoreCase("ERROR")) { | ||
return ERROR; | ||
} | ||
if (severity.equalsIgnoreCase("WARNING")) { | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
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.RESHARPER; | ||
|
||
import java.util.List; | ||
|
||
import org.junit.Test; | ||
|
||
import se.bjurr.violations.lib.model.Violation; | ||
|
||
public class ResharperTest { | ||
|
||
@Test | ||
public void testThatViolationsCanBeParsed() { | ||
String rootFolder = getRootFolder(); | ||
|
||
List<Violation> actual = violationsReporterApi() // | ||
.withPattern(".*/resharper/.*\\.xml$") // | ||
.inFolder(rootFolder) // | ||
.findAll(RESHARPER) // | ||
.violations(); | ||
|
||
assertThat(actual)// | ||
.hasSize(3); | ||
|
||
assertThat(actual.get(0).getReporter())// | ||
.isEqualTo(RESHARPER); | ||
|
||
assertThat(actual.get(0).getMessage())// | ||
.isEqualTo( | ||
"Using directive is not required by the code and can be safely removed. Redundancies in Code. Redundant using directive"); | ||
assertThat(actual.get(0).getRule().get())// | ||
.isEqualTo("RedundantUsingDirective"); | ||
assertThat(actual.get(0).getFile())// | ||
.isEqualTo("MyLibrary/Class1.cs"); | ||
assertThat(actual.get(0).getSeverity())// | ||
.isEqualTo(WARN); | ||
|
||
assertThat(actual.get(1).getMessage())// | ||
.isEqualTo( | ||
"Join declaration and assignment. Common Practices and Code Improvements. Join local variable declaration and assignment"); | ||
assertThat(actual.get(1).getRule().get())// | ||
.isEqualTo("JoinDeclarationAndInitializer"); | ||
assertThat(actual.get(1).getFile())// | ||
.isEqualTo("MyLibrary/Class1.cs"); | ||
assertThat(actual.get(1).getSeverity())// | ||
.isEqualTo(INFO); | ||
|
||
assertThat(actual.get(2).getMessage())// | ||
.isEqualTo( | ||
"Using directive is not required by the code and can be safely removed. Redundancies in Code. Redundant using directive"); | ||
assertThat(actual.get(2).getRule().get())// | ||
.isEqualTo("RedundantUsingDirective"); | ||
assertThat(actual.get(2).getFile())// | ||
.isEqualTo("MyLibrary/Properties/AssemblyInfo.cs"); | ||
assertThat(actual.get(2).getSeverity())// | ||
.isEqualTo(WARN); | ||
} | ||
} |
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,21 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<!-- Generated by InspectCode 8.1.23.523 --> | ||
<Report ToolsVersion="8.1"> | ||
<Information> | ||
<Solution>CSharpPlayground.sln</Solution> | ||
<InspectionScope> | ||
<Element>9B2650A2-C7C6-435F-80D6-D6C7B522FFF9</Element> | ||
</InspectionScope> | ||
</Information> | ||
<IssueTypes> | ||
<IssueType Id="JoinDeclarationAndInitializer" Category="Common Practices and Code Improvements" Description="Join local variable declaration and assignment" Severity="SUGGESTION" /> | ||
<IssueType Id="RedundantUsingDirective" Category="Redundancies in Code" Description="Redundant using directive" Severity="WARNING" WikiUrl="http://confluence.jetbrains.net/display/ReSharper/Redundant+using+directive" /> | ||
</IssueTypes> | ||
<Issues> | ||
<Project Name="MyLibrary"> | ||
<Issue TypeId="RedundantUsingDirective" File="MyLibrary\Class1.cs" Offset="0-13" Message="Using directive is not required by the code and can be safely removed" /> | ||
<Issue TypeId="JoinDeclarationAndInitializer" File="MyLibrary\Class1.cs" Offset="138-144" Line="9" Message="Join declaration and assignment" /> | ||
<Issue TypeId="RedundantUsingDirective" File="MyLibrary\Properties\AssemblyInfo.cs" Offset="26-64" Line="2" Message="Using directive is not required by the code and can be safely removed" /> | ||
</Project> | ||
</Issues> | ||
</Report> |