




















@@ -1,5 +1,6 @@
11import fsSync from "node:fs";
22import { afterEach, describe, expect, it, vi } from "vitest";
3+import { withMockedPlatform } from "../test-utils/vitest-spies.js";
34import { getProcessStartTime, isPidAlive, isPidDefinitelyDead } from "./pid-alive.js";
4556afterEach(() => {
@@ -17,30 +18,6 @@ function mockProcReads(entries: Record<string, string>) {
1718});
1819}
192020-async function withLinuxProcessPlatform<T>(run: () => Promise<T>): Promise<T> {
21-return withProcessPlatform("linux", run);
22-}
23-24-async function withProcessPlatform<T>(
25-platform: NodeJS.Platform,
26-run: () => Promise<T>,
27-): Promise<T> {
28-const originalPlatformDescriptor = Object.getOwnPropertyDescriptor(process, "platform");
29-if (!originalPlatformDescriptor) {
30-throw new Error("missing process.platform descriptor");
31-}
32-Object.defineProperty(process, "platform", {
33- ...originalPlatformDescriptor,
34-value: platform,
35-});
36-try {
37-return await run();
38-} finally {
39-Object.defineProperty(process, "platform", originalPlatformDescriptor);
40-vi.restoreAllMocks();
41-}
42-}
43-4421describe("isPidAlive", () => {
4522it("returns true for the current running process", () => {
4623expect(isPidAlive(process.pid)).toBe(true);
@@ -64,7 +41,7 @@ describe("isPidAlive", () => {
6441mockProcReads({
6542[`/proc/${zombiePid}/status`]: `Name:\tnode\nUmask:\t0022\nState:\tZ (zombie)\nTgid:\t${zombiePid}\nPid:\t${zombiePid}\n`,
6643});
67-await withLinuxProcessPlatform(async () => {
44+await withMockedPlatform("linux", async () => {
6845expect(isPidAlive(zombiePid)).toBe(false);
6946});
7047});
@@ -75,7 +52,7 @@ describe("isPidAlive", () => {
7552});
7653const killSpy = vi.spyOn(process, "kill").mockImplementation(() => true);
775478-await withLinuxProcessPlatform(async () => {
55+await withMockedPlatform("linux", async () => {
7956expect(isPidAlive(42)).toBe(true);
8057});
8158@@ -120,7 +97,7 @@ describe("isPidDefinitelyDead", () => {
12097[`/proc/${zombiePid}/status`]: `Name:\tnode\nUmask:\t0022\nState:\tZ (zombie)\nTgid:\t${zombiePid}\nPid:\t${zombiePid}\n`,
12198});
12299123-await withLinuxProcessPlatform(async () => {
100+await withMockedPlatform("linux", async () => {
124101expect(isPidDefinitelyDead(zombiePid)).toBe(true);
125102});
126103});
@@ -132,7 +109,7 @@ describe("isPidDefinitelyDead", () => {
132109[`/proc/${livePid}/status`]: `Name:\tnode\nUmask:\t0022\nState:\tS (sleeping)\nTgid:\t${livePid}\nPid:\t${livePid}\n`,
133110});
134111135-await withLinuxProcessPlatform(async () => {
112+await withMockedPlatform("linux", async () => {
136113expect(isPidDefinitelyDead(livePid)).toBe(false);
137114});
138115});
@@ -152,7 +129,7 @@ describe("getProcessStartTime", () => {
152129"/proc/46/stat": `${fakeStatPrefix}1.5${fakeStatSuffix}`,
153130});
154131155-await withLinuxProcessPlatform(async () => {
132+await withMockedPlatform("linux", async () => {
156133expect(getProcessStartTime(process.pid)).toBe(98765);
157134expect(getProcessStartTime(42)).toBe(55555);
158135expect(getProcessStartTime(43)).toBeNull();
@@ -163,7 +140,7 @@ describe("getProcessStartTime", () => {
163140});
164141165142it("returns null on non-Linux platforms", () => {
166-return withProcessPlatform("darwin", async () => {
143+return withMockedPlatform("darwin", async () => {
167144expect(getProcessStartTime(process.pid)).toBeNull();
168145});
169146});
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。