mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-01 07:10:26 +00:00
test(infra): reuse temp dir helper in fs safety tests
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import fs from "node:fs/promises";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
import { withTempDir } from "../test-helpers/temp-dir.js";
|
||||
import {
|
||||
isInterpreterLikeSafeBin,
|
||||
listInterpreterLikeSafeBins,
|
||||
@@ -136,29 +136,29 @@ describe("exec safe-bin runtime policy", () => {
|
||||
if (process.platform === "win32") {
|
||||
return;
|
||||
}
|
||||
const dir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-safe-bin-runtime-"));
|
||||
try {
|
||||
await fs.chmod(dir, 0o777);
|
||||
const onWarning = vi.fn();
|
||||
const policy = resolveExecSafeBinRuntimePolicy({
|
||||
global: {
|
||||
safeBinTrustedDirs: [dir],
|
||||
},
|
||||
onWarning,
|
||||
});
|
||||
await withTempDir({ prefix: "openclaw-safe-bin-runtime-" }, async (dir) => {
|
||||
try {
|
||||
await fs.chmod(dir, 0o777);
|
||||
const onWarning = vi.fn();
|
||||
const policy = resolveExecSafeBinRuntimePolicy({
|
||||
global: {
|
||||
safeBinTrustedDirs: [dir],
|
||||
},
|
||||
onWarning,
|
||||
});
|
||||
|
||||
expect(policy.writableTrustedSafeBinDirs).toEqual([
|
||||
{
|
||||
dir: path.resolve(dir),
|
||||
groupWritable: true,
|
||||
worldWritable: true,
|
||||
},
|
||||
]);
|
||||
expect(onWarning).toHaveBeenCalledWith(expect.stringContaining(path.resolve(dir)));
|
||||
expect(onWarning).toHaveBeenCalledWith(expect.stringContaining("world-writable"));
|
||||
} finally {
|
||||
await fs.chmod(dir, 0o755).catch(() => undefined);
|
||||
await fs.rm(dir, { recursive: true, force: true }).catch(() => undefined);
|
||||
}
|
||||
expect(policy.writableTrustedSafeBinDirs).toEqual([
|
||||
{
|
||||
dir: path.resolve(dir),
|
||||
groupWritable: true,
|
||||
worldWritable: true,
|
||||
},
|
||||
]);
|
||||
expect(onWarning).toHaveBeenCalledWith(expect.stringContaining(path.resolve(dir)));
|
||||
expect(onWarning).toHaveBeenCalledWith(expect.stringContaining("world-writable"));
|
||||
} finally {
|
||||
await fs.chmod(dir, 0o755).catch(() => undefined);
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import fs from "node:fs";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { withTempDir } from "../test-helpers/temp-dir.js";
|
||||
import {
|
||||
ensureDir,
|
||||
existsDir,
|
||||
@@ -12,29 +12,31 @@ import {
|
||||
} from "./state-migrations.fs.js";
|
||||
|
||||
describe("state migration fs helpers", () => {
|
||||
it("reads directories safely and creates missing directories", () => {
|
||||
const base = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-state-migrations-fs-"));
|
||||
const nested = path.join(base, "nested");
|
||||
it("reads directories safely and creates missing directories", async () => {
|
||||
await withTempDir({ prefix: "openclaw-state-migrations-fs-" }, async (base) => {
|
||||
const nested = path.join(base, "nested");
|
||||
|
||||
expect(safeReadDir(nested)).toEqual([]);
|
||||
ensureDir(nested);
|
||||
fs.writeFileSync(path.join(nested, "file.txt"), "ok", "utf8");
|
||||
expect(safeReadDir(nested)).toEqual([]);
|
||||
ensureDir(nested);
|
||||
fs.writeFileSync(path.join(nested, "file.txt"), "ok", "utf8");
|
||||
|
||||
expect(safeReadDir(nested).map((entry) => entry.name)).toEqual(["file.txt"]);
|
||||
expect(existsDir(nested)).toBe(true);
|
||||
expect(existsDir(path.join(nested, "file.txt"))).toBe(false);
|
||||
expect(safeReadDir(nested).map((entry) => entry.name)).toEqual(["file.txt"]);
|
||||
expect(existsDir(nested)).toBe(true);
|
||||
expect(existsDir(path.join(nested, "file.txt"))).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
it("distinguishes files from directories", () => {
|
||||
const base = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-state-migrations-fs-"));
|
||||
const filePath = path.join(base, "store.json");
|
||||
const dirPath = path.join(base, "dir");
|
||||
fs.writeFileSync(filePath, "{}", "utf8");
|
||||
fs.mkdirSync(dirPath);
|
||||
it("distinguishes files from directories", async () => {
|
||||
await withTempDir({ prefix: "openclaw-state-migrations-fs-" }, async (base) => {
|
||||
const filePath = path.join(base, "store.json");
|
||||
const dirPath = path.join(base, "dir");
|
||||
fs.writeFileSync(filePath, "{}", "utf8");
|
||||
fs.mkdirSync(dirPath);
|
||||
|
||||
expect(fileExists(filePath)).toBe(true);
|
||||
expect(fileExists(dirPath)).toBe(false);
|
||||
expect(fileExists(path.join(base, "missing.json"))).toBe(false);
|
||||
expect(fileExists(filePath)).toBe(true);
|
||||
expect(fileExists(dirPath)).toBe(false);
|
||||
expect(fileExists(path.join(base, "missing.json"))).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
it("recognizes legacy whatsapp auth file names", () => {
|
||||
@@ -46,26 +48,27 @@ describe("state migration fs helpers", () => {
|
||||
expect(isLegacyWhatsAppAuthFile("other.json")).toBe(false);
|
||||
});
|
||||
|
||||
it("parses json5 session stores and rejects invalid shapes", () => {
|
||||
const base = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-state-migrations-fs-"));
|
||||
const okPath = path.join(base, "store.json");
|
||||
const badPath = path.join(base, "bad.json");
|
||||
const listPath = path.join(base, "list.json");
|
||||
it("parses json5 session stores and rejects invalid shapes", async () => {
|
||||
await withTempDir({ prefix: "openclaw-state-migrations-fs-" }, async (base) => {
|
||||
const okPath = path.join(base, "store.json");
|
||||
const badPath = path.join(base, "bad.json");
|
||||
const listPath = path.join(base, "list.json");
|
||||
|
||||
fs.writeFileSync(okPath, "{session: {sessionId: 'abc', updatedAt: 1}}", "utf8");
|
||||
fs.writeFileSync(badPath, "{not valid", "utf8");
|
||||
fs.writeFileSync(listPath, "[]", "utf8");
|
||||
fs.writeFileSync(okPath, "{session: {sessionId: 'abc', updatedAt: 1}}", "utf8");
|
||||
fs.writeFileSync(badPath, "{not valid", "utf8");
|
||||
fs.writeFileSync(listPath, "[]", "utf8");
|
||||
|
||||
expect(readSessionStoreJson5(okPath)).toEqual({
|
||||
ok: true,
|
||||
store: {
|
||||
session: {
|
||||
sessionId: "abc",
|
||||
updatedAt: 1,
|
||||
expect(readSessionStoreJson5(okPath)).toEqual({
|
||||
ok: true,
|
||||
store: {
|
||||
session: {
|
||||
sessionId: "abc",
|
||||
updatedAt: 1,
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
expect(readSessionStoreJson5(badPath)).toEqual({ ok: false, store: {} });
|
||||
expect(readSessionStoreJson5(listPath)).toEqual({ ok: false, store: {} });
|
||||
});
|
||||
expect(readSessionStoreJson5(badPath)).toEqual({ ok: false, store: {} });
|
||||
expect(readSessionStoreJson5(listPath)).toEqual({ ok: false, store: {} });
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user