repositories / pi-ext
pi-ext
bugabingas pi extensions
owned by admin
extensions/activity/__tests__/windows-executable.test.ts
Rawimport { mkdir, mkdtemp, rm, writeFile } from "node:fs/promises";
import { tmpdir } from "node:os";
import { basename, delimiter, dirname, join, resolve } from "node:path";
import { afterEach, describe, expect, it } from "vitest";
import { resolveWindowsExecutable } from "../windows-executable.ts";
const directories: string[] = [];
afterEach(async () => {
await Promise.all(
directories
.splice(0)
.map((directory) => rm(directory, { force: true, recursive: true })),
);
});
const windowsIt = it.runIf(process.platform === "win32");
describe("resolveWindowsExecutable", () => {
windowsIt(
"finds bare executables in quoted final PATH entry with case-insensitive key",
async () => {
const missing = await mkdtemp(join(tmpdir(), "activity-missing-"));
directories.push(missing);
await expect(
resolveWindowsExecutable("node", process.cwd(), {
pAtH: `${missing}${delimiter}"${dirname(process.execPath)}"`,
}),
).resolves.toBe(resolve(process.execPath));
},
);
windowsIt(
"resolves explicit absolute and relative native executables",
async () => {
const cwd = dirname(process.execPath);
await expect(
resolveWindowsExecutable(process.execPath, cwd, {}),
).resolves.toBe(resolve(process.execPath));
await expect(
resolveWindowsExecutable(`.\\${basename(process.execPath)}`, cwd, {}),
).resolves.toBe(resolve(process.execPath));
await expect(
resolveWindowsExecutable(
`${cwd.slice(0, 2)}.\\${basename(process.execPath)}`,
cwd,
{},
),
).resolves.toBe(resolve(process.execPath));
},
);
windowsIt(
"resolves relative PATH entries, dotted names, and COM files to absolute paths",
async () => {
const cwd = await mkdtemp(join(tmpdir(), "activity path spaces-"));
directories.push(cwd);
await mkdir(join(cwd, "tools"));
await writeFile(join(cwd, "tools", "tool.name.exe"), "fixture");
await writeFile(join(cwd, "tools", "other.com"), "fixture");
await mkdir(join(cwd, "tools", "directory.exe"));
for (const [command, expected] of [
["tool.name", "tool.name.exe"],
["other", "other.com"],
]) {
await expect(
resolveWindowsExecutable(command, cwd, { Path: '"tools"' }),
).resolves.toBe(join(cwd, "tools", expected));
}
await expect(
resolveWindowsExecutable("directory", cwd, { PATH: "tools" }),
).rejects.toThrow("executable was not found");
},
);
windowsIt("rejects missing executable and cwd before spawning", async () => {
await expect(
resolveWindowsExecutable("activity-does-not-exist", process.cwd(), {}),
).rejects.toThrow("executable was not found");
await expect(
resolveWindowsExecutable("node", join(tmpdir(), "activity-no-cwd"), {}),
).rejects.toThrow("cwd does not exist");
});
windowsIt(
"rejects cmd and bat instead of implicitly starting a shell",
async () => {
await expect(
resolveWindowsExecutable("script.cmd", process.cwd(), {}),
).rejects.toThrow("cmd.exe is not started implicitly");
await expect(
resolveWindowsExecutable("script.bat", process.cwd(), {}),
).rejects.toThrow("cmd.exe is not started implicitly");
},
);
});