@@ -18,27 +18,39 @@ tmpdir.refresh();
18
18
19
19
/* Open the file descriptor. */
20
20
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' ) ;
21
25
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' ) ;
31
28
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
+ }
34
34
}
35
35
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
+
36
47
{
37
48
/* writeFile() test. */
38
49
const file = join ( tmpdir . path , 'test1.txt' ) ;
39
50
40
51
/* Open the file descriptor. */
41
52
fs . open ( file , 'w' , common . mustSucceed ( ( fd ) => {
53
+ fdsToCloseOnExit . push ( fd ) ;
42
54
/* Write only five characters, so that the position moves to five. */
43
55
fs . write ( fd , 'Hello' , common . mustSucceed ( ( bytes ) => {
44
56
assert . strictEqual ( bytes , 5 ) ;
@@ -48,9 +60,6 @@ tmpdir.refresh();
48
60
fs . writeFile ( fd , 'World' , common . mustSucceed ( ( ) => {
49
61
/* New content should be written at position five, instead of zero. */
50
62
assert . deepStrictEqual ( fs . readFileSync ( file ) . toString ( ) , 'HelloWorld' ) ;
51
-
52
- /* Close the file descriptor. */
53
- fs . closeSync ( fd ) ;
54
63
} ) ) ;
55
64
} ) ) ;
56
65
} ) ) ;
@@ -65,6 +74,7 @@ tmpdir.refresh();
65
74
const file = join ( tmpdir . path , 'test.txt' ) ;
66
75
67
76
fs . open ( file , 'r' , common . mustSucceed ( ( fd ) => {
77
+ fdsToCloseOnExit . push ( fd ) ;
68
78
fs . writeFile ( fd , 'World' , common . expectsError ( expectedError ) ) ;
69
79
} ) ) ;
70
80
}
@@ -76,6 +86,7 @@ tmpdir.refresh();
76
86
const file = join ( tmpdir . path , 'test.txt' ) ;
77
87
78
88
fs . open ( file , 'w' , common . mustSucceed ( ( fd ) => {
89
+ fdsToCloseOnExit . push ( fd ) ;
79
90
fs . writeFile ( fd , 'World' , { signal } , common . expectsError ( {
80
91
name : 'AbortError'
81
92
} ) ) ;
0 commit comments