Use text blocks in reporters if reasonable (#4733)

This commit is contained in:
Danny Mösch 2023-01-30 22:38:07 +01:00 committed by GitHub
parent 651b00eb70
commit 0796236031
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 142 additions and 139 deletions

View File

@ -11,9 +11,10 @@ public struct EmojiReporter: Reporter {
public static func generateReport(_ violations: [StyleViolation]) -> String {
return violations
.group(by: { $0.location.file ?? "Other" })
.sorted(by: { $0.key < $1.key })
.map(report).joined(separator: "\n")
.group { $0.location.file ?? "Other" }
.sorted { $0.key < $1.key }
.map(report)
.joined(separator: "\n")
}
// MARK: - Private

View File

@ -27,125 +27,127 @@ public struct HTMLReporter: Reporter {
// swiftlint:disable:next function_body_length
internal static func generateReport(_ violations: [StyleViolation], swiftlintVersion: String,
dateString: String) -> String {
let rows = violations.enumerated().reduce(into: "") { rows, indexAndViolation in
rows.append(generateSingleRow(for: indexAndViolation.1, at: indexAndViolation.0 + 1))
}
let rows = violations.enumerated()
.map { generateSingleRow(for: $1, at: $0 + 1) }
.joined(separator: "\n")
let fileCount = Set(violations.compactMap({ $0.location.file })).count
let warningCount = violations.filter({ $0.severity == .warning }).count
let errorCount = violations.filter({ $0.severity == .error }).count
return [
"<!doctype html>\n",
"<html>\n",
"\t<head>\n",
"\t\t<meta charset=\"utf-8\" />\n",
"\t\t<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\" />\n",
"\t\t\n",
"\t\t<style type=\"text/css\">\n",
"\t\t\tbody {\n",
"\t\t\t\tfont-family: Arial, Helvetica, sans-serif;\n",
"\t\t\t\tfont-size: 0.9rem;\n",
"\t\t\t}\n",
"\t\t\t\n",
"\t\t\ttable {\n",
"\t\t\t\tborder: 1px solid gray;\n",
"\t\t\t\tborder-collapse: collapse;\n",
"\t\t\t\t-moz-box-shadow: 3px 3px 4px #AAA;\n",
"\t\t\t\t-webkit-box-shadow: 3px 3px 4px #AAA;\n",
"\t\t\t\tbox-shadow: 3px 3px 4px #AAA;\n",
"\t\t\t\tvertical-align: top;\n",
"\t\t\t\theight: 64px;\n",
"\t\t\t}\n",
"\t\t\t\n",
"\t\t\ttd, th {\n",
"\t\t\t\tborder: 1px solid #D3D3D3;\n",
"\t\t\t\tpadding: 5px 10px 5px 10px;\n",
"\t\t\t}\n",
"\t\t\t\n",
"\t\t\tth {\n",
"\t\t\t\tborder-bottom: 1px solid gray;\n",
"\t\t\t\tbackground-color: rgba(41,52,92,0.313);\n",
"\t\t\t}\n",
"\t\t\t\n",
"\t\t\t.error, .warning {\n",
"\t\t\t\ttext-align: center;\n",
"\t\t\t}\n",
"\t\t\t\n",
"\t\t\t.error {\n",
"\t\t\t\tbackground-color: #FF9D92;\n",
"\t\t\t\tcolor: #7F0800;\n",
"\t\t\t}\n",
"\t\t\t\n",
"\t\t\t.warning {\n",
"\t\t\t\tbackground-color: #FFF59E;\n",
"\t\t\t\tcolor: #7F7000;\n",
"\t\t\t}\n",
"\t\t</style>\n",
"\t\t\n",
"\t\t<title>SwiftLint Report</title>\n",
"\t</head>\n",
"\t<body>\n",
"\t\t<h1>SwiftLint Report</h1>\n",
"\t\t\n",
"\t\t<hr />\n",
"\t\t\n",
"\t\t<h2>Violations</h2>\n",
"\t\t\n",
"\t\t<table>\n",
"\t\t\t<thead>\n",
"\t\t\t\t<tr>\n",
"\t\t\t\t\t<th style=\"width: 60pt;\">\n",
"\t\t\t\t\t\t<b>Serial No.</b>\n",
"\t\t\t\t\t</th>\n",
"\t\t\t\t\t<th style=\"width: 500pt;\">\n",
"\t\t\t\t\t\t<b>File</b>\n",
"\t\t\t\t\t</th>\n",
"\t\t\t\t\t<th style=\"width: 60pt;\">\n",
"\t\t\t\t\t\t<b>Location</b>\n",
"\t\t\t\t\t</th>\n",
"\t\t\t\t\t<th style=\"width: 60pt;\">\n",
"\t\t\t\t\t\t<b>Severity</b>\n",
"\t\t\t\t\t</th>\n",
"\t\t\t\t\t<th style=\"width: 500pt;\">\n",
"\t\t\t\t\t\t<b>Message</b>\n",
"\t\t\t\t\t</th>\n",
"\t\t\t\t</tr>\n",
"\t\t\t</thead>\n",
"\t\t\t<tbody>\n", rows, "\t\t\t</tbody>\n",
"\t\t</table>\n",
"\t\t\n",
"\t\t<br/>\n",
"\t\t\n",
"\t\t<h2>Summary</h2>\n",
"\t\t\n",
"\t\t<table>\n",
"\t\t\t<tbody>\n",
"\t\t\t\t<tr>\n",
"\t\t\t\t\t<td>Total files with violations</td>\n",
"\t\t\t\t\t<td>\(fileCount)</td>\n",
"\t\t\t\t</tr>\n",
"\t\t\t\t<tr>\n",
"\t\t\t\t\t<td>Total warnings</td>\n",
"\t\t\t\t\t<td>\(warningCount)</td>\n",
"\t\t\t\t</tr>\n",
"\t\t\t\t<tr>\n",
"\t\t\t\t\t<td>Total errors</td>\n",
"\t\t\t\t\t<td>\(errorCount)</td>\n",
"\t\t\t\t</tr>\n",
"\t\t\t</tbody>\n",
"\t\t</table>\n",
"\t\t\n",
"\t\t<hr />\n",
"\t\t\n",
"\t\t<p>\n",
"\t\t\tCreated with\n",
"\t\t\t<a href=\"https://github.com/realm/SwiftLint\"><b>SwiftLint</b></a>\n",
"\t\t\t", swiftlintVersion, " on ", dateString, "\n",
"\t\t</p>\n",
"\t</body>\n",
"</html>"
].joined()
return """
<!doctype html>
<html>
\t<head>
\t\t<meta charset="utf-8" />
\t\t<meta name="viewport" content="width=device-width, initial-scale=1.0" />
\t\t
\t\t<style type="text/css">
\t\t\tbody {
\t\t\t\tfont-family: Arial, Helvetica, sans-serif;
\t\t\t\tfont-size: 0.9rem;
\t\t\t}
\t\t\t
\t\t\ttable {
\t\t\t\tborder: 1px solid gray;
\t\t\t\tborder-collapse: collapse;
\t\t\t\t-moz-box-shadow: 3px 3px 4px #AAA;
\t\t\t\t-webkit-box-shadow: 3px 3px 4px #AAA;
\t\t\t\tbox-shadow: 3px 3px 4px #AAA;
\t\t\t\tvertical-align: top;
\t\t\t\theight: 64px;
\t\t\t}
\t\t\t
\t\t\ttd, th {
\t\t\t\tborder: 1px solid #D3D3D3;
\t\t\t\tpadding: 5px 10px 5px 10px;
\t\t\t}
\t\t\t
\t\t\tth {
\t\t\t\tborder-bottom: 1px solid gray;
\t\t\t\tbackground-color: rgba(41,52,92,0.313);
\t\t\t}
\t\t\t
\t\t\t.error, .warning {
\t\t\t\ttext-align: center;
\t\t\t}
\t\t\t
\t\t\t.error {
\t\t\t\tbackground-color: #FF9D92;
\t\t\t\tcolor: #7F0800;
\t\t\t}
\t\t\t
\t\t\t.warning {
\t\t\t\tbackground-color: #FFF59E;
\t\t\t\tcolor: #7F7000;
\t\t\t}
\t\t</style>
\t\t
\t\t<title>SwiftLint Report</title>
\t</head>
\t<body>
\t\t<h1>SwiftLint Report</h1>
\t\t
\t\t<hr />
\t\t
\t\t<h2>Violations</h2>
\t\t
\t\t<table>
\t\t\t<thead>
\t\t\t\t<tr>
\t\t\t\t\t<th style="width: 60pt;">
\t\t\t\t\t\t<b>Serial No.</b>
\t\t\t\t\t</th>
\t\t\t\t\t<th style="width: 500pt;">
\t\t\t\t\t\t<b>File</b>
\t\t\t\t\t</th>
\t\t\t\t\t<th style="width: 60pt;">
\t\t\t\t\t\t<b>Location</b>
\t\t\t\t\t</th>
\t\t\t\t\t<th style="width: 60pt;">
\t\t\t\t\t\t<b>Severity</b>
\t\t\t\t\t</th>
\t\t\t\t\t<th style="width: 500pt;">
\t\t\t\t\t\t<b>Message</b>
\t\t\t\t\t</th>
\t\t\t\t</tr>
\t\t\t</thead>
\t\t\t<tbody>
\(rows)
\t\t\t</tbody>
\t\t</table>
\t\t
\t\t<br/>
\t\t
\t\t<h2>Summary</h2>
\t\t
\t\t<table>
\t\t\t<tbody>
\t\t\t\t<tr>
\t\t\t\t\t<td>Total files with violations</td>
\t\t\t\t\t<td>\(fileCount)</td>
\t\t\t\t</tr>
\t\t\t\t<tr>
\t\t\t\t\t<td>Total warnings</td>
\t\t\t\t\t<td>\(warningCount)</td>
\t\t\t\t</tr>
\t\t\t\t<tr>
\t\t\t\t\t<td>Total errors</td>
\t\t\t\t\t<td>\(errorCount)</td>
\t\t\t\t</tr>
\t\t\t</tbody>
\t\t</table>
\t\t
\t\t<hr />
\t\t
\t\t<p>
\t\t\tCreated with
\t\t\t<a href="https://github.com/realm/SwiftLint"><b>SwiftLint</b></a>
\t\t\t\(swiftlintVersion) on \(dateString)
\t\t</p>
\t</body>
</html>
"""
}
// MARK: - Private
@ -156,14 +158,14 @@ public struct HTMLReporter: Reporter {
let file: String = (violation.location.relativeFile ?? "<nopath>").escapedForXML()
let line: Int = location.line ?? 0
let character: Int = location.character ?? 0
return [
"\t\t\t\t<tr>\n",
"\t\t\t\t\t<td style=\"text-align: right;\">\(index)</td>\n",
"\t\t\t\t\t<td>", file, "</td>\n",
"\t\t\t\t\t<td style=\"text-align: center;\">\(line):\(character)</td>\n",
"\t\t\t\t\t<td class=\"", severity.lowercased(), "\">", severity, "</td>\n",
"\t\t\t\t\t<td>\(violation.reason.escapedForXML())</td>\n",
"\t\t\t\t</tr>\n"
].joined()
return """
\t\t\t\t<tr>
\t\t\t\t\t<td style="text-align: right;">\(index)</td>
\t\t\t\t\t<td>\(file)</td>
\t\t\t\t\t<td style="text-align: center;">\(line):\(character)</td>
\t\t\t\t\t<td class="\(severity.lowercased())">\(severity)</td>
\t\t\t\t\t<td>\(violation.reason.escapedForXML())</td>
\t\t\t\t</tr>
"""
}
}

View File

@ -13,14 +13,14 @@ public struct JUnitReporter: Reporter {
let warningCount = violations.filter({ $0.severity == .warning }).count
let errorCount = violations.filter({ $0.severity == .error }).count
return [
"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n",
"<testsuites failures=\"\(warningCount)\" errors=\"\(errorCount)\">\n",
"\t<testsuite failures=\"\(warningCount)\" errors=\"\(errorCount)\">",
violations.map({ testCase(for: $0) }).joined(),
"\n\t</testsuite>\n",
"</testsuites>"
].joined()
return """
<?xml version="1.0" encoding="utf-8"?>
<testsuites failures="\(warningCount)" errors="\(errorCount)">
\t<testsuite failures="\(warningCount)" errors="\(errorCount)">
\(violations.map(testCase(for:)).joined(separator: "\n"))
\t</testsuite>
</testsuites>
"""
}
private static func testCase(for violation: StyleViolation) -> String {
@ -30,10 +30,10 @@ public struct JUnitReporter: Reporter {
let lineNumber = String(violation.location.line ?? 0)
let message = severity + ":" + "Line:" + lineNumber
return [
"\n\t\t<testcase classname='Formatting Test' name='\(fileName)\'>",
"\n\t\t\t<failure message='\(reason)\'>" + message + "</failure>",
"\n\t\t</testcase>"
].joined()
return """
\t\t<testcase classname='Formatting Test' name='\(fileName)'>
\t\t\t<failure message='\(reason)'>\(message)</failure>
\t\t</testcase>
"""
}
}