|
4 | 4 | * @file The CLI's declared minimum Node, and the one issue that reports a run |
5 | 5 | * on it. |
6 | 6 | * @input `minimum --package <package.json>` prints the lowest Node version its |
7 | | - * `engines.node` range allows. `reportIssue` takes actions/github-script's |
8 | | - * `github` and `context`, whether the run passed, the version and the run |
9 | | - * link. |
| 7 | + * `engines.node` range allows; `slow --times <file> --record <file>` names |
| 8 | + * one leg's files past their minimum-Node record. `reportIssue` takes |
| 9 | + * actions/github-script's `github` and `context`, the run's verdict (pass, |
| 10 | + * fail or none), the version and the run link. |
10 | 11 | * @output The version on stdout (exit 1 when the range has no lower bound this |
11 | | - * reads); an issue opened, commented on or closed. |
| 12 | + * reads); warning lines and a step summary for slow files; an issue opened, |
| 13 | + * commented on or closed. |
12 | 14 | * @position .github/workflows/cli-minimum-node.yml: the daily main-only run of |
13 | 15 | * the CLI's tests and global-mode smoke on that version. The version comes |
14 | 16 | * from the CLI's own `engines.node`, not from .nvmrc, which names the |
15 | 17 | * version the rest of CI uses. |
16 | 18 | */ |
17 | 19 |
|
18 | 20 | import fs from 'node:fs'; |
| 21 | +import path from 'node:path'; |
19 | 22 | import {fileURLToPath} from 'node:url'; |
20 | 23 |
|
| 24 | +import {mergeRecord} from '../../scripts/lib/duration-split.mjs'; |
| 25 | +import {drifted} from './node-time-drift.mjs'; |
| 26 | + |
21 | 27 | export const ISSUE_TITLE = 'CLI fails on its minimum Node'; |
22 | 28 | const BOT = 'github-actions[bot]'; |
23 | 29 |
|
@@ -91,13 +97,153 @@ async function findOpenIssue(github, context) { |
91 | 97 | ); |
92 | 98 | } |
93 | 99 |
|
| 100 | +/** |
| 101 | + * Each leg's verdict from the files the CLI test legs upload |
| 102 | + * (`{"shard": <n>, "verdict": "pass" | "fail" | "none"}`): "none" for a leg |
| 103 | + * with no file, which the job limit cancelled before it could write one. |
| 104 | + * @param {string} dir |
| 105 | + * @param {number} count legs |
| 106 | + * @returns {Array<'pass' | 'fail' | 'none'>} |
| 107 | + */ |
| 108 | +export function readLegVerdicts(dir, count) { |
| 109 | + const verdicts = Array.from({length: count}, () => 'none'); |
| 110 | + let names = []; |
| 111 | + try { |
| 112 | + names = fs.readdirSync(dir); |
| 113 | + } catch { |
| 114 | + // No verdict was uploaded at all. |
| 115 | + } |
| 116 | + for (const name of names) { |
| 117 | + try { |
| 118 | + const {shard, verdict} = JSON.parse( |
| 119 | + fs.readFileSync(path.join(dir, name), 'utf8'), |
| 120 | + ); |
| 121 | + if ( |
| 122 | + Number.isInteger(shard) && |
| 123 | + shard >= 1 && |
| 124 | + shard <= count && |
| 125 | + ['pass', 'fail', 'none'].includes(verdict) |
| 126 | + ) |
| 127 | + verdicts[shard - 1] = verdict; |
| 128 | + } catch { |
| 129 | + // An unreadable file leaves that leg without a verdict. |
| 130 | + } |
| 131 | + } |
| 132 | + return verdicts; |
| 133 | +} |
| 134 | + |
| 135 | +/** Seconds past its expected time a file must also run before it is named. */ |
| 136 | +export const SLOW_MARGIN_S = 10; |
| 137 | + |
| 138 | +/** |
| 139 | + * The files that ran past their minimum-Node record, by the Node legs' own |
| 140 | + * drift rule (node-time-drift.mjs `drifted`): every timed file is judged, the |
| 141 | + * run's own slowdown is taken out, and a file must also run SLOW_MARGIN_S |
| 142 | + * past its expected time. A file the record lacks is not judged. |
| 143 | + * @param {Record<string, number>} times seconds per test file, this run |
| 144 | + * @param {Record<string, number>} recorded seconds per test file, the record |
| 145 | + * @returns {{file: string, recorded: number, measured: number}[]} |
| 146 | + */ |
| 147 | +export function slowFiles(times, recorded) { |
| 148 | + return drifted(times, recorded, [], {all: true, margin: SLOW_MARGIN_S}).map( |
| 149 | + ({file, recorded: before, measured}) => ({ |
| 150 | + file, |
| 151 | + recorded: before, |
| 152 | + measured, |
| 153 | + }), |
| 154 | + ); |
| 155 | +} |
| 156 | + |
| 157 | +/** |
| 158 | + * The record with this run's times in it, by the same rule as the Node legs' |
| 159 | + * record (duration-split.mjs `mergeRecord`): each measured file takes its new |
| 160 | + * time, every other file keeps its own, a swizzle-proof slice keeps its |
| 161 | + * largest time, and a file `exists` no longer finds leaves it. `source` names |
| 162 | + * the run the times came from. |
| 163 | + * @param {{seconds?: Record<string, number>}} record |
| 164 | + * @param {Record<string, number>} times |
| 165 | + * @param {{source?: string, exists?: (file: string) => boolean}} [options] |
| 166 | + */ |
| 167 | +export function refreshRecord( |
| 168 | + record, |
| 169 | + times, |
| 170 | + {source, exists = () => true} = {}, |
| 171 | +) { |
| 172 | + return { |
| 173 | + ...record, |
| 174 | + ...(source |
| 175 | + ? { |
| 176 | + $comment: |
| 177 | + "Seconds per CLI test file on the CLI's minimum Node, which the \"CLI on minimum Node\" workflow packs its legs by (VITEST_DURATIONS_FILE). Refreshed from that workflow's run named in `source`; refresh it from the latest run's min-node-record artifact.", |
| 178 | + source, |
| 179 | + } |
| 180 | + : {}), |
| 181 | + seconds: mergeRecord(record.seconds ?? {}, times, exists), |
| 182 | + }; |
| 183 | +} |
| 184 | + |
| 185 | +/** |
| 186 | + * Every leg's per-file times, as one map, from the files the legs upload: |
| 187 | + * each artifact downloads into its own folder under `dir`. |
| 188 | + * @param {string} dir |
| 189 | + * @returns {Record<string, number>} |
| 190 | + */ |
| 191 | +export function readLegTimes(dir) { |
| 192 | + const times = {}; |
| 193 | + let files = []; |
| 194 | + try { |
| 195 | + files = fs |
| 196 | + .readdirSync(dir, {recursive: true, withFileTypes: true}) |
| 197 | + .filter(entry => entry.isFile() && entry.name.endsWith('.json')) |
| 198 | + .map(entry => path.join(entry.parentPath ?? entry.path, entry.name)); |
| 199 | + } catch { |
| 200 | + // No leg uploaded its times. |
| 201 | + } |
| 202 | + for (const file of files) { |
| 203 | + try { |
| 204 | + Object.assign(times, JSON.parse(fs.readFileSync(file, 'utf8'))); |
| 205 | + } catch { |
| 206 | + // An unreadable file adds no times. |
| 207 | + } |
| 208 | + } |
| 209 | + return times; |
| 210 | +} |
| 211 | + |
| 212 | +/** A job's result as a verdict: only success passes and only failure fails. */ |
| 213 | +const jobVerdict = result => |
| 214 | + result === 'success' ? 'pass' : result === 'failure' ? 'fail' : 'none'; |
| 215 | + |
| 216 | +/** |
| 217 | + * The run's verdict: "fail" when any part failed a check, else "none" when |
| 218 | + * any part judged nothing (stopped, cancelled or skipped), else "pass". |
| 219 | + * @param {{version: string, legs: Array<'pass' | 'fail' | 'none'>, pack: string, smoke: string}} parts |
| 220 | + * @returns {'pass' | 'fail' | 'none'} |
| 221 | + */ |
| 222 | +export function runVerdict({version, legs, pack, smoke}) { |
| 223 | + if (version !== 'success') return 'none'; |
| 224 | + const all = [...legs, jobVerdict(pack), jobVerdict(smoke)]; |
| 225 | + if (all.includes('fail')) return 'fail'; |
| 226 | + return all.includes('none') ? 'none' : 'pass'; |
| 227 | +} |
| 228 | + |
94 | 229 | /** |
95 | 230 | * A failed run opens the issue, or comments on the open one; a passing run |
96 | | - * closes an open issue this job opened. Returns what it did. |
| 231 | + * closes an open issue this job opened; a run with no verdict only notes it |
| 232 | + * on an open issue. Returns what it did. |
97 | 233 | */ |
98 | | -export async function reportIssue({github, context, ok, version, runUrl}) { |
| 234 | +export async function reportIssue({github, context, verdict, version, runUrl}) { |
99 | 235 | const issue = await findOpenIssue(github, context); |
100 | | - if (ok) { |
| 236 | + // No verdict neither closes the issue nor says the CLI failed. |
| 237 | + if (verdict === 'none') { |
| 238 | + if (!issue) return 'none'; |
| 239 | + await github.rest.issues.createComment({ |
| 240 | + ...context.repo, |
| 241 | + issue_number: issue.number, |
| 242 | + body: `No verdict on Node ${version}: part of the run stopped, was cancelled or was skipped before it judged anything ([run](${runUrl})). This issue stays as it is.`, |
| 243 | + }); |
| 244 | + return 'no verdict noted'; |
| 245 | + } |
| 246 | + if (verdict === 'pass') { |
101 | 247 | if (!issue) return 'none'; |
102 | 248 | await github.rest.issues.createComment({ |
103 | 249 | ...context.repo, |
@@ -133,10 +279,36 @@ if ( |
133 | 279 | process.argv[1] && |
134 | 280 | fs.realpathSync(process.argv[1]) === fileURLToPath(import.meta.url) |
135 | 281 | ) { |
136 | | - const [, , command, flag, file] = process.argv; |
| 282 | + const [, , command, flag, file, recordFlag, recordFile] = process.argv; |
| 283 | + if (command === 'slow' && flag === '--times' && recordFlag === '--record') { |
| 284 | + // One leg's files past their minimum-Node record: a warning each, and a |
| 285 | + // line in the step summary. Never a failure. |
| 286 | + let times = {}; |
| 287 | + try { |
| 288 | + times = JSON.parse(fs.readFileSync(file, 'utf8')); |
| 289 | + } catch { |
| 290 | + console.log(`No per-file times at ${file}.`); |
| 291 | + process.exit(0); |
| 292 | + } |
| 293 | + const recorded = |
| 294 | + JSON.parse(fs.readFileSync(recordFile, 'utf8')).seconds ?? {}; |
| 295 | + const slow = slowFiles(times, recorded); |
| 296 | + for (const {file: test, recorded: before, measured} of slow) |
| 297 | + console.log( |
| 298 | + `::warning file=${test},title=Past its minimum-Node time::${test} ran ${measured} s; ${recordFile} records ${before} s.`, |
| 299 | + ); |
| 300 | + if (slow.length && process.env.GITHUB_STEP_SUMMARY) |
| 301 | + fs.appendFileSync( |
| 302 | + process.env.GITHUB_STEP_SUMMARY, |
| 303 | + `### Files past their minimum-Node time\n\n${slow.map(({file: test, recorded: before, measured}) => `- \`${test}\`: ${measured} s (recorded ${before} s)`).join('\n')}\n`, |
| 304 | + ); |
| 305 | + if (!slow.length) |
| 306 | + console.log('Every file ran within its minimum-Node time.'); |
| 307 | + process.exit(0); |
| 308 | + } |
137 | 309 | if (command !== 'minimum' || flag !== '--package' || !file) { |
138 | 310 | console.error( |
139 | | - 'usage: cli-minimum-node.mjs minimum --package <package.json>', |
| 311 | + 'usage: cli-minimum-node.mjs minimum --package <package.json>\n cli-minimum-node.mjs slow --times <file> --record <file>', |
140 | 312 | ); |
141 | 313 | process.exit(2); |
142 | 314 | } |
|
0 commit comments