refactor: share plugin temp dir helpers

This commit is contained in:
Peter Steinberger 2026-03-13 19:15:58 +00:00
parent 5a255809b9
commit da1ec45505
3 changed files with 45 additions and 42 deletions

View File

@ -1,32 +1,22 @@
import { randomUUID } from "node:crypto";
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
import { afterAll, afterEach, describe, expect, it } from "vitest";
import { clearPluginDiscoveryCache, discoverOpenClawPlugins } from "./discovery.js";
import {
cleanupTrackedTempDirs,
makeTrackedTempDir,
mkdirSafeDir,
} from "./test-helpers/fs-fixtures.js";
const tempDirs: string[] = [];
const previousUmask = process.umask(0o022);
function chmodSafeDir(dir: string) {
if (process.platform === "win32") {
return;
}
fs.chmodSync(dir, 0o755);
}
function mkdirSafe(dir: string) {
fs.mkdirSync(dir, { recursive: true });
chmodSafeDir(dir);
}
function makeTempDir() {
const dir = path.join(os.tmpdir(), `openclaw-plugins-${randomUUID()}`);
mkdirSafe(dir);
tempDirs.push(dir);
return dir;
return makeTrackedTempDir("openclaw-plugins", tempDirs);
}
const mkdirSafe = mkdirSafeDir;
function buildDiscoveryEnv(stateDir: string): NodeJS.ProcessEnv {
return {
OPENCLAW_STATE_DIR: stateDir,
@ -66,13 +56,7 @@ function expectEscapesPackageDiagnostic(diagnostics: Array<{ message: string }>)
afterEach(() => {
clearPluginDiscoveryCache();
for (const dir of tempDirs.splice(0)) {
try {
fs.rmSync(dir, { recursive: true, force: true });
} catch {
// ignore cleanup failures
}
}
cleanupTrackedTempDirs(tempDirs);
});
afterAll(() => {

View File

@ -1,6 +1,4 @@
import { randomUUID } from "node:crypto";
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
import { afterAll, afterEach, describe, expect, it } from "vitest";
import type { PluginCandidate } from "./discovery.js";
@ -8,6 +6,7 @@ import {
clearPluginManifestRegistryCache,
loadPluginManifestRegistry,
} from "./manifest-registry.js";
import { cleanupTrackedTempDirs, makeTrackedTempDir } from "./test-helpers/fs-fixtures.js";
const tempDirs: string[] = [];
const previousUmask = process.umask(0o022);
@ -25,10 +24,7 @@ function mkdirSafe(dir: string) {
}
function makeTempDir() {
const dir = path.join(os.tmpdir(), `openclaw-manifest-registry-${randomUUID()}`);
mkdirSafe(dir);
tempDirs.push(dir);
return dir;
return makeTrackedTempDir("openclaw-manifest-registry", tempDirs);
}
function writeManifest(dir: string, manifest: Record<string, unknown>) {
@ -133,17 +129,7 @@ function expectUnsafeWorkspaceManifestRejected(params: {
afterEach(() => {
clearPluginManifestRegistryCache();
while (tempDirs.length > 0) {
const dir = tempDirs.pop();
if (!dir) {
break;
}
try {
fs.rmSync(dir, { recursive: true, force: true });
} catch {
// ignore cleanup failures
}
}
cleanupTrackedTempDirs(tempDirs);
});
afterAll(() => {

View File

@ -0,0 +1,33 @@
import { randomUUID } from "node:crypto";
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
function chmodSafeDir(dir: string) {
if (process.platform === "win32") {
return;
}
fs.chmodSync(dir, 0o755);
}
export function mkdirSafeDir(dir: string) {
fs.mkdirSync(dir, { recursive: true });
chmodSafeDir(dir);
}
export function makeTrackedTempDir(prefix: string, trackedDirs: string[]) {
const dir = path.join(os.tmpdir(), `${prefix}-${randomUUID()}`);
mkdirSafeDir(dir);
trackedDirs.push(dir);
return dir;
}
export function cleanupTrackedTempDirs(trackedDirs: string[]) {
for (const dir of trackedDirs.splice(0)) {
try {
fs.rmSync(dir, { recursive: true, force: true });
} catch {
// ignore cleanup failures
}
}
}