mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-06 17:20:45 +00:00
test: share json file symlink helper
This commit is contained in:
@@ -23,6 +23,31 @@ async function withJsonPath<T>(
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function withJsonSymlink<T>(
|
||||||
|
run: (params: {
|
||||||
|
root: string;
|
||||||
|
targetDir: string;
|
||||||
|
targetPath: string;
|
||||||
|
linkPath: string;
|
||||||
|
}) => Promise<T> | T,
|
||||||
|
): Promise<T> {
|
||||||
|
return withTempDir({ prefix: "openclaw-json-file-" }, async (root) => {
|
||||||
|
const targetDir = path.join(root, "target");
|
||||||
|
return run({
|
||||||
|
root,
|
||||||
|
targetDir,
|
||||||
|
targetPath: path.join(targetDir, "config.json"),
|
||||||
|
linkPath: path.join(root, "config-link.json"),
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function expectSavedPayloadThroughSymlink(linkPath: string, targetPath: string) {
|
||||||
|
expect(fs.lstatSync(linkPath).isSymbolicLink()).toBe(true);
|
||||||
|
expect(loadJsonFile(targetPath)).toEqual(SAVED_PAYLOAD);
|
||||||
|
expect(loadJsonFile(linkPath)).toEqual(SAVED_PAYLOAD);
|
||||||
|
}
|
||||||
|
|
||||||
describe("json-file helpers", () => {
|
describe("json-file helpers", () => {
|
||||||
afterEach(() => {
|
afterEach(() => {
|
||||||
vi.restoreAllMocks();
|
vi.restoreAllMocks();
|
||||||
@@ -106,19 +131,14 @@ describe("json-file helpers", () => {
|
|||||||
it.runIf(process.platform !== "win32")(
|
it.runIf(process.platform !== "win32")(
|
||||||
"preserves symlink destinations when replacing existing JSON files",
|
"preserves symlink destinations when replacing existing JSON files",
|
||||||
async () => {
|
async () => {
|
||||||
await withTempDir({ prefix: "openclaw-json-file-" }, async (root) => {
|
await withJsonSymlink(({ targetDir, targetPath, linkPath }) => {
|
||||||
const targetDir = path.join(root, "target");
|
|
||||||
const targetPath = path.join(targetDir, "config.json");
|
|
||||||
const linkPath = path.join(root, "config-link.json");
|
|
||||||
fs.mkdirSync(targetDir, { recursive: true });
|
fs.mkdirSync(targetDir, { recursive: true });
|
||||||
writeExistingJson(targetPath);
|
writeExistingJson(targetPath);
|
||||||
fs.symlinkSync(targetPath, linkPath);
|
fs.symlinkSync(targetPath, linkPath);
|
||||||
|
|
||||||
saveJsonFile(linkPath, SAVED_PAYLOAD);
|
saveJsonFile(linkPath, SAVED_PAYLOAD);
|
||||||
|
|
||||||
expect(fs.lstatSync(linkPath).isSymbolicLink()).toBe(true);
|
expectSavedPayloadThroughSymlink(linkPath, targetPath);
|
||||||
expect(loadJsonFile(targetPath)).toEqual(SAVED_PAYLOAD);
|
|
||||||
expect(loadJsonFile(linkPath)).toEqual(SAVED_PAYLOAD);
|
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
@@ -126,18 +146,13 @@ describe("json-file helpers", () => {
|
|||||||
it.runIf(process.platform !== "win32")(
|
it.runIf(process.platform !== "win32")(
|
||||||
"creates a missing target file through an existing symlink",
|
"creates a missing target file through an existing symlink",
|
||||||
async () => {
|
async () => {
|
||||||
await withTempDir({ prefix: "openclaw-json-file-" }, async (root) => {
|
await withJsonSymlink(({ targetDir, targetPath, linkPath }) => {
|
||||||
const targetDir = path.join(root, "target");
|
|
||||||
const targetPath = path.join(targetDir, "config.json");
|
|
||||||
const linkPath = path.join(root, "config-link.json");
|
|
||||||
fs.mkdirSync(targetDir, { recursive: true });
|
fs.mkdirSync(targetDir, { recursive: true });
|
||||||
fs.symlinkSync(targetPath, linkPath);
|
fs.symlinkSync(targetPath, linkPath);
|
||||||
|
|
||||||
saveJsonFile(linkPath, SAVED_PAYLOAD);
|
saveJsonFile(linkPath, SAVED_PAYLOAD);
|
||||||
|
|
||||||
expect(fs.lstatSync(linkPath).isSymbolicLink()).toBe(true);
|
expectSavedPayloadThroughSymlink(linkPath, targetPath);
|
||||||
expect(loadJsonFile(targetPath)).toEqual(SAVED_PAYLOAD);
|
|
||||||
expect(loadJsonFile(linkPath)).toEqual(SAVED_PAYLOAD);
|
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|||||||
Reference in New Issue
Block a user