◐ Shell
clean mode source ↗

test: apply promises API to third appendFile test · nodejs/node@185b9e4

@@ -100,23 +100,37 @@ const throwNextTick = (e) => { process.nextTick(() => { throw e; }); };

100100

.catch(throwNextTick);

101101

}

102102103-

// test that appendFile accepts buffers

104-

const filename3 = join(tmpdir.path, 'append3.txt');

105-

fs.writeFileSync(filename3, currentFileData);

103+

// test that appendFile accepts buffers (callback API)

104+

{

105+

const filename = join(tmpdir.path, 'append-buffer.txt');

106+

fs.writeFileSync(filename, currentFileData);

106107107-

const buf = Buffer.from(s, 'utf8');

108+

const buf = Buffer.from(s, 'utf8');

108109109-

fs.appendFile(filename3, buf, function(e) {

110-

assert.ifError(e);

110+

fs.appendFile(filename, buf, common.mustCall((e) => {

111+

assert.ifError(e);

111112112-

ncallbacks++;

113+

fs.readFile(filename, common.mustCall((e, buffer) => {

114+

assert.ifError(e);

115+

assert.strictEqual(buf.length + currentFileData.length, buffer.length);

116+

}));

117+

}));

118+

}

113119114-

fs.readFile(filename3, function(e, buffer) {

115-

assert.ifError(e);

116-

ncallbacks++;

117-

assert.strictEqual(buf.length + currentFileData.length, buffer.length);

118-

});

119-

});

120+

// test that appendFile accepts buffers (promises API)

121+

{

122+

const filename = join(tmpdir.path, 'append-buffer-promises.txt');

123+

fs.writeFileSync(filename, currentFileData);

124+125+

const buf = Buffer.from(s, 'utf8');

126+127+

fs.promises.appendFile(filename, buf)

128+

.then(common.mustCall(() => fs.promises.readFile(filename)))

129+

.then((buffer) => {

130+

assert.strictEqual(buf.length + currentFileData.length, buffer.length);

131+

})

132+

.catch(throwNextTick);

133+

}

120134121135

// test that appendFile accepts numbers.

122136

const filename4 = join(tmpdir.path, 'append4.txt');

@@ -177,9 +191,8 @@ assert.throws(

177191

{ code: 'ERR_INVALID_CALLBACK' });

178192179193

process.on('exit', function() {

180-

assert.strictEqual(ncallbacks, 8);

194+

assert.strictEqual(ncallbacks, 6);

181195182-

fs.unlinkSync(filename3);

183196

fs.unlinkSync(filename4);

184197

fs.unlinkSync(filename5);

185198

});