@@ -3,85 +3,55 @@ import os from "node:os"
33import path from "node:path"
44import { afterEach , beforeEach , describe , expect , it } from "vitest"
55
6- import { publishTreeSitterWasms } from "./copy-tree-sitter-wasms.mjs"
6+ import { prepareTreeSitterWasms } from "./copy-tree-sitter-wasms.mjs"
77
8- describe ( "publishTreeSitterWasms " , ( ) => {
8+ describe ( "prepareTreeSitterWasms " , ( ) => {
99 let root
1010 let source
1111 let destination
1212
1313 beforeEach ( ( ) => {
1414 root = fs . mkdtempSync ( path . join ( os . tmpdir ( ) , "tree-sitter-wasms-" ) )
1515 source = path . join ( root , "source" )
16- destination = path . join ( root , "dist " )
16+ destination = path . join ( root , "generated" , "tree-sitter-wasms ")
1717 fs . mkdirSync ( source )
18- fs . mkdirSync ( destination )
1918 fs . writeFileSync ( path . join ( source , "tree-sitter-a.wasm" ) , "a" )
2019 } )
2120
2221 afterEach ( ( ) => fs . rmSync ( root , { recursive : true , force : true } ) )
2322
24- it ( "publishes the exact staged set and removes stale state " , async ( ) => {
23+ it ( "replaces pre-existing generated output without filesystem rename " , async ( ) => {
2524 fs . writeFileSync ( path . join ( source , "ignored.txt" ) , "ignored" )
25+ fs . mkdirSync ( destination , { recursive : true } )
2626 fs . writeFileSync ( path . join ( destination , "tree-sitter-stale.wasm" ) , "stale" )
27- fs . mkdirSync ( `${ destination } .tree-sitter-wasms-staging` )
28- fs . writeFileSync ( path . join ( `${ destination } .tree-sitter-wasms-staging` , "stale.tmp" ) , "stale" )
29-
30- await publishTreeSitterWasms ( source , destination )
31-
32- expect ( fs . readdirSync ( destination ) ) . toEqual ( [ "tree-sitter-a.wasm" ] )
33- expect ( fs . readFileSync ( path . join ( destination , "tree-sitter-a.wasm" ) , "utf8" ) ) . toBe ( "a" )
34- expect ( fs . existsSync ( `${ destination } .tree-sitter-wasms-staging` ) ) . toBe ( false )
35- } )
36-
37- it ( "fails without publishing when staging fails" , async ( ) => {
3827 const filesystem = {
3928 ...fs . promises ,
40- copyFile : async ( ) => {
41- throw new Error ( "copy failed " )
29+ rename : async ( ) => {
30+ throw new Error ( "rename must not be used " )
4231 } ,
4332 }
44- await expect ( publishTreeSitterWasms ( source , destination , { filesystem } ) ) . rejects . toThrow ( "copy failed" )
45- expect ( fs . readdirSync ( destination ) ) . toEqual ( [ ] )
46- } )
4733
48- it ( "removes partial outputs and fails when publication is interrupted" , async ( ) => {
49- fs . writeFileSync ( path . join ( source , "tree-sitter-b.wasm" ) , "b" )
50- const signalState = { requested : undefined }
51- await expect (
52- publishTreeSitterWasms ( source , destination , {
53- signalState,
54- onStep ( name ) {
55- if ( name === "published" ) signalState . requested = "SIGTERM"
56- } ,
57- } ) ,
58- ) . rejects . toThrow ( "WASM publication cancelled" )
59- expect ( fs . readdirSync ( destination ) ) . toEqual ( [ ] )
34+ await prepareTreeSitterWasms ( source , destination , { filesystem } )
35+
36+ expect ( fs . readdirSync ( destination ) ) . toEqual ( [ "tree-sitter-a.wasm" ] )
37+ expect ( fs . readFileSync ( path . join ( destination , "tree-sitter-a.wasm" ) , "utf8" ) ) . toBe ( "a" )
6038 } )
6139
62- it ( "reports publication and cleanup errors together" , async ( ) => {
63- let destinationReads = 0
40+ it ( "removes partial task output after a copy failure and rebuilds cleanly" , async ( ) => {
41+ fs . writeFileSync ( path . join ( source , "tree-sitter-b.wasm" ) , "b" )
42+ let copies = 0
6443 const filesystem = {
6544 ...fs . promises ,
66- readdir ( directory ) {
67- if ( directory === destination && ++ destinationReads === 2 ) throw new Error ( "cleanup enumeration failed" )
68- return fs . promises . readdir ( directory )
45+ copyFile : async ( ... args ) => {
46+ if ( ++ copies === 2 ) throw new Error ( "copy failed" )
47+ return fs . promises . copyFile ( ... args )
6948 } ,
7049 }
7150
72- await expect (
73- publishTreeSitterWasms ( source , destination , {
74- filesystem,
75- onStep ( name ) {
76- if ( name === "published" ) throw new Error ( "publication failed" )
77- } ,
78- } ) ,
79- ) . rejects . toMatchObject ( {
80- message : expect . stringContaining ( "WASM publication and cleanup failed" ) ,
81- errors : [
82- expect . objectContaining ( { message : "publication failed" } ) ,
83- expect . objectContaining ( { message : "cleanup enumeration failed" } ) ,
84- ] ,
85- } )
51+ await expect ( prepareTreeSitterWasms ( source , destination , { filesystem } ) ) . rejects . toThrow ( "copy failed" )
52+ expect ( fs . existsSync ( destination ) ) . toBe ( false )
53+
54+ await prepareTreeSitterWasms ( source , destination )
55+ expect ( fs . readdirSync ( destination ) ) . toEqual ( [ "tree-sitter-a.wasm" , "tree-sitter-b.wasm" ] )
8656 } )
8757} )
0 commit comments