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