| 1 | | import 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. |
| 5 | | public struct TestResults: Sendable, Equatable { |
| 6 | | public struct Group: Sendable, Equatable { |
| 7 | | public let label: String? |
| 8 | | public let cases: [TestCase] |
| 9 | 41 | public var counts: TestCounts { TestResults(cases: cases).counts } |
| 10 | | } |
| 11 | | |
| 12 | | /// Groups in first-seen order; unlabelled files share the nil-labelled group. |
| 13 | | public let groups: [Group] |
| 14 | | |
| 15 | | public struct TestCase: Sendable, Equatable { |
| 16 | | /// The case's `classname`, or its suite's name when it has none. |
| 17 | | public let suite: String |
| 18 | | public let name: String |
| 19 | | /// Nil when the XML gives no time. |
| 20 | | public let seconds: Double? |
| 21 | | public let outcome: Outcome |
| 22 | | |
| 23 | 83 | public init(suite: String, name: String, seconds: Double?, outcome: Outcome) { |
| 24 | 83 | self.suite = suite |
| 25 | 83 | self.name = name |
| 26 | 83 | self.seconds = seconds |
| 27 | 83 | self.outcome = outcome |
| 28 | 83 | } |
| 29 | | } |
| 30 | | |
| 31 | | public enum Outcome: Sendable, Equatable { |
| 32 | | case passed |
| 33 | | /// A `<failure>` or an `<error>`: its `message` attribute and its text. |
| 34 | | case failed(message: String, details: String) |
| 35 | | case skipped(message: String) |
| 36 | | } |
| 37 | | |
| 38 | | /// In document order. |
| 39 | | public let cases: [TestCase] |
| 40 | | |
| 41 | | /// - Throws: `TestResultsError` when the data isn't well-formed XML or its root is |
| 42 | | /// neither `<testsuites>` nor `<testsuite>`. |
| 43 | 34 | public init(junitXML: Data, label: String? = nil) throws { |
| 44 | 34 | let reader = JUnitReader() |
| 45 | 34 | let parser = XMLParser(data: junitXML) |
| 46 | 34 | parser.delegate = reader |
| 47 | 34 | let isWellFormed = parser.parse() |
| 48 | 34 | // The reader aborts on a foreign root, which the parser reports as a failure too. |
| 49 | 34 | if let root = reader.root, !JUnitReader.roots.contains(root) { |
| 50 | 1 | throw TestResultsError.notJUnit(root: root) |
| 51 | 33 | } |
| 52 | 33 | guard isWellFormed else { |
| 53 | 1 | throw TestResultsError.malformedXML( |
| 54 | 1 | line: parser.lineNumber, column: parser.columnNumber) |
| 55 | 32 | } |
| 56 | 32 | cases = reader.cases |
| 57 | 32 | groups = [Group(label: label, cases: reader.cases)] |
| 58 | 32 | } |
| 59 | | |
| 60 | 55 | public init(cases: [TestCase]) { |
| 61 | 55 | self.cases = cases |
| 62 | 55 | groups = [Group(label: nil, cases: cases)] |
| 63 | 55 | } |
| 64 | | |
| 65 | | /// The cases of several runs, such as the XCTest and Swift Testing files of one |
| 66 | | /// `swift test`, in the order given. |
| 67 | 14 | public init(merging results: [TestResults]) { |
| 68 | 24 | cases = results.flatMap(\.cases) |
| 69 | 14 | var merged: [Group] = [] |
| 70 | 24 | for group in results.flatMap(\.groups) { |
| 71 | 24 | if let index = merged.firstIndex(where: { $0.label == group.label }) { |
| 72 | 4 | merged[index] = Group(label: group.label, cases: merged[index].cases + group.cases) |
| 73 | 20 | } else { |
| 74 | 20 | merged.append(group) |
| 75 | 20 | } |
| 76 | 24 | } |
| 77 | 14 | groups = merged |
| 78 | 14 | } |
| 79 | | |
| 80 | 82 | public var counts: TestCounts { |
| 81 | 82 | var failed = 0 |
| 82 | 82 | var skipped = 0 |
| 83 | 112 | for testCase in cases { |
| 84 | 112 | switch testCase.outcome { |
| 85 | 112 | case .passed: break |
| 86 | 112 | case .failed: failed += 1 |
| 87 | 112 | case .skipped: skipped += 1 |
| 88 | 112 | } |
| 89 | 112 | } |
| 90 | 82 | return TestCounts(total: cases.count, failed: failed, skipped: skipped) |
| 91 | 82 | } |
| 92 | | } |
| 93 | | |
| 94 | | public enum TestResultsError: Error, Equatable { |
| 95 | | case malformedXML(line: Int, column: Int) |
| 96 | | case notJUnit(root: String) |
| 97 | | } |
| 98 | | |
| 99 | | /// Collects cases as `XMLParser` walks the document. A case is passed unless a |
| 100 | | /// `<failure>`, `<error>` or `<skipped>` inside it says otherwise; other elements, such |
| 101 | | /// as `<system-out>`, are ignored. |
| 102 | | private final class JUnitReader: NSObject, XMLParserDelegate { |
| 103 | | static let roots: Set = ["testsuites", "testsuite"] |
| 104 | | |
| 105 | | private(set) var root: String? |
| 106 | 34 | private(set) var cases: [TestResults.TestCase] = [] |
| 107 | | |
| 108 | 34 | private var suiteNames: [String] = [] |
| 109 | | private var openCase: (suite: String, name: String, seconds: Double?)? |
| 110 | 34 | private var outcome = TestResults.Outcome.passed |
| 111 | | /// The `message` of the open `<failure>` or `<error>` and the text read so far. |
| 112 | | private var openFailure: (message: String, text: String)? |
| 113 | | |
| 114 | | func parser( |
| 115 | | _ parser: XMLParser, didStartElement element: String, namespaceURI: String?, |
| 116 | | qualifiedName: String?, attributes: [String: String] = [:] |
| 117 | 149 | ) { |
| 118 | 149 | if root == nil { |
| 119 | 34 | root = element |
| 120 | 34 | guard Self.roots.contains(element) else { |
| 121 | 1 | parser.abortParsing() |
| 122 | 1 | return |
| 123 | 33 | } |
| 124 | 148 | } |
| 125 | 148 | switch element { |
| 126 | 148 | case "testsuite": |
| 127 | 33 | suiteNames.append(attributes["name"] ?? "") |
| 128 | 148 | case "testcase": |
| 129 | 68 | openCase = ( |
| 130 | 68 | suite: attributes["classname"] ?? suiteNames.last ?? "", |
| 131 | 68 | name: attributes["name"] ?? "", |
| 132 | 68 | seconds: attributes["time"].flatMap(Double.init) |
| 133 | 68 | ) |
| 134 | 68 | outcome = .passed |
| 135 | 148 | case "failure", |
| 136 | 28 | "error" where openCase != nil: |
| 137 | 28 | openFailure = (message: attributes["message"] ?? "", text: "") |
| 138 | 148 | case "skipped" where openCase != nil: |
| 139 | 9 | outcome = .skipped(message: attributes["message"] ?? "") |
| 140 | 148 | default: |
| 141 | 10 | break |
| 142 | 148 | } |
| 143 | 148 | } |
| 144 | | |
| 145 | 135 | func parser(_ parser: XMLParser, foundCharacters string: String) { |
| 146 | 135 | openFailure?.text += string |
| 147 | 135 | } |
| 148 | | |
| 149 | | func parser( |
| 150 | | _ parser: XMLParser, didEndElement element: String, namespaceURI: String?, |
| 151 | | qualifiedName: String? |
| 152 | 146 | ) { |
| 153 | 146 | switch element { |
| 154 | 146 | case "testsuite": |
| 155 | 32 | suiteNames.removeLast() |
| 156 | 146 | case "failure", "error": |
| 157 | 28 | if let failure = openFailure { |
| 158 | 28 | outcome = .failed( |
| 159 | 28 | message: failure.message, |
| 160 | 28 | details: failure.text.trimmingCharacters(in: .whitespacesAndNewlines)) |
| 161 | 28 | } |
| 162 | 28 | openFailure = nil |
| 163 | 146 | case "testcase": |
| 164 | 68 | if let testCase = openCase { |
| 165 | 68 | cases.append( |
| 166 | 68 | TestResults.TestCase( |
| 167 | 68 | suite: testCase.suite, name: testCase.name, seconds: testCase.seconds, |
| 168 | 68 | outcome: outcome)) |
| 169 | 68 | } |
| 170 | 68 | openCase = nil |
| 171 | 146 | default: |
| 172 | 18 | break |
| 173 | 146 | } |
| 174 | 146 | } |
| 175 | | } |