@@ -105,9 +105,12 @@ function onWindowsHost<T>(run: () => Promise<T>): Promise<T> {
105105// the Windows host rule (EPERM on an append handle, enforced here at the fs
106106// seam) is answered by an `'r+'` handle instead of a create/reopen window
107107// on the common path (#3291).
108- test ( 'a Windows host empties an existing log through one pinned write handle' , async ( ) => {
109- const { paths, owner } = await acquire ( ) ;
110- fs . writeFileSync ( paths . logPath , 'previous run\n' ) ;
108+
109+ // Emulates the Windows host rule at the fs seam: ftruncate through an
110+ // append-only handle raises EPERM as NTFS would, while every other open and
111+ // truncate passes through. Records the flags each log open used so each test
112+ // can pin the exact handle sequence.
113+ function spyWindowsLogHandleRules ( logPath : string ) : string [ ] {
111114 const flagsByDescriptor = new Map < number , string > ( ) ;
112115 const logOpenFlags : string [ ] = [ ] ;
113116 const realOpen = fs . openSync ;
@@ -116,7 +119,7 @@ test('a Windows host empties an existing log through one pinned write handle', a
116119 flags : fs . OpenMode ,
117120 mode ?: fs . Mode ,
118121 ) => {
119- if ( String ( target ) === paths . logPath ) logOpenFlags . push ( String ( flags ) ) ;
122+ if ( String ( target ) === logPath ) logOpenFlags . push ( String ( flags ) ) ;
120123 const descriptor = realOpen ( target , flags , mode ) ;
121124 flagsByDescriptor . set ( descriptor , String ( flags ) ) ;
122125 return descriptor ;
@@ -130,6 +133,13 @@ test('a Windows host empties an existing log through one pinned write handle', a
130133 }
131134 realTruncate ( descriptor , len ) ;
132135 } ) as typeof fs . ftruncateSync ) ;
136+ return logOpenFlags ;
137+ }
138+
139+ test ( 'a Windows host empties an existing log through one pinned write handle' , async ( ) => {
140+ const { paths, owner } = await acquire ( ) ;
141+ fs . writeFileSync ( paths . logPath , 'previous run\n' ) ;
142+ const logOpenFlags = spyWindowsLogHandleRules ( paths . logPath ) ;
133143 await onWindowsHost ( async ( ) => {
134144 try {
135145 owner . publish ( fields ) ;
@@ -146,28 +156,7 @@ test('a Windows host empties an existing log through one pinned write handle', a
146156
147157test ( 'a missing Windows-host log is created through append and emptied through a write handle' , async ( ) => {
148158 const { paths, owner } = await acquire ( ) ;
149- const flagsByDescriptor = new Map < number , string > ( ) ;
150- const logOpenFlags : string [ ] = [ ] ;
151- const realOpen = fs . openSync ;
152- vi . spyOn ( fs , 'openSync' ) . mockImplementation ( ( (
153- target : fs . PathLike ,
154- flags : fs . OpenMode ,
155- mode ?: fs . Mode ,
156- ) => {
157- if ( String ( target ) === paths . logPath ) logOpenFlags . push ( String ( flags ) ) ;
158- const descriptor = realOpen ( target , flags , mode ) ;
159- flagsByDescriptor . set ( descriptor , String ( flags ) ) ;
160- return descriptor ;
161- } ) as typeof fs . openSync ) ;
162- const realTruncate = fs . ftruncateSync ;
163- vi . spyOn ( fs , 'ftruncateSync' ) . mockImplementation ( ( ( descriptor : number , len ?: number ) => {
164- if ( flagsByDescriptor . get ( descriptor ) === 'a' ) {
165- throw Object . assign ( new Error ( 'EPERM: operation not permitted, ftruncate' ) , {
166- code : 'EPERM' ,
167- } ) ;
168- }
169- realTruncate ( descriptor , len ) ;
170- } ) as typeof fs . ftruncateSync ) ;
159+ const logOpenFlags = spyWindowsLogHandleRules ( paths . logPath ) ;
171160 await onWindowsHost ( async ( ) => {
172161 try {
173162 owner . publish ( fields ) ;
0 commit comments