Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
test: increase coverage for fs.promises.truncate
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>
  • Loading branch information
Masashi Hirano authored and targos committed Jun 6, 2018
commit f4287e61d71098e858a5d389feabac476ad6a51e
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