refactor(test): temp home env + normalize status
This commit is contained in:
32
test/helpers/normalize-text.ts
Normal file
32
test/helpers/normalize-text.ts
Normal file
@@ -0,0 +1,32 @@
|
||||
function stripAnsi(input: string): string {
|
||||
let out = "";
|
||||
for (let i = 0; i < input.length; i++) {
|
||||
const code = input.charCodeAt(i);
|
||||
if (code !== 27) {
|
||||
out += input[i];
|
||||
continue;
|
||||
}
|
||||
|
||||
const next = input[i + 1];
|
||||
if (next !== "[") continue;
|
||||
i += 1;
|
||||
|
||||
while (i + 1 < input.length) {
|
||||
i += 1;
|
||||
const c = input[i];
|
||||
if (!c) break;
|
||||
const isLetter =
|
||||
(c >= "A" && c <= "Z") || (c >= "a" && c <= "z") || c === "~";
|
||||
if (isLetter) break;
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
export function normalizeTestText(input: string): string {
|
||||
return stripAnsi(input)
|
||||
.replaceAll("\r\n", "\n")
|
||||
.replaceAll("…", "...")
|
||||
.replace(/[\uD800-\uDBFF][\uDC00-\uDFFF]/g, "?")
|
||||
.replace(/[\uD800-\uDFFF]/g, "?");
|
||||
}
|
||||
@@ -2,6 +2,8 @@ import fs from "node:fs/promises";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
|
||||
type EnvValue = string | undefined | ((home: string) => string | undefined);
|
||||
|
||||
type EnvSnapshot = {
|
||||
home: string | undefined;
|
||||
userProfile: string | undefined;
|
||||
@@ -35,6 +37,19 @@ function restoreEnv(snapshot: EnvSnapshot) {
|
||||
restoreKey("CLAWDIS_STATE_DIR", snapshot.legacyStateDir);
|
||||
}
|
||||
|
||||
function snapshotExtraEnv(keys: string[]): Record<string, string | undefined> {
|
||||
const snapshot: Record<string, string | undefined> = {};
|
||||
for (const key of keys) snapshot[key] = process.env[key];
|
||||
return snapshot;
|
||||
}
|
||||
|
||||
function restoreExtraEnv(snapshot: Record<string, string | undefined>) {
|
||||
for (const [key, value] of Object.entries(snapshot)) {
|
||||
if (value === undefined) delete process.env[key];
|
||||
else process.env[key] = value;
|
||||
}
|
||||
}
|
||||
|
||||
function setTempHome(base: string) {
|
||||
process.env.HOME = base;
|
||||
process.env.USERPROFILE = base;
|
||||
@@ -50,17 +65,38 @@ function setTempHome(base: string) {
|
||||
|
||||
export async function withTempHome<T>(
|
||||
fn: (home: string) => Promise<T>,
|
||||
opts: { prefix?: string } = {},
|
||||
opts: { env?: Record<string, EnvValue>; prefix?: string } = {},
|
||||
): Promise<T> {
|
||||
const base = await fs.mkdtemp(
|
||||
path.join(os.tmpdir(), opts.prefix ?? "clawdbot-test-home-"),
|
||||
);
|
||||
const snapshot = snapshotEnv();
|
||||
const envKeys = Object.keys(opts.env ?? {});
|
||||
for (const key of envKeys) {
|
||||
if (
|
||||
key === "HOME" ||
|
||||
key === "USERPROFILE" ||
|
||||
key === "HOMEDRIVE" ||
|
||||
key === "HOMEPATH"
|
||||
) {
|
||||
throw new Error(`withTempHome: use built-in home env (got ${key})`);
|
||||
}
|
||||
}
|
||||
const envSnapshot = snapshotExtraEnv(envKeys);
|
||||
|
||||
setTempHome(base);
|
||||
if (opts.env) {
|
||||
for (const [key, raw] of Object.entries(opts.env)) {
|
||||
const value = typeof raw === "function" ? raw(base) : raw;
|
||||
if (value === undefined) delete process.env[key];
|
||||
else process.env[key] = value;
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
return await fn(base);
|
||||
} finally {
|
||||
restoreExtraEnv(envSnapshot);
|
||||
restoreEnv(snapshot);
|
||||
try {
|
||||
await fs.rm(base, {
|
||||
|
||||
Reference in New Issue
Block a user