mcporter/tests/fixtures/test-helpers.ts
Peter Steinberger d85834b13d Release 0.6.2
2025-11-18 10:13:34 +01:00

25 lines
915 B
TypeScript

import fs from 'node:fs/promises';
import os from 'node:os';
import path from 'node:path';
// Generates a short-lived temp directory with a stable, short base to avoid
// UNIX-domain socket length issues on macOS/Linux. On Windows we stick to the
// platform temp dir.
export async function makeShortTempDir(prefix: string): Promise<string> {
const baseDir = process.platform === 'win32' ? os.tmpdir() : '/tmp';
const dir = path.join(baseDir, `${prefix}-${Date.now().toString(36)}-${Math.random().toString(16).slice(2, 8)}`);
await fs.mkdir(dir, { recursive: true });
return dir;
}
// Safely restore cwd when running under Vitest workers (which disallow chdir).
export function restoreCwdSafely(originalCwd: string): void {
try {
process.chdir(originalCwd);
} catch (error) {
if (!(error instanceof Error) || !/chdir\(\) is not supported/.test(error.message)) {
throw error;
}
}
}