test: reuse platform spy helper in infra tests

This commit is contained in:
Vincent Koc
2026-05-17 16:55:23 +08:00
parent 9ca98a6d39
commit 85f8fd0533
3 changed files with 9 additions and 32 deletions

View File

@@ -1,5 +1,6 @@
import { afterEach, describe, expect, it, vi } from "vitest";
import { captureFullEnv } from "../test-utils/env.js";
import { mockProcessPlatform } from "../test-utils/vitest-spies.js";
import { SUPERVISOR_HINT_ENV_VARS } from "./supervisor-markers.js";
const spawnMock = vi.hoisted(() => vi.fn());
@@ -30,16 +31,9 @@ import {
const originalArgv = [...process.argv];
const originalExecArgv = [...process.execArgv];
const envSnapshot = captureFullEnv();
const originalPlatformDescriptor = Object.getOwnPropertyDescriptor(process, "platform");
function setPlatform(platform: string) {
if (!originalPlatformDescriptor) {
return;
}
Object.defineProperty(process, "platform", {
...originalPlatformDescriptor,
value: platform,
});
function setPlatform(platform: NodeJS.Platform) {
mockProcessPlatform(platform);
}
afterEach(() => {
@@ -50,9 +44,7 @@ afterEach(() => {
triggerOpenClawRestartMock.mockClear();
isContainerEnvironmentMock.mockReset();
isContainerEnvironmentMock.mockReturnValue(false);
if (originalPlatformDescriptor) {
Object.defineProperty(process, "platform", originalPlatformDescriptor);
}
vi.restoreAllMocks();
});
function clearSupervisorHints() {

View File

@@ -1,5 +1,6 @@
import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
import { captureFullEnv } from "../test-utils/env.js";
import { mockProcessPlatform } from "../test-utils/vitest-spies.js";
const spawnSyncMock = vi.hoisted(() => vi.fn());
const resolveLsofCommandSyncMock = vi.hoisted(() => vi.fn());
@@ -29,7 +30,6 @@ let triggerOpenClawRestart: typeof import("./restart.js").triggerOpenClawRestart
let currentTimeMs = 0;
const envSnapshot = captureFullEnv();
const originalPlatformDescriptor = Object.getOwnPropertyDescriptor(process, "platform");
beforeAll(async () => {
({ __testing, cleanStaleGatewayProcessesSync, findGatewayPidsOnPortSync } =
@@ -55,20 +55,11 @@ afterEach(() => {
envSnapshot.restore();
__testing.setSleepSyncOverride(null);
__testing.setDateNowOverride(null);
if (originalPlatformDescriptor) {
Object.defineProperty(process, "platform", originalPlatformDescriptor);
}
vi.restoreAllMocks();
});
function setPlatform(platform: NodeJS.Platform): void {
if (!originalPlatformDescriptor) {
return;
}
Object.defineProperty(process, "platform", {
...originalPlatformDescriptor,
value: platform,
});
mockProcessPlatform(platform);
}
function requireFirstSpawnSyncCall(): [unknown, unknown, unknown] {

View File

@@ -1,5 +1,6 @@
import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
import { captureEnv } from "../test-utils/env.js";
import { mockProcessPlatform } from "../test-utils/vitest-spies.js";
const readFileSyncMock = vi.hoisted(() => vi.fn());
const readFileMock = vi.hoisted(() => vi.fn());
@@ -30,13 +31,8 @@ let isWSL2Sync: typeof import("./wsl.js").isWSL2Sync;
let isWSL: typeof import("./wsl.js").isWSL;
let resetWSLStateForTests: typeof import("./wsl.js").resetWSLStateForTests;
const originalPlatformDescriptor = Object.getOwnPropertyDescriptor(process, "platform");
function setPlatform(platform: NodeJS.Platform): void {
Object.defineProperty(process, "platform", {
value: platform,
configurable: true,
});
mockProcessPlatform(platform);
}
describe("wsl detection", () => {
@@ -60,9 +56,7 @@ describe("wsl detection", () => {
afterEach(() => {
envSnapshot.restore();
resetWSLStateForTests();
if (originalPlatformDescriptor) {
Object.defineProperty(process, "platform", originalPlatformDescriptor);
}
vi.restoreAllMocks();
});
it.each([