Skip to content

Commit aa9dbf6

Browse files
Trotttargos
authored andcommitted
test: apply promises API to fourth appendFile test
Add tests for `fs.promises.appendFile()` to the fourth (of five) test case in `test-fs-access`. (The previous test cases already have promises API versions.) PR-URL: #21131 Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Trivikram Kamat <[email protected]>
1 parent 185b9e4 commit aa9dbf6

File tree

1 file changed

+42
-21
lines changed

1 file changed

+42
-21
lines changed

test/parallel/test-fs-append-file.js

+42-21
Original file line numberDiff line numberDiff line change
@@ -132,29 +132,51 @@ const throwNextTick = (e) => { process.nextTick(() => { throw e; }); };
132132
.catch(throwNextTick);
133133
}
134134

135-
// test that appendFile accepts numbers.
136-
const filename4 = join(tmpdir.path, 'append4.txt');
137-
fs.writeFileSync(filename4, currentFileData);
135+
// test that appendFile accepts numbers (callback API)
136+
{
137+
const filename = join(tmpdir.path, 'append-numbers.txt');
138+
fs.writeFileSync(filename, currentFileData);
138139

139-
const m = 0o600;
140-
fs.appendFile(filename4, n, { mode: m }, function(e) {
141-
assert.ifError(e);
140+
const m = 0o600;
141+
fs.appendFile(filename, n, { mode: m }, common.mustCall((e) => {
142+
assert.ifError(e);
142143

143-
ncallbacks++;
144+
// windows permissions aren't unix
145+
if (!common.isWindows) {
146+
const st = fs.statSync(filename);
147+
assert.strictEqual(st.mode & 0o700, m);
148+
}
144149

145-
// windows permissions aren't unix
146-
if (!common.isWindows) {
147-
const st = fs.statSync(filename4);
148-
assert.strictEqual(st.mode & 0o700, m);
149-
}
150+
fs.readFile(filename, common.mustCall((e, buffer) => {
151+
assert.ifError(e);
152+
assert.strictEqual(Buffer.byteLength(String(n)) + currentFileData.length,
153+
buffer.length);
154+
}));
155+
}));
156+
}
150157

151-
fs.readFile(filename4, function(e, buffer) {
152-
assert.ifError(e);
153-
ncallbacks++;
154-
assert.strictEqual(Buffer.byteLength(String(n)) + currentFileData.length,
155-
buffer.length);
156-
});
157-
});
158+
// test that appendFile accepts numbers (promises API)
159+
{
160+
const filename = join(tmpdir.path, 'append-numbers-promises.txt');
161+
fs.writeFileSync(filename, currentFileData);
162+
163+
const m = 0o600;
164+
fs.promises.appendFile(filename, n, { mode: m })
165+
.then(common.mustCall(() => {
166+
// windows permissions aren't unix
167+
if (!common.isWindows) {
168+
const st = fs.statSync(filename);
169+
assert.strictEqual(st.mode & 0o700, m);
170+
}
171+
172+
return fs.promises.readFile(filename);
173+
}))
174+
.then((buffer) => {
175+
assert.strictEqual(Buffer.byteLength(String(n)) + currentFileData.length,
176+
buffer.length);
177+
})
178+
.catch(throwNextTick);
179+
}
158180

159181
// test that appendFile accepts file descriptors
160182
const filename5 = join(tmpdir.path, 'append5.txt');
@@ -191,8 +213,7 @@ assert.throws(
191213
{ code: 'ERR_INVALID_CALLBACK' });
192214

193215
process.on('exit', function() {
194-
assert.strictEqual(ncallbacks, 6);
216+
assert.strictEqual(ncallbacks, 4);
195217

196-
fs.unlinkSync(filename4);
197218
fs.unlinkSync(filename5);
198219
});

0 commit comments

Comments
 (0)