|
| 1 | +import { test } from "node:test"; |
| 2 | +import assert from "node:assert/strict"; |
| 3 | +import { launchNotifyScript, type NotifySpawn } from "../common/notify"; |
| 4 | + |
| 5 | +const bashPath = "C:\\Program Files\\Git\\bin\\bash.exe"; |
| 6 | +const runtime = { platform: "win32" as const, findGitBashPath: () => bashPath }; |
| 7 | + |
| 8 | +function captureSpawn(errorCode?: string) { |
| 9 | + const calls: Parameters<NotifySpawn>[] = []; |
| 10 | + let unrefs = 0; |
| 11 | + const spawn: NotifySpawn = (...args) => { |
| 12 | + calls.push(args); |
| 13 | + return { |
| 14 | + once(_event, listener) { |
| 15 | + if (errorCode) listener({ code: errorCode } as NodeJS.ErrnoException); |
| 16 | + return this; |
| 17 | + }, |
| 18 | + unref() { |
| 19 | + unrefs++; |
| 20 | + }, |
| 21 | + }; |
| 22 | + }; |
| 23 | + return { calls, spawn, unrefs: () => unrefs }; |
| 24 | +} |
| 25 | + |
| 26 | +test("Windows shell notifications pass the script as one Git Bash argument and preserve context", () => { |
| 27 | + const paths = [ |
| 28 | + ["C:\\Users\\张 三\\O'Brien & $(echo test)\\notify.sh", "/c/Users/张 三/O'Brien & $(echo test)/notify.sh"], |
| 29 | + ["D:/通知 文件/notify.SH", "/d/通知 文件/notify.SH"], |
| 30 | + ["/c/Users/you/notify.sh", "/c/Users/you/notify.sh"], |
| 31 | + ["\\\\server\\share\\通知 文件\\notify.sh", "//server/share/通知 文件/notify.sh"], |
| 32 | + ]; |
| 33 | + for (const [script, bashScript] of paths) { |
| 34 | + const capture = captureSpawn(); |
| 35 | + launchNotifyScript( |
| 36 | + ` ${script} `, |
| 37 | + 15999, |
| 38 | + "C:\\project", |
| 39 | + capture.spawn, |
| 40 | + { NOTIFY_CUSTOM: "value" }, |
| 41 | + { status: "failed", failReason: "error", body: "a\nb", title: "test" }, |
| 42 | + runtime |
| 43 | + ); |
| 44 | + assert.equal(capture.calls.length, 1); |
| 45 | + const [command, args, options] = capture.calls[0]; |
| 46 | + assert.equal(command, bashPath); |
| 47 | + assert.deepEqual(args, ["--noprofile", "--norc", "--", bashScript]); |
| 48 | + assert.equal(options.cwd, "C:\\project"); |
| 49 | + assert.equal(options.detached, false); |
| 50 | + assert.equal(options.windowsHide, true); |
| 51 | + assert.equal(options.stdio, "ignore"); |
| 52 | + assert.equal(options.env?.NOTIFY_CUSTOM, "value"); |
| 53 | + assert.equal(options.env?.DURATION, "15"); |
| 54 | + assert.equal(options.env?.STATUS, "failed"); |
| 55 | + assert.equal(options.env?.FAIL_REASON, "error"); |
| 56 | + assert.equal(options.env?.BODY, "a\nb"); |
| 57 | + assert.equal(options.env?.TITLE, "test"); |
| 58 | + assert.equal(capture.unrefs(), 1); |
| 59 | + } |
| 60 | +}); |
| 61 | + |
| 62 | +test("Windows non-shell notifications retain direct execution", () => { |
| 63 | + for (const script of ["C:\\notify.exe", "C:\\notify.py", "C:\\notify.cmd"]) { |
| 64 | + const capture = captureSpawn(); |
| 65 | + launchNotifyScript( |
| 66 | + script, |
| 67 | + 0, |
| 68 | + undefined, |
| 69 | + capture.spawn, |
| 70 | + {}, |
| 71 | + {}, |
| 72 | + { |
| 73 | + platform: "win32", |
| 74 | + findGitBashPath: () => { |
| 75 | + assert.fail("Non-shell notifications must not look up Git Bash"); |
| 76 | + }, |
| 77 | + } |
| 78 | + ); |
| 79 | + assert.equal(capture.calls.length, 1); |
| 80 | + assert.equal(capture.calls[0][0], script); |
| 81 | + assert.deepEqual(capture.calls[0][1], []); |
| 82 | + } |
| 83 | +}); |
| 84 | + |
| 85 | +test("Windows notification lookup and spawn failures do not escape or start a fallback shell", () => { |
| 86 | + const capture = captureSpawn(); |
| 87 | + assert.doesNotThrow(() => |
| 88 | + launchNotifyScript( |
| 89 | + "C:\\notify.sh", |
| 90 | + 0, |
| 91 | + undefined, |
| 92 | + capture.spawn, |
| 93 | + {}, |
| 94 | + {}, |
| 95 | + { |
| 96 | + platform: "win32", |
| 97 | + findGitBashPath: () => { |
| 98 | + throw new Error("Git Bash not found"); |
| 99 | + }, |
| 100 | + } |
| 101 | + ) |
| 102 | + ); |
| 103 | + assert.equal(capture.calls.length, 0); |
| 104 | + |
| 105 | + const throwingSpawn: NotifySpawn = () => { |
| 106 | + throw new Error("spawn failed"); |
| 107 | + }; |
| 108 | + assert.doesNotThrow(() => launchNotifyScript("C:\\notify.sh", 0, undefined, throwingSpawn, {}, {}, runtime)); |
| 109 | + |
| 110 | + for (const code of ["ENOENT", "EACCES", "ENOEXEC"]) { |
| 111 | + const failed = captureSpawn(code); |
| 112 | + assert.doesNotThrow(() => launchNotifyScript("C:\\notify.sh", 0, undefined, failed.spawn, {}, {}, runtime)); |
| 113 | + assert.equal(failed.calls.length, 1); |
| 114 | + assert.equal(failed.unrefs(), 1); |
| 115 | + } |
| 116 | +}); |
| 117 | + |
| 118 | +test("empty notification paths do not look up Git Bash or spawn a process", () => { |
| 119 | + const capture = captureSpawn(); |
| 120 | + for (const script of [undefined, "", " "]) { |
| 121 | + launchNotifyScript( |
| 122 | + script, |
| 123 | + 0, |
| 124 | + undefined, |
| 125 | + capture.spawn, |
| 126 | + {}, |
| 127 | + {}, |
| 128 | + { |
| 129 | + platform: "win32", |
| 130 | + findGitBashPath: () => { |
| 131 | + assert.fail("Empty paths must not look up Git Bash"); |
| 132 | + }, |
| 133 | + } |
| 134 | + ); |
| 135 | + } |
| 136 | + assert.equal(capture.calls.length, 0); |
| 137 | +}); |
0 commit comments