-
Notifications
You must be signed in to change notification settings - Fork 1
/
format_xctest_log.swift
executable file
·183 lines (147 loc) · 5.98 KB
/
format_xctest_log.swift
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#!/usr/bin/env xcrun -sdk macosx swift
import Foundation
// MARK: - -------====================== Patterns ======================--------
extension String {
/// Tries to match the provided regex pattern.
///
/// - Parameter regex: The regex pattern to match.
/// - Returns: The array of matches. The resulting
/// array is empty when no matches were found.
func getMatches(regex: String) -> [String] {
let nsrange = NSRange(self.startIndex..<self.endIndex, in: self)
let regex = try! NSRegularExpression(pattern: regex, options: [])
var result: [String] = []
regex.enumerateMatches(in: self, options: [], range: nsrange) { (match, _, _) in
for i in 0..<match!.numberOfRanges {
let match = String(self[Range(match!.range(at: i), in: self)!])
result.append(match)
}
}
return result
}
}
/// The matcher for the Test Suite started line.
///
/// Matches:
/// "Test Suite 'xxx' started at 2019-04-15 16:54:46.160"
///
/// - Parameters:
/// - line: The string to be searched.
/// - action: The callback called when the match is successful.
/// - name: The name of the suite 'xxx'.
func suiteStarted(_ line: String, action: (_ name: String) -> Void) {
let pattern = #"Test Suite '(.*)' started"#
let matches = line.getMatches(regex: pattern)
guard matches.isEmpty == false else { return }
let name = matches[1]
action(name)
}
/// The matcher for the Test case started line.
///
/// Matches:
/// "Test Case '-[xxx yyy]' started."
///
/// - Parameters:
/// - line: The string to be searched.
/// - action: The callback called when the match is successful.
/// - name: The name of the case 'yyy'.
///
func caseStarted(_ line: String, action: (_ name: String) -> Void) {
let pattern = #"Test Case '-\[(.*) (.*)\]' started"#
let matches = line.getMatches(regex: pattern)
guard matches.isEmpty == false else { return }
let name = matches[2]
action(name)
}
/// The matcher for the Test case passed line.
///
/// Matches:
/// "Test Case '-[xxx yyy]' passed (zzz seconds)."
///
/// - Parameters:
/// - line: The string to be searched.
/// - action: The callback called when the match is successful.
/// - suite: The name of the suite 'xxx'.
/// - name: The name of the case 'yyy'.
/// - duration: The duration of the case 'zzz'.
func casePassed(_ line: String, action: (_ suite: String, _ name: String, _ duration: String) -> Void) {
let pattern = #"(?:Test Case) '-\[(.*) (.*)\]' passed \((\w+.\w+) seconds\)"#
let matches = line.getMatches(regex: pattern)
guard matches.isEmpty == false else { return }
let suite = matches[1]
let name = matches[2]
let duration = matches[3]
action(suite, name, duration)
}
/// The matcher for the Test case failed line.
///
/// Matches:
/// "Test Case '-[xxx yyy]' failed (zzz seconds)."
///
/// - Parameters:
/// - line: The string to be searched.
/// - action: The callback called when the match is successful.
/// - suite: The name of the suite 'xxx'.
/// - name: The name of the case 'yyy'.
/// - duration: The duration of the case 'zzz'.
func caseFailed(_ line: String, action: (_ suite: String, _ name: String, _ duration: String) -> Void) {
let pattern = #"(?:Test Case) '-\[(.*) (.*)\]' failed \((\w+.\w+) seconds\)"#
let matches = line.getMatches(regex: pattern)
guard matches.isEmpty == false else { return }
let suite = matches[1]
let name = matches[2]
let duration = matches[3]
action(suite, name, duration)
}
// MARK: - -------====================== Terminal output formatting ======================--------
func print(_ string: String) {
if let data = (string + "\n").data(using: .utf8) {
FileHandle.standardOutput.write(data)
}
}
func bold(_ string: String) -> String { return "\u{001B}[1m\(string)\u{001B}[22m" }
func italic(_ string: String) -> String { return "\u{001B}[3m\(string)\u{001B}[23m" }
func green(_ string: String) -> String { return "\u{001B}[32m\(string)\u{001B}[0m" }
func red(_ string: String) -> String { return "\u{001B}[31m\(string)\u{001B}[0m" }
func darkGrayBackground(_ string: String) -> String { return "\u{001B}[100m\(string)\u{001B}[0m" }
let passed = green("✓")
let failed = bold(red("✗"))
// MARK: - -------====================== Main Loop ======================--------
var totalNumberOfTests = 0
var failingTests: [String] = []
var currentCaseRawLog: [String] = []
while let line = readLine() {
// Save the line to the current raw log
currentCaseRawLog.append(line)
suiteStarted(line) { name in
print("\n \(bold(name))")
}
caseStarted(line) { _ in
// Clear the raw log
currentCaseRawLog = [line]
totalNumberOfTests += 1
}
casePassed(line) { _, caseName, durationString in
let duration = italic("(\(durationString) sec)")
print(" \(passed) \(caseName) \(duration)")
}
caseFailed(line) { suiteName, caseName, durationString in
failingTests.append("\(suiteName) \(caseName)")
// print the raw log for the failing test
let log = "\n" + currentCaseRawLog.joined(separator: "\n")
print(bold(darkGrayBackground(log)))
print(" \(failed) \(red("\(caseName) \(italic("(\(durationString) sec)"))"))")
}
}
// MARK: - -------====================== Summary ======================--------
print("\n══════════════════════════════════════════════════════════════════════════════")
switch true {
case _ where totalNumberOfTests == 0:
print(" ❌ No tests executed.")
case _ where failingTests.isEmpty:
print(" ✅ \(totalNumberOfTests) tests passed.")
default:
print(" ❌ \(totalNumberOfTests) tests passed, \(failingTests.count) failed:")
failingTests.forEach { print("\t\(red($0))") }
}
print("══════════════════════════════════════════════════════════════════════════════")