skillhub/cli/test/helpers/run-cli.ts
dongmucat 43a201c8d5 fix(cli): use fileURLToPath for cross-platform cwd path compatibility
The issue was that new URL().pathname returns Unix-style paths on Windows
(e.g., /D:/a/skillhub/...) which Bun.spawn() cannot resolve correctly.
Using fileURLToPath() converts the URL to a proper platform-specific path.

Changes:
- cli/test/helpers/run-cli.ts: use fileURLToPath for cwd
- cli/test/integration/version-command.test.ts: same fix for build spawn
2026-04-29 16:13:50 +08:00

22 lines
786 B
TypeScript

import { existsSync } from 'node:fs'
import { fileURLToPath } from 'node:url'
export async function runCli(args: string[], env: Record<string, string> = {}) {
// Use Bun.which() to find bun in PATH, but verify it exists
const whichBun = await Bun.which('bun')
const bunPath = (whichBun && existsSync(whichBun)) ? whichBun : process.execPath
const proc = Bun.spawn({
cmd: [bunPath, 'src/index.ts', ...args],
cwd: fileURLToPath(new URL('../../', import.meta.url)),
env: { ...process.env, ...env },
stdout: 'pipe',
stderr: 'pipe'
})
const [stdout, stderr, exitCode] = await Promise.all([
new Response(proc.stdout).text(),
new Response(proc.stderr).text(),
proc.exited
])
return { stdout: stdout.trim(), stderr: stderr.trim(), exitCode }
}