@@ -22,7 +22,7 @@ tmpdir.refresh();
2222
2323async  function  validateReadFile ( )  { 
2424  const  filePath  =  path . resolve ( tmpDir ,  'tmp-read-file.txt' ) ; 
25-   const  fileHandle  =  await  open ( filePath ,  'w+' ) ; 
25+   await   using  fileHandle  =  await  open ( filePath ,  'w+' ) ; 
2626  const  buffer  =  Buffer . from ( 'Hello world' . repeat ( 100 ) ,  'utf8' ) ; 
2727
2828  const  fd  =  fs . openSync ( filePath ,  'w+' ) ; 
@@ -31,8 +31,6 @@ async function validateReadFile() {
3131
3232  const  readFileData  =  await  fileHandle . readFile ( ) ; 
3333  assert . deepStrictEqual ( buffer ,  readFileData ) ; 
34- 
35-   await  fileHandle . close ( ) ; 
3634} 
3735
3836async  function  validateReadFileProc ( )  { 
@@ -46,48 +44,36 @@ async function validateReadFileProc() {
4644  if  ( ! common . isLinux ) 
4745    return ; 
4846
49-   const  fileHandle  =  await  open ( '/proc/sys/kernel/hostname' ,  'r' ) ; 
50-   try  { 
51-     const  hostname  =  await  fileHandle . readFile ( ) ; 
52-     assert . ok ( hostname . length  >  0 ) ; 
53-   }  finally  { 
54-     await  fileHandle . close ( ) ; 
55-   } 
47+   await  using  fileHandle  =  await  open ( '/proc/sys/kernel/hostname' ,  'r' ) ; 
48+   const  hostname  =  await  fileHandle . readFile ( ) ; 
49+   assert . ok ( hostname . length  >  0 ) ; 
5650} 
5751
5852async  function  doReadAndCancel ( )  { 
5953  // Signal aborted from the start 
6054  { 
6155    const  filePathForHandle  =  path . resolve ( tmpDir ,  'dogs-running.txt' ) ; 
62-     const  fileHandle  =  await  open ( filePathForHandle ,  'w+' ) ; 
63-     try  { 
64-       const  buffer  =  Buffer . from ( 'Dogs running' . repeat ( 10000 ) ,  'utf8' ) ; 
65-       fs . writeFileSync ( filePathForHandle ,  buffer ) ; 
66-       const  signal  =  AbortSignal . abort ( ) ; 
67-       await  assert . rejects ( readFile ( fileHandle ,  common . mustNotMutateObjectDeep ( {  signal } ) ) ,  { 
68-         name : 'AbortError' 
69-       } ) ; 
70-     }  finally  { 
71-       await  fileHandle . close ( ) ; 
72-     } 
56+     await  using  fileHandle  =  await  open ( filePathForHandle ,  'w+' ) ; 
57+     const  buffer  =  Buffer . from ( 'Dogs running' . repeat ( 10000 ) ,  'utf8' ) ; 
58+     fs . writeFileSync ( filePathForHandle ,  buffer ) ; 
59+     const  signal  =  AbortSignal . abort ( ) ; 
60+     await  assert . rejects ( readFile ( fileHandle ,  common . mustNotMutateObjectDeep ( {  signal } ) ) ,  { 
61+       name : 'AbortError' 
62+     } ) ; 
7363  } 
7464
7565  // Signal aborted on first tick 
7666  { 
7767    const  filePathForHandle  =  path . resolve ( tmpDir ,  'dogs-running1.txt' ) ; 
78-     const  fileHandle  =  await  open ( filePathForHandle ,  'w+' ) ; 
79-     try  { 
80-       const  buffer  =  Buffer . from ( 'Dogs running' . repeat ( 10000 ) ,  'utf8' ) ; 
81-       fs . writeFileSync ( filePathForHandle ,  buffer ) ; 
82-       const  controller  =  new  AbortController ( ) ; 
83-       const  {  signal }  =  controller ; 
84-       process . nextTick ( ( )  =>  controller . abort ( ) ) ; 
85-       await  assert . rejects ( readFile ( fileHandle ,  common . mustNotMutateObjectDeep ( {  signal } ) ) ,  { 
86-         name : 'AbortError' 
87-       } ,  'tick-0' ) ; 
88-     }  finally  { 
89-       await  fileHandle . close ( ) ; 
90-     } 
68+     await  using  fileHandle  =  await  open ( filePathForHandle ,  'w+' ) ; 
69+     const  buffer  =  Buffer . from ( 'Dogs running' . repeat ( 10000 ) ,  'utf8' ) ; 
70+     fs . writeFileSync ( filePathForHandle ,  buffer ) ; 
71+     const  controller  =  new  AbortController ( ) ; 
72+     const  {  signal }  =  controller ; 
73+     process . nextTick ( ( )  =>  controller . abort ( ) ) ; 
74+     await  assert . rejects ( readFile ( fileHandle ,  common . mustNotMutateObjectDeep ( {  signal } ) ) ,  { 
75+       name : 'AbortError' 
76+     } ,  'tick-0' ) ; 
9177  } 
9278
9379  // Signal aborted right before buffer read 
@@ -96,18 +82,14 @@ async function doReadAndCancel() {
9682    const  buffer  =  Buffer . from ( 'Dogs running' . repeat ( 1000 ) ,  'utf8' ) ; 
9783    fs . writeFileSync ( newFile ,  buffer ) ; 
9884
99-     const  fileHandle  =  await  open ( newFile ,  'r' ) ; 
100-     try  { 
101-       const  controller  =  new  AbortController ( ) ; 
102-       const  {  signal }  =  controller ; 
103-       tick ( 1 ,  ( )  =>  controller . abort ( ) ) ; 
104-       await  assert . rejects ( fileHandle . readFile ( 
105-         common . mustNotMutateObjectDeep ( {  signal,  encoding : 'utf8'  } ) ) ,  { 
106-         name : 'AbortError' 
107-       } ,  'tick-1' ) ; 
108-     }  finally  { 
109-       await  fileHandle . close ( ) ; 
110-     } 
85+     await  using  fileHandle  =  await  open ( newFile ,  'r' ) ; 
86+     const  controller  =  new  AbortController ( ) ; 
87+     const  {  signal }  =  controller ; 
88+     tick ( 1 ,  ( )  =>  controller . abort ( ) ) ; 
89+     await  assert . rejects ( fileHandle . readFile ( 
90+       common . mustNotMutateObjectDeep ( {  signal,  encoding : 'utf8'  } ) ) ,  { 
91+       name : 'AbortError' 
92+     } ,  'tick-1' ) ; 
11193  } 
11294
11395  // Validate file size is within range for reading 
@@ -123,13 +105,12 @@ async function doReadAndCancel() {
123105      await  writeFile ( newFile ,  Buffer . from ( '0' ) ) ; 
124106      await  truncate ( newFile ,  kIoMaxLength  +  1 ) ; 
125107
126-       const  fileHandle  =  await  open ( newFile ,  'r' ) ; 
108+       await   using  fileHandle  =  await  open ( newFile ,  'r' ) ; 
127109
128110      await  assert . rejects ( fileHandle . readFile ( ) ,  { 
129111        name : 'RangeError' , 
130112        code : 'ERR_FS_FILE_TOO_LARGE' 
131113      } ) ; 
132-       await  fileHandle . close ( ) ; 
133114    } 
134115  } 
135116} 
0 commit comments