mirror of
https://github.com/moltbot/moltbot.git
synced 2026-04-18 20:24:47 +00:00
test(infra): reuse temp dir helper in utility file tests
This commit is contained in:
@@ -1,19 +1,10 @@
|
||||
import fs from "node:fs/promises";
|
||||
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 { resolveBrewExecutable, resolveBrewPathDirs } from "./brew.js";
|
||||
|
||||
describe("brew helpers", () => {
|
||||
async function withBrewRoot(run: (tmp: string) => Promise<void>) {
|
||||
const tmp = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-brew-"));
|
||||
try {
|
||||
await run(tmp);
|
||||
} finally {
|
||||
await fs.rm(tmp, { recursive: true, force: true });
|
||||
}
|
||||
}
|
||||
|
||||
async function writeExecutable(filePath: string) {
|
||||
await fs.mkdir(path.dirname(filePath), { recursive: true });
|
||||
await fs.writeFile(filePath, "#!/bin/sh\necho ok\n", "utf-8");
|
||||
@@ -21,7 +12,7 @@ describe("brew helpers", () => {
|
||||
}
|
||||
|
||||
it("resolves brew from ~/.linuxbrew/bin when executable exists", async () => {
|
||||
await withBrewRoot(async (tmp) => {
|
||||
await withTempDir({ prefix: "openclaw-brew-" }, async (tmp) => {
|
||||
const homebrewBin = path.join(tmp, ".linuxbrew", "bin");
|
||||
const brewPath = path.join(homebrewBin, "brew");
|
||||
await writeExecutable(brewPath);
|
||||
@@ -32,7 +23,7 @@ describe("brew helpers", () => {
|
||||
});
|
||||
|
||||
it("prefers HOMEBREW_PREFIX/bin/brew when present", async () => {
|
||||
await withBrewRoot(async (tmp) => {
|
||||
await withTempDir({ prefix: "openclaw-brew-" }, async (tmp) => {
|
||||
const prefix = path.join(tmp, "prefix");
|
||||
const prefixBin = path.join(prefix, "bin");
|
||||
const prefixBrew = path.join(prefixBin, "brew");
|
||||
@@ -48,7 +39,7 @@ describe("brew helpers", () => {
|
||||
});
|
||||
|
||||
it("prefers HOMEBREW_BREW_FILE over prefix and trims value", async () => {
|
||||
await withBrewRoot(async (tmp) => {
|
||||
await withTempDir({ prefix: "openclaw-brew-" }, async (tmp) => {
|
||||
const explicit = path.join(tmp, "custom", "brew");
|
||||
const prefix = path.join(tmp, "prefix");
|
||||
const prefixBrew = path.join(prefix, "bin", "brew");
|
||||
@@ -64,7 +55,7 @@ describe("brew helpers", () => {
|
||||
});
|
||||
|
||||
it("falls back to prefix when HOMEBREW_BREW_FILE is missing or not executable", async () => {
|
||||
await withBrewRoot(async (tmp) => {
|
||||
await withTempDir({ prefix: "openclaw-brew-" }, async (tmp) => {
|
||||
const explicit = path.join(tmp, "custom", "brew");
|
||||
const prefix = path.join(tmp, "prefix");
|
||||
const prefixBrew = path.join(prefix, "bin", "brew");
|
||||
@@ -89,7 +80,7 @@ describe("brew helpers", () => {
|
||||
});
|
||||
|
||||
it("ignores blank HOMEBREW_BREW_FILE and HOMEBREW_PREFIX values", async () => {
|
||||
await withBrewRoot(async (tmp) => {
|
||||
await withTempDir({ prefix: "openclaw-brew-" }, async (tmp) => {
|
||||
const homebrewBin = path.join(tmp, ".linuxbrew", "bin");
|
||||
const brewPath = path.join(homebrewBin, "brew");
|
||||
await writeExecutable(brewPath);
|
||||
|
||||
@@ -1,21 +1,11 @@
|
||||
import fs from "node:fs/promises";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { writeBuildStamp } from "../../scripts/build-stamp.mjs";
|
||||
|
||||
async function withTempDir<T>(run: (dir: string) => Promise<T>): Promise<T> {
|
||||
const dir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-build-stamp-"));
|
||||
try {
|
||||
return await run(dir);
|
||||
} finally {
|
||||
await fs.rm(dir, { recursive: true, force: true });
|
||||
}
|
||||
}
|
||||
import { withTempDir } from "../test-helpers/temp-dir.js";
|
||||
|
||||
describe("build-stamp script", () => {
|
||||
it("writes dist/.buildstamp with the current git head", async () => {
|
||||
await withTempDir(async (tmp) => {
|
||||
await withTempDir({ prefix: "openclaw-build-stamp-" }, async (tmp) => {
|
||||
const stampPath = writeBuildStamp({
|
||||
cwd: tmp,
|
||||
now: () => 1_700_000_000_000,
|
||||
|
||||
@@ -1,15 +1,10 @@
|
||||
import fs from "node:fs/promises";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
import { setTimeout as sleep } from "node:timers/promises";
|
||||
import { afterEach, describe, expect, it, vi } from "vitest";
|
||||
import { withTempDir } from "../test-helpers/temp-dir.js";
|
||||
import { createAsyncLock, readJsonFile, writeJsonAtomic, writeTextAtomic } from "./json-files.js";
|
||||
|
||||
async function withTempBase<T>(run: (base: string) => Promise<T>): Promise<T> {
|
||||
const base = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-json-files-"));
|
||||
return run(base);
|
||||
}
|
||||
|
||||
const originalPlatformDescriptor = Object.getOwnPropertyDescriptor(process, "platform");
|
||||
|
||||
afterEach(() => {
|
||||
@@ -45,13 +40,13 @@ describe("json file helpers", () => {
|
||||
expected: null,
|
||||
},
|
||||
])("$name", async ({ setup, expected }) => {
|
||||
await withTempBase(async (base) => {
|
||||
await withTempDir({ prefix: "openclaw-json-files-" }, async (base) => {
|
||||
await expect(readJsonFile(await setup(base))).resolves.toEqual(expected);
|
||||
});
|
||||
});
|
||||
|
||||
it("writes json atomically with pretty formatting and optional trailing newline", async () => {
|
||||
await withTempBase(async (base) => {
|
||||
await withTempDir({ prefix: "openclaw-json-files-" }, async (base) => {
|
||||
const filePath = path.join(base, "nested", "config.json");
|
||||
|
||||
await writeJsonAtomic(
|
||||
@@ -70,7 +65,7 @@ describe("json file helpers", () => {
|
||||
{ input: "hello", expected: "hello\n" },
|
||||
{ input: "hello\n", expected: "hello\n" },
|
||||
])("writes text atomically for %j", async ({ input, expected }) => {
|
||||
await withTempBase(async (base) => {
|
||||
await withTempDir({ prefix: "openclaw-json-files-" }, async (base) => {
|
||||
const filePath = path.join(base, "nested", "note.txt");
|
||||
await writeTextAtomic(filePath, input, { appendTrailingNewline: true });
|
||||
await expect(fs.readFile(filePath, "utf8")).resolves.toBe(expected);
|
||||
@@ -78,7 +73,7 @@ describe("json file helpers", () => {
|
||||
});
|
||||
|
||||
it("falls back to copy-on-replace for Windows rename EPERM", async () => {
|
||||
await withTempBase(async (base) => {
|
||||
await withTempDir({ prefix: "openclaw-json-files-" }, async (base) => {
|
||||
const filePath = path.join(base, "state.json");
|
||||
await fs.writeFile(filePath, "old", "utf8");
|
||||
|
||||
|
||||
Reference in New Issue
Block a user