@@ -8,20 +8,115 @@ const path = require('path');
8
8
const tmpdir = require ( '../common/tmpdir' ) ;
9
9
const assert = require ( 'assert' ) ;
10
10
const tmpDir = tmpdir . path ;
11
+ const { Readable } = require ( 'stream' ) ;
11
12
12
13
tmpdir . refresh ( ) ;
13
14
14
15
const dest = path . resolve ( tmpDir , 'tmp.txt' ) ;
15
16
const otherDest = path . resolve ( tmpDir , 'tmp-2.txt' ) ;
16
17
const buffer = Buffer . from ( 'abc' . repeat ( 1000 ) ) ;
17
18
const buffer2 = Buffer . from ( 'xyz' . repeat ( 1000 ) ) ;
19
+ const stream = Readable . from ( [ 'a' , 'b' , 'c' ] ) ;
20
+ const stream2 = Readable . from ( [ 'ümlaut' , ' ' , 'sechzig' ] ) ;
21
+ const iterable = {
22
+ expected : 'abc' ,
23
+ * [ Symbol . iterator ] ( ) {
24
+ yield 'a' ;
25
+ yield 'b' ;
26
+ yield 'c' ;
27
+ }
28
+ } ;
29
+ function iterableWith ( value ) {
30
+ return {
31
+ * [ Symbol . iterator ] ( ) {
32
+ yield value ;
33
+ }
34
+ } ;
35
+ }
36
+ const bufferIterable = {
37
+ expected : 'abc' ,
38
+ * [ Symbol . iterator ] ( ) {
39
+ yield Buffer . from ( 'a' ) ;
40
+ yield Buffer . from ( 'b' ) ;
41
+ yield Buffer . from ( 'c' ) ;
42
+ }
43
+ } ;
44
+ const asyncIterable = {
45
+ expected : 'abc' ,
46
+ async * [ Symbol . asyncIterator ] ( ) {
47
+ yield 'a' ;
48
+ yield 'b' ;
49
+ yield 'c' ;
50
+ }
51
+ } ;
18
52
19
53
async function doWrite ( ) {
20
54
await fsPromises . writeFile ( dest , buffer ) ;
21
55
const data = fs . readFileSync ( dest ) ;
22
56
assert . deepStrictEqual ( data , buffer ) ;
23
57
}
24
58
59
+ async function doWriteStream ( ) {
60
+ await fsPromises . writeFile ( dest , stream ) ;
61
+ const expected = 'abc' ;
62
+ const data = fs . readFileSync ( dest , 'utf-8' ) ;
63
+ assert . deepStrictEqual ( data , expected ) ;
64
+ }
65
+
66
+ async function doWriteStreamWithCancel ( ) {
67
+ const controller = new AbortController ( ) ;
68
+ const { signal } = controller ;
69
+ process . nextTick ( ( ) => controller . abort ( ) ) ;
70
+ assert . rejects ( fsPromises . writeFile ( otherDest , stream , { signal } ) , {
71
+ name : 'AbortError'
72
+ } ) ;
73
+ }
74
+
75
+ async function doWriteIterable ( ) {
76
+ await fsPromises . writeFile ( dest , iterable ) ;
77
+ const data = fs . readFileSync ( dest , 'utf-8' ) ;
78
+ assert . deepStrictEqual ( data , iterable . expected ) ;
79
+ }
80
+
81
+ async function doWriteInvalidIterable ( ) {
82
+ await Promise . all (
83
+ [ 42 , 42n , { } , Symbol ( '42' ) , true , undefined , null , NaN ] . map ( ( value ) =>
84
+ assert . rejects ( fsPromises . writeFile ( dest , iterableWith ( value ) ) , {
85
+ code : 'ERR_INVALID_ARG_TYPE' ,
86
+ } )
87
+ )
88
+ ) ;
89
+ }
90
+
91
+ async function doWriteIterableWithEncoding ( ) {
92
+ await fsPromises . writeFile ( dest , stream2 , 'latin1' ) ;
93
+ const expected = 'ümlaut sechzig' ;
94
+ const data = fs . readFileSync ( dest , 'latin1' ) ;
95
+ assert . deepStrictEqual ( data , expected ) ;
96
+ }
97
+
98
+ async function doWriteBufferIterable ( ) {
99
+ await fsPromises . writeFile ( dest , bufferIterable ) ;
100
+ const data = fs . readFileSync ( dest , 'utf-8' ) ;
101
+ assert . deepStrictEqual ( data , bufferIterable . expected ) ;
102
+ }
103
+
104
+ async function doWriteAsyncIterable ( ) {
105
+ await fsPromises . writeFile ( dest , asyncIterable ) ;
106
+ const data = fs . readFileSync ( dest , 'utf-8' ) ;
107
+ assert . deepStrictEqual ( data , asyncIterable . expected ) ;
108
+ }
109
+
110
+ async function doWriteInvalidValues ( ) {
111
+ await Promise . all (
112
+ [ 42 , 42n , { } , Symbol ( '42' ) , true , undefined , null , NaN ] . map ( ( value ) =>
113
+ assert . rejects ( fsPromises . writeFile ( dest , value ) , {
114
+ code : 'ERR_INVALID_ARG_TYPE' ,
115
+ } )
116
+ )
117
+ ) ;
118
+ }
119
+
25
120
async function doWriteWithCancel ( ) {
26
121
const controller = new AbortController ( ) ;
27
122
const { signal } = controller ;
@@ -51,9 +146,18 @@ async function doReadWithEncoding() {
51
146
assert . deepStrictEqual ( data , syncData ) ;
52
147
}
53
148
54
- doWrite ( )
55
- . then ( doWriteWithCancel )
56
- . then ( doAppend )
57
- . then ( doRead )
58
- . then ( doReadWithEncoding )
59
- . then ( common . mustCall ( ) ) ;
149
+ ( async ( ) => {
150
+ await doWrite ( ) ;
151
+ await doWriteWithCancel ( ) ;
152
+ await doAppend ( ) ;
153
+ await doRead ( ) ;
154
+ await doReadWithEncoding ( ) ;
155
+ await doWriteStream ( ) ;
156
+ await doWriteStreamWithCancel ( ) ;
157
+ await doWriteIterable ( ) ;
158
+ await doWriteInvalidIterable ( ) ;
159
+ await doWriteIterableWithEncoding ( ) ;
160
+ await doWriteBufferIterable ( ) ;
161
+ await doWriteAsyncIterable ( ) ;
162
+ await doWriteInvalidValues ( ) ;
163
+ } ) ( ) . then ( common . mustCall ( ) ) ;
0 commit comments