| 1 | | import Foundation |
| 2 | | |
| 3 | | /// Coverage cards and every module, each folding out its files. |
| 4 | | public struct IndexPage: Sendable { |
| 5 | | public static let sitePath = "index.html" |
| 6 | | private let title: String |
| 7 | | private let modules: [ModuleTotals] |
| 8 | | private let tests: TestRun? |
| 9 | | private let run: ReportHeader |
| 10 | | private let comparison: BaselineComparison? |
| 11 | | private let hasBranches: Bool |
| 12 | | private let trend: CoverageTrend |
| 13 | | private let hasConditions: Bool |
| 14 | | |
| 15 | | /// - Parameters: |
| 16 | | /// - title: the site's name, shown as given in the page's title and heading. |
| 17 | | /// - tests: results add the Tests tab; counts alone do not create a page. |
| 18 | | /// - commit: the commit the report was built from, shortened in the header. |
| 19 | | /// - subject: the commit's first message line. |
| 20 | | /// - historyPath: the History destination relative to the report root, if available. |
| 21 | | /// - trend: the line series shown in the coverage card. |
| 22 | | /// - baseline: an earlier run to compare with; with it, the table gets a change |
| 23 | | /// column. |
| 24 | | public init( |
| 25 | | title: String, files: [FileTotals], tests: TestRun?, commit: String?, |
| 26 | | baseline: RunTotals? = nil, subject: String? = nil, historyPath: String? = nil, |
| 27 | | trend: CoverageTrend = CoverageTrend(runs: []) |
| 28 | 43 | ) { |
| 29 | 43 | self.trend = trend |
| 30 | 43 | self.comparison = baseline.map(BaselineComparison.init) |
| 31 | 43 | self.title = title |
| 32 | 43 | self.modules = ModuleTotals.grouping(files) |
| 33 | 124 | self.hasBranches = files.contains { $0.branches != nil } |
| 34 | 126 | self.hasConditions = files.contains { $0.conditions != nil } |
| 35 | 43 | self.tests = tests |
| 36 | 43 | self.run = ReportHeader(commit: commit, subject: subject, historyPath: historyPath) |
| 37 | 43 | } |
| 38 | | |
| 39 | 43 | public var html: String { |
| 40 | 43 | """ |
| 41 | 43 | <!doctype html> |
| 42 | 43 | <html lang="en"> |
| 43 | 43 | <head> |
| 44 | 43 | <meta charset="utf-8"> |
| 45 | 43 | <meta name="viewport" content="width=device-width, initial-scale=1"> |
| 46 | 43 | <title>\(escapingHTML(title))</title> |
| 47 | 43 | <link rel="stylesheet" href="\(SiteStylesheet.fileName)"> |
| 48 | 43 | </head> |
| 49 | 43 | <body class="\(bodyClasses)"> |
| 50 | 43 | <header class="page-header"> |
| 51 | 43 | \(SiteHeader(title: title, currentTab: .coverage, hasTestsPage: hasTestsPage, rootPath: "", run: run).html) |
| 52 | 43 | </header> |
| 53 | 43 | <main> |
| 54 | 43 | \(cardsMarkup) |
| 55 | 43 | <p class="legend"><span class="key high"></span>90% and up \ |
| 56 | 43 | <span class="key medium"></span>75% and up <span class="key low"></span>below 75%</p> |
| 57 | 43 | <div class="toolbar" data-needs-script hidden> |
| 58 | 43 | <input id="filter" type="search" placeholder="Filter files" aria-label="Filter files"> |
| 59 | 43 | </div> |
| 60 | 43 | <div class="row columns" data-needs-script hidden>\ |
| 61 | 43 | <button type="button" data-sort="name" data-first="ascending">File</button><span class="bar"></span>\ |
| 62 | 43 | <button type="button" data-sort="percent" data-first="ascending">Lines</button>\ |
| 63 | 43 | <button type="button" data-sort="missed" data-first="descending">Missed</button>\ |
| 64 | 43 | \(comparison == nil ? "" : #"<span class="change">Change</span>"#)\ |
| 65 | 43 | \(hasBranches ? #"<span class="branches">Branches</span>"# : "")\ |
| 66 | 43 | \(hasConditions ? #"<span class="conditions">Conditions</span>"# : "")</div> |
| 67 | 43 | <div class="modules"> |
| 68 | 123 | \(modules.map(moduleMarkup).joined(separator: "\n")) |
| 69 | 43 | </div> |
| 70 | 43 | </main> |
| 71 | 43 | <script>\(Self.sortAndFilterScript)</script> |
| 72 | 43 | </body> |
| 73 | 43 | </html> |
| 74 | 43 | |
| 75 | 43 | """ |
| 76 | 43 | } |
| 77 | | |
| 78 | | /// The classes the stylesheet keys its extra columns on. |
| 79 | 43 | private var bodyClasses: String { |
| 80 | 43 | var classes = ["index-page"] |
| 81 | 43 | if comparison != nil { classes.append("has-baseline") } |
| 82 | 43 | if hasBranches { classes.append("has-branches") } |
| 83 | 43 | if hasConditions { classes.append("has-conditions") } |
| 84 | 43 | return classes.joined(separator: " ") |
| 85 | 43 | } |
| 86 | | |
| 87 | 43 | private var hasTestsPage: Bool { |
| 88 | 43 | if case .results = tests { true } else { false } |
| 89 | 43 | } |
| 90 | | |
| 91 | 43 | private var cardsMarkup: String { |
| 92 | 80 | let total = LineSummary(adding: modules.map(\.lines)) |
| 93 | 80 | let files = modules.flatMap(\.files) |
| 94 | 43 | let coverageChange = |
| 95 | 43 | comparison.map { Self.changeSpan($0.changeOfTotal(now: total)) + " since baseline · " } |
| 96 | 43 | ?? "" |
| 97 | 43 | let missedChange = |
| 98 | 43 | comparison.map { baseline in |
| 99 | 7 | let difference = baseline.missedLineChange(now: total) |
| 100 | 7 | let style = difference < 0 ? "up" : difference > 0 ? "down" : "flat" |
| 101 | 7 | let sign = difference > 0 ? "+" : "" |
| 102 | 7 | return |
| 103 | 7 | #"<span class="change \#(style)">\#(sign)\#(groupingDigits(difference)) since baseline</span>"# |
| 104 | 36 | } ?? "" |
| 105 | 43 | let newFiles = comparison.map { "\($0.newFileCount(in: files)) new since baseline" } ?? "" |
| 106 | 43 | let cards = [ |
| 107 | 43 | Self.card( |
| 108 | 43 | title: "Line coverage", value: total.percentText, |
| 109 | 43 | detail: coverageChange |
| 110 | 43 | + "\(groupingDigits(total.covered)) of \(groupingDigits(total.count)) lines" |
| 111 | 43 | + trend.sparkline), |
| 112 | 43 | Self.card( |
| 113 | 43 | title: "Missed lines", value: groupingDigits(total.count - total.covered), |
| 114 | 43 | detail: missedChange), |
| 115 | 43 | Self.card(title: "Files", value: groupingDigits(files.count), detail: newFiles), |
| 116 | 43 | ] |
| 117 | 43 | var additional = [String]() |
| 118 | 126 | if let branches = CoverageCount(addingPresent: files.map(\.branches)) { |
| 119 | 4 | additional.append( |
| 120 | 4 | Self.card( |
| 121 | 4 | title: "Branch coverage", value: branches.percentText, |
| 122 | 4 | detail: metricDetail( |
| 123 | 4 | branches, noun: "outcomes", change: comparison?.branchChange(now: branches)) |
| 124 | 4 | )) |
| 125 | 4 | } |
| 126 | 126 | if let conditions = CoverageCount(addingPresent: files.map(\.conditions)) { |
| 127 | 3 | additional.append( |
| 128 | 3 | Self.card( |
| 129 | 3 | title: "Condition coverage", value: conditions.percentText, |
| 130 | 3 | detail: metricDetail( |
| 131 | 3 | conditions, noun: "conditions", |
| 132 | 3 | change: comparison?.conditionChange(now: conditions)))) |
| 133 | 3 | } |
| 134 | 43 | let secondary = |
| 135 | 43 | additional.isEmpty |
| 136 | 43 | ? "" |
| 137 | 43 | : #"<section class="cards secondary" aria-label="Additional coverage">"# |
| 138 | 4 | + additional.joined() + "</section>" |
| 139 | 43 | return #"<section class="cards" aria-label="Coverage summary">"# + cards.joined() |
| 140 | 43 | + "</section>" + secondary |
| 141 | 43 | } |
| 142 | | |
| 143 | 136 | private static func card(title: String, value: String, detail: String) -> String { |
| 144 | 136 | #"<article class="card"><h2>\#(title)</h2><p class="value">\#(value)</p><p class="detail">\#(detail)</p></article>"# |
| 145 | 136 | } |
| 146 | | |
| 147 | | private func metricDetail( |
| 148 | | _ count: CoverageCount, noun: String, change: CoverageChange? |
| 149 | 7 | ) -> String { |
| 150 | 7 | let delta = change.map { Self.changeSpan($0) + " since baseline · " } ?? "" |
| 151 | 7 | return delta + "\(groupingDigits(count.covered)) of \(groupingDigits(count.total)) \(noun)" |
| 152 | 7 | } |
| 153 | | |
| 154 | | /// Shows the controls, sorts modules and the files inside each by the clicked column |
| 155 | | /// (a second click reverses it), and filters files by path, opening the modules |
| 156 | | /// that still have matches. Without scripts the modules still fold out by hand. |
| 157 | | private static let sortAndFilterScript = """ |
| 158 | | (() => { |
| 159 | | const list = document.querySelector(".modules"); |
| 160 | | const modules = [...list.querySelectorAll("details.module")]; |
| 161 | | const buttons = [...document.querySelectorAll("button[data-sort]")]; |
| 162 | | const filter = document.getElementById("filter"); |
| 163 | | document.querySelectorAll("[data-needs-script]").forEach(element => element.hidden = false); |
| 164 | | let key = "name"; |
| 165 | | let ascending = true; |
| 166 | | const value = row => key === "name" ? row.dataset.name.toLowerCase() : parseFloat(row.dataset[key]); |
| 167 | | const order = (a, b) => { |
| 168 | | const [x, y] = [value(a), value(b)]; |
| 169 | | return (x < y ? -1 : x > y ? 1 : 0) * (ascending ? 1 : -1); |
| 170 | | }; |
| 171 | | const sort = () => { |
| 172 | | for (const module of modules.sort(order)) { |
| 173 | | module.append(...[...module.querySelectorAll("a.file")].sort(order)); |
| 174 | | list.append(module); |
| 175 | | } |
| 176 | | for (const button of buttons) { |
| 177 | | button.dataset.direction = button.dataset.sort === key ? (ascending ? "ascending" : "descending") : ""; |
| 178 | | } |
| 179 | | }; |
| 180 | | for (const button of buttons) { |
| 181 | | button.addEventListener("click", () => { |
| 182 | | ascending = button.dataset.sort === key ? !ascending : button.dataset.first === "ascending"; |
| 183 | | key = button.dataset.sort; |
| 184 | | sort(); |
| 185 | | }); |
| 186 | | } |
| 187 | | filter.addEventListener("input", () => { |
| 188 | | const query = filter.value.trim().toLowerCase(); |
| 189 | | for (const module of modules) { |
| 190 | | let hasMatch = false; |
| 191 | | for (const file of module.querySelectorAll("a.file")) { |
| 192 | | const path = (module.dataset.name + "/" + file.dataset.name).toLowerCase(); |
| 193 | | file.hidden = query !== "" && !path.includes(query); |
| 194 | | hasMatch = hasMatch || !file.hidden; |
| 195 | | } |
| 196 | | module.hidden = !hasMatch; |
| 197 | | module.open = query !== "" && hasMatch; |
| 198 | | } |
| 199 | | }); |
| 200 | | sort(); |
| 201 | | })(); |
| 202 | | """ |
| 203 | | |
| 204 | | /// A `<details>` per module, so its files fold out without any script. |
| 205 | 80 | private func moduleMarkup(_ module: ModuleTotals) -> String { |
| 206 | 126 | let files = module.files.map { file in |
| 207 | 126 | let name = String(file.relativePath.dropFirst(module.name.count + 1)) |
| 208 | 126 | let fileChange = comparison?.change(ofFile: file.relativePath, now: file.lines) |
| 209 | 126 | return |
| 210 | 126 | #"<a class="row file" href="\#(escapingHTML(SourcePage.sitePath(for: file.relativePath)))""# |
| 211 | 126 | + "\(Self.sortKeys(name: name, lines: file.lines))>" |
| 212 | 126 | + "\(cells(name: name, lines: file.lines, change: fileChange, branches: file.branches, conditions: file.conditions))</a>" |
| 213 | 126 | } |
| 214 | 80 | let moduleChange = comparison?.change(ofModule: module.name, now: module.lines) |
| 215 | 80 | return """ |
| 216 | 80 | <details class="module"\(Self.sortKeys(name: module.name, lines: module.lines))> |
| 217 | 80 | <summary class="row">\(cells(name: module.name, lines: module.lines, change: moduleChange, branches: module.branches, conditions: module.conditions))</summary> |
| 218 | 80 | \(files.joined(separator: "\n")) |
| 219 | 80 | </details> |
| 220 | 80 | """ |
| 221 | 80 | } |
| 222 | | |
| 223 | | /// The values the index's sort script orders rows by, unformatted, so it compares |
| 224 | | /// numbers rather than the text shown. |
| 225 | 206 | private static func sortKeys(name: String, lines: LineSummary) -> String { |
| 226 | 206 | #" data-name="\#(escapingHTML(name))""# |
| 227 | 206 | + #" data-percent="\#(String(format: "%.2f", lines.percent))""# |
| 228 | 206 | + #" data-missed="\#(lines.count - lines.covered)" data-lines="\#(lines.count)""# |
| 229 | 206 | } |
| 230 | | |
| 231 | 45 | private static func changeSpan(_ change: CoverageChange) -> String { |
| 232 | 45 | #"<span class="change \#(change.styleName)">\#(change.text)</span>"# |
| 233 | 45 | } |
| 234 | | |
| 235 | | private func cells( |
| 236 | | name: String, lines: LineSummary, change: CoverageChange?, branches: CoverageCount?, |
| 237 | | conditions: CoverageCount? |
| 238 | 206 | ) -> String { |
| 239 | 206 | let tier = lines.tier.rawValue |
| 240 | 206 | let changeCell = change.map(Self.changeSpan) ?? "" |
| 241 | 206 | let branchCell = hasBranches ? Self.countCell(branches, className: "branches") : "" |
| 242 | 206 | let conditionCell = hasConditions ? Self.countCell(conditions, className: "conditions") : "" |
| 243 | 206 | return #"<span class="name">\#(escapingHTML(name))</span>"# |
| 244 | 206 | + #"<span class="bar"><span class="fill \#(tier)" style="width: \#(lines.percentText)"></span></span>"# |
| 245 | 206 | + #"<span class="percent \#(tier)">\#(lines.percentText)</span>"# |
| 246 | 206 | + #"<span class="missed">\#(groupingDigits(lines.count - lines.covered))</span>"# |
| 247 | 206 | + changeCell |
| 248 | 206 | + branchCell + conditionCell |
| 249 | 206 | } |
| 250 | | |
| 251 | | /// The share in its tier colour and the counts it comes from; a dash where this |
| 252 | | /// file or module has no data while others do. |
| 253 | 16 | private static func countCell(_ count: CoverageCount?, className: String) -> String { |
| 254 | 16 | guard let count else { return #"<span class="\#(className)">–</span>"# } |
| 255 | 15 | return #"<span class="\#(className)"><span class="percent \#(count.tier.rawValue)">"# |
| 256 | 15 | + "\(count.percentText)</span> \(groupingDigits(count.covered))/" |
| 257 | 15 | + "\(groupingDigits(count.total))</span>" |
| 258 | 16 | } |
| 259 | | } |