Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions src/pi-channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ export function resolvePiBinary(
): string {
const override = env.BILI_PI_BIN?.trim();
if (override && override.length > 0) return override;
// Probe both binary names: `pi` is the canonical UX name (what users and
// distros link), but a stock `npm install -g pi-stable` only lays down
// `pi-stable`(.cmd) — the npm package's declared bin name. Probing just
// `pi` misses every stock npm install.
const probeNames = ["pi", "pi-stable"];
const sep = platform === "win32" ? ";" : ":";
const extensions = platform === "win32"
? (env.PATHEXT ?? ".COM;.EXE;.BAT;.CMD").split(";").map((e) => e.trim().toLowerCase()).filter((e) => e.length > 0)
Expand All @@ -69,10 +74,12 @@ export function resolvePiBinary(
: [nodeDir, "/usr/local/bin", "/opt/homebrew/bin", "/opt/local/bin", home ? home + "/.local/bin" : ""];
for (const dir of [...(env.PATH ?? "").split(sep), ...extraDirs]) {
if (!dir) continue;
for (const ext of extensions) {
const name = ext === "" ? "pi" : `pi${ext}`;
const candidate = dir.endsWith("/") || dir.endsWith("\\") ? dir + name : dir + "/" + name;
if (existsImpl(candidate)) return candidate;
for (const name of probeNames) {
for (const ext of extensions) {
const fileName = ext === "" ? name : `${name}${ext}`;
const candidate = dir.endsWith("/") || dir.endsWith("\\") ? dir + fileName : dir + "/" + fileName;
if (existsImpl(candidate)) return candidate;
}
}
}
return "pi";
Expand Down
11 changes: 11 additions & 0 deletions tests/e2e/e2e-registry.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,4 +207,15 @@ test("hermetic npm -g install e2e (real npm client, real global layout)", { skip
: spawnSync(shim, ["--version"], { encoding: "utf8", timeout: 60_000, env: biliSpawnEnv(envBase) });
const shimVersion = (probe.stdout ?? "").trim();
assert.match(shimVersion, new RegExp(`^v?${escapeRe(NEW_VERSION)}$`), `bili shim must report the flipped version, got: ${JSON.stringify(shimVersion)}`);

// No-op re-run: already sitting at the registry's latest — must exit
// clean, say so, and touch nothing on disk (no re-download, no churn).
const pkgJson = path.join(installDir, "package.json");
const mtimeBefore = fs.statSync(pkgJson).mtimeMs;
const noop = runBili(installDir, ["update"], { ...envBase, BILI_UPDATE_REGISTRY: reg.url });
assert.equal(noop.code, 0, `no-op update failed:\n${noop.stderr}`);
assert.match(noop.stderr, /\(up to date\)/, "no-op update must log the up-to-date line");
assert.doesNotMatch(noop.stderr, /Restart to finish/, "no-op update must not claim an install happened");
assert.equal(await readPkgVersion(installDir), NEW_VERSION);
assert.equal(fs.statSync(pkgJson).mtimeMs, mtimeBefore, "no-op update must not touch the installed tree");
});
34 changes: 33 additions & 1 deletion tests/update-pi-self-refresh.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,45 @@ const root = fs.realpathSync(fs.mkdtempSync(path.join(os.tmpdir(), "bili-pi-self
process.env.XDG_CACHE_HOME = path.join(root, "cache");

const { refreshPiNpmCopy } = await import("../src/update.ts");
const { isPiNpmCopy, piNpmEntrySpec, _setPiRunnersForTest } = await import("../src/pi-channel.ts");
const { isPiNpmCopy, piNpmEntrySpec, resolvePiBinary, _setPiRunnersForTest } = await import("../src/pi-channel.ts");

after(() => {
delete process.env.XDG_CACHE_HOME;
rmrf(root);
});

test("resolvePiBinary: finds a stock npm install's pi-stable(.cmd), prefers canonical pi, honors BILI_PI_BIN", () => {
const binDir = fs.mkdtempSync(path.join(root, "bindir-"));
const norm = (p: string): string => p.split(path.sep).join("/");

// Stock `npm install -g pi-stable` on Windows lays down pi-stable.cmd in
// %APPDATA%/npm — and NOT a `pi` shim. Probing only `pi` misses it.
const appdata = fs.mkdtempSync(path.join(root, "appdata-"));
const npmDir = path.join(appdata, "npm");
fs.mkdirSync(npmDir);
fs.writeFileSync(path.join(npmDir, "pi-stable.cmd"), "@echo off\r\n");
const win = resolvePiBinary({ PATH: binDir, APPDATA: appdata, PATHEXT: ".COM;.EXE;.BAT;.CMD" }, "win32", fs.existsSync, "C:\\nodejs\\node.exe");
assert.equal(norm(win), norm(path.join(npmDir, "pi-stable.cmd")));

// Canonical `pi` wins when both exist in one dir: `pi` is probed ahead of
// `pi-stable`. Synthetic dir + stubbed exists keeps this host-independent —
// a real temp path carries a drive-letter ':' on Windows, which the linux
// separator below would shred into non-matching fragments.
const pref = resolvePiBinary(
{ PATH: "probedir" },
"linux",
(c) => c === "probedir/pi" || c === "probedir/pi-stable",
process.execPath,
);
assert.equal(pref, "probedir/pi");

// The override short-circuits everything.
assert.equal(resolvePiBinary({ PATH: binDir, BILI_PI_BIN: "/opt/weird/pi-nightly" }, "linux", fs.existsSync, process.execPath), "/opt/weird/pi-nightly");

// Nothing found → the bare name (spawn ENOENT is handled by the caller).
assert.equal(resolvePiBinary({ PATH: binDir }, "linux", () => false, process.execPath), "pi");
});

// — isPiNpmCopy classification ————————————————————————————————————

test("isPiNpmCopy: pi npm layouts yes, everything else no", () => {
Expand Down
Loading