2018-10-17 14:40:08 +05:30
|
|
|
'use strict';
|
|
|
|
|
2020-10-03 13:01:57 -07:00
|
|
|
// This test makes sure that `readFile()` always reads from the current
|
|
|
|
// position of the file, instead of reading from the beginning of the file.
|
2018-10-17 14:40:08 +05:30
|
|
|
|
|
|
|
const common = require('../common');
|
|
|
|
const assert = require('assert');
|
|
|
|
const { writeFileSync } = require('fs');
|
|
|
|
const { open } = require('fs').promises;
|
|
|
|
|
|
|
|
const tmpdir = require('../common/tmpdir');
|
|
|
|
tmpdir.refresh();
|
|
|
|
|
2023-08-15 22:45:14 +09:00
|
|
|
const fn = tmpdir.resolve('test.txt');
|
2018-10-17 14:40:08 +05:30
|
|
|
writeFileSync(fn, 'Hello World');
|
|
|
|
|
|
|
|
async function readFileTest() {
|
|
|
|
const handle = await open(fn, 'r');
|
|
|
|
|
|
|
|
/* Read only five bytes, so that the position moves to five. */
|
|
|
|
const buf = Buffer.alloc(5);
|
|
|
|
const { bytesRead } = await handle.read(buf, 0, 5, null);
|
|
|
|
assert.strictEqual(bytesRead, 5);
|
2021-10-27 16:15:11 +00:00
|
|
|
assert.strictEqual(buf.toString(), 'Hello');
|
2018-10-17 14:40:08 +05:30
|
|
|
|
|
|
|
/* readFile() should read from position five, instead of zero. */
|
2021-10-27 16:15:11 +00:00
|
|
|
assert.strictEqual((await handle.readFile()).toString(), ' World');
|
2019-07-30 07:50:21 +01:00
|
|
|
|
|
|
|
await handle.close();
|
2018-10-17 14:40:08 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
readFileTest()
|
|
|
|
.then(common.mustCall());
|