@@ -5,32 +5,34 @@ const fixtures = require('../common/fixtures');
5
5
const fs = require ( 'fs' ) ;
6
6
const assert = require ( 'assert' ) ;
7
7
const filepath = fixtures . path ( 'x.txt' ) ;
8
- const fd = fs . openSync ( filepath , 'r' ) ;
9
8
10
9
const expected = Buffer . from ( 'xyz\n' ) ;
11
10
const defaultBufferAsync = Buffer . alloc ( 16384 ) ;
12
- const bufferAsOption = Buffer . allocUnsafe ( expected . length ) ;
11
+ const bufferAsOption = Buffer . allocUnsafe ( expected . byteLength ) ;
13
12
14
- // Test not passing in any options object
15
- fs . read ( fd , common . mustCall ( ( err , bytesRead , buffer ) => {
16
- assert . strictEqual ( bytesRead , expected . length ) ;
17
- assert . deepStrictEqual ( defaultBufferAsync . length , buffer . length ) ;
18
- } ) ) ;
13
+ function testValid ( message , ...options ) {
14
+ const paramsMsg = `${ message } (as params)` ;
15
+ const paramsFilehandle = fs . openSync ( filepath , 'r' ) ;
16
+ fs . read ( paramsFilehandle , ...options , common . mustSucceed ( ( bytesRead , buffer ) => {
17
+ assert . strictEqual ( bytesRead , expected . byteLength , paramsMsg ) ;
18
+ assert . deepStrictEqual ( defaultBufferAsync . byteLength , buffer . byteLength , paramsMsg ) ;
19
+ fs . closeSync ( paramsFilehandle ) ;
20
+ } ) ) ;
19
21
20
- // Test passing in an empty options object
21
- fs . read ( fd , { position : 0 } , common . mustCall ( ( err , bytesRead , buffer ) => {
22
- assert . strictEqual ( bytesRead , expected . length ) ;
23
- assert . deepStrictEqual ( defaultBufferAsync . length , buffer . length ) ;
24
- } ) ) ;
22
+ const optionsMsg = `${ message } (as options)` ;
23
+ const optionsFilehandle = fs . openSync ( filepath , 'r' ) ;
24
+ fs . read ( optionsFilehandle , bufferAsOption , ...options , common . mustSucceed ( ( bytesRead , buffer ) => {
25
+ assert . strictEqual ( bytesRead , expected . byteLength , optionsMsg ) ;
26
+ assert . deepStrictEqual ( bufferAsOption . byteLength , buffer . byteLength , optionsMsg ) ;
27
+ fs . closeSync ( optionsFilehandle ) ;
28
+ } ) ) ;
29
+ }
25
30
26
- // Test passing in options
27
- fs . read ( fd , {
28
- buffer : bufferAsOption ,
31
+ testValid ( 'Not passing in any object' ) ;
32
+ testValid ( 'Passing in a null' , null ) ;
33
+ testValid ( 'Passing in an empty object' , { } ) ;
34
+ testValid ( 'Passing in an object' , {
29
35
offset : 0 ,
30
- length : bufferAsOption . length ,
31
- position : 0
32
- } ,
33
- common . mustCall ( ( err , bytesRead , buffer ) => {
34
- assert . strictEqual ( bytesRead , expected . length ) ;
35
- assert . deepStrictEqual ( bufferAsOption . length , buffer . length ) ;
36
- } ) ) ;
36
+ length : bufferAsOption . byteLength ,
37
+ position : 0 ,
38
+ } ) ;
0 commit comments