mirror of
https://github.com/nodejs/node.git
synced 2024-12-01 16:10:02 +01:00
8ef68e66d0
PR-URL: https://github.com/nodejs/node/pull/28858 Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
28 lines
765 B
JavaScript
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());
|