@@ -84,6 +84,17 @@ function realFsBridge(root: string): SandboxFsBridge {
8484 await fs . mkdir ( toAbsolute ( filePath ) , { recursive : true } ) ;
8585 } ,
8686 stat : async ( { filePath } ) => stat ( filePath ) ,
87+ entryExists : async ( { filePath } ) => {
88+ try {
89+ await fs . lstat ( toAbsolute ( filePath ) ) ;
90+ return true ;
91+ } catch ( error ) {
92+ if ( ( error as NodeJS . ErrnoException ) . code === "ENOENT" ) {
93+ return false ;
94+ }
95+ throw error ;
96+ }
97+ } ,
8798 } ;
8899}
89100
@@ -156,4 +167,121 @@ describe("applyPatch write verification on a real filesystem", () => {
156167 // The corrupted bytes are really on disk: no false-success receipt.
157168 expect ( await fs . readFile ( path . join ( dir , "source.txt" ) , "utf8" ) ) . toBe ( "corrupted" ) ;
158169 } ) ;
170+
171+ it ( "persists a delete and a move source removal on a real filesystem" , async ( ) => {
172+ const dir = await makeTempDir ( ) ;
173+ await fs . writeFile ( path . join ( dir , "doomed.txt" ) , "x\n" , "utf8" ) ;
174+ await fs . writeFile ( path . join ( dir , "mover.txt" ) , "foo\nbar\n" , "utf8" ) ;
175+
176+ const patch = `*** Begin Patch
177+ *** Delete File: doomed.txt
178+ *** Update File: mover.txt
179+ *** Move to: moved.txt
180+ @@
181+ foo
182+ -bar
183+ +baz
184+ *** End Patch` ;
185+
186+ const result = await applyPatch ( patch , { cwd : dir , workspaceOnly : false } ) ;
187+
188+ expect ( result . summary . deleted ) . toContain ( "doomed.txt" ) ;
189+ expect ( result . summary . modified ) . toContain ( "moved.txt" ) ;
190+ await expect ( fs . lstat ( path . join ( dir , "doomed.txt" ) ) ) . rejects . toThrow ( ) ;
191+ await expect ( fs . lstat ( path . join ( dir , "mover.txt" ) ) ) . rejects . toThrow ( ) ;
192+ expect ( await fs . readFile ( path . join ( dir , "moved.txt" ) , "utf8" ) ) . toBe ( "foo\nbaz\n" ) ;
193+ } ) ;
194+
195+ it ( "rejects a delete whose real remove leaves the entry behind" , async ( ) => {
196+ const dir = await makeTempDir ( ) ;
197+ const bridge = realFsBridge ( dir ) ;
198+ // A remove that resolves without deleting the entry.
199+ const noOpRemoveBridge : SandboxFsBridge = {
200+ ...bridge ,
201+ remove : async ( ) => { } ,
202+ } ;
203+
204+ await fs . writeFile ( path . join ( dir , "source.txt" ) , "x\n" , "utf8" ) ;
205+
206+ const patch = `*** Begin Patch
207+ *** Delete File: source.txt
208+ *** End Patch` ;
209+
210+ await expect (
211+ applyPatch ( patch , { cwd : dir , sandbox : { root : dir , bridge : noOpRemoveBridge } } ) ,
212+ ) . rejects . toThrow (
213+ "ApplyPatch verification failed for source.txt: the entry still exists after removal." ,
214+ ) ;
215+ expect ( await fs . readFile ( path . join ( dir , "source.txt" ) , "utf8" ) ) . toBe ( "x\n" ) ;
216+ } ) ;
217+
218+ it ( "rejects a move whose real source removal leaves the source behind" , async ( ) => {
219+ const dir = await makeTempDir ( ) ;
220+ const bridge = realFsBridge ( dir ) ;
221+ const noOpRemoveBridge : SandboxFsBridge = {
222+ ...bridge ,
223+ remove : async ( ) => { } ,
224+ } ;
225+
226+ await fs . writeFile ( path . join ( dir , "source.txt" ) , "foo\nbar\n" , "utf8" ) ;
227+
228+ const patch = `*** Begin Patch
229+ *** Update File: source.txt
230+ *** Move to: dest.txt
231+ @@
232+ foo
233+ -bar
234+ +baz
235+ *** End Patch` ;
236+
237+ await expect (
238+ applyPatch ( patch , { cwd : dir , sandbox : { root : dir , bridge : noOpRemoveBridge } } ) ,
239+ ) . rejects . toThrow (
240+ "ApplyPatch verification failed for source.txt: the entry still exists after removal." ,
241+ ) ;
242+ // The verified destination is really on disk, and the unverified source
243+ // removal did not happen — both entries exist, so no success receipt.
244+ expect ( await fs . readFile ( path . join ( dir , "dest.txt" ) , "utf8" ) ) . toBe ( "foo\nbaz\n" ) ;
245+ expect ( await fs . readFile ( path . join ( dir , "source.txt" ) , "utf8" ) ) . toBe ( "foo\nbar\n" ) ;
246+ } ) ;
247+
248+ it ( "treats a still-present dangling symlink as a removal verification failure" , async ( ) => {
249+ const dir = await makeTempDir ( ) ;
250+ const bridge = realFsBridge ( dir ) ;
251+ // `stat` follows symlinks and would report this dangling link as absent;
252+ // the entry-existence check must use lstat semantics and reject the removal.
253+ const noOpRemoveBridge : SandboxFsBridge = {
254+ ...bridge ,
255+ remove : async ( ) => { } ,
256+ } ;
257+
258+ await fs . symlink ( path . join ( dir , "missing-target.txt" ) , path . join ( dir , "dangling.txt" ) ) ;
259+
260+ const patch = `*** Begin Patch
261+ *** Delete File: dangling.txt
262+ *** End Patch` ;
263+
264+ await expect (
265+ applyPatch ( patch , { cwd : dir , sandbox : { root : dir , bridge : noOpRemoveBridge } } ) ,
266+ ) . rejects . toThrow (
267+ "ApplyPatch verification failed for dangling.txt: the entry still exists after removal." ,
268+ ) ;
269+ await expect ( fs . readlink ( path . join ( dir , "dangling.txt" ) ) ) . resolves . toBe (
270+ path . join ( dir , "missing-target.txt" ) ,
271+ ) ;
272+ } ) ;
273+
274+ it ( "verifies a real delete of a dangling symlink succeeds" , async ( ) => {
275+ const dir = await makeTempDir ( ) ;
276+ await fs . symlink ( path . join ( dir , "missing-target.txt" ) , path . join ( dir , "dangling.txt" ) ) ;
277+
278+ const patch = `*** Begin Patch
279+ *** Delete File: dangling.txt
280+ *** End Patch` ;
281+
282+ const result = await applyPatch ( patch , { cwd : dir , workspaceOnly : false } ) ;
283+
284+ expect ( result . summary . deleted ) . toEqual ( [ "dangling.txt" ] ) ;
285+ await expect ( fs . lstat ( path . join ( dir , "dangling.txt" ) ) ) . rejects . toThrow ( ) ;
286+ } ) ;
159287} ) ;
0 commit comments