Skip to content

Commit

Permalink
add checkstyle reporter. addresses #277
Browse files Browse the repository at this point in the history
  • Loading branch information
jpsim committed Dec 25, 2015
1 parent 9840ed1 commit e546ecb
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 3 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@

##### Enhancements

* None.
* Add `checkstyle` reporter to generate XML reports in the Checkstyle 4.3
format.
[JP Simard](https://github.com/jpsim)
[#277](https://github.com/realm/SwiftLint/issues/277)

##### Bug Fixes

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ variable_name_max_length:
variable_name_min_length:
- 3 # warning
- 2 # error
reporter: "csv" # reporter type (xcode, json, csv)
reporter: "csv" # reporter type (xcode, json, csv, checkstyle)
```
### Auto-correct
Expand Down
4 changes: 3 additions & 1 deletion Source/SwiftLintFramework/Models/Configuration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public struct Configuration {
public let disabledRules: [String] // disabled_rules
public let included: [String] // included
public let excluded: [String] // excluded
public let reporter: String // reporter (xcode, json, csv)
public let reporter: String // reporter (xcode, json, csv, checkstyle)
public let rules: [Rule]

public var reporterFromString: Reporter.Type {
Expand All @@ -36,6 +36,8 @@ public struct Configuration {
return JSONReporter.self
case CSVReporter.identifier:
return CSVReporter.self
case CheckstyleReporter.identifier:
return CheckstyleReporter.self
default:
fatalError("no reporter with identifier '\(reporter)' available.")
}
Expand Down
33 changes: 33 additions & 0 deletions Source/SwiftLintFramework/Reporters/CheckstyleReporter.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//
// CheckstyleReporter.swift
// SwiftLint
//
// Created by JP Simard on 12/25/15.
// Copyright © 2015 Realm. All rights reserved.
//

import Foundation

public struct CheckstyleReporter: Reporter {
public static let identifier = "checkstyle"
public static let isRealtime = false

public var description: String {
return "Reports violations as Checkstyle XML."
}

public static func generateReport(violations: [StyleViolation]) -> String {
var report = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<checkstyle version=\"4.3\">"
report += violations.map { violation in
let fileName = violation.location.file ?? "<nopath>"
return "\n\t<file name=\"\(fileName)\">\n" +
"\t\t<error line=\"\(violation.location.line ?? 0)\" " +
"column=\"\(violation.location.character ?? 0)\" " +
"severity=\"\(violation.severity.rawValue.lowercaseString)\" " +
"message=\"\(violation.reason)\"/>\n" +
"\t</file>"
}.joinWithSeparator("")
report += "\n</checkstyle>"
return report
}
}
12 changes: 12 additions & 0 deletions Source/SwiftLintFrameworkTests/ReporterTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,16 @@ class ReporterTests: XCTestCase {
"filename,1,2,Error,Line Length,Violation Reason.,line_length"
)
}

func testCheckstyleReporter() {
XCTAssertEqual(
CheckstyleReporter.generateReport(generateViolations()),
"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<checkstyle version=\"4.3\">\n" +
"\t<file name=\"filename\">\n\t\t<error line=\"1\" column=\"2\" severity=\"warning\" " +
"message=\"Violation Reason.\"/>\n\t</file>\n" +
"\t<file name=\"filename\">\n\t\t<error line=\"1\" column=\"2\" severity=\"error\" " +
"message=\"Violation Reason.\"/>\n\t</file>\n" +
"</checkstyle>"
)
}
}
4 changes: 4 additions & 0 deletions SwiftLint.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@
E8C0DFCD1AD349DB007EE3D4 /* SWXMLHash.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = E8C0DFCC1AD349DB007EE3D4 /* SWXMLHash.framework */; };
E8C0DFCE1AD349E5007EE3D4 /* SWXMLHash.framework in Copy Frameworks */ = {isa = PBXBuildFile; fileRef = E8C0DFCC1AD349DB007EE3D4 /* SWXMLHash.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
E8C164C21BEA9EDA00177258 /* ASTRuleTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = E8BB8F991B17DDB200199606 /* ASTRuleTests.swift */; };
E8EA41171C2D1DBE004F9930 /* CheckstyleReporter.swift in Sources */ = {isa = PBXBuildFile; fileRef = E8EA41161C2D1DBE004F9930 /* CheckstyleReporter.swift */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -235,6 +236,7 @@
E8BB8F991B17DDB200199606 /* ASTRuleTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ASTRuleTests.swift; sourceTree = "<group>"; };
E8BB8F9B1B17DE3B00199606 /* StringRuleTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = StringRuleTests.swift; sourceTree = "<group>"; };
E8C0DFCC1AD349DB007EE3D4 /* SWXMLHash.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; path = SWXMLHash.framework; sourceTree = BUILT_PRODUCTS_DIR; };
E8EA41161C2D1DBE004F9930 /* CheckstyleReporter.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CheckstyleReporter.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -450,6 +452,7 @@
E86396C31BADAC0D002C9E88 /* Reporters */ = {
isa = PBXGroup;
children = (
E8EA41161C2D1DBE004F9930 /* CheckstyleReporter.swift */,
E86396CA1BADB519002C9E88 /* CSVReporter.swift */,
E86396C81BADB2B9002C9E88 /* JSONReporter.swift */,
E86396C41BADAC15002C9E88 /* XcodeReporter.swift */,
Expand Down Expand Up @@ -725,6 +728,7 @@
E88198531BEA944400333A11 /* LineLengthRule.swift in Sources */,
E88198441BEA93D200333A11 /* ColonRule.swift in Sources */,
E809EDA11B8A71DF00399043 /* Configuration.swift in Sources */,
E8EA41171C2D1DBE004F9930 /* CheckstyleReporter.swift in Sources */,
E88DEA731B0984C400A66CB0 /* String+SwiftLint.swift in Sources */,
E88198591BEA95F100333A11 /* LeadingWhitespaceRule.swift in Sources */,
24E17F721B14BB3F008195BE /* File+Cache.swift in Sources */,
Expand Down

0 comments on commit e546ecb

Please sign in to comment.