mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-24 07:09:50 +00:00
test: share process platform spy helper
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import fs from "node:fs";
|
||||
import path from "node:path";
|
||||
import { afterEach, describe, expect, it, vi } from "vitest";
|
||||
import { mockProcessPlatform } from "../test-utils/vitest-spies.js";
|
||||
import {
|
||||
evaluateRuntimeEligibility,
|
||||
evaluateRuntimeRequires,
|
||||
@@ -11,15 +12,11 @@ import {
|
||||
resolveRuntimePlatform,
|
||||
} from "./config-eval.js";
|
||||
|
||||
const originalPlatformDescriptor = Object.getOwnPropertyDescriptor(process, "platform");
|
||||
const originalPath = process.env.PATH;
|
||||
const originalPathExt = process.env.PATHEXT;
|
||||
|
||||
function setPlatform(platform: NodeJS.Platform): void {
|
||||
Object.defineProperty(process, "platform", {
|
||||
value: platform,
|
||||
configurable: true,
|
||||
});
|
||||
mockProcessPlatform(platform);
|
||||
}
|
||||
|
||||
afterEach(() => {
|
||||
@@ -30,9 +27,6 @@ afterEach(() => {
|
||||
} else {
|
||||
process.env.PATHEXT = originalPathExt;
|
||||
}
|
||||
if (originalPlatformDescriptor) {
|
||||
Object.defineProperty(process, "platform", originalPlatformDescriptor);
|
||||
}
|
||||
});
|
||||
|
||||
describe("config-eval helpers", () => {
|
||||
|
||||
@@ -1,23 +1,17 @@
|
||||
import { afterEach, describe, expect, it } from "vitest";
|
||||
import { afterEach, describe, expect, it, vi } from "vitest";
|
||||
import { mockProcessPlatform } from "../test-utils/vitest-spies.js";
|
||||
import {
|
||||
evaluateEntryMetadataRequirements,
|
||||
evaluateEntryMetadataRequirementsForCurrentPlatform,
|
||||
evaluateEntryRequirementsForCurrentPlatform,
|
||||
} from "./entry-status.js";
|
||||
|
||||
const originalPlatformDescriptor = Object.getOwnPropertyDescriptor(process, "platform");
|
||||
|
||||
function setPlatform(platform: NodeJS.Platform): void {
|
||||
Object.defineProperty(process, "platform", {
|
||||
value: platform,
|
||||
configurable: true,
|
||||
});
|
||||
mockProcessPlatform(platform);
|
||||
}
|
||||
|
||||
afterEach(() => {
|
||||
if (originalPlatformDescriptor) {
|
||||
Object.defineProperty(process, "platform", originalPlatformDescriptor);
|
||||
}
|
||||
vi.restoreAllMocks();
|
||||
});
|
||||
|
||||
describe("shared/entry-status", () => {
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import fsSync from "node:fs";
|
||||
import { afterEach, describe, expect, it, vi } from "vitest";
|
||||
import { withMockedPlatform } from "../test-utils/vitest-spies.js";
|
||||
import { getProcessStartTime, isPidAlive, isPidDefinitelyDead } from "./pid-alive.js";
|
||||
|
||||
afterEach(() => {
|
||||
@@ -17,30 +18,6 @@ function mockProcReads(entries: Record<string, string>) {
|
||||
});
|
||||
}
|
||||
|
||||
async function withLinuxProcessPlatform<T>(run: () => Promise<T>): Promise<T> {
|
||||
return withProcessPlatform("linux", run);
|
||||
}
|
||||
|
||||
async function withProcessPlatform<T>(
|
||||
platform: NodeJS.Platform,
|
||||
run: () => Promise<T>,
|
||||
): Promise<T> {
|
||||
const originalPlatformDescriptor = Object.getOwnPropertyDescriptor(process, "platform");
|
||||
if (!originalPlatformDescriptor) {
|
||||
throw new Error("missing process.platform descriptor");
|
||||
}
|
||||
Object.defineProperty(process, "platform", {
|
||||
...originalPlatformDescriptor,
|
||||
value: platform,
|
||||
});
|
||||
try {
|
||||
return await run();
|
||||
} finally {
|
||||
Object.defineProperty(process, "platform", originalPlatformDescriptor);
|
||||
vi.restoreAllMocks();
|
||||
}
|
||||
}
|
||||
|
||||
describe("isPidAlive", () => {
|
||||
it("returns true for the current running process", () => {
|
||||
expect(isPidAlive(process.pid)).toBe(true);
|
||||
@@ -64,7 +41,7 @@ describe("isPidAlive", () => {
|
||||
mockProcReads({
|
||||
[`/proc/${zombiePid}/status`]: `Name:\tnode\nUmask:\t0022\nState:\tZ (zombie)\nTgid:\t${zombiePid}\nPid:\t${zombiePid}\n`,
|
||||
});
|
||||
await withLinuxProcessPlatform(async () => {
|
||||
await withMockedPlatform("linux", async () => {
|
||||
expect(isPidAlive(zombiePid)).toBe(false);
|
||||
});
|
||||
});
|
||||
@@ -75,7 +52,7 @@ describe("isPidAlive", () => {
|
||||
});
|
||||
const killSpy = vi.spyOn(process, "kill").mockImplementation(() => true);
|
||||
|
||||
await withLinuxProcessPlatform(async () => {
|
||||
await withMockedPlatform("linux", async () => {
|
||||
expect(isPidAlive(42)).toBe(true);
|
||||
});
|
||||
|
||||
@@ -120,7 +97,7 @@ describe("isPidDefinitelyDead", () => {
|
||||
[`/proc/${zombiePid}/status`]: `Name:\tnode\nUmask:\t0022\nState:\tZ (zombie)\nTgid:\t${zombiePid}\nPid:\t${zombiePid}\n`,
|
||||
});
|
||||
|
||||
await withLinuxProcessPlatform(async () => {
|
||||
await withMockedPlatform("linux", async () => {
|
||||
expect(isPidDefinitelyDead(zombiePid)).toBe(true);
|
||||
});
|
||||
});
|
||||
@@ -132,7 +109,7 @@ describe("isPidDefinitelyDead", () => {
|
||||
[`/proc/${livePid}/status`]: `Name:\tnode\nUmask:\t0022\nState:\tS (sleeping)\nTgid:\t${livePid}\nPid:\t${livePid}\n`,
|
||||
});
|
||||
|
||||
await withLinuxProcessPlatform(async () => {
|
||||
await withMockedPlatform("linux", async () => {
|
||||
expect(isPidDefinitelyDead(livePid)).toBe(false);
|
||||
});
|
||||
});
|
||||
@@ -152,7 +129,7 @@ describe("getProcessStartTime", () => {
|
||||
"/proc/46/stat": `${fakeStatPrefix}1.5${fakeStatSuffix}`,
|
||||
});
|
||||
|
||||
await withLinuxProcessPlatform(async () => {
|
||||
await withMockedPlatform("linux", async () => {
|
||||
expect(getProcessStartTime(process.pid)).toBe(98765);
|
||||
expect(getProcessStartTime(42)).toBe(55555);
|
||||
expect(getProcessStartTime(43)).toBeNull();
|
||||
@@ -163,7 +140,7 @@ describe("getProcessStartTime", () => {
|
||||
});
|
||||
|
||||
it("returns null on non-Linux platforms", () => {
|
||||
return withProcessPlatform("darwin", async () => {
|
||||
return withMockedPlatform("darwin", async () => {
|
||||
expect(getProcessStartTime(process.pid)).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user