@@ -577,6 +577,12 @@ function patchDocument(path: string, diff: string): string {
577577 ] . join ( "\n" ) ;
578578}
579579
580+ function printPatchRefusal ( patch : string ) : string {
581+ return [ "<PrintErrors>" , ...patch . split ( "\n" ) . map ( ( line ) => " " + line ) , "</PrintErrors>" ] . join (
582+ "\n" ,
583+ ) ;
584+ }
585+
580586const SMALL_PATCH = [
581587 "```diff" ,
582588 "--- a/target.txt" ,
@@ -722,6 +728,30 @@ describe("File.Patch", () => {
722728 }
723729 } ) ;
724730
731+ it ( "stops later effects on refusal unless PrintErrors encloses the patch" , function * ( ) {
732+ const fixture = yield * useFixture ( ) ;
733+ const target = join ( fixture . workspace , "target.txt" ) ;
734+ const later = join ( fixture . workspace , "later.txt" ) ;
735+ const before = new TextEncoder ( ) . encode ( "first\nchanged\r\nlast" ) ;
736+ yield * until ( writeFile ( target , before ) ) ;
737+ const patch = patchDocument ( "target.txt" , SMALL_PATCH ) ;
738+ const following = '\n<File path="later.txt">continued</File>' ;
739+ let refusal = "" ;
740+ try {
741+ yield * run ( fixture , patch + following ) ;
742+ } catch ( error ) {
743+ refusal = String ( error ) ;
744+ }
745+ expect ( refusal ) . toContain ( "the diff no longer matches" ) ;
746+ expect ( yield * exists ( later ) ) . toBe ( false ) ;
747+ expect ( new Uint8Array ( yield * until ( readFile ( target ) ) ) ) . toEqual ( before ) ;
748+
749+ const output = yield * run ( fixture , printPatchRefusal ( patch + following ) ) ;
750+ expect ( output ) . toContain ( "the diff no longer matches" ) ;
751+ expect ( yield * readTextFile ( later ) ) . toBe ( "continued" ) ;
752+ expect ( new Uint8Array ( yield * until ( readFile ( target ) ) ) ) . toEqual ( before ) ;
753+ } ) ;
754+
725755 it ( "refuses the entire edit for stale, malformed or wrong-target diffs" , function * ( ) {
726756 const fixture = yield * useFixture ( ) ;
727757 const target = join ( fixture . workspace , "target.txt" ) ;
@@ -735,7 +765,9 @@ describe("File.Patch", () => {
735765 [ SMALL_PATCH . replace ( "-2 +2" , "-2,2 +2" ) , "exact, single-file unified diff" ] ,
736766 [ SMALL_PATCH . replaceAll ( "target.txt" , "other.txt" ) , "exact, single-file unified diff" ] ,
737767 ] ) {
738- expect ( yield * run ( fixture , patchDocument ( "target.txt" , diff ) ) ) . toContain ( message ) ;
768+ expect ( yield * run ( fixture , printPatchRefusal ( patchDocument ( "target.txt" , diff ) ) ) ) . toContain (
769+ message ,
770+ ) ;
739771 expect ( new Uint8Array ( yield * until ( readFile ( target ) ) ) ) . toEqual ( before ) ;
740772 expect ( new Uint8Array ( yield * until ( readFile ( other ) ) ) ) . toEqual ( before ) ;
741773 }
@@ -847,9 +879,9 @@ describe("File.Patch", () => {
847879 const target = join ( fixture . workspace , "target.txt" ) ;
848880 const invalid = new Uint8Array ( [ 0xff , ...new TextEncoder ( ) . encode ( "\nold\n" ) ] ) ;
849881 yield * until ( writeFile ( target , invalid ) ) ;
850- expect ( yield * run ( fixture , patchDocument ( "target.txt" , SMALL_PATCH ) ) ) . toContain (
851- "existing bytes no longer match" ,
852- ) ;
882+ expect (
883+ yield * run ( fixture , printPatchRefusal ( patchDocument ( "target.txt" , SMALL_PATCH ) ) ) ,
884+ ) . toContain ( "existing bytes no longer match" ) ;
853885 expect ( new Uint8Array ( yield * until ( readFile ( target ) ) ) ) . toEqual ( invalid ) ;
854886
855887 yield * writeTextFile ( target , "first\r\nold" ) ;
@@ -868,13 +900,19 @@ describe("File.Patch", () => {
868900 yield * writeTextFile ( other , before ) ;
869901 const outside = "../outside/other.txt" ;
870902 const outsidePatch = SMALL_PATCH . replaceAll ( "target.txt" , outside ) ;
871- expect ( yield * run ( fixture , patchDocument ( outside , outsidePatch ) ) ) . toContain ( "outside" ) ;
903+ expect ( yield * run ( fixture , printPatchRefusal ( patchDocument ( outside , outsidePatch ) ) ) ) . toContain (
904+ "outside" ,
905+ ) ;
872906 expect (
873- yield * run ( fixture , patchDocument ( "target.txt" , SMALL_PATCH ) , function * ( ) {
874- yield * Permissions . around ( {
875- files : ( [ operation ] , next ) => ( operation === "write" ? false : next ( operation ) ) ,
876- } ) ;
877- } ) ,
907+ yield * run (
908+ fixture ,
909+ printPatchRefusal ( patchDocument ( "target.txt" , SMALL_PATCH ) ) ,
910+ function * ( ) {
911+ yield * Permissions . around ( {
912+ files : ( [ operation ] , next ) => ( operation === "write" ? false : next ( operation ) ) ,
913+ } ) ;
914+ } ,
915+ ) ,
878916 ) . toContain ( "permission denied" ) ;
879917 expect ( yield * readTextFile ( target ) ) . toBe ( before ) ;
880918 expect ( yield * readTextFile ( other ) ) . toBe ( before ) ;
@@ -885,6 +923,7 @@ describe("File.Patch", () => {
885923 const output = yield * run ( fixture , '<Syntax names={["File.Patch"]} />' ) ;
886924 expect ( output ) . toContain ( '<File.Patch path="notes.md">' ) ;
887925 expect ( output ) . toContain ( "Exactly one passive fenced unified diff" ) ;
888- expect ( output ) . toContain ( "refused patch" ) ;
926+ expect ( output ) . toContain ( "Apply a unified diff to a file." ) ;
927+ expect ( output ) . toContain ( "A refusal throws by default." ) ;
889928 } ) ;
890929} ) ;
0 commit comments