@@ -54,18 +54,26 @@ async function validateLargeRead(options) {
54
54
// from the current position in the file.
55
55
const filePath = fixtures . path ( 'x.txt' ) ;
56
56
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
+ }
62
66
}
63
67
64
68
async function validateReadNoParams ( ) {
65
69
const filePath = fixtures . path ( 'x.txt' ) ;
66
70
const fileHandle = await open ( filePath , 'r' ) ;
67
71
// Should not throw
68
- await fileHandle . read ( ) ;
72
+ try {
73
+ await fileHandle . read ( ) ;
74
+ } finally {
75
+ await fileHandle . close ( ) ;
76
+ }
69
77
}
70
78
71
79
// Validates that the zero position is respected after the position has been
@@ -75,15 +83,19 @@ async function validateReadWithPositionZero() {
75
83
const opts = { useConf : true } ;
76
84
const filePath = fixtures . path ( 'x.txt' ) ;
77
85
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 ( ) ;
87
99
}
88
100
}
89
101
@@ -92,24 +104,32 @@ async function validateReadLength(len) {
92
104
const opts = { useConf : true } ;
93
105
const filePath = fixtures . path ( 'x.txt' ) ;
94
106
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
+ }
97
113
}
98
114
99
115
async function validateReadWithNoOptions ( byte ) {
100
116
const buf = Buffer . alloc ( byte ) ;
101
117
const filePath = fixtures . path ( 'x.txt' ) ;
102
118
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
+ }
113
133
}
114
134
115
135
( async function ( ) {
0 commit comments