Skip to content

Commit 642b168

Browse files
christian-bromannMylesBorins
authored andcommitted
test: add test coverage for fs.truncate
Add test to check: - for `null` as len parameter - if error is propagated into callback if file doesn't exist - if an error is actually thrown if len is not a number PR-URL: #23620 Reviewed-By: Refael Ackermann <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Trivikram Kamat <[email protected]> Reviewed-By: Gireesh Punathil <[email protected]> Reviewed-By: Sakthipriyan Vairamani <[email protected]> Reviewed-By: Denys Otrishko <[email protected]>
1 parent f82611a commit 642b168

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

test/parallel/test-fs-truncate.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,41 @@ function testFtruncate(cb) {
249249
}));
250250
}
251251

252+
{
253+
const file7 = path.resolve(tmp, 'truncate-file-7.txt');
254+
fs.writeFileSync(file7, 'Hi');
255+
fs.truncate(file7, undefined, common.mustCall(function(err) {
256+
assert.ifError(err);
257+
assert(fs.readFileSync(file7).equals(Buffer.from('')));
258+
}));
259+
}
260+
261+
{
262+
const file8 = path.resolve(tmp, 'non-existent-truncate-file.txt');
263+
const validateError = (err) => {
264+
assert.strictEqual(file8, err.path);
265+
assert.strictEqual(
266+
err.message,
267+
`ENOENT: no such file or directory, open '${file8}'`);
268+
assert.strictEqual(err.code, 'ENOENT');
269+
assert.strictEqual(err.syscall, 'open');
270+
return true;
271+
};
272+
fs.truncate(file8, 0, common.mustCall(validateError));
273+
}
274+
275+
['', false, null, {}, []].forEach((input) => {
276+
assert.throws(
277+
() => fs.truncate('/foo/bar', input),
278+
{
279+
code: 'ERR_INVALID_ARG_TYPE',
280+
name: 'TypeError [ERR_INVALID_ARG_TYPE]',
281+
message: 'The "len" argument must be of type number. ' +
282+
`Received type ${typeof input}`
283+
}
284+
);
285+
});
286+
252287
['', false, null, undefined, {}, []].forEach((input) => {
253288
['ftruncate', 'ftruncateSync'].forEach((fnName) => {
254289
assert.throws(

0 commit comments

Comments
 (0)