Skip to content

Commit

Permalink
test: increase coverage for fs.promises.truncate
Browse files Browse the repository at this point in the history
To increase test coverage, added tests for fs.promises.truncate
and FileHandle.truncate.

PR-URL: #20638
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>

Backport-PR-URL: #21172
  • Loading branch information
Masashi Hirano authored and targos committed Jun 13, 2018
1 parent bacb2cb commit 94dcdfb
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
26 changes: 26 additions & 0 deletions test/parallel/test-fs-promises-file-handle-truncate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
'use strict';

const common = require('../common');
const assert = require('assert');
const path = require('path');
const { open, readFile } = require('fs').promises;
const tmpdir = require('../common/tmpdir');

tmpdir.refresh();
common.crashOnUnhandledRejection();

async function validateTruncate() {
const text = 'Hello world';
const filename = path.resolve(tmpdir.path, 'truncate-file.txt');
const fileHandle = await open(filename, 'w+');

const buffer = Buffer.from(text, 'utf8');
await fileHandle.write(buffer, 0, buffer.length);

assert.deepStrictEqual((await readFile(filename)).toString(), text);

await fileHandle.truncate(5);
assert.deepStrictEqual((await readFile(filename)).toString(), 'Hello');
}

validateTruncate().then(common.mustCall());
4 changes: 4 additions & 0 deletions test/parallel/test-fs-promises.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@ const {
mkdir,
mkdtemp,
open,
readFile,
readdir,
readlink,
realpath,
rename,
rmdir,
stat,
symlink,
truncate,
unlink,
utimes
} = fsPromises;
Expand Down Expand Up @@ -99,6 +101,8 @@ function verifyStatObject(stat) {
const ret2 = await handle.read(Buffer.alloc(buf2Len), 0, buf2Len, 0);
assert.strictEqual(ret2.bytesRead, buf2Len);
assert.deepStrictEqual(ret2.buffer, buf2);
await truncate(dest, 5);
assert.deepStrictEqual((await readFile(dest)).toString(), 'hello');

await chmod(dest, 0o666);
await handle.chmod(0o666);
Expand Down

0 comments on commit 94dcdfb

Please sign in to comment.