|
| 1 | +import { spawnSync } from 'node:child_process' |
| 2 | +import { |
| 3 | + copyFileSync, |
| 4 | + mkdirSync, |
| 5 | + mkdtempSync, |
| 6 | + readFileSync, |
| 7 | + realpathSync, |
| 8 | + rmSync, |
| 9 | + writeFileSync, |
| 10 | +} from 'node:fs' |
| 11 | +import { tmpdir } from 'node:os' |
| 12 | +import { dirname, join } from 'node:path' |
| 13 | +import { afterEach, describe, expect, it } from 'vitest' |
| 14 | +import { RELEASE_TRAIN_MANIFESTS } from '../../languages/typescript/packages/cli/src/release-train.ts' |
| 15 | +import { |
| 16 | + releaseTrain, |
| 17 | + stableVersion, |
| 18 | + syncPins, |
| 19 | + syncSkillPins, |
| 20 | + trainVersions, |
| 21 | +} from '../sync-skill-pins.mjs' |
| 22 | +import { REPO_ROOT } from './lib/repo-root.mjs' |
| 23 | + |
| 24 | +/** |
| 25 | + * The version step moves the skill pins, so a Version Packages PR cannot leave |
| 26 | + * them behind. #1020 did, because no CI ran on it, and `main` then failed |
| 27 | + * `release-train.test.ts` until #1029 moved them by hand. |
| 28 | + */ |
| 29 | + |
| 30 | +const CONFIG = JSON.parse( |
| 31 | + readFileSync(join(REPO_ROOT, '.changeset/config.json'), 'utf8'), |
| 32 | +) |
| 33 | +const VERSIONS = new Map([ |
| 34 | + ['stash', '1.2.1'], |
| 35 | + ['@cipherstash/stack', '1.2.1'], |
| 36 | +]) |
| 37 | + |
| 38 | +describe('the release train', () => { |
| 39 | + it('is the fixed group that holds stash, and the CLI release train', () => { |
| 40 | + // Two lists of one set: changesets moves these together, and |
| 41 | + // release-train.test.ts checks their pins. |
| 42 | + expect(releaseTrain(CONFIG).sort()).toEqual( |
| 43 | + Object.keys(RELEASE_TRAIN_MANIFESTS).sort(), |
| 44 | + ) |
| 45 | + }) |
| 46 | + |
| 47 | + it('refuses a config with no group holding stash', () => { |
| 48 | + expect(() => releaseTrain({ fixed: [['@cipherstash/auth']] })).toThrow( |
| 49 | + /holds `stash`/, |
| 50 | + ) |
| 51 | + }) |
| 52 | + |
| 53 | + it('reads each version from the manifest the CLI embeds', () => { |
| 54 | + const versions = trainVersions(REPO_ROOT, releaseTrain(CONFIG)) |
| 55 | + for (const [name, rel] of Object.entries(RELEASE_TRAIN_MANIFESTS)) { |
| 56 | + const manifest = JSON.parse( |
| 57 | + readFileSync( |
| 58 | + join(REPO_ROOT, 'languages/typescript/packages/cli', rel), |
| 59 | + 'utf8', |
| 60 | + ), |
| 61 | + ) |
| 62 | + expect(versions.get(name), name).toBe(manifest.version) |
| 63 | + } |
| 64 | + }) |
| 65 | + |
| 66 | + it('refuses a train package with no manifest', () => { |
| 67 | + expect(() => |
| 68 | + trainVersions(REPO_ROOT, ['stash', '@cipherstash/no-such-package']), |
| 69 | + ).toThrow(/@cipherstash\/no-such-package/) |
| 70 | + }) |
| 71 | +}) |
| 72 | + |
| 73 | +describe('the pins', () => { |
| 74 | + it('move the three forms the skills use', () => { |
| 75 | + const before = [ |
| 76 | + "npx --package=stash@1.2.0 stash eql install --database-url 'postgres://...'", |
| 77 | + "} from 'npm:@cipherstash/stack@1.2.0/wasm-inline'", |
| 78 | + '"@cipherstash/stack/wasm-inline": "npm:@cipherstash/stack@1.2.0/wasm-inline"', |
| 79 | + ].join('\n') |
| 80 | + const { text, changes } = syncPins(before, VERSIONS) |
| 81 | + expect(text).toBe( |
| 82 | + [ |
| 83 | + "npx --package=stash@1.2.1 stash eql install --database-url 'postgres://...'", |
| 84 | + "} from 'npm:@cipherstash/stack@1.2.1/wasm-inline'", |
| 85 | + '"@cipherstash/stack/wasm-inline": "npm:@cipherstash/stack@1.2.1/wasm-inline"', |
| 86 | + ].join('\n'), |
| 87 | + ) |
| 88 | + expect(changes).toHaveLength(3) |
| 89 | + }) |
| 90 | + |
| 91 | + it('pin the stable version during a prerelease', () => { |
| 92 | + expect(stableVersion('1.3.0-rc.0')).toBe('1.3.0') |
| 93 | + const { text } = syncPins( |
| 94 | + 'npx --package=stash@1.2.1 stash', |
| 95 | + new Map([['stash', '1.3.0-rc.0']]), |
| 96 | + ) |
| 97 | + expect(text).toBe('npx --package=stash@1.3.0 stash') |
| 98 | + }) |
| 99 | + |
| 100 | + it('leave everything that is not a release-train pin alone', () => { |
| 101 | + const text = [ |
| 102 | + '@cipherstash/eql@3.0.6', |
| 103 | + '@cipherstash/protect-ffi@0.33.0', |
| 104 | + '"stash": "^1.0.0"', |
| 105 | + 'cipherstash@1.0.0', |
| 106 | + 'mystash@1.0.0', |
| 107 | + 'stash@latest', |
| 108 | + ].join('\n') |
| 109 | + expect(syncPins(text, VERSIONS)).toEqual({ text, changes: [] }) |
| 110 | + }) |
| 111 | + |
| 112 | + it('change nothing that is already current', () => { |
| 113 | + const text = 'npx --package=stash@1.2.1 stash eql install' |
| 114 | + expect(syncPins(text, VERSIONS)).toEqual({ text, changes: [] }) |
| 115 | + }) |
| 116 | + |
| 117 | + it('in this tree already name the release-train versions', () => { |
| 118 | + // What the next Version Packages PR starts from. A failure here names the |
| 119 | + // file: run `node scripts/sync-skill-pins.mjs`. |
| 120 | + expect(syncSkillPins({ root: REPO_ROOT, write: false })).toEqual([]) |
| 121 | + }) |
| 122 | +}) |
| 123 | + |
| 124 | +/** The real script over a throwaway tree with one stale skill. */ |
| 125 | +describe('the script', () => { |
| 126 | + let root |
| 127 | + afterEach(() => root && rmSync(root, { recursive: true, force: true })) |
| 128 | + |
| 129 | + function fixture() { |
| 130 | + root = realpathSync(mkdtempSync(join(tmpdir(), 'skill-pins-'))) |
| 131 | + const write = (rel, text) => { |
| 132 | + mkdirSync(dirname(join(root, rel)), { recursive: true }) |
| 133 | + writeFileSync(join(root, rel), text) |
| 134 | + } |
| 135 | + write( |
| 136 | + '.changeset/config.json', |
| 137 | + JSON.stringify({ fixed: [['stash', '@cipherstash/stack']] }), |
| 138 | + ) |
| 139 | + write( |
| 140 | + 'languages/typescript/packages/cli/package.json', |
| 141 | + JSON.stringify({ name: 'stash', version: '9.9.9' }), |
| 142 | + ) |
| 143 | + write( |
| 144 | + 'languages/typescript/packages/stack/package.json', |
| 145 | + JSON.stringify({ name: '@cipherstash/stack', version: '9.9.9' }), |
| 146 | + ) |
| 147 | + write( |
| 148 | + 'skills/stash-cli/SKILL.md', |
| 149 | + 'npx --package=stash@9.9.8 stash eql install\n', |
| 150 | + ) |
| 151 | + write('skills/other/SKILL.md', 'nothing pinned here\n') |
| 152 | + mkdirSync(join(root, 'scripts')) |
| 153 | + copyFileSync( |
| 154 | + join(REPO_ROOT, 'scripts/sync-skill-pins.mjs'), |
| 155 | + join(root, 'scripts/sync-skill-pins.mjs'), |
| 156 | + ) |
| 157 | + return () => |
| 158 | + spawnSync(process.execPath, [join(root, 'scripts/sync-skill-pins.mjs')], { |
| 159 | + cwd: root, |
| 160 | + encoding: 'utf8', |
| 161 | + }) |
| 162 | + } |
| 163 | + |
| 164 | + it('moves a stale pin, and a second run changes nothing', () => { |
| 165 | + const run = fixture() |
| 166 | + const first = run() |
| 167 | + expect(first.status).toBe(0) |
| 168 | + expect(first.stdout).toContain( |
| 169 | + 'skills/stash-cli/SKILL.md: stash@9.9.8 -> stash@9.9.9', |
| 170 | + ) |
| 171 | + expect(readFileSync(join(root, 'skills/stash-cli/SKILL.md'), 'utf8')).toBe( |
| 172 | + 'npx --package=stash@9.9.9 stash eql install\n', |
| 173 | + ) |
| 174 | + |
| 175 | + const second = run() |
| 176 | + expect(second.status).toBe(0) |
| 177 | + expect(second.stdout).toContain('already name the release-train versions') |
| 178 | + }) |
| 179 | +}) |
| 180 | + |
| 181 | +describe('the root version script moves the pins', () => { |
| 182 | + const version = JSON.parse( |
| 183 | + readFileSync(join(REPO_ROOT, 'package.json'), 'utf8'), |
| 184 | + ).scripts.version |
| 185 | + |
| 186 | + it('runs the pin sync after `changeset version`, which sets the versions', () => { |
| 187 | + expect(version).toContain('node scripts/sync-skill-pins.mjs') |
| 188 | + expect(version.indexOf('changeset version')).toBeLessThan( |
| 189 | + version.indexOf('sync-skill-pins.mjs'), |
| 190 | + ) |
| 191 | + // `&&`, so a failed sync stops the release rather than shipping stale pins. |
| 192 | + expect(version).toMatch( |
| 193 | + /changeset version && .*node scripts\/sync-skill-pins\.mjs &&/, |
| 194 | + ) |
| 195 | + }) |
| 196 | +}) |
0 commit comments