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