|
| 1 | +/** |
| 2 | + * The Squad launcher (`.squad/squad.mjs`, and the plugin's `scripts/squad.mjs`, built from the |
| 3 | + * same template) passes argv to the CLI byte for byte, on every platform (#2469). |
| 4 | + * |
| 5 | + * On Windows, npm installs `squad` and `npx` as `.cmd` shims. Running a shim means going through |
| 6 | + * cmd.exe, which rewrites `&`, `|`, `%VAR%`, `^` and quotes. The launcher must instead run the |
| 7 | + * shim's JavaScript entry with Node, with no shell. These tests put fake shims on PATH that would |
| 8 | + * print `SHELL-USED` if cmd.exe ever ran them, and check every argument arrives unchanged. |
| 9 | + * |
| 10 | + * CI runs this file on Windows and Linux (`.github/workflows/squad-launcher-platforms.yml`). |
| 11 | + */ |
| 12 | +import { afterEach, beforeEach, describe, expect, it } from 'vitest'; |
| 13 | +import fs from 'node:fs'; |
| 14 | +import os from 'node:os'; |
| 15 | +import path from 'node:path'; |
| 16 | +import { spawnSync } from 'node:child_process'; |
| 17 | +import { squadRepoLauncher } from '@bradygaster/squad-sdk/client'; |
| 18 | + |
| 19 | +const win = process.platform === 'win32'; |
| 20 | +const PKG = ['@bradygaster', 'squad-cli']; |
| 21 | + |
| 22 | +/** Arguments cmd.exe or a shell would rewrite, split or expand. */ |
| 23 | +const HOSTILE = [ |
| 24 | + 'state-mcp', |
| 25 | + 'two words', |
| 26 | + 'quote"inside', |
| 27 | + "single'quote", |
| 28 | + '&echo pwned', |
| 29 | + 'pipe|more', |
| 30 | + '%PATH%', |
| 31 | + '^caret', |
| 32 | + '!bang!', |
| 33 | + '<in>out', |
| 34 | + '$HOME', |
| 35 | + '`tick`', |
| 36 | + '', |
| 37 | + 'trailing\\', |
| 38 | + 'back\\"slash', |
| 39 | + 'ünïcødé ✓', |
| 40 | +]; |
| 41 | + |
| 42 | +/** A script that prints its argv as JSON. */ |
| 43 | +const ECHO = "process.stdout.write(JSON.stringify(process.argv.slice(2)));\n"; |
| 44 | + |
| 45 | +let root: string; |
| 46 | +let bin: string; |
| 47 | +beforeEach(() => { |
| 48 | + root = fs.mkdtempSync(path.join(os.tmpdir(), 'squad-launcher-argv-')); |
| 49 | + bin = path.join(root, 'fake bin'); // a space in the directory, as in "Program Files" |
| 50 | + fs.mkdirSync(bin, { recursive: true }); |
| 51 | + fs.mkdirSync(path.join(root, '.squad'), { recursive: true }); |
| 52 | + fs.writeFileSync(path.join(root, '.squad', 'squad.mjs'), squadRepoLauncher('2.0.0', '.squad')); |
| 53 | +}); |
| 54 | +afterEach(() => { |
| 55 | + fs.rmSync(root, { recursive: true, force: true }); |
| 56 | +}); |
| 57 | + |
| 58 | +function write(file: string, body: string, mode?: number): void { |
| 59 | + fs.mkdirSync(path.dirname(file), { recursive: true }); |
| 60 | + fs.writeFileSync(file, body); |
| 61 | + if (mode !== undefined) fs.chmodSync(file, mode); |
| 62 | +} |
| 63 | + |
| 64 | +/** A shim that must never run: under cmd.exe it prints SHELL-USED. */ |
| 65 | +function cmdShim(name: string): void { |
| 66 | + write(path.join(bin, `${name}.cmd`), '@echo SHELL-USED %*\r\n'); |
| 67 | +} |
| 68 | + |
| 69 | +function run(args: string[], env: Record<string, string> = {}) { |
| 70 | + const keep = ['SystemRoot', 'SYSTEMROOT', 'PATHEXT', 'TEMP', 'TMP', 'windir']; |
| 71 | + const base = Object.fromEntries(keep.filter((k) => process.env[k]).map((k) => [k, process.env[k] as string])); |
| 72 | + return spawnSync(process.execPath, [path.join(root, '.squad', 'squad.mjs'), ...args], { |
| 73 | + cwd: os.tmpdir(), |
| 74 | + encoding: 'utf8', |
| 75 | + env: { |
| 76 | + ...base, |
| 77 | + // The fake bin first, then Node's own directory; no real squad or npx. |
| 78 | + PATH: [bin, path.dirname(process.execPath)].join(path.delimiter), |
| 79 | + SQUAD_HOME: path.join(root, 'squad-home'), |
| 80 | + ...env, |
| 81 | + }, |
| 82 | + }); |
| 83 | +} |
| 84 | + |
| 85 | +function parsed(out: ReturnType<typeof run>): string[] { |
| 86 | + expect(out.stderr).toBe(''); |
| 87 | + expect(out.stdout).not.toContain('SHELL-USED'); |
| 88 | + expect(out.status).toBe(0); |
| 89 | + return JSON.parse(out.stdout); |
| 90 | +} |
| 91 | + |
| 92 | +describe('the launcher passes argv through unchanged', () => { |
| 93 | + it('to a squad CLI installed on PATH', () => { |
| 94 | + if (win) { |
| 95 | + cmdShim('squad'); |
| 96 | + const pkg = path.join(bin, 'node_modules', ...PKG); |
| 97 | + write(path.join(pkg, 'package.json'), JSON.stringify({ name: PKG.join('/'), version: '2.0.0' })); |
| 98 | + write(path.join(pkg, 'dist', 'cli-entry.js'), ECHO); |
| 99 | + } else { |
| 100 | + write(path.join(bin, 'squad'), `#!/usr/bin/env node\n${ECHO}`, 0o755); |
| 101 | + } |
| 102 | + expect(parsed(run(HOSTILE))).toEqual(HOSTILE); |
| 103 | + }); |
| 104 | + |
| 105 | + it('to npx when SQUAD_ALLOW_NPX=1', () => { |
| 106 | + if (win) { |
| 107 | + cmdShim('npx'); |
| 108 | + write(path.join(bin, 'node_modules', 'npm', 'bin', 'npx-cli.js'), ECHO); |
| 109 | + } else { |
| 110 | + write(path.join(bin, 'npx'), `#!/usr/bin/env node\n${ECHO}`, 0o755); |
| 111 | + } |
| 112 | + expect(parsed(run(HOSTILE, { SQUAD_ALLOW_NPX: '1' }))).toEqual(['-y', `${PKG.join('/')}@2.0.0`, ...HOSTILE]); |
| 113 | + }); |
| 114 | + |
| 115 | + it('to SQUAD_CLI given as a script', () => { |
| 116 | + const entry = path.join(root, 'cli dir', 'cli-entry.js'); |
| 117 | + write(entry, ECHO); |
| 118 | + expect(parsed(run(HOSTILE, { SQUAD_CLI: entry }))).toEqual(HOSTILE); |
| 119 | + }); |
| 120 | + |
| 121 | + it('to the project\'s own install', () => { |
| 122 | + const pkg = path.join(root, 'node_modules', ...PKG); |
| 123 | + write(path.join(pkg, 'package.json'), JSON.stringify({ name: PKG.join('/'), version: '2.0.0' })); |
| 124 | + write(path.join(pkg, 'dist', 'cli-entry.js'), ECHO); |
| 125 | + expect(parsed(run(HOSTILE))).toEqual(HOSTILE); |
| 126 | + }); |
| 127 | + |
| 128 | + it.runIf(win)('fails closed when a .cmd shim has no JavaScript entry beside it', () => { |
| 129 | + cmdShim('squad'); |
| 130 | + const out = run(['state-mcp']); |
| 131 | + expect(out.status).toBe(1); |
| 132 | + expect(out.stdout).not.toContain('SHELL-USED'); |
| 133 | + expect(out.stderr).toContain('set SQUAD_CLI to cli-entry.js'); |
| 134 | + }); |
| 135 | +}); |
0 commit comments