mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-11 16:53:49 +00:00
test(gateway): dedupe gateway and infra test scaffolds
This commit is contained in:
@@ -32,6 +32,21 @@ function buildNestedEnvShellCommand(params: {
|
||||
return [...Array(params.depth).fill(params.envExecutable), "/bin/sh", "-c", params.payload];
|
||||
}
|
||||
|
||||
function analyzeEnvWrapperAllowlist(params: { argv: string[]; envPath: string; cwd: string }) {
|
||||
const analysis = analyzeArgvCommand({
|
||||
argv: params.argv,
|
||||
cwd: params.cwd,
|
||||
env: makePathEnv(params.envPath),
|
||||
});
|
||||
const allowlistEval = evaluateExecAllowlist({
|
||||
analysis,
|
||||
allowlist: [{ pattern: params.envPath }],
|
||||
safeBins: normalizeSafeBins([]),
|
||||
cwd: params.cwd,
|
||||
});
|
||||
return { analysis, allowlistEval };
|
||||
}
|
||||
|
||||
describe("exec approvals allowlist matching", () => {
|
||||
const baseResolution = {
|
||||
rawExecutable: "rg",
|
||||
@@ -288,16 +303,9 @@ describe("exec approvals command resolution", () => {
|
||||
if (process.platform !== "win32") {
|
||||
fs.chmodSync(envPath, 0o755);
|
||||
}
|
||||
|
||||
const analysis = analyzeArgvCommand({
|
||||
const { analysis, allowlistEval } = analyzeEnvWrapperAllowlist({
|
||||
argv: [envPath, "-S", 'sh -c "echo pwned"'],
|
||||
cwd: dir,
|
||||
env: makePathEnv(binDir),
|
||||
});
|
||||
const allowlistEval = evaluateExecAllowlist({
|
||||
analysis,
|
||||
allowlist: [{ pattern: envPath }],
|
||||
safeBins: normalizeSafeBins([]),
|
||||
envPath: envPath,
|
||||
cwd: dir,
|
||||
});
|
||||
|
||||
@@ -317,20 +325,13 @@ describe("exec approvals command resolution", () => {
|
||||
const envPath = path.join(binDir, "env");
|
||||
fs.writeFileSync(envPath, "#!/bin/sh\n");
|
||||
fs.chmodSync(envPath, 0o755);
|
||||
|
||||
const analysis = analyzeArgvCommand({
|
||||
const { analysis, allowlistEval } = analyzeEnvWrapperAllowlist({
|
||||
argv: buildNestedEnvShellCommand({
|
||||
envExecutable: envPath,
|
||||
depth: 5,
|
||||
payload: "echo pwned",
|
||||
}),
|
||||
cwd: dir,
|
||||
env: makePathEnv(binDir),
|
||||
});
|
||||
const allowlistEval = evaluateExecAllowlist({
|
||||
analysis,
|
||||
allowlist: [{ pattern: envPath }],
|
||||
safeBins: normalizeSafeBins([]),
|
||||
envPath,
|
||||
cwd: dir,
|
||||
});
|
||||
|
||||
|
||||
@@ -5,18 +5,28 @@ const mocks = vi.hoisted(() => ({
|
||||
loadOpenClawPlugins: vi.fn(),
|
||||
}));
|
||||
|
||||
const TEST_WORKSPACE_ROOT = "/tmp/openclaw-test-workspace";
|
||||
|
||||
function normalizeChannel(value?: string) {
|
||||
return value?.trim().toLowerCase() ?? undefined;
|
||||
}
|
||||
|
||||
function passthroughPluginAutoEnable(config: unknown) {
|
||||
return { config, changes: [] as unknown[] };
|
||||
}
|
||||
|
||||
vi.mock("../../channels/plugins/index.js", () => ({
|
||||
getChannelPlugin: mocks.getChannelPlugin,
|
||||
normalizeChannelId: (channel?: string) => channel?.trim().toLowerCase() ?? undefined,
|
||||
normalizeChannelId: normalizeChannel,
|
||||
}));
|
||||
|
||||
vi.mock("../../agents/agent-scope.js", () => ({
|
||||
resolveDefaultAgentId: () => "main",
|
||||
resolveAgentWorkspaceDir: () => "/tmp/openclaw-test-workspace",
|
||||
resolveAgentWorkspaceDir: () => TEST_WORKSPACE_ROOT,
|
||||
}));
|
||||
|
||||
vi.mock("../../config/plugin-auto-enable.js", () => ({
|
||||
applyPluginAutoEnable: ({ config }: { config: unknown }) => ({ config, changes: [] }),
|
||||
applyPluginAutoEnable: ({ config }: { config: unknown }) => passthroughPluginAutoEnable(config),
|
||||
}));
|
||||
|
||||
vi.mock("../../plugins/loader.js", () => ({
|
||||
|
||||
@@ -182,6 +182,39 @@ describe("runGatewayUpdate", () => {
|
||||
);
|
||||
}
|
||||
|
||||
function createGlobalNpmUpdateRunner(params: {
|
||||
pkgRoot: string;
|
||||
nodeModules: string;
|
||||
onBaseInstall?: () => Promise<CommandResult>;
|
||||
onOmitOptionalInstall?: () => Promise<CommandResult>;
|
||||
}) {
|
||||
const baseInstallKey = "npm i -g openclaw@latest --no-fund --no-audit --loglevel=error";
|
||||
const omitOptionalInstallKey =
|
||||
"npm i -g openclaw@latest --omit=optional --no-fund --no-audit --loglevel=error";
|
||||
|
||||
return async (argv: string[]): Promise<CommandResult> => {
|
||||
const key = argv.join(" ");
|
||||
if (key === `git -C ${params.pkgRoot} rev-parse --show-toplevel`) {
|
||||
return { stdout: "", stderr: "not a git repository", code: 128 };
|
||||
}
|
||||
if (key === "npm root -g") {
|
||||
return { stdout: params.nodeModules, stderr: "", code: 0 };
|
||||
}
|
||||
if (key === "pnpm root -g") {
|
||||
return { stdout: "", stderr: "", code: 1 };
|
||||
}
|
||||
if (key === baseInstallKey) {
|
||||
return (await params.onBaseInstall?.()) ?? { stdout: "ok", stderr: "", code: 0 };
|
||||
}
|
||||
if (key === omitOptionalInstallKey) {
|
||||
return (
|
||||
(await params.onOmitOptionalInstall?.()) ?? { stdout: "", stderr: "not found", code: 1 }
|
||||
);
|
||||
}
|
||||
return { stdout: "", stderr: "", code: 0 };
|
||||
};
|
||||
}
|
||||
|
||||
it("skips git update when worktree is dirty", async () => {
|
||||
await setupGitCheckout();
|
||||
const { runner, calls } = createRunner({
|
||||
@@ -392,23 +425,14 @@ describe("runGatewayUpdate", () => {
|
||||
await seedGlobalPackageRoot(pkgRoot);
|
||||
|
||||
let stalePresentAtInstall = true;
|
||||
const runCommand = async (argv: string[]) => {
|
||||
const key = argv.join(" ");
|
||||
if (key === `git -C ${pkgRoot} rev-parse --show-toplevel`) {
|
||||
return { stdout: "", stderr: "not a git repository", code: 128 };
|
||||
}
|
||||
if (key === "npm root -g") {
|
||||
return { stdout: nodeModules, stderr: "", code: 0 };
|
||||
}
|
||||
if (key === "pnpm root -g") {
|
||||
return { stdout: "", stderr: "", code: 1 };
|
||||
}
|
||||
if (key === "npm i -g openclaw@latest --no-fund --no-audit --loglevel=error") {
|
||||
const runCommand = createGlobalNpmUpdateRunner({
|
||||
nodeModules,
|
||||
pkgRoot,
|
||||
onBaseInstall: async () => {
|
||||
stalePresentAtInstall = await pathExists(staleDir);
|
||||
return { stdout: "ok", stderr: "", code: 0 };
|
||||
}
|
||||
return { stdout: "", stderr: "", code: 0 };
|
||||
};
|
||||
},
|
||||
});
|
||||
|
||||
const result = await runWithCommand(runCommand, { cwd: pkgRoot });
|
||||
|
||||
@@ -423,33 +447,22 @@ describe("runGatewayUpdate", () => {
|
||||
await seedGlobalPackageRoot(pkgRoot);
|
||||
|
||||
let firstAttempt = true;
|
||||
const runCommand = async (argv: string[]) => {
|
||||
const key = argv.join(" ");
|
||||
if (key === `git -C ${pkgRoot} rev-parse --show-toplevel`) {
|
||||
return { stdout: "", stderr: "not a git repository", code: 128 };
|
||||
}
|
||||
if (key === "npm root -g") {
|
||||
return { stdout: nodeModules, stderr: "", code: 0 };
|
||||
}
|
||||
if (key === "pnpm root -g") {
|
||||
return { stdout: "", stderr: "", code: 1 };
|
||||
}
|
||||
if (key === "npm i -g openclaw@latest --no-fund --no-audit --loglevel=error") {
|
||||
const runCommand = createGlobalNpmUpdateRunner({
|
||||
nodeModules,
|
||||
pkgRoot,
|
||||
onBaseInstall: async () => {
|
||||
firstAttempt = false;
|
||||
return { stdout: "", stderr: "node-gyp failed", code: 1 };
|
||||
}
|
||||
if (
|
||||
key === "npm i -g openclaw@latest --omit=optional --no-fund --no-audit --loglevel=error"
|
||||
) {
|
||||
},
|
||||
onOmitOptionalInstall: async () => {
|
||||
await fs.writeFile(
|
||||
path.join(pkgRoot, "package.json"),
|
||||
JSON.stringify({ name: "openclaw", version: "2.0.0" }),
|
||||
"utf-8",
|
||||
);
|
||||
return { stdout: "ok", stderr: "", code: 0 };
|
||||
}
|
||||
return { stdout: "", stderr: "", code: 0 };
|
||||
};
|
||||
},
|
||||
});
|
||||
|
||||
const result = await runWithCommand(runCommand, { cwd: pkgRoot });
|
||||
|
||||
|
||||
Reference in New Issue
Block a user