@@ -792,6 +792,65 @@ describe("safeWriteJson", () => {
792792 expect ( await readFileContent ( currentTestFilePath ) ) . toEqual ( { ordered : true } )
793793 } , 20_000 )
794794
795+ // The mode half of the same finding, run for real on a platform that honours mode bits. A target
796+ // whose mode is 0o444 withholds write access, so a mode copy that ran first would hand the staged
797+ // file that read-only mode and the flush open of this very file would then fail: the flush only
798+ // logs what it could not do, so the sync would be skipped while the publish still succeeded. This
799+ // row therefore asserts the two facts the ordering comment above was standing in for, on the
800+ // handle the publish opened and on the target after the rename, and it drives them with a real
801+ // 0o444 mode instead of a recorded call order. Skipped on windows, where 0o444 lands as the
802+ // read-only attribute and the commit rename reaches the EPERM fallback for reasons that belong
803+ // to the platform and not to this order.
804+ test . skipIf ( process . platform === "win32" ) (
805+ "publishes onto a 0o444 target, syncing the staged file and leaving the published mode 0o444" ,
806+ async ( ) => {
807+ const actual = await vi . importActual < typeof import ( "fs/promises" ) > ( "fs/promises" )
808+ await fsPromisesActuals . writeFile ! ( currentTestFilePath , JSON . stringify ( { before : true } ) )
809+ await actual . chmod ( currentTestFilePath , 0o444 )
810+ // The precondition, asserted so that a runner whose filesystem ignores mode bits turns this
811+ // row red instead of passing it for the wrong reason.
812+ const targetModeBefore = ( ( await actual . stat ( currentTestFilePath ) ) . mode & 0o777 ) >>> 0
813+ expect ( targetModeBefore ) . toBe ( 0o444 )
814+ const steps : string [ ] = [ ]
815+ const syncPaths : string [ ] = [ ]
816+ let flushPath : string | undefined
817+ vi . mocked ( fs . open ) . mockImplementation ( async ( filePath , flags ) => {
818+ flushPath = String ( filePath )
819+ steps . push ( "flush-open" )
820+ const handle = await actual . open ( filePath , flags )
821+ const realSync = handle . sync . bind ( handle )
822+ vi . spyOn ( handle , "sync" ) . mockImplementation ( async ( ) => {
823+ syncPaths . push ( String ( filePath ) )
824+ steps . push ( "sync" )
825+ await realSync ( )
826+ } )
827+ return handle
828+ } )
829+ vi . mocked ( fs . chmod ) . mockImplementation ( async ( filePath , mode ) => {
830+ steps . push ( "mode-copy" )
831+ return actual . chmod ( filePath , mode )
832+ } )
833+ const consoleErrorSpy = vi . spyOn ( console , "error" ) . mockImplementation ( ( ) => { } )
834+ await safeWriteJson ( currentTestFilePath , { after : true } )
835+ // The handle belongs to the staged file and not to the target, and its sync ran against it.
836+ expect ( flushPath ) . toBeDefined ( )
837+ expect ( path . basename ( String ( flushPath ) ) ) . toContain ( ".new_" )
838+ expect ( syncPaths ) . toEqual ( [ String ( flushPath ) ] )
839+ // It ran before the target mode was copied onto the staged file, the only order in which a
840+ // 0o444 target cannot deny the open above.
841+ expect ( steps ) . toEqual ( [ "flush-open" , "sync" , "mode-copy" ] )
842+ expect ( consoleErrorSpy ) . not . toHaveBeenCalled ( )
843+ consoleErrorSpy . mockRestore ( )
844+ expect ( await readFileContent ( currentTestFilePath ) ) . toEqual ( { after : true } )
845+ // The published target still reads the mode its owner set and not the staged file mode.
846+ expect ( ( ( await actual . stat ( currentTestFilePath ) ) . mode & 0o777 ) >>> 0 ) . toBe ( 0o444 )
847+ const leftovers = ( await fs . readdir ( path . dirname ( currentTestFilePath ) ) ) . filter (
848+ ( entry ) => entry . includes ( ".new_" ) || entry . endsWith ( ".lock" ) ,
849+ )
850+ expect ( leftovers ) . toEqual ( [ ] )
851+ } ,
852+ )
853+
795854 test ( "should garbage collect orphaned replacement files when the canonical path exists" , async ( ) => {
796855 const initialData = { message : "Initial content" }
797856
0 commit comments