• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2
3/*
4 * This test makes sure that `readFile()` always reads from the current
5 * position of the file, instead of reading from the beginning of the file.
6 */
7
8const common = require('../common');
9const assert = require('assert');
10const path = require('path');
11const { writeFileSync } = require('fs');
12const { open } = require('fs').promises;
13
14const tmpdir = require('../common/tmpdir');
15tmpdir.refresh();
16
17const fn = path.join(tmpdir.path, 'test.txt');
18writeFileSync(fn, 'Hello World');
19
20async function readFileTest() {
21  const handle = await open(fn, 'r');
22
23  /* Read only five bytes, so that the position moves to five. */
24  const buf = Buffer.alloc(5);
25  const { bytesRead } = await handle.read(buf, 0, 5, null);
26  assert.strictEqual(bytesRead, 5);
27  assert.deepStrictEqual(buf.toString(), 'Hello');
28
29  /* readFile() should read from position five, instead of zero. */
30  assert.deepStrictEqual((await handle.readFile()).toString(), ' World');
31}
32
33
34readFileTest()
35  .then(common.mustCall());
36