main
1// scripts/ply-main-check.sh and its wiring in .github/workflows/ply-main.yml. Each case runs the real script in a
2// scratch package whose test/lint/typecheck scripts are stubs, with a fake `npm` first on PATH: `npm install` writes
3// the lockfile entry npm would (resolved `...ply.git#<full sha>`) or fails on demand; every other npm command is the
4// real npm. Expected values are the exit-code contract in the script header (0 pass, 1 gate/install failure, 2 usage)
5// and shas chosen here, never output of the script under test. Offline: nothing is fetched.
6import { test } from 'node:test';
7import assert from 'node:assert/strict';
8import { spawnSync, execFileSync } from 'node:child_process';
9import { mkdtempSync, writeFileSync, readFileSync, existsSync, chmodSync, mkdirSync } from 'node:fs';
10import { tmpdir } from 'node:os';
11import { join, dirname, resolve } from 'node:path';
12import { fileURLToPath } from 'node:url';
14const root = resolve(dirname(fileURLToPath(import.meta.url)), '..');
15const script = join(root, 'scripts/ply-main-check.sh');
16const realNpm = execFileSync('sh', ['-c', 'command -v npm'], { encoding: 'utf8' }).trim();
17const PREV = '1111111111111111111111111111111111111111';
18const FULL = 'abcdef0123456789abcdef0123456789abcdef01';
20function run(sha, { failGate = '', failInstall = false, failInstallFor = '', resolveTo = '', noOut = false, gateLines = null } = {}) {
21 const dir = mkdtempSync(join(tmpdir(), 'ply-main-check-'));
22 const bin = join(dir, 'bin');
23 mkdirSync(bin);
24 const gate = name => (failGate === name && gateLines
25 ? `node -e ${JSON.stringify(`${gateLines.map(l => `console.log(${JSON.stringify(l)});`).join(' ')} process.exit(1)`)}`
26 : failGate === name
27 ? `node -e "console.log('✔ fine (1ms)'); console.log('✖ ${name} breaks (2ms)'); console.log('ℹ fail 1'); console.log('✖ failing tests:'); console.log('✖ ${name} breaks (2ms)'); process.exit(1)"`
28 : `node -e "console.log('✔ ${name} ok')"`);
29 writeFileSync(join(dir, 'package.json'), JSON.stringify({
30 name: 'fixture', private: true, dependencies: { ply: `github:ivarvong/ply#${PREV}` },
31 scripts: { test: gate('test'), lint: gate('lint'), typecheck: gate('typecheck') },
32 }, null, 2));
33 // Fake npm install: a short sha resolves to FULL when FULL starts with it (what the git ref lookup does).
34 writeFileSync(join(bin, 'npm'), `#!/bin/sh
35if [ "$1" = install ]; then
36 [ -z "$FAKE_INSTALL_FAIL" ] || { echo "npm error prepare: tsc exited 2" >&2; exit 1; }
37 if [ -n "$FAKE_INSTALL_FAIL_SHA" ] && [ "$(node -p 'require("./package.json").dependencies.ply.split("#")[1]')" = "$FAKE_INSTALL_FAIL_SHA" ]; then
38 echo "npm error prepare: tsc exited 2 (full-sha reinstall)" >&2; exit 1
39 fi
40 exec node -e '
41 const fs = require("fs"); const sha = require("./package.json").dependencies.ply.split("#")[1];
42 const full = process.env.FAKE_RESOLVE || (process.env.FAKE_FULL.startsWith(sha) ? process.env.FAKE_FULL : sha);
43 fs.writeFileSync("package-lock.json", JSON.stringify({ packages: { "node_modules/ply": { resolved: "git+ssh://git@github.com/ivarvong/ply.git#" + full } } }));'
44fi
45exec "${realNpm}" "$@"
46`);
47 chmodSync(join(bin, 'npm'), 0o755);
48 const out = join(dir, 'out');
49 const ghOut = join(dir, 'github-output');
50 writeFileSync(ghOut, '');
51 const r = spawnSync('sh', [script, sha, ...(noOut ? [] : [out])], {
52 cwd: dir, encoding: 'utf8',
53 env: { ...process.env, PATH: `${bin}:${process.env.PATH}`, FAKE_FULL: FULL, FAKE_INSTALL_FAIL: failInstall ? '1' : '', FAKE_INSTALL_FAIL_SHA: failInstallFor, FAKE_RESOLVE: resolveTo, GITHUB_OUTPUT: ghOut },
54 });
55 const read = f => (existsSync(join(out, f)) ? readFileSync(join(out, f), 'utf8') : null);
56 const summary = read('summary.env');
57 return {
58 code: r.status, stderr: r.stderr, stdout: r.stdout, report: read('report.md'), logs: ['test', 'lint', 'typecheck'].filter(g => read(`${g}.log`) !== null),
59 env: summary && Object.fromEntries(summary.trim().split('\n').map(l => l.split('='))),
60 ghOutput: readFileSync(ghOut, 'utf8'),
61 pin: JSON.parse(readFileSync(join(dir, 'package.json'), 'utf8')).dependencies.ply,
62 };
63}
65test('every gate passes: exit 0, OUTCOME=pass, outcome=pass for the workflow', () => {
66 const r = run(FULL);
67 assert.equal(r.code, 0, r.stderr);
68 assert.equal(r.env.OUTCOME, 'pass');
69 assert.equal(r.env.PLY_PREV, PREV);
70 assert.equal(r.ghOutput, 'outcome=pass\n');
71});
73for (const g of ['test', 'lint', 'typecheck']) {
74 test(`npm run ${g} fails: exit 1, OUTCOME=fail, report names it with the compare range`, () => {
75 const r = run(FULL, { failGate: g });
76 assert.equal(r.code, 1, r.stderr);
77 assert.equal(r.env.OUTCOME, 'fail');
78 assert.equal(r.env.PLY_SHORT, 'abcdef0');
79 assert.equal(r.ghOutput, 'outcome=fail\n');
80 assert.match(r.report, new RegExp(`^Failed gates: ${g}$`, 'm'));
81 assert.ok(r.report.includes(`https://github.com/ivarvong/ply/compare/${PREV}...${FULL}`));
82 if (g === 'test') {
83 assert.match(r.report, /^Failing tests:\n✖ test breaks \(2ms\)\n/m);
84 assert.ok(!/^Failing tests:\n✔/m.test(r.report));
85 // node:test's summary header `✖ failing tests:` is not a test name; the list holds only the test.
86 const list = r.report.slice(r.report.indexOf('Failing tests:\n')).split('\n\n')[0];
87 assert.equal(list, 'Failing tests:\n✖ test breaks (2ms)');
88 }
89 });
90}
92test('npm install of a valid sha fails: exit 1 and a report, so the workflow opens the issue', () => {
93 const r = run(FULL, { failInstall: true });
94 assert.equal(r.code, 1, r.stderr);
95 assert.equal(r.env.OUTCOME, 'fail');
96 assert.equal(r.env.PLY_SHORT, 'abcdef0');
97 assert.equal(r.ghOutput, 'outcome=fail\n');
98 assert.match(r.report, /^Failed gates: install$/m);
99 assert.match(r.report, /^### npm install$/m);
100 assert.ok(r.report.includes('prepare: tsc exited 2'));
101 // No gate runs after a failed install: no gate log, and the stub gates never printed.
102 assert.deepEqual(r.logs, []);
103 assert.ok(!/npm run (test|lint|typecheck) exit/.test(r.stdout), r.stdout);
104});
106test('short sha resolves to FULL but the re-install of FULL fails: exit 1, install failure, no gate ran', () => {
107 // The first install (short pin) succeeds and resolves to FULL; the second (full pin, written back for vite.config.ts)
108 // fails. The gates must not run on whatever the first install left in node_modules, and the run must not pass.
109 const r = run(FULL.slice(0, 7), { failInstallFor: FULL });
110 assert.equal(r.code, 1, r.stderr);
111 assert.equal(r.env.OUTCOME, 'fail');
112 assert.equal(r.env.PLY_SHA, FULL);
113 assert.equal(r.ghOutput, 'outcome=fail\n');
114 assert.match(r.report, /^Failed gates: install$/m);
115 assert.ok(r.report.includes('full-sha reinstall'), r.report);
116 assert.ok(r.stderr.includes(`npm install of ply#${FULL} failed`), r.stderr);
117 assert.deepEqual(r.logs, []);
118 assert.ok(!/npm run (test|lint|typecheck) exit/.test(r.stdout), r.stdout);
119});
121test('failing tests list: vitest FAIL / × lines and TAP not ok lines are listed, passing lines are not', () => {
122 // Lines as a real failing vitest test prints them (hand-checked: `FAIL test/flags/zz.test.ts > zz deliberately
123 // fails`, `× zz deliberately fails 5ms`), plus TAP's `not ok N - name` (node:test --test-reporter=tap).
124 const lines = [
125 ' ✓ test/flags/ok.test.ts > fine 1ms',
126 ' FAIL test/flags/zz.test.ts > zz deliberately fails',
127 ' × zz deliberately fails 5ms',
128 'ok 1 - tap passes',
129 'not ok 2 - tap breaks',
130 ];
131 const r = run(FULL, { failGate: 'test', gateLines: lines });
132 assert.equal(r.code, 1, r.stderr);
133 const list = r.report.slice(r.report.indexOf('Failing tests:\n')).split('\n\n')[0].split('\n').slice(1);
134 // Expected: exactly the three failure lines, leading whitespace stripped (order is `sort -u`'s, not asserted).
135 assert.deepEqual(new Set(list), new Set([
136 'FAIL test/flags/zz.test.ts > zz deliberately fails',
137 '× zz deliberately fails 5ms',
138 'not ok 2 - tap breaks',
139 ]));
140 assert.equal(list.length, 3);
141});
143test('lockfile resolves ply to a different sha than asked: usage error, exit 2, no report, no gate ran', () => {
144 const other = '2222222222222222222222222222222222222222';
145 const r = run(FULL, { resolveTo: other });
146 assert.equal(r.code, 2, r.stderr);
147 assert.ok(r.stderr.includes(`lockfile resolved ply to '${other}', not ${FULL}`), r.stderr);
148 assert.equal(r.report, null);
149 assert.equal(r.ghOutput, '');
150 assert.deepEqual(r.logs, []);
151});
153test('no out-dir: usage error, exit 2, the pin is untouched and nothing is installed', () => {
154 const r = run(FULL, { noOut: true });
155 assert.equal(r.code, 2, r.stderr);
156 assert.match(r.stderr, /^usage: sh scripts\/ply-main-check\.sh <ply-sha> <out-dir>$/m);
157 assert.equal(r.pin, `github:ivarvong/ply#${PREV}`);
158 assert.equal(r.ghOutput, '');
159});
161test('a short sha resolves to the full one before the gates run', () => {
162 const r = run(FULL.slice(0, 7));
163 assert.equal(r.code, 0, r.stderr);
164 assert.equal(r.env.PLY_SHA, FULL);
165 assert.equal(r.pin, `github:ivarvong/ply#${FULL}`);
166});
168test('sha bounds: 7 and 40 lowercase hex are accepted', () => {
169 assert.equal(run(FULL.slice(0, 7)).code, 0);
170 assert.equal(run(FULL).code, 0);
171});
173for (const [name, sha] of [['6 hex', FULL.slice(0, 6)], ['41 hex', `${FULL}a`], ['uppercase', FULL.toUpperCase()], ['empty', ''], ['non-hex', 'abcdefg']]) {
174 test(`sha ${name}: usage error, exit 2, no report, no workflow outcome`, () => {
175 const r = run(sha);
176 assert.equal(r.code, 2);
177 assert.equal(r.report, null);
178 assert.equal(r.ghOutput, '');
179 });
180}
182test('workflow: the check step runs the script unmapped and the issue step keys on outcome == fail', () => {
183 const wf = readFileSync(join(root, '.github/workflows/ply-main.yml'), 'utf8');
184 const step = wf.slice(wf.indexOf('id: check'));
185 assert.match(step, /^\s+run: sh scripts\/ply-main-check\.sh "\$\{PLY_SHA\}" "\$RUNNER_TEMP\/ply-main"$/m);
186 assert.match(wf, /^\s+if: failure\(\) && steps\.check\.outputs\.outcome == 'fail'$/m);
187});
189test('workflow: concurrency is keyed by the ply sha, so a queued sha is never cancelled by a newer one', () => {
190 // GitHub keeps one pending run per concurrency group and cancels the older pending one when another queues, so a
191 // single fixed group skips the middle sha of three quick dispatches. One group per sha only dedupes the same sha.
192 const wf = readFileSync(join(root, '.github/workflows/ply-main.yml'), 'utf8');
193 const groups = [...wf.matchAll(/^\s+group: (.*)$/gm)].map(m => m[1]);
194 assert.deepEqual(groups, ['ply-main-consumer-check-${{ github.event.client_payload.sha || inputs.sha }}']);
195});
Diagnostics4 git calls · 370 ms
CallStatusSizeTime0370 ms
discover200152 B2 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=2 git.response.bytes=152 git.server.agent=gitty/1.0 git.server.object_format=sha1 git.server.caps=agent ls-refs fetch object-format git.server.fetch=shallow filter sideband-all git.protocol.version=2 git.pkt.frames=8 git.pkt.data=6 git.pkt.flush=2 git.pkt.delim=0 git.pkt.bytes=152 git.pkt.payload.max=34
ls-refs200145 B · hit2 ms
git.upload-pack ls-refs http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=2 git.response.bytes=145 git.response.cache=HIT git.response.duration_ms=2 git.protocol.version=2 git.pkt.frames=3 git.pkt.data=2 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=145 git.pkt.payload.max=76 git.ls_refs.count=2 git.ls_refs.head=refs/heads/main
fetchblob:none · deepen 1020040 KiB · hit7 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=3 git.response.bytes=41349 git.response.cache=HIT git.response.duration_ms=7 git.fetch.filter=blob:none git.fetch.filter.requested=tree:0 git.fetch.filter.fallback=blob:none git.fetch.filter.learned=false git.fetch.wants=526b305930b8ef382f2d7a1f9c0238730a585921 git.fetch.haves.count=0 git.fetch.deepen=10 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=6 git.pkt.data=4 git.pkt.flush=1 git.pkt.delim=1 git.pkt.bytes=41349 git.pkt.payload.max=41254 git.sideband.band1.bytes=41253 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=61 git.pack.bytes=41253 git.pack.ofs_delta=0 git.pack.ref_delta=0 git.pack.chain.max=0 git.pack.trailer=ab60efef4b7243678c75de64a4386986ee885c1c git.pack.full=61Open pack map
fetch2003.7 KiB361 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=361 git.response.bytes=3823 git.response.cache=MISS git.response.duration_ms=361 git.fetch.wants=8b182912ced21717bf885882f0a29beea263bcdc git.fetch.haves.count=0 git.fetch.deepen=0 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=3 git.pkt.data=2 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=3823 git.pkt.payload.max=3802 git.sideband.band1.bytes=3801 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0
Each row is one HTTP round trip ply made to the git server for this page, on one timeline. discover asks what the server speaks, ls-refs lists branch tips, fetch brings objects by id. A hit was served by Workers Cache. Tap a row for its wire details.
- Remote
- https://5ad2236b39de4c8bd8f6270ea63f3ea9.artifacts.cloudflare.net/git/public-v0/ply-web.git
- Ref
- refs/heads/main
- Commit
- 526b305930b8ef382f2d7a1f9c0238730a585921
- cf-ray
- a5e36818e89f1de6
- Request
- 65891799…
- Protocol
- v2 · sha1
- Agent
- gitty/1.0
- Filter
- advertised
- Fetch
- shallow filter sideband-all
- Capabilities
- agent ls-refs fetch object-format