| 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 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 | | |
| 14 | 52 | public init(suite: String, name: String, seconds: Double?, outcome: Outcome) { |
| 15 | 52 | self.suite = suite |
| 16 | 52 | self.name = name |
| 17 | 52 | self.seconds = seconds |
| 18 | 52 | self.outcome = outcome |
| 19 | 52 | } |
| 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>`. |
| 34 | 11 | public init(junitXML: Data) throws { |
| 35 | 11 | let reader = JUnitReader() |
| 36 | 11 | let parser = XMLParser(data: junitXML) |
| 37 | 11 | parser.delegate = reader |
| 38 | 11 | let isWellFormed = parser.parse() |
| 39 | 11 | // The reader aborts on a foreign root, which the parser reports as a failure too. |
| 40 | 11 | if let root = reader.root, !JUnitReader.roots.contains(root) { |
| 41 | 1 | throw TestResultsError.notJUnit(root: root) |
| 42 | 10 | } |
| 43 | 10 | guard isWellFormed else { |
| 44 | 1 | throw TestResultsError.malformedXML( |
| 45 | 1 | line: parser.lineNumber, column: parser.columnNumber) |
| 46 | 9 | } |
| 47 | 9 | cases = reader.cases |
| 48 | 9 | } |
| 49 | | |
| 50 | 13 | public init(cases: [TestCase]) { |
| 51 | 13 | self.cases = cases |
| 52 | 13 | } |
| 53 | | |
| 54 | | /// The cases of several runs, such as the XCTest and Swift Testing files of one |
| 55 | | /// `swift test`, in the order given. |
| 56 | 1 | public init(merging results: [TestResults]) { |
| 57 | 2 | cases = results.flatMap(\.cases) |
| 58 | 1 | } |
| 59 | | |
| 60 | 22 | public var counts: TestCounts { |
| 61 | 22 | var failed = 0 |
| 62 | 22 | var skipped = 0 |
| 63 | 34 | for testCase in cases { |
| 64 | 34 | switch testCase.outcome { |
| 65 | 34 | case .passed: break |
| 66 | 34 | case .failed: failed += 1 |
| 67 | 34 | case .skipped: skipped += 1 |
| 68 | 34 | } |
| 69 | 34 | } |
| 70 | 22 | return TestCounts(total: cases.count, failed: failed, skipped: skipped) |
| 71 | 22 | } |
| 72 | | } |
| 73 | | |
| 74 | | public 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. |
| 82 | | private final class JUnitReader: NSObject, XMLParserDelegate { |
| 83 | | static let roots: Set = ["testsuites", "testsuite"] |
| 84 | | |
| 85 | | private(set) var root: String? |
| 86 | 11 | private(set) var cases: [TestResults.TestCase] = [] |
| 87 | | |
| 88 | 11 | private var suiteNames: [String] = [] |
| 89 | | private var openCase: (suite: String, name: String, seconds: Double?)? |
| 90 | 11 | 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] = [:] |
| 97 | 77 | ) { |
| 98 | 77 | if root == nil { |
| 99 | 11 | root = element |
| 100 | 11 | guard Self.roots.contains(element) else { |
| 101 | 1 | parser.abortParsing() |
| 102 | 1 | return |
| 103 | 10 | } |
| 104 | 76 | } |
| 105 | 76 | switch element { |
| 106 | 76 | case "testsuite": |
| 107 | 10 | suiteNames.append(attributes["name"] ?? "") |
| 108 | 76 | case "testcase": |
| 109 | 37 | openCase = ( |
| 110 | 37 | suite: attributes["classname"] ?? suiteNames.last ?? "", |
| 111 | 37 | name: attributes["name"] ?? "", |
| 112 | 37 | seconds: attributes["time"].flatMap(Double.init) |
| 113 | 37 | ) |
| 114 | 37 | outcome = .passed |
| 115 | 76 | case "failure", |
| 116 | 14 | "error" where openCase != nil: |
| 117 | 14 | openFailure = (message: attributes["message"] ?? "", text: "") |
| 118 | 76 | case "skipped" where openCase != nil: |
| 119 | 7 | outcome = .skipped(message: attributes["message"] ?? "") |
| 120 | 76 | default: |
| 121 | 8 | break |
| 122 | 76 | } |
| 123 | 76 | } |
| 124 | | |
| 125 | 105 | func parser(_ parser: XMLParser, foundCharacters string: String) { |
| 126 | 105 | openFailure?.text += string |
| 127 | 105 | } |
| 128 | | |
| 129 | | func parser( |
| 130 | | _ parser: XMLParser, didEndElement element: String, namespaceURI: String?, |
| 131 | | qualifiedName: String? |
| 132 | 74 | ) { |
| 133 | 74 | switch element { |
| 134 | 74 | case "testsuite": |
| 135 | 9 | suiteNames.removeLast() |
| 136 | 74 | case "failure", "error": |
| 137 | 14 | if let failure = openFailure { |
| 138 | 14 | outcome = .failed( |
| 139 | 14 | message: failure.message, |
| 140 | 14 | details: failure.text.trimmingCharacters(in: .whitespacesAndNewlines)) |
| 141 | 14 | } |
| 142 | 14 | openFailure = nil |
| 143 | 74 | case "testcase": |
| 144 | 37 | if let testCase = openCase { |
| 145 | 37 | cases.append( |
| 146 | 37 | TestResults.TestCase( |
| 147 | 37 | suite: testCase.suite, name: testCase.name, seconds: testCase.seconds, |
| 148 | 37 | outcome: outcome)) |
| 149 | 37 | } |
| 150 | 37 | openCase = nil |
| 151 | 74 | default: |
| 152 | 14 | break |
| 153 | 74 | } |
| 154 | 74 | } |
| 155 | | } |