Files
VoiceStudio/tests/frontend/bunCommand.test.mjs
Palash Debnath bc6d564cfc fix(dev): spawn the real Bun so npm-installed Bun works on Windows (#4603)
* fix(dev): spawn the real Bun so npm-installed Bun works on Windows

bunCommand() prefers the running Bun, then npm_execpath, then the executable behind a Windows npm shim on PATH. A missing Bun gives an install hint from every script that spawns it, and a test guards against bare bun/bunx spawns. Builds on the report and first fix in #4525.

* Add changelog fragment for the Bun spawn fix
2026-12-02 20:25:13 +00:00

109 lines
4.4 KiB
JavaScript

import assert from "node:assert/strict";
import { readFileSync, readdirSync } from "node:fs";
import { join } from "node:path";
import test from "node:test";
import { fileURLToPath } from "node:url";
import { BUN_MISSING_MESSAGE, bunCommand, explainBunError } from "../../electron/scripts/bun-command.mjs";
import { prepareDev } from "../../scripts/prepare-dev.mjs";
const npmBun = "C:\\Users\\me\\AppData\\Roaming\\npm\\node_modules\\bun\\bin\\bun.exe";
const prefix = "C:\\Users\\me\\AppData\\Roaming\\npm";
const files = (...paths) => (file) => paths.includes(file);
const onWindows = (env, ...existing) =>
bunCommand({ env, versions: {}, platform: "win32", exists: files(...existing) });
test("scripts started by bun run spawn that Bun, not the PATH shim", () => {
assert.equal(bunCommand({ env: { npm_execpath: npmBun }, versions: {} }), npmBun);
assert.equal(
bunCommand({ env: { npm_execpath: "/home/me/.bun/bin/bun" }, versions: {} }),
"/home/me/.bun/bin/bun",
);
});
test("a script running inside Bun spawns its own executable", () => {
assert.equal(bunCommand({ env: {}, versions: { bun: "1.4.2" }, execPath: npmBun }), npmBun);
});
test("direct Node on Windows finds the executable behind the npm shim", () => {
const env = { Path: `C:\\Windows;${prefix}` };
assert.equal(onWindows(env, `${prefix}\\bun.cmd`, npmBun), npmBun);
});
test("a project-local npm shim resolves inside that project's node_modules", () => {
const modules = "C:\\work\\app\\node_modules";
const env = { PATH: `${modules}\\.bin;${prefix}` };
const local = `${modules}\\bun\\bin\\bun.exe`;
assert.equal(onWindows(env, `${modules}\\.bin\\bun.cmd`, local, `${prefix}\\bun.cmd`, npmBun), local);
});
test("PATH order wins, a native bun.exe beats a later shim, and a shim without its package is skipped", () => {
const native = "D:\\tools\\bun.exe";
assert.equal(onWindows({ Path: `D:\\tools;${prefix}` }, native, `${prefix}\\bun.cmd`, npmBun), native);
assert.equal(onWindows({ Path: `${prefix};D:\\tools` }, `${prefix}\\bun.cmd`, native), native);
assert.equal(onWindows({ Path: prefix }, `${prefix}\\bun.cmd`), "bun");
});
test("quoted and relative PATH entries are handled", () => {
assert.equal(onWindows({ Path: `.;"${prefix}"` }, `${prefix}\\bun.cmd`, npmBun), npmBun);
});
test("Windows lookup is skipped elsewhere, and other runners fall back to PATH", () => {
assert.equal(bunCommand({ env: { PATH: "/usr/bin" }, versions: {}, platform: "linux", exists: () => true }), "bun");
for (const npm_execpath of [
undefined,
"",
"C:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js",
"/usr/lib/node_modules/pnpm/bin/pnpm.cjs",
"C:\\bun\\bunx.exe",
]) {
assert.equal(onWindows({ npm_execpath }), "bun");
}
});
test("dev setup runs the Bun that started it", () => {
const calls = [];
prepareDev({
env: { npm_execpath: npmBun },
platform: "linux",
arch: "x64",
run(command, args) {
calls.push([command, ...args]);
return { status: 0 };
},
});
assert.deepEqual(calls, [
[npmBun, "install"],
[npmBun, "run", "setup:api"],
]);
});
test("a missing Bun explains how to install it and keeps the cause", () => {
const error = Object.assign(new Error("spawnSync bun ENOENT"), { code: "ENOENT" });
assert.throws(
() => prepareDev({ env: {}, platform: "linux", arch: "x64", run: () => ({ error }) }),
(thrown) => thrown.message === BUN_MISSING_MESSAGE && thrown.cause === error,
);
const other = Object.assign(new Error("denied"), { code: "EACCES" });
assert.equal(explainBunError(other), other);
});
test("no script spawns a bare bun or bunx", () => {
const root = fileURLToPath(new URL("../../", import.meta.url));
// First argument of any exec/spawn/run-style call, however it is wrapped.
const bare = /\b\w*(?:spawn|exec|run)\w*\(\s*[`'"]bunx?[`'"]/i;
const offenders = [];
const walk = (dir) => {
for (const entry of readdirSync(join(root, dir), { withFileTypes: true })) {
const path = join(dir, entry.name);
if (entry.isDirectory()) {
if (entry.name !== "node_modules") walk(path);
} else if (/\.(?:mjs|cjs|js|ts)$/.test(entry.name) && !/\.test\./.test(entry.name)) {
if (bare.test(readFileSync(join(root, path), "utf8"))) offenders.push(path);
}
}
};
for (const dir of ["scripts", "electron/scripts"]) walk(dir);
assert.deepEqual(offenders, [], "spawn bunCommand() from electron/scripts/bun-command.mjs");
});