@@ -8,6 +8,7 @@ const common = require('../common');
8
8
const fs = require ( 'fs' ) ;
9
9
const { open, writeFile } = fs . promises ;
10
10
const path = require ( 'path' ) ;
11
+ const { Readable } = require ( 'stream' ) ;
11
12
const tmpdir = require ( '../common/tmpdir' ) ;
12
13
const assert = require ( 'assert' ) ;
13
14
const tmpDir = tmpdir . path ;
@@ -17,13 +18,15 @@ tmpdir.refresh();
17
18
async function validateWriteFile ( ) {
18
19
const filePathForHandle = path . resolve ( tmpDir , 'tmp-write-file2.txt' ) ;
19
20
const fileHandle = await open ( filePathForHandle , 'w+' ) ;
20
- const buffer = Buffer . from ( 'Hello world' . repeat ( 100 ) , 'utf8' ) ;
21
+ try {
22
+ const buffer = Buffer . from ( 'Hello world' . repeat ( 100 ) , 'utf8' ) ;
21
23
22
- await fileHandle . writeFile ( buffer ) ;
23
- const readFileData = fs . readFileSync ( filePathForHandle ) ;
24
- assert . deepStrictEqual ( buffer , readFileData ) ;
25
-
26
- await fileHandle . close ( ) ;
24
+ await fileHandle . writeFile ( buffer ) ;
25
+ const readFileData = fs . readFileSync ( filePathForHandle ) ;
26
+ assert . deepStrictEqual ( buffer , readFileData ) ;
27
+ } finally {
28
+ await fileHandle . close ( ) ;
29
+ }
27
30
}
28
31
29
32
// Signal aborted while writing file
@@ -39,6 +42,155 @@ async function doWriteAndCancel() {
39
42
} ) ;
40
43
}
41
44
42
- validateWriteFile ( )
43
- . then ( doWriteAndCancel )
44
- . then ( common . mustCall ( ) ) ;
45
+ const dest = path . resolve ( tmpDir , 'tmp.txt' ) ;
46
+ const otherDest = path . resolve ( tmpDir , 'tmp-2.txt' ) ;
47
+ const stream = Readable . from ( [ 'a' , 'b' , 'c' ] ) ;
48
+ const stream2 = Readable . from ( [ 'ümlaut' , ' ' , 'sechzig' ] ) ;
49
+ const iterable = {
50
+ expected : 'abc' ,
51
+ * [ Symbol . iterator ] ( ) {
52
+ yield 'a' ;
53
+ yield 'b' ;
54
+ yield 'c' ;
55
+ }
56
+ } ;
57
+ function iterableWith ( value ) {
58
+ return {
59
+ * [ Symbol . iterator ] ( ) {
60
+ yield value ;
61
+ }
62
+ } ;
63
+ }
64
+ const bufferIterable = {
65
+ expected : 'abc' ,
66
+ * [ Symbol . iterator ] ( ) {
67
+ yield Buffer . from ( 'a' ) ;
68
+ yield Buffer . from ( 'b' ) ;
69
+ yield Buffer . from ( 'c' ) ;
70
+ }
71
+ } ;
72
+ const asyncIterable = {
73
+ expected : 'abc' ,
74
+ async * [ Symbol . asyncIterator ] ( ) {
75
+ yield 'a' ;
76
+ yield 'b' ;
77
+ yield 'c' ;
78
+ }
79
+ } ;
80
+
81
+ async function doWriteStream ( ) {
82
+ const fileHandle = await open ( dest , 'w+' ) ;
83
+ try {
84
+ await fileHandle . writeFile ( stream ) ;
85
+ const expected = 'abc' ;
86
+ const data = fs . readFileSync ( dest , 'utf-8' ) ;
87
+ assert . deepStrictEqual ( data , expected ) ;
88
+ } finally {
89
+ await fileHandle . close ( ) ;
90
+ }
91
+ }
92
+
93
+ async function doWriteStreamWithCancel ( ) {
94
+ const controller = new AbortController ( ) ;
95
+ const { signal } = controller ;
96
+ process . nextTick ( ( ) => controller . abort ( ) ) ;
97
+ const fileHandle = await open ( otherDest , 'w+' ) ;
98
+ try {
99
+ await assert . rejects (
100
+ fileHandle . writeFile ( stream , { signal } ) ,
101
+ { name : 'AbortError' }
102
+ ) ;
103
+ } finally {
104
+ await fileHandle . close ( ) ;
105
+ }
106
+ }
107
+
108
+ async function doWriteIterable ( ) {
109
+ const fileHandle = await open ( dest , 'w+' ) ;
110
+ try {
111
+ await fileHandle . writeFile ( iterable ) ;
112
+ const data = fs . readFileSync ( dest , 'utf-8' ) ;
113
+ assert . deepStrictEqual ( data , iterable . expected ) ;
114
+ } finally {
115
+ await fileHandle . close ( ) ;
116
+ }
117
+ }
118
+
119
+ async function doWriteInvalidIterable ( ) {
120
+ const fileHandle = await open ( dest , 'w+' ) ;
121
+ try {
122
+ await Promise . all (
123
+ [ 42 , 42n , { } , Symbol ( '42' ) , true , undefined , null , NaN ] . map ( ( value ) =>
124
+ assert . rejects (
125
+ fileHandle . writeFile ( iterableWith ( value ) ) ,
126
+ { code : 'ERR_INVALID_ARG_TYPE' }
127
+ )
128
+ )
129
+ ) ;
130
+ } finally {
131
+ await fileHandle . close ( ) ;
132
+ }
133
+ }
134
+
135
+ async function doWriteIterableWithEncoding ( ) {
136
+ const fileHandle = await open ( dest , 'w+' ) ;
137
+ try {
138
+ await fileHandle . writeFile ( stream2 , 'latin1' ) ;
139
+ const expected = 'ümlaut sechzig' ;
140
+ const data = fs . readFileSync ( dest , 'latin1' ) ;
141
+ assert . deepStrictEqual ( data , expected ) ;
142
+ } finally {
143
+ await fileHandle . close ( ) ;
144
+ }
145
+ }
146
+
147
+ async function doWriteBufferIterable ( ) {
148
+ const fileHandle = await open ( dest , 'w+' ) ;
149
+ try {
150
+ await fileHandle . writeFile ( bufferIterable ) ;
151
+ const data = fs . readFileSync ( dest , 'utf-8' ) ;
152
+ assert . deepStrictEqual ( data , bufferIterable . expected ) ;
153
+ } finally {
154
+ await fileHandle . close ( ) ;
155
+ }
156
+ }
157
+
158
+ async function doWriteAsyncIterable ( ) {
159
+ const fileHandle = await open ( dest , 'w+' ) ;
160
+ try {
161
+ await fileHandle . writeFile ( asyncIterable ) ;
162
+ const data = fs . readFileSync ( dest , 'utf-8' ) ;
163
+ assert . deepStrictEqual ( data , asyncIterable . expected ) ;
164
+ } finally {
165
+ await fileHandle . close ( ) ;
166
+ }
167
+ }
168
+
169
+ async function doWriteInvalidValues ( ) {
170
+ const fileHandle = await open ( dest , 'w+' ) ;
171
+ try {
172
+ await Promise . all (
173
+ [ 42 , 42n , { } , Symbol ( '42' ) , true , undefined , null , NaN ] . map ( ( value ) =>
174
+ assert . rejects (
175
+ fileHandle . writeFile ( value ) ,
176
+ { code : 'ERR_INVALID_ARG_TYPE' }
177
+ )
178
+ )
179
+ ) ;
180
+ } finally {
181
+ await fileHandle . close ( ) ;
182
+ }
183
+ }
184
+
185
+ ( async ( ) => {
186
+ await validateWriteFile ( ) ;
187
+ await doWriteAndCancel ( ) ;
188
+ await doWriteStream ( ) ;
189
+ await doWriteStreamWithCancel ( ) ;
190
+ await doWriteIterable ( ) ;
191
+ await doWriteInvalidIterable ( ) ;
192
+ await doWriteIterableWithEncoding ( ) ;
193
+ await doWriteBufferIterable ( ) ;
194
+ await doWriteAsyncIterable ( ) ;
195
+ await doWriteInvalidValues ( ) ;
196
+ } ) ( ) . then ( common . mustCall ( ) ) ;
0 commit comments