Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 642b168

Browse filesBrowse 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 <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 f82611a commit 642b168
Copy full SHA for 642b168

File tree

Expand file treeCollapse file tree

1 file changed

+35
-0
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

1 file changed

+35
-0
lines changed
Open diff view settings
Collapse file

‎test/parallel/test-fs-truncate.js‎

Copy file name to clipboardExpand all lines: test/parallel/test-fs-truncate.js
+35Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.