| 1 | | /// What the `covane` command was asked to do, read from its arguments. |
| 2 | | public struct ReportOptions: Sendable, Equatable { |
| 3 | | public static let usage = """ |
| 4 | | usage: covane --output <site folder> [--export <llvm-cov export.json>] |
| 5 | | [--sources <sources folder>] [--summary <summary.md>] |
| 6 | | [--title <text>] [--commit <sha>] [--subject <text>] |
| 7 | | [--baseline <totals.json> | --pages-branch <name>] |
| 8 | | [--junit <[label=]file|folder>]... |
| 9 | | | [--tests <n> --failed <n> --skipped <n>] |
| 10 | | """ |
| 11 | | |
| 12 | | /// The `llvm-cov export` to report; nil makes one for the SwiftPM package in the |
| 13 | | /// current folder (D2). |
| 14 | | public let exportPath: String? |
| 15 | | /// The folder whose files are reported; files in the export from elsewhere are left out. |
| 16 | | public let sourcesRoot: String |
| 17 | | public let outputFolder: String |
| 18 | | /// Where to write the job summary's Markdown; nil writes none. |
| 19 | | public let summaryPath: String? |
| 20 | | /// The site's name, in the index page's heading and every page's title. |
| 21 | | public let title: String |
| 22 | | /// Nil to take the repository's current commit, if there is one. |
| 23 | | public let commit: String? |
| 24 | | /// The commit's first message line, recorded in `totals.json` for the history page; |
| 25 | | /// nil to take the current commit's. |
| 26 | | public let subject: String? |
| 27 | | /// A previous run's `totals.json` to compare with; nil compares with nothing. |
| 28 | | public let baselinePath: String? |
| 29 | | /// The branch whose newest run with coverage is the baseline (D6); nil reads none. |
| 30 | | public let pagesBranch: String? |
| 31 | | public let tests: TestCounts? |
| 32 | | /// JUnit files and folders in argument order, with optional labels removed. |
| 33 | 4 | public var junitPaths: [String] { junitInputs.map(\.path) } |
| 34 | | public let junitInputs: [JUnitInput] |
| 35 | | |
| 36 | | /// - Throws: `ReportOptionsError` for an unknown option, an option without its |
| 37 | | /// value, a missing required option, a count that isn't a whole number, or only |
| 38 | | /// some of the three test counts, or JUnit files together with test counts. |
| 39 | 17 | public init(arguments: [String]) throws { |
| 40 | 17 | var values: [String: String] = [:] |
| 41 | 17 | var junitPaths: [String] = [] |
| 42 | 17 | var index = arguments.startIndex |
| 43 | 86 | while index < arguments.endIndex { |
| 44 | 71 | let option = arguments[index] |
| 45 | 71 | guard Self.options.contains(option) else { |
| 46 | 1 | throw ReportOptionsError.unknownOption(option) |
| 47 | 70 | } |
| 48 | 70 | guard index + 1 < arguments.endIndex else { |
| 49 | 1 | throw ReportOptionsError.missingValue(option: option) |
| 50 | 69 | } |
| 51 | 69 | if option == "--junit" { |
| 52 | 5 | junitPaths.append(arguments[index + 1]) |
| 53 | 64 | } else { |
| 54 | 64 | values[option] = arguments[index + 1] |
| 55 | 64 | } |
| 56 | 69 | index += 2 |
| 57 | 69 | } |
| 58 | 15 | func required(_ option: String) throws -> String { |
| 59 | 15 | guard let value = values[option] else { throw ReportOptionsError.missingOption(option) } |
| 60 | 14 | return value |
| 61 | 15 | } |
| 62 | 15 | exportPath = values["--export"] |
| 63 | 15 | sourcesRoot = values["--sources"] ?? Self.defaultSourcesRoot |
| 64 | 15 | outputFolder = try required("--output") |
| 65 | 14 | summaryPath = values["--summary"] |
| 66 | 14 | title = values["--title"] ?? Self.defaultTitle |
| 67 | 14 | commit = values["--commit"] |
| 68 | 14 | subject = values["--subject"] |
| 69 | 14 | baselinePath = values["--baseline"] |
| 70 | 14 | pagesBranch = values["--pages-branch"] |
| 71 | 14 | guard baselinePath == nil || pagesBranch == nil else { |
| 72 | 1 | throw ReportOptionsError.baselineWithPagesBranch |
| 73 | 13 | } |
| 74 | 13 | tests = try Self.testCounts(from: values) |
| 75 | 11 | guard junitPaths.isEmpty || tests == nil else { |
| 76 | 1 | throw ReportOptionsError.junitWithTestCounts |
| 77 | 10 | } |
| 78 | 10 | self.junitInputs = try junitPaths.map(JUnitInput.init) |
| 79 | 10 | } |
| 80 | | |
| 81 | | public static let defaultTitle = "Coverage" |
| 82 | | public static let defaultSourcesRoot = "Sources" |
| 83 | | |
| 84 | | private static let options: Set = [ |
| 85 | | "--export", "--sources", "--output", "--summary", "--title", "--commit", "--subject", |
| 86 | | "--tests", |
| 87 | | "--failed", "--junit", "--skipped", "--baseline", "--pages-branch", |
| 88 | | ] |
| 89 | | |
| 90 | 13 | private static func testCounts(from values: [String: String]) throws -> TestCounts? { |
| 91 | 38 | let counts = try ["--tests", "--failed", "--skipped"].map { option in |
| 92 | 38 | try values[option].map { value in |
| 93 | 9 | guard let count = Int(value), count >= 0 else { |
| 94 | 1 | throw ReportOptionsError.notACount(option: option, value: value) |
| 95 | 8 | } |
| 96 | 8 | return count |
| 97 | 37 | } |
| 98 | 37 | } |
| 99 | 30 | if counts.allSatisfy({ $0 == nil }) { |
| 100 | 9 | return nil |
| 101 | 9 | } |
| 102 | 3 | guard let total = counts[0], let failed = counts[1], let skipped = counts[2] else { |
| 103 | 1 | throw ReportOptionsError.partialTestCounts |
| 104 | 2 | } |
| 105 | 2 | return TestCounts(total: total, failed: failed, skipped: skipped) |
| 106 | 13 | } |
| 107 | | } |
| 108 | | |
| 109 | | public enum ReportOptionsError: Error, Equatable { |
| 110 | | case invalidRunURL(String) |
| 111 | | case invalidJUnitInput(String) |
| 112 | | case unknownOption(String) |
| 113 | | case missingValue(option: String) |
| 114 | | case missingOption(String) |
| 115 | | case notACount(option: String, value: String) |
| 116 | | /// `--tests`, `--failed` and `--skipped` come together or not at all. |
| 117 | | case partialTestCounts |
| 118 | | /// JUnit files carry the counts, so the two can't both be given. |
| 119 | | case junitWithTestCounts |
| 120 | | /// Two baselines were named; the command won't guess which one is meant. |
| 121 | | case baselineWithPagesBranch |
| 122 | | case notAnOutcome(option: String, value: String) |
| 123 | | case notPositive(option: String, value: String) |
| 124 | | } |
| 125 | | |
| 126 | | extension ReportOptionsError: CustomStringConvertible { |
| 127 | 53 | public var description: String { |
| 128 | 53 | switch self { |
| 129 | 53 | case .invalidRunURL(let value): "--run-url needs an http or https URL, not \"\(value)\"." |
| 130 | 53 | case .invalidJUnitInput(let value): |
| 131 | 6 | "--junit takes [label=]file or [label=]folder, not \"\(value)\"." |
| 132 | 53 | case .unknownOption(let option): "Unknown option \(option)." |
| 133 | 53 | case .missingValue(let option): "\(option) needs a value." |
| 134 | 53 | case .missingOption(let option): "\(option) is required." |
| 135 | 53 | case .notACount(let option, let value): "\(option) takes a whole number, not \"\(value)\"." |
| 136 | 53 | case .partialTestCounts: "--tests, --failed and --skipped go together." |
| 137 | 53 | case .junitWithTestCounts: "--junit replaces --tests, --failed and --skipped." |
| 138 | 53 | case .baselineWithPagesBranch: "--baseline and --pages-branch both name a baseline." |
| 139 | 53 | case .notAnOutcome(let option, let value): |
| 140 | 2 | "\(option) takes success, failure, skipped or cancelled, not \"\(value)\"." |
| 141 | 53 | case .notPositive(let option, let value): |
| 142 | 6 | "\(option) takes a whole number of at least 1, not \"\(value)\"." |
| 143 | 53 | } |
| 144 | 53 | } |
| 145 | | } |
| 146 | | |
| 147 | | extension RunTotalsError: CustomStringConvertible { |
| 148 | 4 | public var description: String { |
| 149 | 4 | switch self { |
| 150 | 4 | case .unsupportedVersion(let version): |
| 151 | 2 | "Unsupported totals.json format version \(version); this Covane reads version " |
| 152 | 2 | + "\(RunTotals.currentFormatVersion)." |
| 153 | 4 | case .noCoverage(let reason): |
| 154 | 2 | "The run has no coverage (\(reason)), so there is nothing to compare with." |
| 155 | 4 | } |
| 156 | 4 | } |
| 157 | | } |
| 158 | | |
| 159 | | extension CoverageSiteError: CustomStringConvertible { |
| 160 | 5 | public var description: String { |
| 161 | 5 | switch self { |
| 162 | 5 | case .noFilesInSources(let folder): |
| 163 | 2 | "No file in the export is inside the sources folder \(folder)." |
| 164 | 5 | case .sourceChanged(let path, let sourceLines, let coveredLine): |
| 165 | 3 | "\(path) has \(sourceLines) lines but its coverage reaches line \(coveredLine): " |
| 166 | 3 | + "the export and the sources come from different builds." |
| 167 | 5 | } |
| 168 | 5 | } |
| 169 | | } |
| 170 | | |
| 171 | | extension TestResultsError: CustomStringConvertible { |
| 172 | 5 | public var description: String { |
| 173 | 5 | switch self { |
| 174 | 5 | case .malformedXML(let line, let column): |
| 175 | 3 | "Not well-formed XML (line \(line), column \(column))." |
| 176 | 5 | case .notJUnit(let root): "Not JUnit XML: the root element is <\(root)>." |
| 177 | 5 | } |
| 178 | 5 | } |
| 179 | | } |
| 180 | | |
| 181 | | extension CoverageExportError: CustomStringConvertible { |
| 182 | 7 | public var description: String { |
| 183 | 7 | switch self { |
| 184 | 7 | case .unsupportedFormat(let type, let version): |
| 185 | 5 | "Not an llvm-cov export of format 3 (type \(type), version \(version))." |
| 186 | 7 | case .unexpectedDataCount(let count): |
| 187 | 2 | "Expected one export in the file, found \(count)." |
| 188 | 7 | } |
| 189 | 7 | } |
| 190 | | } |
| 191 | | |
| 192 | | /// One JUnit file or folder of them, optionally assigned to a named test group. |
| 193 | | public struct JUnitInput: Sendable, Equatable { |
| 194 | | public let label: String? |
| 195 | | public let path: String |
| 196 | | |
| 197 | 4 | init(label: String?, path: String) { |
| 198 | 4 | self.label = label |
| 199 | 4 | self.path = path |
| 200 | 4 | } |
| 201 | | |
| 202 | | /// - Throws: `ReportOptionsError.invalidJUnitInput` for an empty label or path. |
| 203 | 19 | public init(_ value: String) throws { |
| 204 | 19 | let parts = value.split(separator: "=", maxSplits: 1, omittingEmptySubsequences: false) |
| 205 | 19 | guard let file = parts.last, !file.isEmpty, |
| 206 | 19 | parts.count == 1 || !parts[0].isEmpty |
| 207 | 19 | else { throw ReportOptionsError.invalidJUnitInput(value) } |
| 208 | 16 | label = parts.count == 2 ? String(parts[0]) : nil |
| 209 | 16 | path = String(file) |
| 210 | 16 | } |
| 211 | | } |