1import Foundation
2
3/// What the command found at a `--junit` path.
4public 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
11public enum JUnitFolderError: Error, Equatable {
12 case missing(path: String)
13 case noXMLFiles(path: String)
14}
15
16extension JUnitFolderError: CustomStringConvertible {
176 public var description: String {
186 switch self {
196 case .missing(let path): "There is no JUnit folder at \(path)."
206 case .noXMLFiles(let path): "There is no .xml file in the JUnit folder \(path)."
216 }
226 }
23}
24
25extension 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.
316 public func files(at found: JUnitPath) throws -> [JUnitInput] {
326 switch found {
336 case .file:
341 return [self]
356 case .nothing:
362 guard path.hasSuffix(Self.fileSuffix) else {
371 throw JUnitFolderError.missing(path: path)
381 }
391 return [self]
406 case .folder(let fileNames):
417 let names = fileNames.filter { $0.hasSuffix(Self.fileSuffix) }.sorted()
423 guard !names.isEmpty else { throw JUnitFolderError.noXMLFiles(path: path) }
434 return names.map {
444 JUnitInput(label: label, path: (path as NSString).appendingPathComponent($0))
454 }
466 }
476 }
48
49 private static let fileSuffix = ".xml"
50}