executable-resolution.test.ts 1.1 KB

12345678910111213141516171819202122232425262728293031323334
  1. import {
  2. chmodSync,
  3. mkdirSync,
  4. mkdtempSync,
  5. rmSync,
  6. writeFileSync,
  7. } from "node:fs";
  8. import { tmpdir } from "node:os";
  9. import path from "node:path";
  10. import { expect, it } from "vitest";
  11. import { whichSync } from "../third_party/sandbox-runtime/src/utils/which.js";
  12. it("finds executable files without starting an external command", () => {
  13. const dir = mkdtempSync(path.join(tmpdir(), "mcode-executable-"));
  14. try {
  15. const name = process.platform === "win32" ? "fixture.EXE" : "fixture";
  16. const executable = path.join(dir, name);
  17. writeFileSync(executable, "fixture");
  18. chmodSync(executable, 0o700);
  19. expect(whichSync(name, dir)).toBe(executable);
  20. expect(whichSync(executable, "")).toBe(executable);
  21. mkdirSync(path.join(dir, "directory"));
  22. expect(whichSync("directory", dir)).toBeNull();
  23. expect(whichSync("missing", dir)).toBeNull();
  24. expect(whichSync("", dir)).toBeNull();
  25. expect(whichSync("bad\0name", dir)).toBeNull();
  26. if (process.platform !== "win32") {
  27. chmodSync(executable, 0o600);
  28. expect(whichSync(name, dir)).toBeNull();
  29. }
  30. } finally {
  31. rmSync(dir, { recursive: true, force: true });
  32. }
  33. });