Skip to content

Commit

Permalink
Preliminary support for StyleCop
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasbjerre committed Oct 1, 2016
1 parent ac5a58c commit 81e3032
Show file tree
Hide file tree
Showing 6 changed files with 176 additions and 1 deletion.
27 changes: 26 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,36 @@ Changelog of Git Changelog.
## Unreleased
### No issue

**Preliminary support for StyleCop**


[82b0da58252b97e](https://github.com/tomasbjerre/git-changelog-lib/commit/82b0da58252b97e) Tomas Bjerre *2016-10-01 10:22:55*

**Support for FxCop**


[ac5a58c002f8c97](https://github.com/tomasbjerre/git-changelog-lib/commit/ac5a58c002f8c97) Tomas Bjerre *2016-10-01 09:55:19*

**Refactoring, adding ViolationsParser interface**

* Also preparing for FxCop.

[adef3f6ed9c6cfc](https://github.com/tomasbjerre/git-changelog-lib/commit/adef3f6ed9c6cfc) Tomas Bjerre *2016-10-01 08:38:19*

**doc**


[4f54260b7b47e96](https://github.com/tomasbjerre/git-changelog-lib/commit/4f54260b7b47e96) Tomas Bjerre *2016-07-28 20:22:22*


## 1.8
### No issue

**Updating README.md**

* And formatting code after merge of PR.

[a8bef0a533ffc34](https://github.com/tomasbjerre/git-changelog-lib/commit/a8bef0a533ffc34) Tomas Bjerre *2016-04-27 05:33:29*
[34cc693dfcc7bad](https://github.com/tomasbjerre/git-changelog-lib/commit/34cc693dfcc7bad) Tomas Bjerre *2016-04-27 05:34:51*

**Add Android Lint parser**

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ It supports:
* [_PiTest_](http://pitest.org/)
* [_PMD_](https://pmd.github.io/)
* [_ReSharper_](https://www.jetbrains.com/resharper/)
* [_StyleCop_](https://stylecop.codeplex.com/) Not fully supported. Cannot figure out how to get the filename from the reportfile.
* [_XMLLint_](http://xmlsoft.org/xmllint.html)

Example reports are available [in the test resources](https://github.com/tomasbjerre/violations-lib/tree/master/src/test/resources), examples of how to generate them are available [here](https://github.com/tomasbjerre/violations-test/blob/master/build.sh).
Expand Down
72 changes: 72 additions & 0 deletions src/main/java/se/bjurr/violations/lib/parsers/StyleCopParser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package se.bjurr.violations.lib.parsers;

import static com.google.common.collect.Lists.newArrayList;
import static javax.xml.stream.XMLStreamConstants.START_ELEMENT;
import static se.bjurr.violations.lib.model.SEVERITY.INFO;
import static se.bjurr.violations.lib.model.Violation.violationBuilder;
import static se.bjurr.violations.lib.parsers.ViolationParserUtils.getAttribute;
import static se.bjurr.violations.lib.parsers.ViolationParserUtils.getIntegerAttribute;
import static se.bjurr.violations.lib.reports.Reporter.STYLECOP;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.List;

import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamReader;

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

public class StyleCopParser implements ViolationsParser {

@Override
public List<Violation> parseFile(File file) throws Exception {
List<Violation> violations = newArrayList();

try (InputStream input = new FileInputStream(file)) {

XMLInputFactory factory = XMLInputFactory.newInstance();
XMLStreamReader xmlr = factory.createXMLStreamReader(input);

while (xmlr.hasNext()) {
int eventType = xmlr.next();
if (eventType == START_ELEMENT) {
if (xmlr.getLocalName().equals("Violation")) {
String section = getAttribute(xmlr, "Section");
String source = getAttribute(xmlr, "Source");
String ruleNamespace = getAttribute(xmlr, "RuleNamespace");
String rule = getAttribute(xmlr, "Rule");
String ruleId = getAttribute(xmlr, "RuleId");
Integer lineNumber = getIntegerAttribute(xmlr, "LineNumber");
String message = xmlr.getElementText().replaceAll("\\s+", " ");
SEVERITY severity = INFO;
String filename = toFile(source);
violations.add(//
violationBuilder()//
.setReporter(STYLECOP)//
.setMessage(message)//
.setFile(filename)//
.setStartLine(lineNumber)//
.setRule(rule)//
.setSeverity(severity)//
.setSource(source)//
.setSpecific("section", section)//
.setSpecific("source", source)//
.setSpecific("ruleNamespace", ruleNamespace)//
.setSpecific("rule", rule)//
.setSpecific("ruleId", ruleId)//
.build()//
);
}
}
}
}
return violations;
}

private String toFile(String source) {
return source.replaceAll("\\.", "/");
}
}
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 @@ -22,6 +22,7 @@
import se.bjurr.violations.lib.parsers.PerlCriticParser;
import se.bjurr.violations.lib.parsers.PiTestParser;
import se.bjurr.violations.lib.parsers.ResharperParser;
import se.bjurr.violations.lib.parsers.StyleCopParser;
import se.bjurr.violations.lib.parsers.ViolationsParser;
import se.bjurr.violations.lib.parsers.XMLLintParser;

Expand All @@ -40,6 +41,7 @@ public enum Reporter {
PITEST(new PiTestParser()), //
PMD(new PMDParser()), //
RESHARPER(new ResharperParser()), //
STYLECOP(new StyleCopParser()), //
XMLLINT(new XMLLintParser());

private static Logger LOG = Logger.getLogger(Reporter.class.getSimpleName());
Expand Down
63 changes: 63 additions & 0 deletions src/test/java/se/bjurr/violations/lib/StyleCopTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
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.reports.Reporter.STYLECOP;

import java.util.List;

import org.junit.Test;

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

public class StyleCopTest {

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

List<Violation> actual = violationsReporterApi() //
.withPattern(".*/stylecop/.*\\.xml$") //
.inFolder(rootFolder) //
.findAll(STYLECOP) //
.violations();

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

Violation actualViolationZero = actual.get(0);
assertThat(actualViolationZero.getFile())//
.isEqualTo("Form1/Designer/cs");
assertThat(actualViolationZero.getStartLine())//
.isEqualTo(18);
assertThat(actualViolationZero.getMessage())//
.startsWith("The call to");
assertThat(actualViolationZero.getReporter())//
.isEqualTo(STYLECOP);
assertThat(actualViolationZero.getRule().orNull())//
.isEqualTo("PrefixLocalCallsWithThis");
assertThat(actualViolationZero.getSeverity())//
.isEqualTo(INFO);
assertThat(actualViolationZero.getSource().orNull())//
.isEqualTo("Form1.Designer.cs");

Violation actualViolationOne = actual.get(1);
assertThat(actualViolationOne.getFile())//
.isEqualTo("Form1/Designer/cs");
assertThat(actualViolationOne.getStartLine())//
.isEqualTo(16);
assertThat(actualViolationOne.getMessage())//
.startsWith("The call to");
assertThat(actualViolationOne.getReporter())//
.isEqualTo(STYLECOP);
assertThat(actualViolationOne.getRule().orNull())//
.isEqualTo("PrefixLocalCallsWithThis");
assertThat(actualViolationOne.getSeverity())//
.isEqualTo(INFO);
assertThat(actualViolationOne.getSource().orNull())//
.isEqualTo("Form1.Designer.cs");

}
}
12 changes: 12 additions & 0 deletions src/test/resources/stylecop/stylecop.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<StyleCopViolations>
<Violation Section="Root.ListViewProblem.Form1.Dispose%bool"
LineNumber="18" Source="Form1.Designer.cs" RuleNamespace="Microsoft.StyleCop.CSharp.ReadabilityRules"
Rule="PrefixLocalCallsWithThis" RuleId="SA1101">The call to components must
begin with the 'this.' prefix to indicate that the item is a member of
the class.</Violation>
<Violation Section="Root.ListViewProblem.Form1.Dispose%bool"
LineNumber="16" Source="Form1.Designer.cs" RuleNamespace="Microsoft.StyleCop.CSharp.ReadabilityRules"
Rule="PrefixLocalCallsWithThis" RuleId="SA1101">The call to components must
begin with the 'this.' prefix to indicate that the item is a member of
the class.</Violation>
</StyleCopViolations>

0 comments on commit 81e3032

Please sign in to comment.