|
| 1 | +import { |
| 2 | + lstatSync, |
| 3 | + mkdtempSync, |
| 4 | + readFileSync, |
| 5 | + readlinkSync, |
| 6 | + rmSync, |
| 7 | + statSync, |
| 8 | + symlinkSync, |
| 9 | + utimesSync, |
| 10 | + writeFileSync, |
| 11 | +} from "node:fs"; |
| 12 | +import { tmpdir } from "node:os"; |
| 13 | +import { join } from "node:path"; |
| 14 | +import { afterEach, describe, expect, it } from "vitest"; |
| 15 | +import { buildNodeShimScript, ensureNodeShim } from "./node-shim"; |
| 16 | + |
| 17 | +const EXEC_PATH = "/Applications/PostHog Code.app/Contents/MacOS/PostHog Code"; |
| 18 | + |
| 19 | +describe("ensureNodeShim", () => { |
| 20 | + const dirs: string[] = []; |
| 21 | + |
| 22 | + function makeDir(): string { |
| 23 | + const dir = mkdtempSync(join(tmpdir(), "node-shim-test-")); |
| 24 | + dirs.push(dir); |
| 25 | + return join(dir, "shim"); |
| 26 | + } |
| 27 | + |
| 28 | + afterEach(() => { |
| 29 | + while (dirs.length > 0) { |
| 30 | + const dir = dirs.pop(); |
| 31 | + if (dir) rmSync(dir, { recursive: true, force: true }); |
| 32 | + } |
| 33 | + }); |
| 34 | + |
| 35 | + it("writes an executable wrapper that sets ELECTRON_RUN_AS_NODE itself", () => { |
| 36 | + const dir = makeDir(); |
| 37 | + ensureNodeShim(dir, EXEC_PATH, "darwin"); |
| 38 | + |
| 39 | + const shim = join(dir, "node"); |
| 40 | + const content = readFileSync(shim, "utf-8"); |
| 41 | + expect(content).toContain("#!/bin/sh"); |
| 42 | + expect(content).toContain("export ELECTRON_RUN_AS_NODE=1"); |
| 43 | + expect(content).toContain(`exec "${EXEC_PATH}" "$@"`); |
| 44 | + expect(statSync(shim).mode & 0o111).not.toBe(0); |
| 45 | + }); |
| 46 | + |
| 47 | + it("escapes shell-special characters in the binary path", () => { |
| 48 | + expect(buildNodeShimScript('/odd/pa"th/$app`bin\\x')).toContain( |
| 49 | + 'exec "/odd/pa\\"th/\\$app\\`bin\\\\x" "$@"', |
| 50 | + ); |
| 51 | + }); |
| 52 | + |
| 53 | + it("replaces a legacy symlink shim with the wrapper script", () => { |
| 54 | + const dir = makeDir(); |
| 55 | + ensureNodeShim(dir, EXEC_PATH, "darwin"); |
| 56 | + const shim = join(dir, "node"); |
| 57 | + rmSync(shim); |
| 58 | + symlinkSync(EXEC_PATH, shim); |
| 59 | + |
| 60 | + ensureNodeShim(dir, EXEC_PATH, "darwin"); |
| 61 | + |
| 62 | + expect(lstatSync(shim).isSymbolicLink()).toBe(false); |
| 63 | + expect(readFileSync(shim, "utf-8")).toBe(buildNodeShimScript(EXEC_PATH)); |
| 64 | + }); |
| 65 | + |
| 66 | + it("rewrites the wrapper when the binary path changes", () => { |
| 67 | + const dir = makeDir(); |
| 68 | + ensureNodeShim(dir, "/old/location/App", "darwin"); |
| 69 | + |
| 70 | + ensureNodeShim(dir, EXEC_PATH, "darwin"); |
| 71 | + |
| 72 | + expect(readFileSync(join(dir, "node"), "utf-8")).toBe( |
| 73 | + buildNodeShimScript(EXEC_PATH), |
| 74 | + ); |
| 75 | + }); |
| 76 | + |
| 77 | + it("leaves an up-to-date wrapper untouched", () => { |
| 78 | + const dir = makeDir(); |
| 79 | + ensureNodeShim(dir, EXEC_PATH, "darwin"); |
| 80 | + const shim = join(dir, "node"); |
| 81 | + const past = new Date("2020-01-01T00:00:00Z"); |
| 82 | + utimesSync(shim, past, past); |
| 83 | + |
| 84 | + ensureNodeShim(dir, EXEC_PATH, "darwin"); |
| 85 | + |
| 86 | + expect(statSync(shim).mtimeMs).toBe(past.getTime()); |
| 87 | + }); |
| 88 | + |
| 89 | + it("replaces a corrupt shim that is not the expected script", () => { |
| 90 | + const dir = makeDir(); |
| 91 | + ensureNodeShim(dir, EXEC_PATH, "darwin"); |
| 92 | + const shim = join(dir, "node"); |
| 93 | + writeFileSync(shim, "not a shim"); |
| 94 | + |
| 95 | + ensureNodeShim(dir, EXEC_PATH, "darwin"); |
| 96 | + |
| 97 | + expect(readFileSync(shim, "utf-8")).toBe(buildNodeShimScript(EXEC_PATH)); |
| 98 | + }); |
| 99 | + |
| 100 | + it("keeps the symlink behavior on windows and tolerates an existing link", () => { |
| 101 | + const dir = makeDir(); |
| 102 | + ensureNodeShim(dir, EXEC_PATH, "win32"); |
| 103 | + ensureNodeShim(dir, EXEC_PATH, "win32"); |
| 104 | + |
| 105 | + const shim = join(dir, "node"); |
| 106 | + expect(lstatSync(shim).isSymbolicLink()).toBe(true); |
| 107 | + expect(readlinkSync(shim)).toBe(EXEC_PATH); |
| 108 | + }); |
| 109 | +}); |
0 commit comments