@@ -18,27 +18,39 @@ tmpdir.refresh();
1818
1919 /* Open the file descriptor. */
2020 const fd = fs . openSync ( filename , 'w' ) ;
21+ try {
22+ /* Write only five characters, so that the position moves to five. */
23+ assert . deepStrictEqual ( fs . writeSync ( fd , 'Hello' ) , 5 ) ;
24+ assert . deepStrictEqual ( fs . readFileSync ( filename ) . toString ( ) , 'Hello' ) ;
2125
22- /* Write only five characters, so that the position moves to five. */
23- assert . deepStrictEqual ( fs . writeSync ( fd , 'Hello' ) , 5 ) ;
24- assert . deepStrictEqual ( fs . readFileSync ( filename ) . toString ( ) , 'Hello' ) ;
25-
26- /* Write some more with writeFileSync(). */
27- fs . writeFileSync ( fd , 'World' ) ;
28-
29- /* New content should be written at position five, instead of zero. */
30- assert . deepStrictEqual ( fs . readFileSync ( filename ) . toString ( ) , 'HelloWorld' ) ;
26+ /* Write some more with writeFileSync(). */
27+ fs . writeFileSync ( fd , 'World' ) ;
3128
32- /* Close the file descriptor. */
33- fs . closeSync ( fd ) ;
29+ /* New content should be written at position five, instead of zero. */
30+ assert . deepStrictEqual ( fs . readFileSync ( filename ) . toString ( ) , 'HelloWorld' ) ;
31+ } finally {
32+ fs . closeSync ( fd ) ;
33+ }
3434}
3535
36+ const fdsToCloseOnExit = [ ] ;
37+ process . on ( 'beforeExit' , common . mustCall ( ( ) => {
38+ for ( const fd of fdsToCloseOnExit ) {
39+ try {
40+ fs . closeSync ( fd ) ;
41+ } catch {
42+ // Failed to close, ignore
43+ }
44+ }
45+ } ) ) ;
46+
3647{
3748 /* writeFile() test. */
3849 const file = join ( tmpdir . path , 'test1.txt' ) ;
3950
4051 /* Open the file descriptor. */
4152 fs . open ( file , 'w' , common . mustSucceed ( ( fd ) => {
53+ fdsToCloseOnExit . push ( fd ) ;
4254 /* Write only five characters, so that the position moves to five. */
4355 fs . write ( fd , 'Hello' , common . mustSucceed ( ( bytes ) => {
4456 assert . strictEqual ( bytes , 5 ) ;
@@ -48,9 +60,6 @@ tmpdir.refresh();
4860 fs . writeFile ( fd , 'World' , common . mustSucceed ( ( ) => {
4961 /* New content should be written at position five, instead of zero. */
5062 assert . deepStrictEqual ( fs . readFileSync ( file ) . toString ( ) , 'HelloWorld' ) ;
51-
52- /* Close the file descriptor. */
53- fs . closeSync ( fd ) ;
5463 } ) ) ;
5564 } ) ) ;
5665 } ) ) ;
0 commit comments