• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2
3
4// Test to assert the desired functioning of fs.read
5// when {offset:null} is passed as options parameter
6
7const common = require('../common');
8const assert = require('assert');
9const fs = require('fs');
10const fsPromises = fs.promises;
11const fixtures = require('../common/fixtures');
12const filepath = fixtures.path('x.txt');
13
14const buf = Buffer.alloc(1);
15// Reading only one character, hence buffer of one byte is enough.
16
17// Test for callback API.
18fs.open(filepath, 'r', common.mustSucceed((fd) => {
19  fs.read(fd, { offset: null, buffer: buf },
20          common.mustSucceed((bytesRead, buffer) => {
21            // Test is done by making sure the first letter in buffer is
22            // same as first letter in file.
23            // 120 is the hex for ascii code of letter x.
24            assert.strictEqual(buffer[0], 120);
25            fs.close(fd, common.mustSucceed(() => {}));
26          }));
27}));
28
29let filehandle = null;
30
31// Test for promise api
32(async () => {
33  filehandle = await fsPromises.open(filepath, 'r');
34  const readObject = await filehandle.read(buf, null, buf.length);
35  assert.strictEqual(readObject.buffer[0], 120);
36})()
37.then(common.mustCall())
38.finally(async () => {
39// Close the file handle if it is opened
40  if (filehandle)
41    await filehandle.close();
42});
43