Skip to content

Commit 78a9ddc

Browse files
anandgupta42claude
andcommitted
test: scope the dangling-symlink teardown
Three reviewers converged: the symlink's unlink ran only on the success path — a failed assertion would leak it into the real data dir, where the fail-safe under test deliberately keeps it forever. New `symlinkScoped` helper (mirroring `writeFileStringScoped`) unlinks via a scope finalizer, which runs regardless of assertion outcome. Co-Authored-By: Claude Fable 5 <[email protected]>
1 parent 439a145 commit 78a9ddc

2 files changed

Lines changed: 15 additions & 3 deletions

File tree

‎packages/opencode/test/lib/filesystem.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,13 @@ export const writeFileStringScoped = Effect.fn("test.writeFileStringScoped")(fun
88
yield* Effect.addFinalizer(() => fs.remove(file, { force: true }).pipe(Effect.orDie))
99
return file
1010
})
11+
12+
/** Create a symlink whose removal is guaranteed by the test scope, even when
13+
* an assertion fails mid-test (finalizers run on scope close regardless). */
14+
export const symlinkScoped = Effect.fn("test.symlinkScoped")(function* (target: string, link: string) {
15+
yield* Effect.promise(() => import("node:fs/promises").then((nfs) => nfs.symlink(target, link)))
16+
yield* Effect.addFinalizer(() =>
17+
Effect.promise(() => import("node:fs/promises").then((nfs) => nfs.unlink(link).catch(() => {}))),
18+
)
19+
return link
20+
})

‎packages/opencode/test/tool/truncation.test.ts‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { Identifier } from "../../src/id/id"
99
import { Process } from "@/util/process"
1010
import path from "path"
1111
import { testEffect } from "../lib/effect"
12-
import { writeFileStringScoped } from "../lib/filesystem"
12+
import { symlinkScoped, writeFileStringScoped } from "../lib/filesystem"
1313
import { TestConfig } from "../fixture/config"
1414

1515
const FIXTURES_DIR = path.join(import.meta.dir, "fixtures")
@@ -262,7 +262,10 @@ describe("Truncate", () => {
262262
yield* writeFileStringScoped(old, "old content")
263263
yield* writeFileStringScoped(recent, "recent content")
264264
const nfs = yield* Effect.promise(() => import("node:fs/promises"))
265-
yield* Effect.promise(() => nfs.symlink(path.join(Truncate.DIR, "nonexistent-target"), dangling))
265+
// Scoped: the finalizer unlinks it even when an assertion fails —
266+
// otherwise a failed expect would leak the link into the real data
267+
// dir, where the fail-safe under test deliberately keeps it forever.
268+
yield* symlinkScoped(path.join(Truncate.DIR, "nonexistent-target"), dangling)
266269
const oldTime = new Date(Date.now() - 10 * DAY_MS)
267270
const recentTime = new Date(Date.now() - 3 * DAY_MS)
268271
yield* Effect.promise(() => nfs.utimes(old, oldTime, oldTime))
@@ -275,7 +278,6 @@ describe("Truncate", () => {
275278
// dangling link even when the link itself survived.
276279
const danglingKept = yield* Effect.promise(() => nfs.lstat(dangling).then(() => true).catch(() => false))
277280
expect(danglingKept).toBe(true)
278-
yield* Effect.promise(() => nfs.unlink(dangling).catch(() => {}))
279281
}),
280282
)
281283
})

0 commit comments

Comments
 (0)