Skip to content

Commit

Permalink
test: add test coverage for fs.truncate
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
christian-bromann authored and addaleax committed Oct 20, 2018
1 parent bf2e6c7 commit 0a1dde4
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions test/parallel/test-fs-truncate.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,41 @@ function testFtruncate(cb) {
}));
}

{
const file7 = path.resolve(tmp, 'truncate-file-7.txt');
fs.writeFileSync(file7, 'Hi');
fs.truncate(file7, undefined, common.mustCall(function(err) {
assert.ifError(err);
assert(fs.readFileSync(file7).equals(Buffer.from('')));
}));
}

{
const file8 = path.resolve(tmp, 'non-existent-truncate-file.txt');
const validateError = (err) => {
assert.strictEqual(file8, err.path);
assert.strictEqual(
err.message,
`ENOENT: no such file or directory, open '${file8}'`);
assert.strictEqual(err.code, 'ENOENT');
assert.strictEqual(err.syscall, 'open');
return true;
};
fs.truncate(file8, 0, common.mustCall(validateError));
}

['', false, null, {}, []].forEach((input) => {
assert.throws(
() => fs.truncate('/foo/bar', input),
{
code: 'ERR_INVALID_ARG_TYPE',
name: 'TypeError [ERR_INVALID_ARG_TYPE]',
message: 'The "len" argument must be of type number. ' +
`Received type ${typeof input}`
}
);
});

['', false, null, undefined, {}, []].forEach((input) => {
['ftruncate', 'ftruncateSync'].forEach((fnName) => {
assert.throws(
Expand Down

0 comments on commit 0a1dde4

Please sign in to comment.