Skip to content

Commit 06890ff

Browse files
christian-bromannjasnell
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 <refack@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com> Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com> Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com> Reviewed-By: Denys Otrishko <shishugi@gmail.com>
1 parent eb48f28 commit 06890ff

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

test/parallel/test-fs-truncate.js

+35
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,41 @@ function testFtruncate(cb) {
239239
}));
240240
}
241241

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

0 commit comments

Comments
 (0)