mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-11 02:14:33 +00:00
refactor(test): dedupe plugin env overrides via env helpers
This commit is contained in:
@@ -3,6 +3,7 @@ import fs from "node:fs";
|
|||||||
import os from "node:os";
|
import os from "node:os";
|
||||||
import path from "node:path";
|
import path from "node:path";
|
||||||
import { afterEach, describe, expect, it } from "vitest";
|
import { afterEach, describe, expect, it } from "vitest";
|
||||||
|
import { withEnvAsync } from "../test-utils/env.js";
|
||||||
import { discoverOpenClawPlugins } from "./discovery.js";
|
import { discoverOpenClawPlugins } from "./discovery.js";
|
||||||
|
|
||||||
const tempDirs: string[] = [];
|
const tempDirs: string[] = [];
|
||||||
@@ -15,24 +16,14 @@ function makeTempDir() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function withStateDir<T>(stateDir: string, fn: () => Promise<T>) {
|
async function withStateDir<T>(stateDir: string, fn: () => Promise<T>) {
|
||||||
const prev = process.env.OPENCLAW_STATE_DIR;
|
return await withEnvAsync(
|
||||||
const prevBundled = process.env.OPENCLAW_BUNDLED_PLUGINS_DIR;
|
{
|
||||||
process.env.OPENCLAW_STATE_DIR = stateDir;
|
OPENCLAW_STATE_DIR: stateDir,
|
||||||
process.env.OPENCLAW_BUNDLED_PLUGINS_DIR = "/nonexistent/bundled/plugins";
|
CLAWDBOT_STATE_DIR: undefined,
|
||||||
try {
|
OPENCLAW_BUNDLED_PLUGINS_DIR: "/nonexistent/bundled/plugins",
|
||||||
return await fn();
|
},
|
||||||
} finally {
|
fn,
|
||||||
if (prev === undefined) {
|
);
|
||||||
delete process.env.OPENCLAW_STATE_DIR;
|
|
||||||
} else {
|
|
||||||
process.env.OPENCLAW_STATE_DIR = prev;
|
|
||||||
}
|
|
||||||
if (prevBundled === undefined) {
|
|
||||||
delete process.env.OPENCLAW_BUNDLED_PLUGINS_DIR;
|
|
||||||
} else {
|
|
||||||
process.env.OPENCLAW_BUNDLED_PLUGINS_DIR = prevBundled;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
afterEach(() => {
|
afterEach(() => {
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ import fs from "node:fs";
|
|||||||
import os from "node:os";
|
import os from "node:os";
|
||||||
import path from "node:path";
|
import path from "node:path";
|
||||||
import { afterAll, afterEach, describe, expect, it } from "vitest";
|
import { afterAll, afterEach, describe, expect, it } from "vitest";
|
||||||
|
import { withEnv } from "../test-utils/env.js";
|
||||||
import { loadOpenClawPlugins } from "./loader.js";
|
import { loadOpenClawPlugins } from "./loader.js";
|
||||||
|
|
||||||
type TempPlugin = { dir: string; file: string; id: string };
|
type TempPlugin = { dir: string; file: string; id: string };
|
||||||
@@ -516,10 +517,8 @@ describe("loadOpenClawPlugins", () => {
|
|||||||
|
|
||||||
it("warns when loaded non-bundled plugin has no install/load-path provenance", () => {
|
it("warns when loaded non-bundled plugin has no install/load-path provenance", () => {
|
||||||
process.env.OPENCLAW_BUNDLED_PLUGINS_DIR = "/nonexistent/bundled/plugins";
|
process.env.OPENCLAW_BUNDLED_PLUGINS_DIR = "/nonexistent/bundled/plugins";
|
||||||
const prevStateDir = process.env.OPENCLAW_STATE_DIR;
|
|
||||||
const stateDir = makeTempDir();
|
const stateDir = makeTempDir();
|
||||||
process.env.OPENCLAW_STATE_DIR = stateDir;
|
withEnv({ OPENCLAW_STATE_DIR: stateDir, CLAWDBOT_STATE_DIR: undefined }, () => {
|
||||||
try {
|
|
||||||
const globalDir = path.join(stateDir, "extensions", "rogue");
|
const globalDir = path.join(stateDir, "extensions", "rogue");
|
||||||
fs.mkdirSync(globalDir, { recursive: true });
|
fs.mkdirSync(globalDir, { recursive: true });
|
||||||
writePlugin({
|
writePlugin({
|
||||||
@@ -552,13 +551,7 @@ describe("loadOpenClawPlugins", () => {
|
|||||||
msg.includes("rogue") && msg.includes("loaded without install/load-path provenance"),
|
msg.includes("rogue") && msg.includes("loaded without install/load-path provenance"),
|
||||||
),
|
),
|
||||||
).toBe(true);
|
).toBe(true);
|
||||||
} finally {
|
});
|
||||||
if (prevStateDir === undefined) {
|
|
||||||
delete process.env.OPENCLAW_STATE_DIR;
|
|
||||||
} else {
|
|
||||||
process.env.OPENCLAW_STATE_DIR = prevStateDir;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it("rejects plugin entry files that escape plugin root via symlink", () => {
|
it("rejects plugin entry files that escape plugin root via symlink", () => {
|
||||||
|
|||||||
Reference in New Issue
Block a user