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