@@ -54,18 +54,26 @@ async function validateLargeRead(options) {
5454 // from the current position in the file.
5555 const filePath = fixtures . path ( 'x.txt' ) ;
5656 const fileHandle = await open ( filePath , 'r' ) ;
57- const pos = 0xffffffff + 1 ; // max-uint32 + 1
58- const readHandle =
59- await read ( fileHandle , Buffer . alloc ( 1 ) , 0 , 1 , pos , options ) ;
60-
61- assert . strictEqual ( readHandle . bytesRead , 0 ) ;
57+ try {
58+ const pos = 0xffffffff + 1 ; // max-uint32 + 1
59+ const readHandle =
60+ await read ( fileHandle , Buffer . alloc ( 1 ) , 0 , 1 , pos , options ) ;
61+
62+ assert . strictEqual ( readHandle . bytesRead , 0 ) ;
63+ } finally {
64+ await fileHandle . close ( ) ;
65+ }
6266}
6367
6468async function validateReadNoParams ( ) {
6569 const filePath = fixtures . path ( 'x.txt' ) ;
6670 const fileHandle = await open ( filePath , 'r' ) ;
6771 // Should not throw
68- await fileHandle . read ( ) ;
72+ try {
73+ await fileHandle . read ( ) ;
74+ } finally {
75+ await fileHandle . close ( ) ;
76+ }
6977}
7078
7179// Validates that the zero position is respected after the position has been
@@ -75,15 +83,19 @@ async function validateReadWithPositionZero() {
7583 const opts = { useConf : true } ;
7684 const filePath = fixtures . path ( 'x.txt' ) ;
7785 const fileHandle = await open ( filePath , 'r' ) ;
78- const expectedSequence = [ 'x' , 'y' , 'z' ] ;
79-
80- for ( let i = 0 ; i < expectedSequence . length * 2 ; i ++ ) {
81- const len = 1 ;
82- const pos = i % 3 ;
83- const buf = Buffer . alloc ( len ) ;
84- const { bytesRead } = await read ( fileHandle , buf , 0 , len , pos , opts ) ;
85- assert . strictEqual ( bytesRead , len ) ;
86- assert . strictEqual ( buf . toString ( ) , expectedSequence [ pos ] ) ;
86+ try {
87+ const expectedSequence = [ 'x' , 'y' , 'z' ] ;
88+
89+ for ( let i = 0 ; i < expectedSequence . length * 2 ; i ++ ) {
90+ const len = 1 ;
91+ const pos = i % 3 ;
92+ const buf = Buffer . alloc ( len ) ;
93+ const { bytesRead } = await read ( fileHandle , buf , 0 , len , pos , opts ) ;
94+ assert . strictEqual ( bytesRead , len ) ;
95+ assert . strictEqual ( buf . toString ( ) , expectedSequence [ pos ] ) ;
96+ }
97+ } finally {
98+ await fileHandle . close ( ) ;
8799 }
88100}
89101
@@ -92,24 +104,32 @@ async function validateReadLength(len) {
92104 const opts = { useConf : true } ;
93105 const filePath = fixtures . path ( 'x.txt' ) ;
94106 const fileHandle = await open ( filePath , 'r' ) ;
95- const { bytesRead } = await read ( fileHandle , buf , 0 , len , 0 , opts ) ;
96- assert . strictEqual ( bytesRead , len ) ;
107+ try {
108+ const { bytesRead } = await read ( fileHandle , buf , 0 , len , 0 , opts ) ;
109+ assert . strictEqual ( bytesRead , len ) ;
110+ } finally {
111+ await fileHandle . close ( ) ;
112+ }
97113}
98114
99115async function validateReadWithNoOptions ( byte ) {
100116 const buf = Buffer . alloc ( byte ) ;
101117 const filePath = fixtures . path ( 'x.txt' ) ;
102118 const fileHandle = await open ( filePath , 'r' ) ;
103- let response = await fileHandle . read ( buf ) ;
104- assert . strictEqual ( response . bytesRead , byte ) ;
105- response = await read ( fileHandle , buf , 0 , undefined , 0 ) ;
106- assert . strictEqual ( response . bytesRead , byte ) ;
107- response = await read ( fileHandle , buf , 0 , null , 0 ) ;
108- assert . strictEqual ( response . bytesRead , byte ) ;
109- response = await read ( fileHandle , buf , 0 , undefined , 0 , { useConf : true } ) ;
110- assert . strictEqual ( response . bytesRead , byte ) ;
111- response = await read ( fileHandle , buf , 0 , null , 0 , { useConf : true } ) ;
112- assert . strictEqual ( response . bytesRead , byte ) ;
119+ try {
120+ let response = await fileHandle . read ( buf ) ;
121+ assert . strictEqual ( response . bytesRead , byte ) ;
122+ response = await read ( fileHandle , buf , 0 , undefined , 0 ) ;
123+ assert . strictEqual ( response . bytesRead , byte ) ;
124+ response = await read ( fileHandle , buf , 0 , null , 0 ) ;
125+ assert . strictEqual ( response . bytesRead , byte ) ;
126+ response = await read ( fileHandle , buf , 0 , undefined , 0 , { useConf : true } ) ;
127+ assert . strictEqual ( response . bytesRead , byte ) ;
128+ response = await read ( fileHandle , buf , 0 , null , 0 , { useConf : true } ) ;
129+ assert . strictEqual ( response . bytesRead , byte ) ;
130+ } finally {
131+ await fileHandle . close ( ) ;
132+ }
113133}
114134
115135( async function ( ) {
0 commit comments