Skip to content

Commit

Permalink
Supporting StyleCop
Browse files Browse the repository at this point in the history
 * And project level issue in FxCop.
  • Loading branch information
tomasbjerre committed Oct 3, 2016
1 parent 0934794 commit d68d368
Show file tree
Hide file tree
Showing 8 changed files with 258 additions and 47 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +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.
* [_StyleCop_](https://stylecop.codeplex.com/)
* [_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
13 changes: 11 additions & 2 deletions src/main/java/se/bjurr/violations/lib/parsers/FxCopParser.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package se.bjurr.violations.lib.parsers;

import static com.google.common.collect.Lists.newArrayList;
import static java.util.logging.Level.FINE;
import static javax.xml.stream.XMLStreamConstants.START_ELEMENT;
import static se.bjurr.violations.lib.model.SEVERITY.ERROR;
import static se.bjurr.violations.lib.model.SEVERITY.INFO;
Expand All @@ -14,14 +15,18 @@
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.List;
import java.util.logging.Logger;

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

import com.google.common.base.Optional;

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

public class FxCopParser implements ViolationsParser {
private static Logger LOG = Logger.getLogger(FxCopParser.class.getSimpleName());

@Override
public List<Violation> parseFile(File file) throws Exception {
Expand Down Expand Up @@ -49,12 +54,16 @@ public List<Violation> parseFile(File file) throws Exception {
}
if (xmlr.getLocalName().equals("Issue")) {
String level = getAttribute(xmlr, "Level");
String path = getAttribute(xmlr, "Path");
Optional<String> path = ViolationParserUtils.findAttribute(xmlr, "Path");
if (!path.isPresent()) {
LOG.log(FINE, "Ignoring project level issue");
continue;
}
String fileName = getAttribute(xmlr, "File");
Integer line = getIntegerAttribute(xmlr, "Line");
String message = xmlr.getElementText().replaceAll("\\s+", " ");

String filename = path + "/" + fileName;
String filename = path.get() + "/" + fileName;
SEVERITY severity = toSeverity(level);
violations.add(//
violationBuilder()//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public List<Violation> parseFile(File file) throws Exception {
Integer lineNumber = getIntegerAttribute(xmlr, "LineNumber");
String message = xmlr.getElementText().replaceAll("\\s+", " ");
SEVERITY severity = INFO;
String filename = toFile(source);
String filename = source.replaceAll("\\\\", "/");
violations.add(//
violationBuilder()//
.setReporter(STYLECOP)//
Expand All @@ -51,7 +51,7 @@ public List<Violation> parseFile(File file) throws Exception {
.setStartLine(lineNumber)//
.setRule(rule)//
.setSeverity(severity)//
.setSource(source)//
.setSource(filename)//
.setSpecific("section", section)//
.setSpecific("source", source)//
.setSpecific("ruleNamespace", ruleNamespace)//
Expand All @@ -65,8 +65,4 @@ public List<Violation> parseFile(File file) throws Exception {
}
return violations;
}

private String toFile(String source) {
return source.replaceAll("\\.", "/");
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package se.bjurr.violations.lib.parsers;

import static com.google.common.base.Optional.absent;
import static com.google.common.base.Optional.fromNullable;
import static com.google.common.base.Optional.of;
import static com.google.common.base.Throwables.propagate;
import static com.google.common.collect.Lists.newArrayList;
Expand Down Expand Up @@ -44,6 +45,10 @@ public static Optional<String> findAttribute(String in, String attribute) {
return absent();
}

public static Optional<String> findAttribute(XMLStreamReader in, String attribute) {
return fromNullable(in.getAttributeValue("", attribute));
}

public static Optional<Integer> findIntegerAttribute(String in, String attribute) {
if (findAttribute(in, attribute).isPresent()) {
return of(parseInt(getAttribute(in, attribute)));
Expand Down
32 changes: 16 additions & 16 deletions src/test/java/se/bjurr/violations/lib/FxCopTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
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.model.SEVERITY.WARN;
import static se.bjurr.violations.lib.reports.Reporter.FXCOP;

import java.util.List;
Expand All @@ -25,43 +25,43 @@ public void testThatViolationsCanBeParsed() {
.violations();

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

Violation actualViolationZero = actual.get(0);
assertThat(actualViolationZero.getFile())//
.isEqualTo("c:/Hudson/data/jobs/job1/workspace/test/Space/TestBase.cs");
.isEqualTo("C:/git/test-project/Test Solution 1/GenericsSample/Form1.Designer.cs");
assertThat(actualViolationZero.getStartLine())//
.isEqualTo(299);
.isEqualTo(212);
assertThat(actualViolationZero.getMessage())//
.startsWith("Because the behavior o");
.startsWith("Method 'Form");
assertThat(actualViolationZero.getReporter())//
.isEqualTo(FXCOP);
assertThat(actualViolationZero.getRule().orNull())//
.isEqualTo("SpecifyIFormatProvider");
.isEqualTo("Do not pass literals as localized parameters");
assertThat(actualViolationZero.getSeverity())//
.isEqualTo(ERROR);
.isEqualTo(WARN);
assertThat(actualViolationZero.getSource().orNull())//
.isEqualTo("TestBase");
.isEqualTo("Form1");
assertThat(actualViolationZero.getSpecifics().get("TARGET_NAME"))//
.isEqualTo("C:/Hudson/data/jobs/job1/workspace/test/bin/test.dll");
.isEqualTo("C:/git/test-project/Test Solution 1/GenericsSample/bin/Debug/GenericsSample.exe");

Violation actualViolationOne = actual.get(1);
assertThat(actualViolationOne.getFile())//
.isEqualTo("c:/Hudson/data/jobs/job1/workspace/web/UserControls/MyControl.ascx.cs");
.isEqualTo("C:/git/test-project/Test Solution 1/GenericsSample/Form1.Designer.cs");
assertThat(actualViolationOne.getStartLine())//
.isEqualTo(37);
.isEqualTo(203);
assertThat(actualViolationOne.getMessage())//
.startsWith("In member");
.startsWith("Method 'Form");
assertThat(actualViolationOne.getReporter())//
.isEqualTo(FXCOP);
assertThat(actualViolationOne.getRule().orNull())//
.isEqualTo("CompoundWordsShouldBeCasedCorrectly");
.isEqualTo("Do not pass literals as localized parameters");
assertThat(actualViolationOne.getSeverity())//
.isEqualTo(ERROR);
.isEqualTo(WARN);
assertThat(actualViolationOne.getSource().orNull())//
.isEqualTo("MyControl");
.isEqualTo("Form1");
assertThat(actualViolationOne.getSpecifics().get("TARGET_NAME"))//
.isEqualTo("C:/Hudson/data/jobs/job1/workspace/web/bin/web.dll");
.isEqualTo("C:/git/test-project/Test Solution 1/GenericsSample/bin/Debug/GenericsSample.exe");

}
}
22 changes: 11 additions & 11 deletions src/test/java/se/bjurr/violations/lib/StyleCopTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,39 +25,39 @@ public void testThatViolationsCanBeParsed() {
.violations();

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

Violation actualViolationZero = actual.get(0);
assertThat(actualViolationZero.getFile())//
.isEqualTo("Form1/Designer/cs");
.isEqualTo("E:/Jenkins/jobs/Tools Development/workspace/Tools/Tools.SSOTester/Form1.cs");
assertThat(actualViolationZero.getStartLine())//
.isEqualTo(18);
.isEqualTo(17);
assertThat(actualViolationZero.getMessage())//
.startsWith("The call to");
.startsWith("The do");
assertThat(actualViolationZero.getReporter())//
.isEqualTo(STYLECOP);
assertThat(actualViolationZero.getRule().orNull())//
.isEqualTo("PrefixLocalCallsWithThis");
.isEqualTo("ElementDocumentationMustNotHaveDefaultSummary");
assertThat(actualViolationZero.getSeverity())//
.isEqualTo(INFO);
assertThat(actualViolationZero.getSource().orNull())//
.isEqualTo("Form1.Designer.cs");
.isEqualTo("E:/Jenkins/jobs/Tools Development/workspace/Tools/Tools.SSOTester/Form1.cs");

Violation actualViolationOne = actual.get(1);
assertThat(actualViolationOne.getFile())//
.isEqualTo("Form1/Designer/cs");
.isEqualTo("E:/Jenkins/jobs/Tools Development/workspace/Tools/Tools.SSOTester/Form1.cs");
assertThat(actualViolationOne.getStartLine())//
.isEqualTo(16);
.isEqualTo(19);
assertThat(actualViolationOne.getMessage())//
.startsWith("The call to");
.startsWith("The field");
assertThat(actualViolationOne.getReporter())//
.isEqualTo(STYLECOP);
assertThat(actualViolationOne.getRule().orNull())//
.isEqualTo("PrefixLocalCallsWithThis");
.isEqualTo("ElementsMustBeDocumented");
assertThat(actualViolationOne.getSeverity())//
.isEqualTo(INFO);
assertThat(actualViolationOne.getSource().orNull())//
.isEqualTo("Form1.Designer.cs");
.isEqualTo("E:/Jenkins/jobs/Tools Development/workspace/Tools/Tools.SSOTester/Form1.cs");

}
}
Loading

0 comments on commit d68d368

Please sign in to comment.