test: apply promises API to fourth appendFile test · nodejs/node@aa9dbf6
@@ -132,29 +132,51 @@ const throwNextTick = (e) => { process.nextTick(() => { throw e; }); };
132132.catch(throwNextTick);
133133}
134134135-// 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);
138139139-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);
142143143-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+}
144149145-// 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+}
150157151-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+}
158180159181// test that appendFile accepts file descriptors
160182const filename5 = join(tmpdir.path, 'append5.txt');
@@ -191,8 +213,7 @@ assert.throws(
191213{ code: 'ERR_INVALID_CALLBACK' });
192214193215process.on('exit', function() {
194-assert.strictEqual(ncallbacks, 6);
216+assert.strictEqual(ncallbacks, 4);
195217196-fs.unlinkSync(filename4);
197218fs.unlinkSync(filename5);
198219});