1import Foundation
2
3/// The test cases of one run, read from JUnit XML such as `swift test --xunit-output`
4/// writes. Values only; reading the files is the command's job.
5public struct TestResults: Sendable, Equatable {
6 public struct TestCase: Sendable, Equatable {
7 /// The case's `classname`, or its suite's name when it has none.
8 public let suite: String
9 public let name: String
10 /// Nil when the XML gives no time.
11 public let seconds: Double?
12 public let outcome: Outcome
13
1452 public init(suite: String, name: String, seconds: Double?, outcome: Outcome) {
1552 self.suite = suite
1652 self.name = name
1752 self.seconds = seconds
1852 self.outcome = outcome
1952 }
20 }
21
22 public enum Outcome: Sendable, Equatable {
23 case passed
24 /// A `<failure>` or an `<error>`: its `message` attribute and its text.
25 case failed(message: String, details: String)
26 case skipped(message: String)
27 }
28
29 /// In document order.
30 public let cases: [TestCase]
31
32 /// - Throws: `TestResultsError` when the data isn't well-formed XML or its root is
33 /// neither `<testsuites>` nor `<testsuite>`.
3411 public init(junitXML: Data) throws {
3511 let reader = JUnitReader()
3611 let parser = XMLParser(data: junitXML)
3711 parser.delegate = reader
3811 let isWellFormed = parser.parse()
3911 // The reader aborts on a foreign root, which the parser reports as a failure too.
4011 if let root = reader.root, !JUnitReader.roots.contains(root) {
411 throw TestResultsError.notJUnit(root: root)
4210 }
4310 guard isWellFormed else {
441 throw TestResultsError.malformedXML(
451 line: parser.lineNumber, column: parser.columnNumber)
469 }
479 cases = reader.cases
489 }
49
5013 public init(cases: [TestCase]) {
5113 self.cases = cases
5213 }
53
54 /// The cases of several runs, such as the XCTest and Swift Testing files of one
55 /// `swift test`, in the order given.
561 public init(merging results: [TestResults]) {
572 cases = results.flatMap(\.cases)
581 }
59
6022 public var counts: TestCounts {
6122 var failed = 0
6222 var skipped = 0
6334 for testCase in cases {
6434 switch testCase.outcome {
6534 case .passed: break
6634 case .failed: failed += 1
6734 case .skipped: skipped += 1
6834 }
6934 }
7022 return TestCounts(total: cases.count, failed: failed, skipped: skipped)
7122 }
72}
73
74public enum TestResultsError: Error, Equatable {
75 case malformedXML(line: Int, column: Int)
76 case notJUnit(root: String)
77}
78
79/// Collects cases as `XMLParser` walks the document. A case is passed unless a
80/// `<failure>`, `<error>` or `<skipped>` inside it says otherwise; other elements, such
81/// as `<system-out>`, are ignored.
82private final class JUnitReader: NSObject, XMLParserDelegate {
83 static let roots: Set = ["testsuites", "testsuite"]
84
85 private(set) var root: String?
8611 private(set) var cases: [TestResults.TestCase] = []
87
8811 private var suiteNames: [String] = []
89 private var openCase: (suite: String, name: String, seconds: Double?)?
9011 private var outcome = TestResults.Outcome.passed
91 /// The `message` of the open `<failure>` or `<error>` and the text read so far.
92 private var openFailure: (message: String, text: String)?
93
94 func parser(
95 _ parser: XMLParser, didStartElement element: String, namespaceURI: String?,
96 qualifiedName: String?, attributes: [String: String] = [:]
9777 ) {
9877 if root == nil {
9911 root = element
10011 guard Self.roots.contains(element) else {
1011 parser.abortParsing()
1021 return
10310 }
10476 }
10576 switch element {
10676 case "testsuite":
10710 suiteNames.append(attributes["name"] ?? "")
10876 case "testcase":
10937 openCase = (
11037 suite: attributes["classname"] ?? suiteNames.last ?? "",
11137 name: attributes["name"] ?? "",
11237 seconds: attributes["time"].flatMap(Double.init)
11337 )
11437 outcome = .passed
11576 case "failure",
11614 "error" where openCase != nil:
11714 openFailure = (message: attributes["message"] ?? "", text: "")
11876 case "skipped" where openCase != nil:
1197 outcome = .skipped(message: attributes["message"] ?? "")
12076 default:
1218 break
12276 }
12376 }
124
125105 func parser(_ parser: XMLParser, foundCharacters string: String) {
126105 openFailure?.text += string
127105 }
128
129 func parser(
130 _ parser: XMLParser, didEndElement element: String, namespaceURI: String?,
131 qualifiedName: String?
13274 ) {
13374 switch element {
13474 case "testsuite":
1359 suiteNames.removeLast()
13674 case "failure", "error":
13714 if let failure = openFailure {
13814 outcome = .failed(
13914 message: failure.message,
14014 details: failure.text.trimmingCharacters(in: .whitespacesAndNewlines))
14114 }
14214 openFailure = nil
14374 case "testcase":
14437 if let testCase = openCase {
14537 cases.append(
14637 TestResults.TestCase(
14737 suite: testCase.suite, name: testCase.name, seconds: testCase.seconds,
14837 outcome: outcome))
14937 }
15037 openCase = nil
15174 default:
15214 break
15374 }
15474 }
155}