0
0
mirror of https://github.com/nodejs/node.git synced 2024-12-01 16:10:02 +01:00
nodejs/test/parallel/test-fs-promises-file-handle-truncate.js
João Reis 8ef68e66d0 test: clean tmpdir on process exit
PR-URL: https://github.com/nodejs/node/pull/28858
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
2019-08-10 19:22:11 -07:00

28 lines
765 B
JavaScript

'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();
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');
await fileHandle.close();
}
validateTruncate().then(common.mustCall());