Remove use of Bun.file (#14215)

This commit is contained in:
Dax
2026-02-19 11:32:32 -05:00
committed by GitHub
parent 0fcba68d4c
commit 02a9495063
44 changed files with 634 additions and 473 deletions

View File

@@ -1,7 +1,9 @@
import { test, expect } from "bun:test"
import { $ } from "bun"
import fs from "fs/promises"
import { Snapshot } from "../../src/snapshot"
import { Instance } from "../../src/project/instance"
import { Filesystem } from "../../src/util/filesystem"
import { tmpdir } from "../fixture/fixture"
async function bootstrap() {
@@ -11,8 +13,8 @@ async function bootstrap() {
const unique = Math.random().toString(36).slice(2)
const aContent = `A${unique}`
const bContent = `B${unique}`
await Bun.write(`${dir}/a.txt`, aContent)
await Bun.write(`${dir}/b.txt`, bContent)
await Filesystem.write(`${dir}/a.txt`, aContent)
await Filesystem.write(`${dir}/b.txt`, bContent)
await $`git add .`.cwd(dir).quiet()
await $`git commit --no-gpg-sign -m init`.cwd(dir).quiet()
return {
@@ -46,11 +48,16 @@ test("revert should remove new files", async () => {
const before = await Snapshot.track()
expect(before).toBeTruthy()
await Bun.write(`${tmp.path}/new.txt`, "NEW")
await Filesystem.write(`${tmp.path}/new.txt`, "NEW")
await Snapshot.revert([await Snapshot.patch(before!)])
expect(await Bun.file(`${tmp.path}/new.txt`).exists()).toBe(false)
expect(
await fs
.access(`${tmp.path}/new.txt`)
.then(() => true)
.catch(() => false),
).toBe(false)
},
})
})
@@ -64,11 +71,16 @@ test("revert in subdirectory", async () => {
expect(before).toBeTruthy()
await $`mkdir -p ${tmp.path}/sub`.quiet()
await Bun.write(`${tmp.path}/sub/file.txt`, "SUB")
await Filesystem.write(`${tmp.path}/sub/file.txt`, "SUB")
await Snapshot.revert([await Snapshot.patch(before!)])
expect(await Bun.file(`${tmp.path}/sub/file.txt`).exists()).toBe(false)
expect(
await fs
.access(`${tmp.path}/sub/file.txt`)
.then(() => true)
.catch(() => false),
).toBe(false)
// Note: revert currently only removes files, not directories
// The empty subdirectory will remain
},
@@ -84,18 +96,23 @@ test("multiple file operations", async () => {
expect(before).toBeTruthy()
await $`rm ${tmp.path}/a.txt`.quiet()
await Bun.write(`${tmp.path}/c.txt`, "C")
await Filesystem.write(`${tmp.path}/c.txt`, "C")
await $`mkdir -p ${tmp.path}/dir`.quiet()
await Bun.write(`${tmp.path}/dir/d.txt`, "D")
await Bun.write(`${tmp.path}/b.txt`, "MODIFIED")
await Filesystem.write(`${tmp.path}/dir/d.txt`, "D")
await Filesystem.write(`${tmp.path}/b.txt`, "MODIFIED")
await Snapshot.revert([await Snapshot.patch(before!)])
expect(await Bun.file(`${tmp.path}/a.txt`).text()).toBe(tmp.extra.aContent)
expect(await Bun.file(`${tmp.path}/c.txt`).exists()).toBe(false)
expect(await fs.readFile(`${tmp.path}/a.txt`, "utf-8")).toBe(tmp.extra.aContent)
expect(
await fs
.access(`${tmp.path}/c.txt`)
.then(() => true)
.catch(() => false),
).toBe(false)
// Note: revert currently only removes files, not directories
// The empty directory will remain
expect(await Bun.file(`${tmp.path}/b.txt`).text()).toBe(tmp.extra.bContent)
expect(await fs.readFile(`${tmp.path}/b.txt`, "utf-8")).toBe(tmp.extra.bContent)
},
})
})
@@ -123,13 +140,18 @@ test("binary file handling", async () => {
const before = await Snapshot.track()
expect(before).toBeTruthy()
await Bun.write(`${tmp.path}/image.png`, new Uint8Array([0x89, 0x50, 0x4e, 0x47]))
await Filesystem.write(`${tmp.path}/image.png`, new Uint8Array([0x89, 0x50, 0x4e, 0x47]))
const patch = await Snapshot.patch(before!)
expect(patch.files).toContain(`${tmp.path}/image.png`)
await Snapshot.revert([patch])
expect(await Bun.file(`${tmp.path}/image.png`).exists()).toBe(false)
expect(
await fs
.access(`${tmp.path}/image.png`)
.then(() => true)
.catch(() => false),
).toBe(false)
},
})
})
@@ -157,7 +179,7 @@ test("large file handling", async () => {
const before = await Snapshot.track()
expect(before).toBeTruthy()
await Bun.write(`${tmp.path}/large.txt`, "x".repeat(1024 * 1024))
await Filesystem.write(`${tmp.path}/large.txt`, "x".repeat(1024 * 1024))
expect((await Snapshot.patch(before!)).files).toContain(`${tmp.path}/large.txt`)
},
@@ -173,11 +195,16 @@ test("nested directory revert", async () => {
expect(before).toBeTruthy()
await $`mkdir -p ${tmp.path}/level1/level2/level3`.quiet()
await Bun.write(`${tmp.path}/level1/level2/level3/deep.txt`, "DEEP")
await Filesystem.write(`${tmp.path}/level1/level2/level3/deep.txt`, "DEEP")
await Snapshot.revert([await Snapshot.patch(before!)])
expect(await Bun.file(`${tmp.path}/level1/level2/level3/deep.txt`).exists()).toBe(false)
expect(
await fs
.access(`${tmp.path}/level1/level2/level3/deep.txt`)
.then(() => true)
.catch(() => false),
).toBe(false)
},
})
})
@@ -190,9 +217,9 @@ test("special characters in filenames", async () => {
const before = await Snapshot.track()
expect(before).toBeTruthy()
await Bun.write(`${tmp.path}/file with spaces.txt`, "SPACES")
await Bun.write(`${tmp.path}/file-with-dashes.txt`, "DASHES")
await Bun.write(`${tmp.path}/file_with_underscores.txt`, "UNDERSCORES")
await Filesystem.write(`${tmp.path}/file with spaces.txt`, "SPACES")
await Filesystem.write(`${tmp.path}/file-with-dashes.txt`, "DASHES")
await Filesystem.write(`${tmp.path}/file_with_underscores.txt`, "UNDERSCORES")
const files = (await Snapshot.patch(before!)).files
expect(files).toContain(`${tmp.path}/file with spaces.txt`)
@@ -225,7 +252,7 @@ test("patch with invalid hash", async () => {
expect(before).toBeTruthy()
// Create a change
await Bun.write(`${tmp.path}/test.txt`, "TEST")
await Filesystem.write(`${tmp.path}/test.txt`, "TEST")
// Try to patch with invalid hash - should handle gracefully
const patch = await Snapshot.patch("invalid-hash-12345")
@@ -273,7 +300,7 @@ test("unicode filenames", async () => {
]
for (const file of unicodeFiles) {
await Bun.write(file.path, file.content)
await Filesystem.write(file.path, file.content)
}
const patch = await Snapshot.patch(before!)
@@ -286,7 +313,12 @@ test("unicode filenames", async () => {
await Snapshot.revert([patch])
for (const file of unicodeFiles) {
expect(await Bun.file(file.path).exists()).toBe(false)
expect(
await fs
.access(file.path)
.then(() => true)
.catch(() => false),
).toBe(false)
}
},
})
@@ -300,14 +332,14 @@ test.skip("unicode filenames modification and restore", async () => {
const chineseFile = `${tmp.path}/文件.txt`
const cyrillicFile = `${tmp.path}/файл.txt`
await Bun.write(chineseFile, "original chinese")
await Bun.write(cyrillicFile, "original cyrillic")
await Filesystem.write(chineseFile, "original chinese")
await Filesystem.write(cyrillicFile, "original cyrillic")
const before = await Snapshot.track()
expect(before).toBeTruthy()
await Bun.write(chineseFile, "modified chinese")
await Bun.write(cyrillicFile, "modified cyrillic")
await Filesystem.write(chineseFile, "modified chinese")
await Filesystem.write(cyrillicFile, "modified cyrillic")
const patch = await Snapshot.patch(before!)
expect(patch.files).toContain(chineseFile)
@@ -315,8 +347,8 @@ test.skip("unicode filenames modification and restore", async () => {
await Snapshot.revert([patch])
expect(await Bun.file(chineseFile).text()).toBe("original chinese")
expect(await Bun.file(cyrillicFile).text()).toBe("original cyrillic")
expect(await fs.readFile(chineseFile, "utf-8")).toBe("original chinese")
expect(await fs.readFile(cyrillicFile, "utf-8")).toBe("original cyrillic")
},
})
})
@@ -331,13 +363,18 @@ test("unicode filenames in subdirectories", async () => {
await $`mkdir -p "${tmp.path}/目录/подкаталог"`.quiet()
const deepFile = `${tmp.path}/目录/подкаталог/文件.txt`
await Bun.write(deepFile, "deep unicode content")
await Filesystem.write(deepFile, "deep unicode content")
const patch = await Snapshot.patch(before!)
expect(patch.files).toContain(deepFile)
await Snapshot.revert([patch])
expect(await Bun.file(deepFile).exists()).toBe(false)
expect(
await fs
.access(deepFile)
.then(() => true)
.catch(() => false),
).toBe(false)
},
})
})
@@ -353,13 +390,18 @@ test("very long filenames", async () => {
const longName = "a".repeat(200) + ".txt"
const longFile = `${tmp.path}/${longName}`
await Bun.write(longFile, "long filename content")
await Filesystem.write(longFile, "long filename content")
const patch = await Snapshot.patch(before!)
expect(patch.files).toContain(longFile)
await Snapshot.revert([patch])
expect(await Bun.file(longFile).exists()).toBe(false)
expect(
await fs
.access(longFile)
.then(() => true)
.catch(() => false),
).toBe(false)
},
})
})
@@ -372,9 +414,9 @@ test("hidden files", async () => {
const before = await Snapshot.track()
expect(before).toBeTruthy()
await Bun.write(`${tmp.path}/.hidden`, "hidden content")
await Bun.write(`${tmp.path}/.gitignore`, "*.log")
await Bun.write(`${tmp.path}/.config`, "config content")
await Filesystem.write(`${tmp.path}/.hidden`, "hidden content")
await Filesystem.write(`${tmp.path}/.gitignore`, "*.log")
await Filesystem.write(`${tmp.path}/.config`, "config content")
const patch = await Snapshot.patch(before!)
expect(patch.files).toContain(`${tmp.path}/.hidden`)
@@ -393,7 +435,7 @@ test("nested symlinks", async () => {
expect(before).toBeTruthy()
await $`mkdir -p ${tmp.path}/sub/dir`.quiet()
await Bun.write(`${tmp.path}/sub/dir/target.txt`, "target content")
await Filesystem.write(`${tmp.path}/sub/dir/target.txt`, "target content")
await $`ln -s ${tmp.path}/sub/dir/target.txt ${tmp.path}/sub/dir/link.txt`.quiet()
await $`ln -s ${tmp.path}/sub ${tmp.path}/sub-link`.quiet()
@@ -450,9 +492,9 @@ test("gitignore changes", async () => {
const before = await Snapshot.track()
expect(before).toBeTruthy()
await Bun.write(`${tmp.path}/.gitignore`, "*.ignored")
await Bun.write(`${tmp.path}/test.ignored`, "ignored content")
await Bun.write(`${tmp.path}/normal.txt`, "normal content")
await Filesystem.write(`${tmp.path}/.gitignore`, "*.ignored")
await Filesystem.write(`${tmp.path}/test.ignored`, "ignored content")
await Filesystem.write(`${tmp.path}/normal.txt`, "normal content")
const patch = await Snapshot.patch(before!)
@@ -477,7 +519,7 @@ test("concurrent file operations during patch", async () => {
// Start creating files
const createPromise = (async () => {
for (let i = 0; i < 10; i++) {
await Bun.write(`${tmp.path}/concurrent${i}.txt`, `concurrent${i}`)
await Filesystem.write(`${tmp.path}/concurrent${i}.txt`, `concurrent${i}`)
// Small delay to simulate concurrent operations
await new Promise((resolve) => setTimeout(resolve, 1))
}
@@ -504,7 +546,7 @@ test("snapshot state isolation between projects", async () => {
directory: tmp1.path,
fn: async () => {
const before1 = await Snapshot.track()
await Bun.write(`${tmp1.path}/project1.txt`, "project1 content")
await Filesystem.write(`${tmp1.path}/project1.txt`, "project1 content")
const patch1 = await Snapshot.patch(before1!)
expect(patch1.files).toContain(`${tmp1.path}/project1.txt`)
},
@@ -514,7 +556,7 @@ test("snapshot state isolation between projects", async () => {
directory: tmp2.path,
fn: async () => {
const before2 = await Snapshot.track()
await Bun.write(`${tmp2.path}/project2.txt`, "project2 content")
await Filesystem.write(`${tmp2.path}/project2.txt`, "project2 content")
const patch2 = await Snapshot.patch(before2!)
expect(patch2.files).toContain(`${tmp2.path}/project2.txt`)
@@ -544,7 +586,7 @@ test("patch detects changes in secondary worktree", async () => {
expect(before).toBeTruthy()
const worktreeFile = `${worktreePath}/worktree.txt`
await Bun.write(worktreeFile, "worktree content")
await Filesystem.write(worktreeFile, "worktree content")
const patch = await Snapshot.patch(before!)
expect(patch.files).toContain(worktreeFile)
@@ -569,7 +611,7 @@ test("revert only removes files in invoking worktree", async () => {
},
})
const primaryFile = `${tmp.path}/worktree.txt`
await Bun.write(primaryFile, "primary content")
await Filesystem.write(primaryFile, "primary content")
await Instance.provide({
directory: worktreePath,
@@ -578,16 +620,21 @@ test("revert only removes files in invoking worktree", async () => {
expect(before).toBeTruthy()
const worktreeFile = `${worktreePath}/worktree.txt`
await Bun.write(worktreeFile, "worktree content")
await Filesystem.write(worktreeFile, "worktree content")
const patch = await Snapshot.patch(before!)
await Snapshot.revert([patch])
expect(await Bun.file(worktreeFile).exists()).toBe(false)
expect(
await fs
.access(worktreeFile)
.then(() => true)
.catch(() => false),
).toBe(false)
},
})
expect(await Bun.file(primaryFile).text()).toBe("primary content")
expect(await fs.readFile(primaryFile, "utf-8")).toBe("primary content")
} finally {
await $`git worktree remove --force ${worktreePath}`.cwd(tmp.path).quiet().nothrow()
await $`rm -rf ${worktreePath}`.quiet()
@@ -614,10 +661,10 @@ test("diff reports worktree-only/shared edits and ignores primary-only", async (
const before = await Snapshot.track()
expect(before).toBeTruthy()
await Bun.write(`${worktreePath}/worktree-only.txt`, "worktree diff content")
await Bun.write(`${worktreePath}/shared.txt`, "worktree edit")
await Bun.write(`${tmp.path}/shared.txt`, "primary edit")
await Bun.write(`${tmp.path}/primary-only.txt`, "primary change")
await Filesystem.write(`${worktreePath}/worktree-only.txt`, "worktree diff content")
await Filesystem.write(`${worktreePath}/shared.txt`, "worktree edit")
await Filesystem.write(`${tmp.path}/shared.txt`, "primary edit")
await Filesystem.write(`${tmp.path}/primary-only.txt`, "primary change")
const diff = await Snapshot.diff(before!)
expect(diff).toContain("worktree-only.txt")
@@ -662,8 +709,8 @@ test("diff function with various changes", async () => {
// Make various changes
await $`rm ${tmp.path}/a.txt`.quiet()
await Bun.write(`${tmp.path}/new.txt`, "new content")
await Bun.write(`${tmp.path}/b.txt`, "modified content")
await Filesystem.write(`${tmp.path}/new.txt`, "new content")
await Filesystem.write(`${tmp.path}/b.txt`, "modified content")
const diff = await Snapshot.diff(before!)
expect(diff).toContain("a.txt")
@@ -683,16 +730,26 @@ test("restore function", async () => {
// Make changes
await $`rm ${tmp.path}/a.txt`.quiet()
await Bun.write(`${tmp.path}/new.txt`, "new content")
await Bun.write(`${tmp.path}/b.txt`, "modified")
await Filesystem.write(`${tmp.path}/new.txt`, "new content")
await Filesystem.write(`${tmp.path}/b.txt`, "modified")
// Restore to original state
await Snapshot.restore(before!)
expect(await Bun.file(`${tmp.path}/a.txt`).exists()).toBe(true)
expect(await Bun.file(`${tmp.path}/a.txt`).text()).toBe(tmp.extra.aContent)
expect(await Bun.file(`${tmp.path}/new.txt`).exists()).toBe(true) // New files should remain
expect(await Bun.file(`${tmp.path}/b.txt`).text()).toBe(tmp.extra.bContent)
expect(
await fs
.access(`${tmp.path}/a.txt`)
.then(() => true)
.catch(() => false),
).toBe(true)
expect(await fs.readFile(`${tmp.path}/a.txt`, "utf-8")).toBe(tmp.extra.aContent)
expect(
await fs
.access(`${tmp.path}/new.txt`)
.then(() => true)
.catch(() => false),
).toBe(true) // New files should remain
expect(await fs.readFile(`${tmp.path}/b.txt`, "utf-8")).toBe(tmp.extra.bContent)
},
})
})
@@ -710,14 +767,19 @@ test("revert should not delete files that existed but were deleted in snapshot",
const snapshot2 = await Snapshot.track()
expect(snapshot2).toBeTruthy()
await Bun.write(`${tmp.path}/a.txt`, "recreated content")
await Filesystem.write(`${tmp.path}/a.txt`, "recreated content")
const patch = await Snapshot.patch(snapshot2!)
expect(patch.files).toContain(`${tmp.path}/a.txt`)
await Snapshot.revert([patch])
expect(await Bun.file(`${tmp.path}/a.txt`).exists()).toBe(false)
expect(
await fs
.access(`${tmp.path}/a.txt`)
.then(() => true)
.catch(() => false),
).toBe(false)
},
})
})
@@ -727,14 +789,14 @@ test("revert preserves file that existed in snapshot when deleted then recreated
await Instance.provide({
directory: tmp.path,
fn: async () => {
await Bun.write(`${tmp.path}/existing.txt`, "original content")
await Filesystem.write(`${tmp.path}/existing.txt`, "original content")
const snapshot = await Snapshot.track()
expect(snapshot).toBeTruthy()
await $`rm ${tmp.path}/existing.txt`.quiet()
await Bun.write(`${tmp.path}/existing.txt`, "recreated")
await Bun.write(`${tmp.path}/newfile.txt`, "new")
await Filesystem.write(`${tmp.path}/existing.txt`, "recreated")
await Filesystem.write(`${tmp.path}/newfile.txt`, "new")
const patch = await Snapshot.patch(snapshot!)
expect(patch.files).toContain(`${tmp.path}/existing.txt`)
@@ -742,9 +804,19 @@ test("revert preserves file that existed in snapshot when deleted then recreated
await Snapshot.revert([patch])
expect(await Bun.file(`${tmp.path}/newfile.txt`).exists()).toBe(false)
expect(await Bun.file(`${tmp.path}/existing.txt`).exists()).toBe(true)
expect(await Bun.file(`${tmp.path}/existing.txt`).text()).toBe("original content")
expect(
await fs
.access(`${tmp.path}/newfile.txt`)
.then(() => true)
.catch(() => false),
).toBe(false)
expect(
await fs
.access(`${tmp.path}/existing.txt`)
.then(() => true)
.catch(() => false),
).toBe(true)
expect(await fs.readFile(`${tmp.path}/existing.txt`, "utf-8")).toBe("original content")
},
})
})
@@ -754,17 +826,17 @@ test("diffFull sets status based on git change type", async () => {
await Instance.provide({
directory: tmp.path,
fn: async () => {
await Bun.write(`${tmp.path}/grow.txt`, "one\n")
await Bun.write(`${tmp.path}/trim.txt`, "line1\nline2\n")
await Bun.write(`${tmp.path}/delete.txt`, "gone")
await Filesystem.write(`${tmp.path}/grow.txt`, "one\n")
await Filesystem.write(`${tmp.path}/trim.txt`, "line1\nline2\n")
await Filesystem.write(`${tmp.path}/delete.txt`, "gone")
const before = await Snapshot.track()
expect(before).toBeTruthy()
await Bun.write(`${tmp.path}/grow.txt`, "one\ntwo\n")
await Bun.write(`${tmp.path}/trim.txt`, "line1\n")
await Filesystem.write(`${tmp.path}/grow.txt`, "one\ntwo\n")
await Filesystem.write(`${tmp.path}/trim.txt`, "line1\n")
await $`rm ${tmp.path}/delete.txt`.quiet()
await Bun.write(`${tmp.path}/added.txt`, "new")
await Filesystem.write(`${tmp.path}/added.txt`, "new")
const after = await Snapshot.track()
expect(after).toBeTruthy()
@@ -803,7 +875,7 @@ test("diffFull with new file additions", async () => {
const before = await Snapshot.track()
expect(before).toBeTruthy()
await Bun.write(`${tmp.path}/new.txt`, "new content")
await Filesystem.write(`${tmp.path}/new.txt`, "new content")
const after = await Snapshot.track()
expect(after).toBeTruthy()
@@ -829,7 +901,7 @@ test("diffFull with file modifications", async () => {
const before = await Snapshot.track()
expect(before).toBeTruthy()
await Bun.write(`${tmp.path}/b.txt`, "modified content")
await Filesystem.write(`${tmp.path}/b.txt`, "modified content")
const after = await Snapshot.track()
expect(after).toBeTruthy()
@@ -881,7 +953,7 @@ test("diffFull with multiple line additions", async () => {
const before = await Snapshot.track()
expect(before).toBeTruthy()
await Bun.write(`${tmp.path}/multi.txt`, "line1\nline2\nline3")
await Filesystem.write(`${tmp.path}/multi.txt`, "line1\nline2\nline3")
const after = await Snapshot.track()
expect(after).toBeTruthy()
@@ -907,7 +979,7 @@ test("diffFull with addition and deletion", async () => {
const before = await Snapshot.track()
expect(before).toBeTruthy()
await Bun.write(`${tmp.path}/added.txt`, "added content")
await Filesystem.write(`${tmp.path}/added.txt`, "added content")
await $`rm ${tmp.path}/a.txt`.quiet()
const after = await Snapshot.track()
@@ -941,8 +1013,8 @@ test("diffFull with multiple additions and deletions", async () => {
const before = await Snapshot.track()
expect(before).toBeTruthy()
await Bun.write(`${tmp.path}/multi1.txt`, "line1\nline2\nline3")
await Bun.write(`${tmp.path}/multi2.txt`, "single line")
await Filesystem.write(`${tmp.path}/multi1.txt`, "line1\nline2\nline3")
await Filesystem.write(`${tmp.path}/multi2.txt`, "single line")
await $`rm ${tmp.path}/a.txt`.quiet()
await $`rm ${tmp.path}/b.txt`.quiet()
@@ -1000,7 +1072,7 @@ test("diffFull with binary file changes", async () => {
const before = await Snapshot.track()
expect(before).toBeTruthy()
await Bun.write(`${tmp.path}/binary.bin`, new Uint8Array([0x00, 0x01, 0x02, 0x03]))
await Filesystem.write(`${tmp.path}/binary.bin`, new Uint8Array([0x00, 0x01, 0x02, 0x03]))
const after = await Snapshot.track()
expect(after).toBeTruthy()
@@ -1020,11 +1092,11 @@ test("diffFull with whitespace changes", async () => {
await Instance.provide({
directory: tmp.path,
fn: async () => {
await Bun.write(`${tmp.path}/whitespace.txt`, "line1\nline2")
await Filesystem.write(`${tmp.path}/whitespace.txt`, "line1\nline2")
const before = await Snapshot.track()
expect(before).toBeTruthy()
await Bun.write(`${tmp.path}/whitespace.txt`, "line1\n\nline2\n")
await Filesystem.write(`${tmp.path}/whitespace.txt`, "line1\n\nline2\n")
const after = await Snapshot.track()
expect(after).toBeTruthy()