|
1 | 1 | 'use strict'; |
2 | 2 | const common = require('../common'); |
| 3 | +const assert = require('assert'); |
3 | 4 | const fs = require('fs'); |
4 | 5 |
|
5 | 6 | // This test ensures that input for fchmod is valid, testing for valid |
6 | 7 | // inputs for fd and mode |
7 | 8 |
|
8 | 9 | // Check input type |
9 | | -['', false, null, undefined, {}, [], Infinity, -1].forEach((i) => { |
10 | | - common.expectsError( |
11 | | - () => fs.fchmod(i), |
12 | | - { |
13 | | - code: 'ERR_INVALID_ARG_TYPE', |
14 | | - type: TypeError, |
15 | | - message: 'The "fd" argument must be of type integer' |
16 | | - } |
17 | | - ); |
18 | | - common.expectsError( |
19 | | - () => fs.fchmodSync(i), |
20 | | - { |
21 | | - code: 'ERR_INVALID_ARG_TYPE', |
22 | | - type: TypeError, |
23 | | - message: 'The "fd" argument must be of type integer' |
24 | | - } |
25 | | - ); |
| 10 | +[false, null, undefined, {}, [], ''].forEach((input) => { |
| 11 | + const errObj = { |
| 12 | + code: 'ERR_INVALID_ARG_TYPE', |
| 13 | + name: 'TypeError [ERR_INVALID_ARG_TYPE]', |
| 14 | + message: 'The "fd" argument must be of type number. Received type ' + |
| 15 | + typeof input |
| 16 | + }; |
| 17 | + assert.throws(() => fs.fchmod(input), errObj); |
| 18 | + assert.throws(() => fs.fchmodSync(input), errObj); |
| 19 | + errObj.message = errObj.message.replace('fd', 'mode'); |
| 20 | + assert.throws(() => fs.fchmod(1, input), errObj); |
| 21 | + assert.throws(() => fs.fchmodSync(1, input), errObj); |
| 22 | +}); |
| 23 | + |
| 24 | +[-1, 2 ** 32].forEach((input) => { |
| 25 | + const errObj = { |
| 26 | + code: 'ERR_OUT_OF_RANGE', |
| 27 | + name: 'RangeError [ERR_OUT_OF_RANGE]', |
| 28 | + message: 'The value of "fd" is out of range. It must be >= 0 && < ' + |
| 29 | + `${2 ** 32}. Received ${input}` |
| 30 | + }; |
| 31 | + assert.throws(() => fs.fchmod(input), errObj); |
| 32 | + assert.throws(() => fs.fchmodSync(input), errObj); |
| 33 | + errObj.message = errObj.message.replace('fd', 'mode'); |
| 34 | + assert.throws(() => fs.fchmod(1, input), errObj); |
| 35 | + assert.throws(() => fs.fchmodSync(1, input), errObj); |
| 36 | +}); |
26 | 37 |
|
27 | | - common.expectsError( |
28 | | - () => fs.fchmod(1, i), |
29 | | - { |
30 | | - code: 'ERR_INVALID_ARG_TYPE', |
31 | | - type: TypeError, |
32 | | - message: 'The "mode" argument must be of type integer' |
33 | | - } |
34 | | - ); |
35 | | - common.expectsError( |
36 | | - () => fs.fchmodSync(1, i), |
37 | | - { |
38 | | - code: 'ERR_INVALID_ARG_TYPE', |
39 | | - type: TypeError, |
40 | | - message: 'The "mode" argument must be of type integer' |
41 | | - } |
42 | | - ); |
| 38 | +[NaN, Infinity].forEach((input) => { |
| 39 | + const errObj = { |
| 40 | + code: 'ERR_OUT_OF_RANGE', |
| 41 | + name: 'RangeError [ERR_OUT_OF_RANGE]', |
| 42 | + message: 'The value of "fd" is out of range. It must be an integer. ' + |
| 43 | + `Received ${input}` |
| 44 | + }; |
| 45 | + assert.throws(() => fs.fchmod(input), errObj); |
| 46 | + assert.throws(() => fs.fchmodSync(input), errObj); |
| 47 | + errObj.message = errObj.message.replace('fd', 'mode'); |
| 48 | + assert.throws(() => fs.fchmod(1, input), errObj); |
| 49 | + assert.throws(() => fs.fchmodSync(1, input), errObj); |
| 50 | +}); |
| 51 | + |
| 52 | +[1.5].forEach((input) => { |
| 53 | + const errObj = { |
| 54 | + code: 'ERR_OUT_OF_RANGE', |
| 55 | + name: 'RangeError [ERR_OUT_OF_RANGE]', |
| 56 | + message: 'The value of "fd" is out of range. It must be an integer. ' + |
| 57 | + `Received ${input}` |
| 58 | + }; |
| 59 | + assert.throws(() => fs.fchmod(input), errObj); |
| 60 | + assert.throws(() => fs.fchmodSync(input), errObj); |
| 61 | + errObj.message = errObj.message.replace('fd', 'mode'); |
| 62 | + assert.throws(() => fs.fchmod(1, input), errObj); |
| 63 | + assert.throws(() => fs.fchmodSync(1, input), errObj); |
43 | 64 | }); |
44 | 65 |
|
45 | 66 | // Check for mode values range |
|
0 commit comments