Skip to content

Commit 6d2d975

Browse files
committed
test(native): move pinned-tool snapshots to a new version without a native build
The Coana help snapshots show the pinned Coana version, so every Coana bump needs them refreshed. `pnpm snapshots:update:dep coana` needs a full native build, which Coana's release automation can't run. `scripts/test/swap-snapshot-version.mts --dep coana --from <old>` edits the version text instead. It keeps each row's width, so the rendered pixels match a real capture. It edits nothing if a snapshot lacks the old version or a row has no room, and then points at snapshots:update:dep. /bump-coana now runs it as a step.
1 parent b5c19e4 commit 6d2d975

5 files changed

Lines changed: 236 additions & 15 deletions

File tree

‎.claude/skills/bump-coana/SKILL.md‎

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,19 @@ advance.
5555
**Values to extract**:
5656
- `CURRENT_VERSION`: The old Coana CLI version (for PR body)
5757

58-
### Step 3: Update CHANGELOG.md
58+
### Step 3: Update the native help snapshots
59+
60+
Some native help snapshots show the pinned Coana version, and native CI fails
61+
until they match the pin. Move them to the new version:
62+
63+
```bash
64+
node scripts/test/swap-snapshot-version.mts --dep coana --from "$CURRENT_VERSION"
65+
```
66+
67+
If it reports that a snapshot doesn't show `CURRENT_VERSION` or a row has no
68+
room, run `pnpm snapshots:update:dep coana` instead.
69+
70+
### Step 4: Update CHANGELOG.md
5971

6072
Do not hand-edit the file. Run the helper, which owns the shape:
6173

@@ -93,20 +105,20 @@ if it finds one; hand-editing is what puts one there.)
93105
- Updated the Coana CLI to v `COANA_VERSION`.
94106
```
95107

96-
### Step 4: Create Branch and Commit
108+
### Step 5: Create Branch and Commit
97109

98110
```bash
99111
# Create branch
100112
git checkout -b "coana-$COANA_VERSION"
101113

102114
# Stage changes
103-
git add src/constants.mts CHANGELOG.md
115+
git add src/constants.mts CHANGELOG.md test/native-snapshots
104116

105117
# Commit (skip pre-commit hooks with -n)
106118
git commit -n -m "upgrading coana to version $COANA_VERSION"
107119
```
108120

109-
### Step 5: Push and Create PR
121+
### Step 6: Push and Create PR
110122

111123
```bash
112124
# Push branch
@@ -130,7 +142,8 @@ Replace `CURRENT_VERSION` and `COANA_VERSION` with actual values.
130142
- Branch: `coana-<VERSION>` pushed to origin
131143
- PR: Created targeting `v1.x` branch
132144
- Files modified: `src/constants.mts` (the `COANA_CLI_VERSION` line only),
133-
`CHANGELOG.md` (under `## [Unreleased]`)
145+
`CHANGELOG.md` (under `## [Unreleased]`), and the Coana help snapshots in
146+
`test/native-snapshots/`
134147

135148
Report the PR URL to the user when complete.
136149

‎.gitignore‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
!/scripts/test/terminal-report.mts
4343
!/scripts/test/snapshots.mts
4444
!/scripts/test/snapshot-dependencies.mts
45+
!/scripts/test/swap-snapshot-version.mts
4546
!/scripts/utils/
4647
!/shadow-bin/
4748
!/src/
@@ -253,6 +254,7 @@
253254
!/test/native-snapshots/**
254255
!/test/unit/scripts/test/snapshots.test.mts
255256
!/test/unit/scripts/test/snapshot-dependencies.test.mts
257+
!/test/unit/scripts/test/swap-snapshot-version.test.mts
256258
!/test/package-matrix.test.mts
257259
!/test/fixtures/
258260
!/test/fixtures/commands/

‎scripts/test/snapshot-dependencies.mts‎

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,19 @@ export interface StaleSnapshotDependency {
5555
readonly snapshots: readonly string[]
5656
}
5757

58+
export async function readPinnedVersion(
59+
dependency: SnapshotDependency,
60+
): Promise<string> {
61+
const source = await fs.readFile(path.join(ROOT, dependency.source), 'utf8')
62+
const version = dependency.versionPattern.exec(source)?.[1]
63+
if (!version) {
64+
throw new Error(
65+
`Could not read ${dependency.packageName} version from ${dependency.source}`,
66+
)
67+
}
68+
return version
69+
}
70+
5871
export async function findStaleSnapshotDependencies(): Promise<
5972
StaleSnapshotDependency[]
6073
> {
@@ -63,16 +76,7 @@ export async function findStaleSnapshotDependencies(): Promise<
6376
string,
6477
SnapshotDependency,
6578
]>).map(async ([name, dependency]) => {
66-
const source = await fs.readFile(
67-
path.join(ROOT, dependency.source),
68-
'utf8',
69-
)
70-
const version = dependency.versionPattern.exec(source)?.[1]
71-
if (!version) {
72-
throw new Error(
73-
`Could not read ${dependency.packageName} version from ${dependency.source}`,
74-
)
75-
}
79+
const version = await readPinnedVersion(dependency)
7680
const versionSnapshots = dependency.versionSnapshots ?? dependency.snapshots
7781
const snapshots = await Promise.all(
7882
versionSnapshots.map(async snapshot => ({
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
#!/usr/bin/env node
2+
/**
3+
* @file Move a pinned tool's native snapshots from its old version to the
4+
* pinned one by editing the version text, for automation that bumps the pin
5+
* but cannot build the native CLI.
6+
*
7+
* Usage:
8+
* node scripts/test/swap-snapshot-version.mts --dep coana --from 15.15.0
9+
*/
10+
11+
import { promises as fs } from 'node:fs'
12+
import path from 'node:path'
13+
import process from 'node:process'
14+
import { parseArgs } from 'node:util'
15+
16+
import {
17+
SNAPSHOT_DEPENDENCIES,
18+
findStaleSnapshotDependencies,
19+
normalizeSnapshotDependencyName,
20+
readPinnedVersion,
21+
snapshotDependencyUpdateHint,
22+
} from './snapshot-dependencies.mts'
23+
import { isMainModule } from '../lib/is-main-module.mts'
24+
import { runMain } from '../lib/run-main.mts'
25+
26+
import type { SnapshotDependency } from './snapshot-dependencies.mts'
27+
import type { ScriptMeta } from '../lib/run-main.mts'
28+
29+
const ROOT = path.resolve(import.meta.dirname, '../..')
30+
31+
export interface SwapArgs {
32+
readonly dependencyName: string
33+
readonly from: string
34+
}
35+
36+
export function parseSwapArgs(argv: readonly string[]): SwapArgs {
37+
const { values } = parseArgs({
38+
args: [...argv],
39+
options: { dep: { type: 'string' }, from: { type: 'string' } },
40+
strict: true,
41+
})
42+
if (!values.dep || !values.from) {
43+
throw new Error('Pass both --dep <name> and --from <old version>')
44+
}
45+
return { dependencyName: values.dep, from: values.from }
46+
}
47+
48+
function escapeRegExp(text: string): string {
49+
return text.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
50+
}
51+
52+
/**
53+
* Replace `from` with `to` in a terminal snapshot, taking or giving the
54+
* difference from the spaces that pad the row, so every row keeps its column
55+
* count and the rendered pixels match a real capture.
56+
*/
57+
export function swapVersionInSnapshot(
58+
contents: string,
59+
from: string,
60+
to: string,
61+
): string {
62+
const pattern = new RegExp(
63+
`(?<![\\w.])${escapeRegExp(from)}(?!\\w|\\.\\d)(\\S*)( *)`,
64+
'g',
65+
)
66+
let found = false
67+
const swapped = contents.replace(
68+
pattern,
69+
(_match, tail: string, padding: string) => {
70+
found = true
71+
const width = from.length + padding.length
72+
if (to.length > width) {
73+
throw new Error(`No room in the row to replace ${from} with ${to}`)
74+
}
75+
return `${to}${tail}${' '.repeat(width - to.length)}`
76+
},
77+
)
78+
if (!found) {
79+
throw new Error(`The snapshot does not mention ${from}`)
80+
}
81+
return swapped
82+
}
83+
84+
export async function main(): Promise<void> {
85+
const { dependencyName, from } = parseSwapArgs(process.argv.slice(2))
86+
const name = normalizeSnapshotDependencyName(dependencyName)
87+
if (!name) {
88+
throw new Error(`Unknown snapshot dependency: ${dependencyName}`)
89+
}
90+
const dependency: SnapshotDependency = SNAPSHOT_DEPENDENCIES[name]
91+
const to = await readPinnedVersion(dependency)
92+
const snapshots = dependency.versionSnapshots ?? dependency.snapshots
93+
// Swap every file in memory first, so a failure leaves none of them edited.
94+
const swapped = await Promise.all(
95+
snapshots.map(async snapshot => {
96+
const file = path.join(ROOT, snapshot)
97+
try {
98+
const contents = await fs.readFile(file, 'utf8')
99+
return { file, contents: swapVersionInSnapshot(contents, from, to) }
100+
} catch (e) {
101+
throw new Error(`${snapshot}: ${(e as Error).message}`)
102+
}
103+
}),
104+
)
105+
await Promise.all(
106+
swapped.map(({ contents, file }) => fs.writeFile(file, contents)),
107+
)
108+
const stale = (await findStaleSnapshotDependencies()).filter(
109+
finding => normalizeSnapshotDependencyName(finding.dependencyName) === name,
110+
)
111+
if (stale.length) {
112+
throw new Error(snapshotDependencyUpdateHint(stale))
113+
}
114+
process.stdout.write(
115+
`Moved ${snapshots.length} ${dependency.packageName} snapshots from ${from} to ${to}\n`,
116+
)
117+
}
118+
119+
const SCRIPT_META: ScriptMeta = {
120+
describe:
121+
"moves a pinned tool's native snapshots to its pinned version without a native build",
122+
help: `Usage: node scripts/test/swap-snapshot-version.mts --dep <name> --from <old version>
123+
124+
--dep coana, socket-patch or patch
125+
--from the version the snapshots show now
126+
127+
Run it after bumping the pin. It fails without editing anything when a
128+
snapshot does not show the old version or a row has no room for the new one.
129+
Fall back to pnpm snapshots:update:dep <name> in that case.`,
130+
}
131+
132+
if (isMainModule(import.meta.url)) {
133+
runMain(main, SCRIPT_META)
134+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import { readFileSync } from 'node:fs'
2+
import path from 'node:path'
3+
4+
import { describe, expect, it } from 'vitest'
5+
6+
import {
7+
SNAPSHOT_DEPENDENCIES,
8+
readPinnedVersion,
9+
} from '../../../../scripts/test/snapshot-dependencies.mts'
10+
import {
11+
parseSwapArgs,
12+
swapVersionInSnapshot,
13+
} from '../../../../scripts/test/swap-snapshot-version.mts'
14+
15+
const ROW = '<tspan> Default: 15.15.0. </tspan>'
16+
17+
describe('swapVersionInSnapshot', () => {
18+
it('keeps the row width when the version grows or shrinks', () => {
19+
expect(swapVersionInSnapshot(ROW, '15.15.0', '15.15.10')).toBe(
20+
'<tspan> Default: 15.15.10. </tspan>',
21+
)
22+
expect(swapVersionInSnapshot(ROW, '15.15.0', '9.0.0')).toBe(
23+
'<tspan> Default: 9.0.0. </tspan>',
24+
)
25+
})
26+
27+
it('leaves longer versions that start with the old one alone', () => {
28+
expect(() =>
29+
swapVersionInSnapshot('Default: 15.15.01. ', '15.15.0', '15.16.0'),
30+
).toThrow('does not mention 15.15.0')
31+
expect(() =>
32+
swapVersionInSnapshot('Default: 15.15.0.1 ', '15.15.0', '15.16.0'),
33+
).toThrow('does not mention 15.15.0')
34+
})
35+
36+
it('refuses a version too long for the row', () => {
37+
expect(() =>
38+
swapVersionInSnapshot('Default: 1.0.0.</tspan>', '1.0.0', '10.0.0'),
39+
).toThrow('No room in the row')
40+
})
41+
42+
it('round-trips the checked-in Coana snapshots', async () => {
43+
const pinned = await readPinnedVersion(SNAPSHOT_DEPENDENCIES.coana)
44+
for (const snapshot of SNAPSHOT_DEPENDENCIES.coana.snapshots) {
45+
const contents = readFileSync(path.join(process.cwd(), snapshot), 'utf8')
46+
const moved = swapVersionInSnapshot(contents, pinned, '99.99.999')
47+
expect(moved).toHaveLength(contents.length)
48+
expect(moved).not.toContain(pinned)
49+
expect(swapVersionInSnapshot(moved, '99.99.999', pinned)).toBe(contents)
50+
}
51+
})
52+
})
53+
54+
describe('parseSwapArgs', () => {
55+
it('reads --dep and --from', () => {
56+
expect(parseSwapArgs(['--dep', 'coana', '--from', '15.15.0'])).toEqual({
57+
dependencyName: 'coana',
58+
from: '15.15.0',
59+
})
60+
})
61+
62+
it('needs both flags', () => {
63+
expect(() => parseSwapArgs(['--dep', 'coana'])).toThrow('Pass both')
64+
expect(() => parseSwapArgs(['--to', '1.0.0'])).toThrow(
65+
"Unknown option '--to'",
66+
)
67+
})
68+
})

0 commit comments

Comments
 (0)