node/test/parallel/test-fs-promises-file-handle-truncate.js
Livia Medeiros 74e0ca3f49
test: use tmpdir.resolve() in fs tests
PR-URL: https://github.com/nodejs/node/pull/49125
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Chemi Atlow <chemi@atlow.co.il>
2023-08-15 13:45:14 +00:00

27 lines
716 B
JavaScript

'use strict';
const common = require('../common');
const assert = require('assert');
const { open, readFile } = require('fs').promises;
const tmpdir = require('../common/tmpdir');
tmpdir.refresh();
async function validateTruncate() {
const text = 'Hello world';
const filename = tmpdir.resolve('truncate-file.txt');
const fileHandle = await open(filename, 'w+');
const buffer = Buffer.from(text, 'utf8');
await fileHandle.write(buffer, 0, buffer.length);
assert.strictEqual((await readFile(filename)).toString(), text);
await fileHandle.truncate(5);
assert.strictEqual((await readFile(filename)).toString(), 'Hello');
await fileHandle.close();
}
validateTruncate().then(common.mustCall());