|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const common = require('../common'); |
| 4 | +const assert = require('assert'); |
| 5 | +const path = require('path'); |
| 6 | +const { open, readFile } = require('fs').promises; |
| 7 | +const tmpdir = require('../common/tmpdir'); |
| 8 | + |
| 9 | +tmpdir.refresh(); |
| 10 | +common.crashOnUnhandledRejection(); |
| 11 | + |
| 12 | +async function validateTruncate() { |
| 13 | +const text = 'Hello world'; |
| 14 | +const filename = path.resolve(tmpdir.path, 'truncate-file.txt'); |
| 15 | +const fileHandle = await open(filename, 'w+'); |
| 16 | + |
| 17 | +const buffer = Buffer.from(text, 'utf8'); |
| 18 | +await fileHandle.write(buffer, 0, buffer.length); |
| 19 | + |
| 20 | +assert.deepStrictEqual((await readFile(filename)).toString(), text); |
| 21 | + |
| 22 | +await fileHandle.truncate(5); |
| 23 | +assert.deepStrictEqual((await readFile(filename)).toString(), 'Hello'); |
| 24 | +} |
| 25 | + |
| 26 | +validateTruncate().then(common.mustCall()); |