it("falls back to fuser when lsof is permission denied", async () => {
(execFileSync as unknown as Mock).mockImplementation((cmd: string) => { if (cmd.includes("lsof")) { const err = new Error("spawnSync lsof EACCES") as NodeJS.ErrnoException;
err.code = "EACCES"; throw err;
} return"18789/tcp: 4242\n";
});
tryListenOnPortMock.mockResolvedValue(undefined);
it("throws when lsof is unavailable and fuser is missing", async () => {
(execFileSync as unknown as Mock).mockImplementation((cmd: string) => { const err = new Error(`spawnSync ${cmd} ENOENT`) as NodeJS.ErrnoException;
err.code = "ENOENT"; throw err;
});
it("returns empty list when netstat finds no listeners on the port", () => {
(execFileSync as unknown as Mock).mockReturnValue(makeNetstatOutput(9999, 42));
expect(listPortListeners(18789)).toEqual([]);
});
it("parses PIDs from netstat output correctly", () => {
(execFileSync as unknown as Mock).mockReturnValue(makeNetstatOutput(18789, 42, 99));
expect(listPortListeners(18789)).toEqual<PortProcess[]>([{ pid: 42 }, { pid: 99 }]);
});
it("does not incorrectly match a port that is a substring (e.g. 80 vs 8080)", () => {
(execFileSync as unknown as Mock).mockReturnValue(makeNetstatOutput(8080, 42));
expect(listPortListeners(80)).toEqual([]);
});
it("deduplicates PIDs that appear multiple times", () => {
(execFileSync as unknown as Mock).mockReturnValue(makeNetstatOutput(18789, 42, 42));
expect(listPortListeners(18789)).toEqual<PortProcess[]>([{ pid: 42 }]);
});
it("throws a descriptive error when netstat fails", () => {
(execFileSync as unknown as Mock).mockImplementation(() => { thrownew Error("access denied");
});
expect(() => listPortListeners(18789)).toThrow(/netstat failed/);
});
it("kills Windows listeners and returns metadata", () => {
(execFileSync as unknown as Mock).mockReturnValue(makeNetstatOutput(18789, 42, 99)); const killMock = vi.fn();
process.kill = killMock;
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.