|
1 | 1 | import { afterEach, beforeEach, describe, expect, test } from "bun:test"; |
2 | 2 | import { |
3 | 3 | chmod, |
| 4 | + lstat, |
4 | 5 | mkdtemp, |
5 | 6 | mkdir, |
| 7 | + readFile, |
6 | 8 | realpath, |
7 | 9 | rm, |
8 | 10 | stat, |
@@ -31,6 +33,15 @@ async function exists(path: string): Promise<boolean> { |
31 | 33 | } |
32 | 34 | } |
33 | 35 |
|
| 36 | +async function linkExists(path: string): Promise<boolean> { |
| 37 | + try { |
| 38 | + await lstat(path); |
| 39 | + return true; |
| 40 | + } catch { |
| 41 | + return false; |
| 42 | + } |
| 43 | +} |
| 44 | + |
34 | 45 | describe("deleteFilePlugin", () => { |
35 | 46 | let cwd: string; |
36 | 47 |
|
@@ -130,6 +141,43 @@ describe("deleteFilePlugin", () => { |
130 | 141 | await rm(outside, { recursive: true, force: true }); |
131 | 142 | }); |
132 | 143 |
|
| 144 | + test("deletes a dangling symlink inside cwd (CL-6729)", async () => { |
| 145 | + const link = join(cwd, "broken-link"); |
| 146 | + await symlink(join(cwd, "does-not-exist.txt"), link); |
| 147 | + expect(await linkExists(link)).toBe(true); |
| 148 | + |
| 149 | + const result = await handler()( |
| 150 | + call("broken-link"), |
| 151 | + new AbortController().signal, |
| 152 | + ); |
| 153 | + |
| 154 | + expect(result.isError ?? false).toBe(false); |
| 155 | + expect(String(result.content)).toContain("Deleted file: broken-link"); |
| 156 | + expect(await linkExists(link)).toBe(false); |
| 157 | + }); |
| 158 | + |
| 159 | + test("deletes a link with an outside referent without touching the referent (CL-6729)", async () => { |
| 160 | + const outside = await mkdtemp( |
| 161 | + join(tmpdir(), "corbits-delete-link-referent-"), |
| 162 | + ); |
| 163 | + const referent = join(outside, "keep.txt"); |
| 164 | + await writeFile(referent, "keep"); |
| 165 | + const link = join(cwd, "outside-link"); |
| 166 | + await symlink(referent, link); |
| 167 | + expect(await linkExists(link)).toBe(true); |
| 168 | + |
| 169 | + const result = await handler()( |
| 170 | + call("outside-link"), |
| 171 | + new AbortController().signal, |
| 172 | + ); |
| 173 | + |
| 174 | + expect(result.isError ?? false).toBe(false); |
| 175 | + expect(String(result.content)).toContain("Deleted file: outside-link"); |
| 176 | + expect(await linkExists(link)).toBe(false); |
| 177 | + expect(await readFile(referent, "utf8")).toBe("keep"); |
| 178 | + await rm(outside, { recursive: true, force: true }); |
| 179 | + }); |
| 180 | + |
133 | 181 | test("allowOutside deletes a file outside the working directory", async () => { |
134 | 182 | const outside = await mkdtemp(join(tmpdir(), "corbits-delete-yolo-")); |
135 | 183 | const path = join(outside, "gone.txt"); |
|
0 commit comments