summaryrefslogtreecommitdiff
path: root/test/parallel/test-fs-read-offset-null.js
blob: 012c94e41e92c7985be977e8f5a0d3a836561115 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
'use strict';


// Test to assert the desired functioning of fs.read
// when {offset:null} is passed as options parameter

const common = require('../common');
const assert = require('assert');
const fs = require('fs');
const fsPromises = fs.promises;
const fixtures = require('../common/fixtures');
const filepath = fixtures.path('x.txt');

const buf = Buffer.alloc(1);
// Reading only one character, hence buffer of one byte is enough.

// Tests are done by making sure the first letter in buffer is
// same as first letter in file.
// 120 is the ascii code of letter x.

// Tests for callback API.
fs.open(filepath, 'r', common.mustSucceed((fd) => {
  fs.read(fd, { offset: null, buffer: buf },
          common.mustSucceed((bytesRead, buffer) => {
            assert.strictEqual(buffer[0], 120);
            fs.close(fd, common.mustSucceed(() => {}));
          }));
}));

fs.open(filepath, 'r', common.mustSucceed((fd) => {
  fs.read(fd, buf, { offset: null },
          common.mustSucceed((bytesRead, buffer) => {
            assert.strictEqual(buffer[0], 120);
            fs.close(fd, common.mustSucceed(() => {}));
          }));
}));

let filehandle = null;

// Tests for promises api
(async () => {
  filehandle = await fsPromises.open(filepath, 'r');
  const readObject = await filehandle.read(buf, { offset: null });
  assert.strictEqual(readObject.buffer[0], 120);
})()
.finally(() => filehandle?.close())
.then(common.mustCall());

// Undocumented: omitted position works the same as position === null
(async () => {
  filehandle = await fsPromises.open(filepath, 'r');
  const readObject = await filehandle.read(buf, null, buf.length);
  assert.strictEqual(readObject.buffer[0], 120);
})()
.finally(() => filehandle?.close())
.then(common.mustCall());

(async () => {
  filehandle = await fsPromises.open(filepath, 'r');
  const readObject = await filehandle.read(buf, null, buf.length, 0);
  assert.strictEqual(readObject.buffer[0], 120);
})()
.finally(() => filehandle?.close())
.then(common.mustCall());