@@ -30,12 +30,14 @@ describe("publishTreeSitterWasms", () => {
3030 expect ( fs . readFileSync ( path . join ( destination , "tree-sitter-a.wasm" ) , "utf8" ) ) . toBe ( "a" )
3131 } )
3232
33- it ( "removes published and temporary outputs when publication fails" , ( ) => {
33+ it ( "restores published outputs when publication fails" , ( ) => {
3434 const source = path . join ( root , "source" )
3535 const destination = path . join ( root , "dist" )
3636 fs . mkdirSync ( source )
37+ fs . mkdirSync ( destination )
3738 fs . writeFileSync ( path . join ( source , "tree-sitter-a.wasm" ) , "a" )
3839 fs . writeFileSync ( path . join ( source , "tree-sitter-b.wasm" ) , "b" )
40+ fs . writeFileSync ( path . join ( destination , "tree-sitter-existing.wasm" ) , "existing" )
3941 let copies = 0
4042 const filesystem = {
4143 ...fs ,
@@ -46,22 +48,31 @@ describe("publishTreeSitterWasms", () => {
4648 }
4749
4850 expect ( ( ) => publishTreeSitterWasms ( source , destination , filesystem ) ) . toThrow ( "copy failed" )
49- expect ( fs . readdirSync ( destination ) ) . toEqual ( [ ] )
51+ expect ( fs . readdirSync ( destination ) ) . toEqual ( [ "tree-sitter-existing.wasm" ] )
52+ expect ( fs . readFileSync ( path . join ( destination , "tree-sitter-existing.wasm" ) , "utf8" ) ) . toBe ( "existing" )
5053 } )
5154
52- it ( "removes published and temporary outputs when an atomic rename fails" , ( ) => {
55+ it ( "restores published outputs when an atomic rename fails" , ( ) => {
5356 const source = path . join ( root , "source" )
5457 const destination = path . join ( root , "dist" )
5558 fs . mkdirSync ( source )
59+ fs . mkdirSync ( destination )
5660 fs . writeFileSync ( path . join ( source , "tree-sitter-a.wasm" ) , "a" )
61+ fs . writeFileSync ( path . join ( source , "tree-sitter-b.wasm" ) , "b" )
62+ fs . writeFileSync ( path . join ( destination , "tree-sitter-a.wasm" ) , "previous-a" )
63+ fs . writeFileSync ( path . join ( destination , "tree-sitter-b.wasm" ) , "previous-b" )
64+ let renames = 0
5765 const filesystem = {
5866 ...fs ,
59- renameSync ( ) {
60- throw new Error ( "rename failed" )
67+ renameSync ( ...args ) {
68+ if ( ++ renames === 2 ) throw new Error ( "rename failed" )
69+ return fs . renameSync ( ...args )
6170 } ,
6271 }
6372
6473 expect ( ( ) => publishTreeSitterWasms ( source , destination , filesystem ) ) . toThrow ( "rename failed" )
65- expect ( fs . readdirSync ( destination ) ) . toEqual ( [ ] )
74+ expect ( fs . readdirSync ( destination ) ) . toEqual ( [ "tree-sitter-a.wasm" , "tree-sitter-b.wasm" ] )
75+ expect ( fs . readFileSync ( path . join ( destination , "tree-sitter-a.wasm" ) , "utf8" ) ) . toBe ( "previous-a" )
76+ expect ( fs . readFileSync ( path . join ( destination , "tree-sitter-b.wasm" ) , "utf8" ) ) . toBe ( "previous-b" )
6677 } )
6778} )
0 commit comments