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 <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.
3314 public init(arguments: [String]) throws {
3414 var values: [String: String] = [:]
3514 var junitPaths: [String] = []
3614 var index = arguments.startIndex
3776 while index < arguments.endIndex {
3864 let option = arguments[index]
3964 guard Self.options.contains(option) else {
401 throw ReportOptionsError.unknownOption(option)
4163 }
4263 guard index + 1 < arguments.endIndex else {
431 throw ReportOptionsError.missingValue(option: option)
4462 }
4562 if option == "--junit" {
463 junitPaths.append(arguments[index + 1])
4759 } else {
4859 values[option] = arguments[index + 1]
4959 }
5062 index += 2
5162 }
5235 func required(_ option: String) throws -> String {
5335 guard let value = values[option] else { throw ReportOptionsError.missingOption(option) }
5434 return value
5535 }
5612 exportPath = try required("--export")
5712 sourcesRoot = try required("--sources")
5811 outputFolder = try required("--output")
5911 summaryPath = values["--summary"]
6011 title = values["--title"] ?? Self.defaultTitle
6111 commit = values["--commit"]
6211 subject = values["--subject"]
6311 baselinePath = values["--baseline"]
6411 pagesBranch = values["--pages-branch"]
6511 guard baselinePath == nil || pagesBranch == nil else {
661 throw ReportOptionsError.baselineWithPagesBranch
6710 }
6810 tests = try Self.testCounts(from: values)
698 guard junitPaths.isEmpty || tests == nil else {
701 throw ReportOptionsError.junitWithTestCounts
717 }
727 self.junitPaths = junitPaths
737 }
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
8310 private static func testCounts(from values: [String: String]) throws -> TestCounts? {
8429 let counts = try ["--tests", "--failed", "--skipped"].map { option in
8529 try values[option].map { value in
869 guard let count = Int(value), count >= 0 else {
871 throw ReportOptionsError.notACount(option: option, value: value)
888 }
898 return count
9028 }
9128 }
9221 if counts.allSatisfy({ $0 == nil }) {
936 return nil
946 }
953 guard let total = counts[0], let failed = counts[1], let skipped = counts[2] else {
961 throw ReportOptionsError.partialTestCounts
972 }
982 return TestCounts(total: total, failed: failed, skipped: skipped)
9910 }
100}
101
102public 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
117extension ReportOptionsError: CustomStringConvertible {
11845 public var description: String {
11945 switch self {
12045 case .unknownOption(let option): "Unknown option \(option)."
12145 case .missingValue(let option): "\(option) needs a value."
12245 case .missingOption(let option): "\(option) is required."
12345 case .notACount(let option, let value): "\(option) takes a whole number, not \"\(value)\"."
12445 case .partialTestCounts: "--tests, --failed and --skipped go together."
12545 case .junitWithTestCounts: "--junit replaces --tests, --failed and --skipped."
12645 case .baselineWithPagesBranch: "--baseline and --pages-branch both name a baseline."
12745 case .notAnOutcome(let option, let value):
1282 "\(option) takes success, failure, skipped or cancelled, not \"\(value)\"."
12945 case .notPositive(let option, let value):
1306 "\(option) takes a whole number of at least 1, not \"\(value)\"."
13145 }
13245 }
133}
134
135extension RunTotalsError: CustomStringConvertible {
1364 public var description: String {
1374 switch self {
1384 case .unsupportedVersion(let version):
1392 "Unsupported totals.json format version \(version); this Covane reads version "
1402 + "\(RunTotals.currentFormatVersion)."
1414 case .noCoverage(let reason):
1422 "The run has no coverage (\(reason)), so there is nothing to compare with."
1434 }
1444 }
145}
146
147extension CoverageSiteError: CustomStringConvertible {
1485 public var description: String {
1495 switch self {
1505 case .outsideSources(let path):
1512 "\(path) is not inside the sources folder."
1525 case .sourceChanged(let path, let sourceLines, let coveredLine):
1533 "\(path) has \(sourceLines) lines but its coverage reaches line \(coveredLine): "
1543 + "the export and the sources come from different builds."
1555 }
1565 }
157}
158
159extension TestResultsError: CustomStringConvertible {
1605 public var description: String {
1615 switch self {
1625 case .malformedXML(let line, let column):
1633 "Not well-formed XML (line \(line), column \(column))."
1645 case .notJUnit(let root): "Not JUnit XML: the root element is <\(root)>."
1655 }
1665 }
167}
168
169extension CoverageExportError: CustomStringConvertible {
1707 public var description: String {
1717 switch self {
1727 case .unsupportedFormat(let type, let version):
1735 "Not an llvm-cov export of format 3 (type \(type), version \(version))."
1747 case .unexpectedDataCount(let count):
1752 "Expected one export in the file, found \(count)."
1767 }
1777 }
178}