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 --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.
334 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.
3917 public init(arguments: [String]) throws {
4017 var values: [String: String] = [:]
4117 var junitPaths: [String] = []
4217 var index = arguments.startIndex
4386 while index < arguments.endIndex {
4471 let option = arguments[index]
4571 guard Self.options.contains(option) else {
461 throw ReportOptionsError.unknownOption(option)
4770 }
4870 guard index + 1 < arguments.endIndex else {
491 throw ReportOptionsError.missingValue(option: option)
5069 }
5169 if option == "--junit" {
525 junitPaths.append(arguments[index + 1])
5364 } else {
5464 values[option] = arguments[index + 1]
5564 }
5669 index += 2
5769 }
5815 func required(_ option: String) throws -> String {
5915 guard let value = values[option] else { throw ReportOptionsError.missingOption(option) }
6014 return value
6115 }
6215 exportPath = values["--export"]
6315 sourcesRoot = values["--sources"] ?? Self.defaultSourcesRoot
6415 outputFolder = try required("--output")
6514 summaryPath = values["--summary"]
6614 title = values["--title"] ?? Self.defaultTitle
6714 commit = values["--commit"]
6814 subject = values["--subject"]
6914 baselinePath = values["--baseline"]
7014 pagesBranch = values["--pages-branch"]
7114 guard baselinePath == nil || pagesBranch == nil else {
721 throw ReportOptionsError.baselineWithPagesBranch
7313 }
7413 tests = try Self.testCounts(from: values)
7511 guard junitPaths.isEmpty || tests == nil else {
761 throw ReportOptionsError.junitWithTestCounts
7710 }
7810 self.junitInputs = try junitPaths.map(JUnitInput.init)
7910 }
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
9013 private static func testCounts(from values: [String: String]) throws -> TestCounts? {
9138 let counts = try ["--tests", "--failed", "--skipped"].map { option in
9238 try values[option].map { value in
939 guard let count = Int(value), count >= 0 else {
941 throw ReportOptionsError.notACount(option: option, value: value)
958 }
968 return count
9737 }
9837 }
9930 if counts.allSatisfy({ $0 == nil }) {
1009 return nil
1019 }
1023 guard let total = counts[0], let failed = counts[1], let skipped = counts[2] else {
1031 throw ReportOptionsError.partialTestCounts
1042 }
1052 return TestCounts(total: total, failed: failed, skipped: skipped)
10613 }
107}
108
109public 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
126extension ReportOptionsError: CustomStringConvertible {
12753 public var description: String {
12853 switch self {
12953 case .invalidRunURL(let value): "--run-url needs an http or https URL, not \"\(value)\"."
13053 case .invalidJUnitInput(let value):
1316 "--junit takes [label=]file or [label=]folder, not \"\(value)\"."
13253 case .unknownOption(let option): "Unknown option \(option)."
13353 case .missingValue(let option): "\(option) needs a value."
13453 case .missingOption(let option): "\(option) is required."
13553 case .notACount(let option, let value): "\(option) takes a whole number, not \"\(value)\"."
13653 case .partialTestCounts: "--tests, --failed and --skipped go together."
13753 case .junitWithTestCounts: "--junit replaces --tests, --failed and --skipped."
13853 case .baselineWithPagesBranch: "--baseline and --pages-branch both name a baseline."
13953 case .notAnOutcome(let option, let value):
1402 "\(option) takes success, failure, skipped or cancelled, not \"\(value)\"."
14153 case .notPositive(let option, let value):
1426 "\(option) takes a whole number of at least 1, not \"\(value)\"."
14353 }
14453 }
145}
146
147extension RunTotalsError: CustomStringConvertible {
1484 public var description: String {
1494 switch self {
1504 case .unsupportedVersion(let version):
1512 "Unsupported totals.json format version \(version); this Covane reads version "
1522 + "\(RunTotals.currentFormatVersion)."
1534 case .noCoverage(let reason):
1542 "The run has no coverage (\(reason)), so there is nothing to compare with."
1554 }
1564 }
157}
158
159extension CoverageSiteError: CustomStringConvertible {
1605 public var description: String {
1615 switch self {
1625 case .noFilesInSources(let folder):
1632 "No file in the export is inside the sources folder \(folder)."
1645 case .sourceChanged(let path, let sourceLines, let coveredLine):
1653 "\(path) has \(sourceLines) lines but its coverage reaches line \(coveredLine): "
1663 + "the export and the sources come from different builds."
1675 }
1685 }
169}
170
171extension TestResultsError: CustomStringConvertible {
1725 public var description: String {
1735 switch self {
1745 case .malformedXML(let line, let column):
1753 "Not well-formed XML (line \(line), column \(column))."
1765 case .notJUnit(let root): "Not JUnit XML: the root element is <\(root)>."
1775 }
1785 }
179}
180
181extension CoverageExportError: CustomStringConvertible {
1827 public var description: String {
1837 switch self {
1847 case .unsupportedFormat(let type, let version):
1855 "Not an llvm-cov export of format 3 (type \(type), version \(version))."
1867 case .unexpectedDataCount(let count):
1872 "Expected one export in the file, found \(count)."
1887 }
1897 }
190}
191
192/// One JUnit file or folder of them, optionally assigned to a named test group.
193public struct JUnitInput: Sendable, Equatable {
194 public let label: String?
195 public let path: String
196
1974 init(label: String?, path: String) {
1984 self.label = label
1994 self.path = path
2004 }
201
202 /// - Throws: `ReportOptionsError.invalidJUnitInput` for an empty label or path.
20319 public init(_ value: String) throws {
20419 let parts = value.split(separator: "=", maxSplits: 1, omittingEmptySubsequences: false)
20519 guard let file = parts.last, !file.isEmpty,
20619 parts.count == 1 || !parts[0].isEmpty
20719 else { throw ReportOptionsError.invalidJUnitInput(value) }
20816 label = parts.count == 2 ? String(parts[0]) : nil
20916 path = String(file)
21016 }
211}