1/// What the `covane` command was asked to do, read from its arguments.
2public 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 <[label=]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 paths in argument order, with optional labels removed.
284 public var junitPaths: [String] { junitInputs.map(\.path) }
29 public let junitInputs: [JUnitInput]
30
31 /// - Throws: `ReportOptionsError` for an unknown option, an option without its
32 /// value, a missing required option, a count that isn't a whole number, or only
33 /// some of the three test counts, or JUnit files together with test counts.
3415 public init(arguments: [String]) throws {
3515 var values: [String: String] = [:]
3615 var junitPaths: [String] = []
3715 var index = arguments.startIndex
3882 while index < arguments.endIndex {
3969 let option = arguments[index]
4069 guard Self.options.contains(option) else {
411 throw ReportOptionsError.unknownOption(option)
4268 }
4368 guard index + 1 < arguments.endIndex else {
441 throw ReportOptionsError.missingValue(option: option)
4567 }
4667 if option == "--junit" {
475 junitPaths.append(arguments[index + 1])
4862 } else {
4962 values[option] = arguments[index + 1]
5062 }
5167 index += 2
5267 }
5338 func required(_ option: String) throws -> String {
5438 guard let value = values[option] else { throw ReportOptionsError.missingOption(option) }
5537 return value
5638 }
5713 exportPath = try required("--export")
5813 sourcesRoot = try required("--sources")
5912 outputFolder = try required("--output")
6012 summaryPath = values["--summary"]
6112 title = values["--title"] ?? Self.defaultTitle
6212 commit = values["--commit"]
6312 subject = values["--subject"]
6412 baselinePath = values["--baseline"]
6512 pagesBranch = values["--pages-branch"]
6612 guard baselinePath == nil || pagesBranch == nil else {
671 throw ReportOptionsError.baselineWithPagesBranch
6811 }
6911 tests = try Self.testCounts(from: values)
709 guard junitPaths.isEmpty || tests == nil else {
711 throw ReportOptionsError.junitWithTestCounts
728 }
738 self.junitInputs = try junitPaths.map(JUnitInput.init)
748 }
75
76 public static let defaultTitle = "Coverage"
77
78 private static let options: Set = [
79 "--export", "--sources", "--output", "--summary", "--title", "--commit", "--subject",
80 "--tests",
81 "--failed", "--junit", "--skipped", "--baseline", "--pages-branch",
82 ]
83
8411 private static func testCounts(from values: [String: String]) throws -> TestCounts? {
8532 let counts = try ["--tests", "--failed", "--skipped"].map { option in
8632 try values[option].map { value in
879 guard let count = Int(value), count >= 0 else {
881 throw ReportOptionsError.notACount(option: option, value: value)
898 }
908 return count
9131 }
9231 }
9324 if counts.allSatisfy({ $0 == nil }) {
947 return nil
957 }
963 guard let total = counts[0], let failed = counts[1], let skipped = counts[2] else {
971 throw ReportOptionsError.partialTestCounts
982 }
992 return TestCounts(total: total, failed: failed, skipped: skipped)
10011 }
101}
102
103public enum ReportOptionsError: Error, Equatable {
104 case invalidRunURL(String)
105 case invalidJUnitInput(String)
106 case unknownOption(String)
107 case missingValue(option: String)
108 case missingOption(String)
109 case notACount(option: String, value: String)
110 /// `--tests`, `--failed` and `--skipped` come together or not at all.
111 case partialTestCounts
112 /// JUnit files carry the counts, so the two can't both be given.
113 case junitWithTestCounts
114 /// Two baselines were named; the command won't guess which one is meant.
115 case baselineWithPagesBranch
116 case notAnOutcome(option: String, value: String)
117 case notPositive(option: String, value: String)
118}
119
120extension ReportOptionsError: CustomStringConvertible {
12153 public var description: String {
12253 switch self {
12353 case .invalidRunURL(let value): "--run-url needs an http or https URL, not \"\(value)\"."
12453 case .invalidJUnitInput(let value): "--junit needs a file or label=file, not \"\(value)\"."
12553 case .unknownOption(let option): "Unknown option \(option)."
12653 case .missingValue(let option): "\(option) needs a value."
12753 case .missingOption(let option): "\(option) is required."
12853 case .notACount(let option, let value): "\(option) takes a whole number, not \"\(value)\"."
12953 case .partialTestCounts: "--tests, --failed and --skipped go together."
13053 case .junitWithTestCounts: "--junit replaces --tests, --failed and --skipped."
13153 case .baselineWithPagesBranch: "--baseline and --pages-branch both name a baseline."
13253 case .notAnOutcome(let option, let value):
1332 "\(option) takes success, failure, skipped or cancelled, not \"\(value)\"."
13453 case .notPositive(let option, let value):
1356 "\(option) takes a whole number of at least 1, not \"\(value)\"."
13653 }
13753 }
138}
139
140extension RunTotalsError: CustomStringConvertible {
1414 public var description: String {
1424 switch self {
1434 case .unsupportedVersion(let version):
1442 "Unsupported totals.json format version \(version); this Covane reads version "
1452 + "\(RunTotals.currentFormatVersion)."
1464 case .noCoverage(let reason):
1472 "The run has no coverage (\(reason)), so there is nothing to compare with."
1484 }
1494 }
150}
151
152extension CoverageSiteError: CustomStringConvertible {
1535 public var description: String {
1545 switch self {
1555 case .outsideSources(let path):
1562 "\(path) is not inside the sources folder."
1575 case .sourceChanged(let path, let sourceLines, let coveredLine):
1583 "\(path) has \(sourceLines) lines but its coverage reaches line \(coveredLine): "
1593 + "the export and the sources come from different builds."
1605 }
1615 }
162}
163
164extension TestResultsError: CustomStringConvertible {
1655 public var description: String {
1665 switch self {
1675 case .malformedXML(let line, let column):
1683 "Not well-formed XML (line \(line), column \(column))."
1695 case .notJUnit(let root): "Not JUnit XML: the root element is <\(root)>."
1705 }
1715 }
172}
173
174extension CoverageExportError: CustomStringConvertible {
1757 public var description: String {
1767 switch self {
1777 case .unsupportedFormat(let type, let version):
1785 "Not an llvm-cov export of format 3 (type \(type), version \(version))."
1797 case .unexpectedDataCount(let count):
1802 "Expected one export in the file, found \(count)."
1817 }
1827 }
183}
184
185/// One JUnit file, optionally assigned to a named test group.
186public struct JUnitInput: Sendable, Equatable {
187 public let label: String?
188 public let path: String
189
190 /// - Throws: `ReportOptionsError.invalidJUnitInput` for an empty label or path.
19111 public init(_ value: String) throws {
19211 let parts = value.split(separator: "=", maxSplits: 1, omittingEmptySubsequences: false)
19311 guard let file = parts.last, !file.isEmpty,
19411 parts.count == 1 || !parts[0].isEmpty
19511 else { throw ReportOptionsError.invalidJUnitInput(value) }
1968 label = parts.count == 2 ? String(parts[0]) : nil
1978 path = String(file)
1988 }
199}