| 1 | | import Foundation |
| 2 | | |
| 3 | | /// What the command found at a `--junit` path. |
| 4 | | public enum JUnitPath: Sendable, Equatable { |
| 5 | | case file |
| 6 | | /// The names of the files directly in the folder, in any order. |
| 7 | | case folder(fileNames: [String]) |
| 8 | | case nothing |
| 9 | | } |
| 10 | | |
| 11 | | public enum JUnitFolderError: Error, Equatable { |
| 12 | | case missing(path: String) |
| 13 | | case noXMLFiles(path: String) |
| 14 | | } |
| 15 | | |
| 16 | | extension JUnitFolderError: CustomStringConvertible { |
| 17 | 6 | public var description: String { |
| 18 | 6 | switch self { |
| 19 | 6 | case .missing(let path): "There is no JUnit folder at \(path)." |
| 20 | 6 | case .noXMLFiles(let path): "There is no .xml file in the JUnit folder \(path)." |
| 21 | 6 | } |
| 22 | 6 | } |
| 23 | | } |
| 24 | | |
| 25 | | extension JUnitInput { |
| 26 | | /// The files to read for this input, each under its label: a folder's `.xml` files in |
| 27 | | /// name order, otherwise the path itself. A missing `.xml` path counts as a file, so |
| 28 | | /// that reading it fails. |
| 29 | | /// - Throws: `JUnitFolderError` for a folder without `.xml` files, or nothing at a path |
| 30 | | /// that doesn't name an `.xml` file. |
| 31 | 6 | public func files(at found: JUnitPath) throws -> [JUnitInput] { |
| 32 | 6 | switch found { |
| 33 | 6 | case .file: |
| 34 | 1 | return [self] |
| 35 | 6 | case .nothing: |
| 36 | 2 | guard path.hasSuffix(Self.fileSuffix) else { |
| 37 | 1 | throw JUnitFolderError.missing(path: path) |
| 38 | 1 | } |
| 39 | 1 | return [self] |
| 40 | 6 | case .folder(let fileNames): |
| 41 | 7 | let names = fileNames.filter { $0.hasSuffix(Self.fileSuffix) }.sorted() |
| 42 | 3 | guard !names.isEmpty else { throw JUnitFolderError.noXMLFiles(path: path) } |
| 43 | 4 | return names.map { |
| 44 | 4 | JUnitInput(label: label, path: (path as NSString).appendingPathComponent($0)) |
| 45 | 4 | } |
| 46 | 6 | } |
| 47 | 6 | } |
| 48 | | |
| 49 | | private static let fileSuffix = ".xml" |
| 50 | | } |