|
| 1 | +/** |
| 2 | + * Bundle launcher names (issue #326). |
| 3 | + * |
| 4 | + * Agent MCP configs and Dysflow launch `codegraph-vba`, so a GitHub-only |
| 5 | + * install (no npm) must put that name on PATH next to `codegraph`. These tests |
| 6 | + * extract the REAL launcher block from `scripts/build-bundle.sh` and the REAL |
| 7 | + * link block from `install.sh` (between their marker comments) and run them |
| 8 | + * against temp fixtures — no build, no download. |
| 9 | + * |
| 10 | + * POSIX only: both blocks are shell, and the install.sh block uses symlinks. |
| 11 | + */ |
| 12 | + |
| 13 | +import { describe, it, expect, beforeEach, afterEach } from 'vitest'; |
| 14 | +import { spawnSync } from 'child_process'; |
| 15 | +import * as fs from 'fs'; |
| 16 | +import * as os from 'os'; |
| 17 | +import * as path from 'path'; |
| 18 | + |
| 19 | +const ROOT = path.join(__dirname, '..'); |
| 20 | + |
| 21 | +function extractBlock(file: string, marker: string): string { |
| 22 | + const lines = fs.readFileSync(path.join(ROOT, file), 'utf8').replace(/\r\n/g, '\n').split('\n'); |
| 23 | + const i = lines.findIndex((l) => l.trim() === `# >>> ${marker}`); |
| 24 | + const j = lines.findIndex((l) => l.trim() === `# <<< ${marker}`); |
| 25 | + if (i < 0 || j < 0 || j <= i) throw new Error(`${marker} markers not found in ${file}`); |
| 26 | + return lines.slice(i + 1, j).join('\n'); |
| 27 | +} |
| 28 | + |
| 29 | +function shq(s: string): string { |
| 30 | + return `'${s.replace(/'/g, `'\\''`)}'`; |
| 31 | +} |
| 32 | + |
| 33 | +function run(shell: string, script: string): { code: number; stdout: string; stderr: string } { |
| 34 | + const r = spawnSync(shell, ['-c', script], { encoding: 'utf8' }); |
| 35 | + return { code: r.status ?? -1, stdout: r.stdout ?? '', stderr: r.stderr ?? '' }; |
| 36 | +} |
| 37 | + |
| 38 | +describe.skipIf(process.platform === 'win32')('bundle launchers (#326)', () => { |
| 39 | + let tmp: string; |
| 40 | + |
| 41 | + beforeEach(() => { |
| 42 | + tmp = fs.mkdtempSync(path.join(os.tmpdir(), 'cg-launchers-')); |
| 43 | + }); |
| 44 | + afterEach(() => { |
| 45 | + fs.rmSync(tmp, { recursive: true, force: true }); |
| 46 | + }); |
| 47 | + |
| 48 | + function stageBundle(osfam: string): string { |
| 49 | + const stage = path.join(tmp, 'stage'); |
| 50 | + fs.mkdirSync(path.join(stage, 'bin'), { recursive: true }); |
| 51 | + // Stand-in for the vendored Node: prints the args it was exec'd with. |
| 52 | + const fakeNode = path.join(tmp, 'fake-node'); |
| 53 | + fs.writeFileSync(fakeNode, '#!/bin/sh\necho "node $*"\n', { mode: 0o755 }); |
| 54 | + const script = |
| 55 | + `set -eu\nOSFAM=${shq(osfam)}\nSTAGE=${shq(stage)}\nNODE_BIN=${shq(fakeNode)}\n` + |
| 56 | + extractBlock('scripts/build-bundle.sh', 'CODEGRAPH_LAUNCHERS'); |
| 57 | + const r = run('bash', script); |
| 58 | + expect(r.code, r.stderr).toBe(0); |
| 59 | + return stage; |
| 60 | + } |
| 61 | + |
| 62 | + it('POSIX bundles ship an executable codegraph-vba launcher that runs the CLI', () => { |
| 63 | + const stage = stageBundle('linux'); |
| 64 | + const launcher = path.join(stage, 'bin', 'codegraph-vba'); |
| 65 | + expect(fs.statSync(launcher).mode & 0o111).not.toBe(0); |
| 66 | + |
| 67 | + const r = spawnSync(launcher, ['--version'], { encoding: 'utf8' }); |
| 68 | + expect(r.status).toBe(0); |
| 69 | + expect(r.stdout).toContain(`${path.join(stage, 'lib/dist/bin/codegraph.js')} --version`); |
| 70 | + }); |
| 71 | + |
| 72 | + it('Windows bundles ship codegraph-vba.cmd identical to codegraph.cmd', () => { |
| 73 | + const stage = stageBundle('win32'); |
| 74 | + const cmd = fs.readFileSync(path.join(stage, 'bin', 'codegraph.cmd'), 'utf8'); |
| 75 | + expect(fs.readFileSync(path.join(stage, 'bin', 'codegraph-vba.cmd'), 'utf8')).toBe(cmd); |
| 76 | + }); |
| 77 | + |
| 78 | + function linkLaunchers(dest: string): { binDir: string; stdout: string } { |
| 79 | + const binDir = path.join(tmp, 'bin'); |
| 80 | + const script = |
| 81 | + `set -eu\nBIN_DIR=${shq(binDir)}\ndest=${shq(dest)}\n` + |
| 82 | + extractBlock('install.sh', 'CODEGRAPH_LINK_LAUNCHERS'); |
| 83 | + const r = run('sh', script); |
| 84 | + expect(r.code, r.stderr).toBe(0); |
| 85 | + return { binDir, stdout: r.stdout }; |
| 86 | + } |
| 87 | + |
| 88 | + it('install.sh links both launchers onto PATH', () => { |
| 89 | + const dest = stageBundle('linux'); |
| 90 | + const { binDir } = linkLaunchers(dest); |
| 91 | + expect(fs.readlinkSync(path.join(binDir, 'codegraph'))).toBe(path.join(dest, 'bin', 'codegraph')); |
| 92 | + expect(fs.readlinkSync(path.join(binDir, 'codegraph-vba'))).toBe(path.join(dest, 'bin', 'codegraph-vba')); |
| 93 | + }); |
| 94 | + |
| 95 | + it('install.sh leaves no dangling codegraph-vba link for an older bundle that lacks it', () => { |
| 96 | + const dest = path.join(tmp, 'old-bundle'); |
| 97 | + fs.mkdirSync(path.join(dest, 'bin'), { recursive: true }); |
| 98 | + fs.writeFileSync(path.join(dest, 'bin', 'codegraph'), '#!/bin/sh\n', { mode: 0o755 }); |
| 99 | + const { binDir } = linkLaunchers(dest); |
| 100 | + expect(fs.existsSync(path.join(binDir, 'codegraph'))).toBe(true); |
| 101 | + expect(fs.existsSync(path.join(binDir, 'codegraph-vba'))).toBe(false); |
| 102 | + }); |
| 103 | +}); |
0 commit comments