-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #133 from realm/jp-custom-reporters
added custom reporters: xcode (default), json, csv. fixes #42
- Loading branch information
Showing
12 changed files
with
246 additions
and
11 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
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,12 @@ | ||
// | ||
// Reporter.swift | ||
// SwiftLint | ||
// | ||
// Created by JP Simard on 9/19/15. | ||
// Copyright © 2015 Realm. All rights reserved. | ||
// | ||
|
||
public protocol Reporter: CustomStringConvertible { | ||
static var identifier: String { get } | ||
static func generateReport(violations: [StyleViolation]) -> String | ||
} |
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,41 @@ | ||
// | ||
// CSVReporter.swift | ||
// SwiftLint | ||
// | ||
// Created by JP Simard on 9/19/15. | ||
// Copyright © 2015 Realm. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
public struct CSVReporter: Reporter { | ||
public static let identifier = "csv" | ||
|
||
public var description: String { | ||
return "Reports violations as a newline-separated string of comma-separated values (CSV)." | ||
} | ||
|
||
public static func generateReport(violations: [StyleViolation]) -> String { | ||
let keys = [ | ||
"file", | ||
"line", | ||
"character", | ||
"severity", | ||
"type", | ||
"reason" | ||
] | ||
return (keys + violations.flatMap(arrayForViolation)).joinWithSeparator(",") | ||
} | ||
|
||
private static func arrayForViolation(violation: StyleViolation) -> [String] { | ||
let values: [AnyObject?] = [ | ||
violation.location.file, | ||
violation.location.line, | ||
violation.location.character, | ||
violation.severity.rawValue, | ||
violation.type.description, | ||
violation.reason | ||
] | ||
return values.map({ $0?.description ?? "" }) | ||
} | ||
} |
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,33 @@ | ||
// | ||
// JSONReporter.swift | ||
// SwiftLint | ||
// | ||
// Created by JP Simard on 9/19/15. | ||
// Copyright © 2015 Realm. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
import SourceKittenFramework | ||
|
||
public struct JSONReporter: Reporter { | ||
public static let identifier = "json" | ||
|
||
public var description: String { | ||
return "Reports violations as a JSON array." | ||
} | ||
|
||
public static func generateReport(violations: [StyleViolation]) -> String { | ||
return toJSON(violations.map(dictionaryForViolation)) | ||
} | ||
|
||
private static func dictionaryForViolation(violation: StyleViolation) -> NSDictionary { | ||
return [ | ||
"file": violation.location.file ?? NSNull(), | ||
"line": violation.location.line ?? NSNull(), | ||
"character": violation.location.character ?? NSNull(), | ||
"severity": violation.severity.rawValue, | ||
"type": violation.type.description, | ||
"reason": violation.reason ?? NSNull() | ||
] | ||
} | ||
} |
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,27 @@ | ||
// | ||
// XcodeReporter.swift | ||
// SwiftLint | ||
// | ||
// Created by JP Simard on 9/19/15. | ||
// Copyright © 2015 Realm. All rights reserved. | ||
// | ||
|
||
public struct XcodeReporter: Reporter { | ||
public static let identifier = "xcode" | ||
|
||
public var description: String { | ||
return "Reports violations in the format Xcode uses to display in the IDE. (default)" | ||
} | ||
|
||
public static func generateReport(violations: [StyleViolation]) -> String { | ||
return violations.map(generateForSingleViolation).joinWithSeparator("\n") | ||
} | ||
|
||
internal static func generateForSingleViolation(violation: StyleViolation) -> String { | ||
// {full_path_to_file}{:line}{:character}: {error,warning}: {content} | ||
return "\(violation.location): " + | ||
"\(violation.severity.rawValue.lowercaseString): " + | ||
"\(violation.type) Violation: " + | ||
(violation.reason ?? "") | ||
} | ||
} |
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,66 @@ | ||
// | ||
// ReporterTests.swift | ||
// SwiftLint | ||
// | ||
// Created by JP Simard on 9/19/15. | ||
// Copyright © 2015 Realm. All rights reserved. | ||
// | ||
|
||
import SwiftLintFramework | ||
import XCTest | ||
|
||
class ReporterTests: XCTestCase { | ||
func generateViolations() -> [StyleViolation] { | ||
return [ | ||
StyleViolation(type: .Length, | ||
location: Location(file: "filename", line: 1, character: 2), | ||
severity: .Warning, | ||
reason: "Violation Reason."), | ||
StyleViolation(type: .Length, | ||
location: Location(file: "filename", line: 1, character: 2), | ||
severity: .Error, | ||
reason: "Violation Reason.") | ||
] | ||
} | ||
|
||
func testXcodeReporter() { | ||
XCTAssertEqual( | ||
XcodeReporter.generateReport(generateViolations()), | ||
"filename:1:2: warning: Length Violation: Violation Reason.\n" + | ||
"filename:1:2: error: Length Violation: Violation Reason." | ||
) | ||
} | ||
|
||
func testJSONReporter() { | ||
XCTAssertEqual( | ||
JSONReporter.generateReport(generateViolations()), | ||
"[\n" + | ||
" {\n" + | ||
" \"type\" : \"Length\",\n" + | ||
" \"line\" : 1,\n" + | ||
" \"reason\" : \"Violation Reason.\",\n" + | ||
" \"file\" : \"filename\",\n" + | ||
" \"character\" : 2,\n" + | ||
" \"severity\" : \"Warning\"\n" + | ||
" },\n" + | ||
" {\n" + | ||
" \"type\" : \"Length\",\n" + | ||
" \"line\" : 1,\n" + | ||
" \"reason\" : \"Violation Reason.\",\n" + | ||
" \"file\" : \"filename\",\n" + | ||
" \"character\" : 2,\n" + | ||
" \"severity\" : \"Error\"\n" + | ||
" }\n" + | ||
"]" | ||
) | ||
} | ||
|
||
func testCSVReporter() { | ||
XCTAssertEqual( | ||
CSVReporter.generateReport(generateViolations()), | ||
"file,line,character,severity,type,reason," + | ||
"filename,1,2,Warning,Length,Violation Reason.," + | ||
"filename,1,2,Error,Length,Violation Reason." | ||
) | ||
} | ||
} |
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
Oops, something went wrong.