@@ -7,14 +7,14 @@ const tmpdir = require('../common/tmpdir');
7
7
const file = path . join ( tmpdir . path , 'read_stream_filehandle_test.txt' ) ;
8
8
const input = 'hello world' ;
9
9
10
- let output = '' ;
11
10
tmpdir . refresh ( ) ;
12
11
fs . writeFileSync ( file , input ) ;
13
12
14
- fs . promises . open ( file , 'r' ) . then ( common . mustCall ( ( handle ) => {
13
+ fs . promises . open ( file , 'r' ) . then ( ( handle ) => {
15
14
handle . on ( 'close' , common . mustCall ( ) ) ;
16
15
const stream = fs . createReadStream ( null , { fd : handle } ) ;
17
16
17
+ let output = '' ;
18
18
stream . on ( 'data' , common . mustCallAtLeast ( ( data ) => {
19
19
output += data ;
20
20
} ) ) ;
@@ -24,42 +24,60 @@ fs.promises.open(file, 'r').then(common.mustCall((handle) => {
24
24
} ) ) ;
25
25
26
26
stream . on ( 'close' , common . mustCall ( ) ) ;
27
- } ) ) ;
27
+ } ) . then ( common . mustCall ( ) ) ;
28
28
29
- fs . promises . open ( file , 'r' ) . then ( common . mustCall ( ( handle ) => {
29
+ fs . promises . open ( file , 'r' ) . then ( ( handle ) => {
30
30
handle . on ( 'close' , common . mustCall ( ) ) ;
31
31
const stream = fs . createReadStream ( null , { fd : handle } ) ;
32
32
stream . on ( 'data' , common . mustNotCall ( ) ) ;
33
33
stream . on ( 'close' , common . mustCall ( ) ) ;
34
34
35
- handle . close ( ) ;
36
- } ) ) ;
35
+ return handle . close ( ) ;
36
+ } ) . then ( common . mustCall ( ) ) ;
37
37
38
- fs . promises . open ( file , 'r' ) . then ( common . mustCall ( ( handle ) => {
38
+ fs . promises . open ( file , 'r' ) . then ( ( handle ) => {
39
39
handle . on ( 'close' , common . mustCall ( ) ) ;
40
40
const stream = fs . createReadStream ( null , { fd : handle } ) ;
41
41
stream . on ( 'close' , common . mustCall ( ) ) ;
42
42
43
43
stream . on ( 'data' , common . mustCall ( ( ) => {
44
44
handle . close ( ) ;
45
45
} ) ) ;
46
- } ) ) ;
46
+ } ) . then ( common . mustCall ( ) ) ;
47
47
48
- fs . promises . open ( file , 'r' ) . then ( common . mustCall ( ( handle ) => {
48
+ fs . promises . open ( file , 'r' ) . then ( ( handle ) => {
49
49
handle . on ( 'close' , common . mustCall ( ) ) ;
50
50
const stream = fs . createReadStream ( null , { fd : handle } ) ;
51
51
stream . on ( 'close' , common . mustCall ( ) ) ;
52
52
53
53
stream . close ( ) ;
54
- } ) ) ;
54
+ } ) . then ( common . mustCall ( ) ) ;
55
55
56
- fs . promises . open ( file , 'r' ) . then ( common . mustCall ( ( handle ) => {
56
+ fs . promises . open ( file , 'r' ) . then ( ( handle ) => {
57
57
assert . throws ( ( ) => {
58
58
fs . createReadStream ( null , { fd : handle , fs } ) ;
59
59
} , {
60
60
code : 'ERR_METHOD_NOT_IMPLEMENTED' ,
61
61
name : 'Error' ,
62
62
message : 'The FileHandle with fs method is not implemented'
63
63
} ) ;
64
- handle . close ( ) ;
65
- } ) ) ;
64
+ return handle . close ( ) ;
65
+ } ) . then ( common . mustCall ( ) ) ;
66
+
67
+ fs . promises . open ( file , 'r' ) . then ( ( handle ) => {
68
+ const { read : originalReadFunction } = handle ;
69
+ handle . read = common . mustCallAtLeast ( function read ( ) {
70
+ return Reflect . apply ( originalReadFunction , this , arguments ) ;
71
+ } ) ;
72
+
73
+ const stream = fs . createReadStream ( null , { fd : handle } ) ;
74
+
75
+ let output = '' ;
76
+ stream . on ( 'data' , common . mustCallAtLeast ( ( data ) => {
77
+ output += data ;
78
+ } ) ) ;
79
+
80
+ stream . on ( 'end' , common . mustCall ( ( ) => {
81
+ assert . strictEqual ( output , input ) ;
82
+ } ) ) ;
83
+ } ) . then ( common . mustCall ( ) ) ;
0 commit comments