|
|
@@ -1,27 +1,33 @@
|
|
|
import { $ } from "bun"
|
|
|
-import { realpathSync } from "fs"
|
|
|
+import * as fs from "fs/promises"
|
|
|
import os from "os"
|
|
|
import path from "path"
|
|
|
|
|
|
+// Strip null bytes from paths (defensive fix for CI environment issues)
|
|
|
+function sanitizePath(p: string): string {
|
|
|
+ return p.replace(/\0/g, "")
|
|
|
+}
|
|
|
+
|
|
|
type TmpDirOptions<T> = {
|
|
|
git?: boolean
|
|
|
init?: (dir: string) => Promise<T>
|
|
|
dispose?: (dir: string) => Promise<T>
|
|
|
}
|
|
|
export async function tmpdir<T>(options?: TmpDirOptions<T>) {
|
|
|
- const dirpath = path.join(os.tmpdir(), "opencode-test-" + Math.random().toString(36).slice(2))
|
|
|
- await $`mkdir -p ${dirpath}`.quiet()
|
|
|
+ const dirpath = sanitizePath(path.join(os.tmpdir(), "opencode-test-" + Math.random().toString(36).slice(2)))
|
|
|
+ await fs.mkdir(dirpath, { recursive: true })
|
|
|
if (options?.git) {
|
|
|
await $`git init`.cwd(dirpath).quiet()
|
|
|
await $`git commit --allow-empty -m "root commit ${dirpath}"`.cwd(dirpath).quiet()
|
|
|
}
|
|
|
const extra = await options?.init?.(dirpath)
|
|
|
+ const realpath = sanitizePath(await fs.realpath(dirpath))
|
|
|
const result = {
|
|
|
[Symbol.asyncDispose]: async () => {
|
|
|
await options?.dispose?.(dirpath)
|
|
|
- await $`rm -rf ${dirpath}`.quiet()
|
|
|
+ await fs.rm(dirpath, { recursive: true, force: true })
|
|
|
},
|
|
|
- path: realpathSync(dirpath),
|
|
|
+ path: realpath,
|
|
|
extra: extra as T,
|
|
|
}
|
|
|
return result
|