-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Expand file tree
/
Copy pathJUnitReporter.swift
More file actions
39 lines (33 loc) · 1.56 KB
/
JUnitReporter.swift
File metadata and controls
39 lines (33 loc) · 1.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import Foundation
/// Reports violations as JUnit XML.
struct JUnitReporter: Reporter {
// MARK: - Reporter Conformance
static let identifier = "junit"
static let isRealtime = false
static let description = "Reports violations as JUnit XML."
static func generateReport(_ violations: [StyleViolation]) -> String {
let warningCount = violations.filter({ $0.severity == .warning }).count
let errorCount = violations.filter({ $0.severity == .error }).count
let testCount = warningCount + errorCount
return """
<?xml version="1.0" encoding="utf-8"?>
<testsuites name="SwiftLint" failures="\(warningCount)" errors="\(errorCount)" tests="\(testCount)">
\t<testsuite name="SwiftLint" failures="\(warningCount)" errors="\(errorCount)" tests="\(testCount)">
\(violations.map(testCase(for:)).joined(separator: "\n"))
\t</testsuite>
</testsuites>
"""
}
private static func testCase(for violation: StyleViolation) -> String {
let fileName = (violation.location.file ?? "<nopath>").escapedForXML()
let reason = violation.reason.escapedForXML()
let severity = violation.severity.rawValue.capitalized
let lineNumber = String(violation.location.line ?? 0)
let message = severity + ":" + "Line:" + lineNumber
return """
\t\t<testcase classname='Formatting Test' name='\(fileName)'>
\t\t\t<failure message='\(reason)'>\(message)</failure>
\t\t</testcase>
"""
}
}