mirror of
https://gitea.toothfairyai.com/ToothFairyAI/tf_code.git
synced 2026-03-30 13:54:01 +00:00
2.0 KiB
2.0 KiB
Test Fixtures Guide
Temporary Directory Fixture
The tmpdir function in fixture/fixture.ts creates temporary directories for tests with automatic cleanup.
Basic Usage
import { tmpdir } from "./fixture/fixture"
test("example", async () => {
await using tmp = await tmpdir()
// tmp.path is the temp directory path
// automatically cleaned up when test ends
})
Options
git?: boolean- Initialize a git repo with a root commitconfig?: Partial<Config.Info>- Write anopencode.jsonconfig fileinit?: (dir: string) => Promise<T>- Custom setup function, returns value accessible astmp.extradispose?: (dir: string) => Promise<T>- Custom cleanup function
Examples
Git repository:
await using tmp = await tmpdir({ git: true })
With config file:
await using tmp = await tmpdir({
config: { model: "test/model", username: "testuser" },
})
Custom initialization (returns extra data):
await using tmp = await tmpdir<string>({
init: async (dir) => {
await Bun.write(path.join(dir, "file.txt"), "content")
return "extra data"
},
})
// Access extra data via tmp.extra
console.log(tmp.extra) // "extra data"
With cleanup:
await using tmp = await tmpdir({
init: async (dir) => {
const specialDir = path.join(dir, "special")
await fs.mkdir(specialDir)
return specialDir
},
dispose: async (dir) => {
// Custom cleanup logic
await fs.rm(path.join(dir, "special"), { recursive: true })
},
})
Returned Object
path: string- Absolute path to the temp directory (realpath resolved)extra: T- Value returned by theinitfunction[Symbol.asyncDispose]- Enables automatic cleanup viaawait using
Notes
- Directories are created in the system temp folder with prefix
opencode-test- - Use
await usingfor automatic cleanup when the variable goes out of scope - Paths are sanitized to strip null bytes (defensive fix for CI environments)