import { spawnSync } from 'node:child_process'; import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs'; import { tmpdir } from 'node:os'; import { join } from 'node:path'; let root; const manifestPath = () => join(root, 'packages/plugin-godot/package.json'); const writeManifest = (fields) => writeFileSync( manifestPath(), `${JSON.stringify({ name: '@stim-cli/plugin-godot', version: '1.2.0', ...fields, stimPlugin: { kinds: { project: 1 } } }, null, 2)}\n`, ); const prep = (...args) => spawnSync(process.execPath, [join(import.meta.dirname, 'plugin-release-prep.mjs'), ...args], { encoding: 'utf8', env: { ...process.env, PLUGIN_RELEASE_PREP_ROOT: root }, }); beforeEach(() => { root = mkdtempSync(join(tmpdir(), 'stim-plugin-release-prep-')); mkdirSync(join(root, 'packages/plugin-godot'), { recursive: true }); writeManifest({}); }); afterEach(() => rmSync(root, { recursive: true, force: true })); test('a bump rewrites only the version line of the named plugin', () => { const before = readFileSync(manifestPath(), 'utf8'); const result = prep('godot', '1.3.0'); expect(result.status).toBe(0); expect(readFileSync(manifestPath(), 'utf8')).toBe(before.replace('"version": "1.2.0"', '"version": "1.3.0"')); }); test.each([ ['a version that is not after the current one', ['godot', '1.2.0'], /does not come after 1\.2\.0/], ['a prerelease, which would land on latest', ['godot', '1.3.0-rc.1'], /no prerelease suffix/], ['a plugin with no package', ['exact', '1.0.0'], /packages\/plugin-exact\/package\.json does not exist/], ])('a bump refuses %s and leaves the manifest unchanged', (_, args, message) => { const before = readFileSync(manifestPath(), 'utf8'); const result = prep(...args); expect(result.status).toBe(1); expect(result.stderr).toMatch(message); expect(readFileSync(manifestPath(), 'utf8')).toBe(before); }); test.each([ ['a private package', { private: true }, /is private/], ['a package named for another folder', { name: '@stim-cli/plugin-godot-4' }, /not "@stim-cli\/plugin-godot"/], ])('bump and tag both refuse %s', (_, fields, message) => { writeManifest(fields); for (const args of [ ['godot', '1.3.0'], ['--tag', 'plugin-godot-v1.2.0'], ]) { const result = prep(...args); expect(result.status).toBe(1); expect(result.stderr).toMatch(message); } }); test('a tag that matches its manifest prints the package for the release job', () => { const result = prep('--tag', 'plugin-godot-v1.2.0', '--latest', '1.1.0'); expect(result.status).toBe(0); expect(result.stdout).toBe('dir=packages/plugin-godot\nname=@stim-cli/plugin-godot\nversion=1.2.0\n'); }); test.each([ [ 'a version the manifest does not carry', ['--tag', 'plugin-godot-v1.3.0'], /carries 1\.2\.0, but the tag is 1\.3\.0/, ], ['a prerelease tag', ['--tag', 'plugin-godot-v1.2.0-rc.1'], /not a plugin tag/], [ 'a latest that is not a release version', ['--tag', 'plugin-godot-v1.2.0', '--latest', '2.0.0-rc.1'], /--latest takes/, ], ['a version below latest', ['--tag', 'plugin-godot-v1.2.0', '--latest', '1.10.0'], /would move latest back/], ])('a tag release refuses %s', (_, args, message) => { const result = prep(...args); expect(result.status).toBe(1); expect(result.stderr).toMatch(message); expect(result.stdout).toBe(''); });