• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2const common = require('../common');
3const fs = require('fs');
4const assert = require('assert');
5const fixtures = require('../common/fixtures');
6
7const filepath = fixtures.path('x.txt');
8const fd = fs.openSync(filepath, 'r');
9const expected = 'xyz\n';
10
11
12// Error must be thrown with string
13assert.throws(
14  () => fs.read(fd, expected.length, 0, 'utf-8', common.mustNotCall()),
15  {
16    code: 'ERR_INVALID_ARG_TYPE',
17    name: 'TypeError',
18    message: 'The "buffer" argument must be an instance of Buffer, ' +
19             'TypedArray, or DataView. Received type number (4)'
20  }
21);
22
23[true, null, undefined, () => {}, {}].forEach((value) => {
24  assert.throws(() => {
25    fs.read(value,
26            Buffer.allocUnsafe(expected.length),
27            0,
28            expected.length,
29            0,
30            common.mustNotCall());
31  }, {
32    code: 'ERR_INVALID_ARG_TYPE',
33    name: 'TypeError'
34  });
35});
36
37assert.throws(() => {
38  fs.read(fd,
39          Buffer.allocUnsafe(expected.length),
40          -1,
41          expected.length,
42          0,
43          common.mustNotCall());
44}, {
45  code: 'ERR_OUT_OF_RANGE',
46  name: 'RangeError',
47  message: 'The value of "offset" is out of range. It must be >= 0. ' +
48           'Received -1'
49});
50
51assert.throws(() => {
52  fs.read(fd,
53          Buffer.allocUnsafe(expected.length),
54          0,
55          -1,
56          0,
57          common.mustNotCall());
58}, {
59  code: 'ERR_OUT_OF_RANGE',
60  name: 'RangeError',
61  message: 'The value of "length" is out of range. ' +
62           'It must be >= 0. Received -1'
63});
64
65
66assert.throws(
67  () => fs.readSync(fd, expected.length, 0, 'utf-8'),
68  {
69    code: 'ERR_INVALID_ARG_TYPE',
70    name: 'TypeError',
71    message: 'The "buffer" argument must be an instance of Buffer, ' +
72             'TypedArray, or DataView. Received type number (4)'
73  }
74);
75
76[true, null, undefined, () => {}, {}].forEach((value) => {
77  assert.throws(() => {
78    fs.readSync(value,
79                Buffer.allocUnsafe(expected.length),
80                0,
81                expected.length,
82                0);
83  }, {
84    code: 'ERR_INVALID_ARG_TYPE',
85    name: 'TypeError'
86  });
87});
88
89assert.throws(() => {
90  fs.readSync(fd,
91              Buffer.allocUnsafe(expected.length),
92              -1,
93              expected.length,
94              0);
95}, {
96  code: 'ERR_OUT_OF_RANGE',
97  name: 'RangeError',
98  message: 'The value of "offset" is out of range. ' +
99           'It must be >= 0. Received -1'
100});
101
102assert.throws(() => {
103  fs.readSync(fd,
104              Buffer.allocUnsafe(expected.length),
105              0,
106              -1,
107              0);
108}, {
109  code: 'ERR_OUT_OF_RANGE',
110  name: 'RangeError',
111  message: 'The value of "length" is out of range. ' +
112           'It must be >= 0. Received -1'
113});
114
115assert.throws(() => {
116  fs.readSync(fd,
117              Buffer.allocUnsafe(expected.length),
118              0,
119              expected.length + 1,
120              0);
121}, {
122  code: 'ERR_OUT_OF_RANGE',
123  name: 'RangeError',
124  message: 'The value of "length" is out of range. ' +
125           'It must be <= 4. Received 5'
126});
127