@@ -18,13 +18,15 @@ tmpdir.refresh();
18
18
async function validateWriteFile ( ) {
19
19
const filePathForHandle = path . resolve ( tmpDir , 'tmp-write-file2.txt' ) ;
20
20
const fileHandle = await open ( filePathForHandle , 'w+' ) ;
21
- const buffer = Buffer . from ( 'Hello world' . repeat ( 100 ) , 'utf8' ) ;
22
-
23
- await fileHandle . writeFile ( buffer ) ;
24
- const readFileData = fs . readFileSync ( filePathForHandle ) ;
25
- assert . deepStrictEqual ( buffer , readFileData ) ;
21
+ try {
22
+ const buffer = Buffer . from ( 'Hello world' . repeat ( 100 ) , 'utf8' ) ;
26
23
27
- await fileHandle . close ( ) ;
24
+ await fileHandle . writeFile ( buffer ) ;
25
+ const readFileData = fs . readFileSync ( filePathForHandle ) ;
26
+ assert . deepStrictEqual ( buffer , readFileData ) ;
27
+ } finally {
28
+ await fileHandle . close ( ) ;
29
+ }
28
30
}
29
31
30
32
// Signal aborted while writing file
@@ -82,80 +84,106 @@ const asyncIterable = {
82
84
83
85
async function doWriteStream ( ) {
84
86
const fileHandle = await open ( dest , 'w+' ) ;
85
- await fileHandle . writeFile ( stream ) ;
86
- const expected = 'abc' ;
87
- const data = fs . readFileSync ( dest , 'utf-8' ) ;
88
- assert . deepStrictEqual ( data , expected ) ;
89
- await fileHandle . close ( ) ;
87
+ try {
88
+ await fileHandle . writeFile ( stream ) ;
89
+ const expected = 'abc' ;
90
+ const data = fs . readFileSync ( dest , 'utf-8' ) ;
91
+ assert . deepStrictEqual ( data , expected ) ;
92
+ } finally {
93
+ await fileHandle . close ( ) ;
94
+ }
90
95
}
91
96
92
97
async function doWriteStreamWithCancel ( ) {
93
98
const controller = new AbortController ( ) ;
94
99
const { signal } = controller ;
95
100
process . nextTick ( ( ) => controller . abort ( ) ) ;
96
101
const fileHandle = await open ( otherDest , 'w+' ) ;
97
- await assert . rejects (
98
- fileHandle . writeFile ( stream , { signal } ) ,
99
- { name : 'AbortError' }
100
- ) ;
101
- await fileHandle . close ( ) ;
102
+ try {
103
+ await assert . rejects (
104
+ fileHandle . writeFile ( stream , { signal } ) ,
105
+ { name : 'AbortError' }
106
+ ) ;
107
+ } finally {
108
+ await fileHandle . close ( ) ;
109
+ }
102
110
}
103
111
104
112
async function doWriteIterable ( ) {
105
113
const fileHandle = await open ( dest , 'w+' ) ;
106
- await fileHandle . writeFile ( iterable ) ;
107
- const data = fs . readFileSync ( dest , 'utf-8' ) ;
108
- assert . deepStrictEqual ( data , iterable . expected ) ;
109
- await fileHandle . close ( ) ;
114
+ try {
115
+ await fileHandle . writeFile ( iterable ) ;
116
+ const data = fs . readFileSync ( dest , 'utf-8' ) ;
117
+ assert . deepStrictEqual ( data , iterable . expected ) ;
118
+ } finally {
119
+ await fileHandle . close ( ) ;
120
+ }
110
121
}
111
122
112
123
async function doWriteInvalidIterable ( ) {
113
124
const fileHandle = await open ( dest , 'w+' ) ;
114
- await Promise . all (
115
- [ 42 , 42n , { } , Symbol ( '42' ) , true , undefined , null , NaN ] . map ( ( value ) =>
116
- assert . rejects ( fileHandle . writeFile ( iterableWith ( value ) ) , {
117
- code : 'ERR_INVALID_ARG_TYPE' ,
118
- } )
119
- )
120
- ) ;
121
- await fileHandle . close ( ) ;
125
+ try {
126
+ await Promise . all (
127
+ [ 42 , 42n , { } , Symbol ( '42' ) , true , undefined , null , NaN ] . map ( ( value ) =>
128
+ assert . rejects (
129
+ fileHandle . writeFile ( iterableWith ( value ) ) ,
130
+ { code : 'ERR_INVALID_ARG_TYPE' }
131
+ )
132
+ )
133
+ ) ;
134
+ } finally {
135
+ await fileHandle . close ( ) ;
136
+ }
122
137
}
123
138
124
139
async function doWriteIterableWithEncoding ( ) {
125
140
const fileHandle = await open ( dest , 'w+' ) ;
126
- await fileHandle . writeFile ( stream2 , 'latin1' ) ;
127
- const expected = 'ümlaut sechzig' ;
128
- const data = fs . readFileSync ( dest , 'latin1' ) ;
129
- assert . deepStrictEqual ( data , expected ) ;
130
- await fileHandle . close ( ) ;
141
+ try {
142
+ await fileHandle . writeFile ( stream2 , 'latin1' ) ;
143
+ const expected = 'ümlaut sechzig' ;
144
+ const data = fs . readFileSync ( dest , 'latin1' ) ;
145
+ assert . deepStrictEqual ( data , expected ) ;
146
+ } finally {
147
+ await fileHandle . close ( ) ;
148
+ }
131
149
}
132
150
133
151
async function doWriteBufferIterable ( ) {
134
152
const fileHandle = await open ( dest , 'w+' ) ;
135
- await fileHandle . writeFile ( bufferIterable ) ;
136
- const data = fs . readFileSync ( dest , 'utf-8' ) ;
137
- assert . deepStrictEqual ( data , bufferIterable . expected ) ;
138
- await fileHandle . close ( ) ;
153
+ try {
154
+ await fileHandle . writeFile ( bufferIterable ) ;
155
+ const data = fs . readFileSync ( dest , 'utf-8' ) ;
156
+ assert . deepStrictEqual ( data , bufferIterable . expected ) ;
157
+ } finally {
158
+ await fileHandle . close ( ) ;
159
+ }
139
160
}
140
161
141
162
async function doWriteAsyncIterable ( ) {
142
163
const fileHandle = await open ( dest , 'w+' ) ;
143
- await fileHandle . writeFile ( asyncIterable ) ;
144
- const data = fs . readFileSync ( dest , 'utf-8' ) ;
145
- assert . deepStrictEqual ( data , asyncIterable . expected ) ;
146
- await fileHandle . close ( ) ;
164
+ try {
165
+ await fileHandle . writeFile ( asyncIterable ) ;
166
+ const data = fs . readFileSync ( dest , 'utf-8' ) ;
167
+ assert . deepStrictEqual ( data , asyncIterable . expected ) ;
168
+ } finally {
169
+ await fileHandle . close ( ) ;
170
+ }
147
171
}
148
172
149
173
async function doWriteInvalidValues ( ) {
150
174
const fileHandle = await open ( dest , 'w+' ) ;
151
- await Promise . all (
152
- [ 42 , 42n , { } , Symbol ( '42' ) , true , undefined , null , NaN ] . map ( ( value ) =>
153
- assert . rejects ( fileHandle . writeFile ( value ) , {
154
- code : 'ERR_INVALID_ARG_TYPE' ,
155
- } )
156
- )
157
- ) ;
158
- await fileHandle . close ( ) ;
175
+ try {
176
+ await Promise . all (
177
+ [ 42 , 42n , { } , Symbol ( '42' ) , true , undefined , null , NaN ] . map ( ( value ) =>
178
+ assert . rejects (
179
+ fileHandle . writeFile ( value ) ,
180
+ { code : 'ERR_INVALID_ARG_TYPE' }
181
+ )
182
+ )
183
+ ) ;
184
+ } finally {
185
+ await fileHandle . close ( ) ;
186
+ }
159
187
}
160
188
161
189
( async ( ) => {
0 commit comments