• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2
3const common = require('../common');
4const assert = require('assert');
5const util = require('util');
6const fs = require('fs');
7const { promises } = fs;
8const f = __filename;
9
10// This test ensures that input for lchmod is valid, testing for valid
11// inputs for path, mode and callback
12
13if (!common.isOSX) {
14  common.skip('lchmod is only available on macOS');
15}
16
17// Check callback
18assert.throws(() => fs.lchmod(f), { code: 'ERR_INVALID_CALLBACK' });
19assert.throws(() => fs.lchmod(), { code: 'ERR_INVALID_CALLBACK' });
20assert.throws(() => fs.lchmod(f, {}), { code: 'ERR_INVALID_CALLBACK' });
21
22// Check path
23[false, 1, {}, [], null, undefined].forEach((i) => {
24  assert.throws(
25    () => fs.lchmod(i, 0o777, common.mustNotCall()),
26    {
27      code: 'ERR_INVALID_ARG_TYPE',
28      name: 'TypeError'
29    }
30  );
31  assert.throws(
32    () => fs.lchmodSync(i),
33    {
34      code: 'ERR_INVALID_ARG_TYPE',
35      name: 'TypeError'
36    }
37  );
38});
39
40// Check mode
41[false, null, undefined, {}, [], '', '123x'].forEach((input) => {
42  const errObj = {
43    code: 'ERR_INVALID_ARG_VALUE',
44    name: 'TypeError',
45    message: 'The argument \'mode\' must be a 32-bit unsigned integer or an ' +
46             `octal string. Received ${util.inspect(input)}`
47  };
48
49  assert.rejects(promises.lchmod(f, input, () => {}), errObj);
50  assert.throws(() => fs.lchmodSync(f, input), errObj);
51});
52
53[-1, 2 ** 32].forEach((input) => {
54  const errObj = {
55    code: 'ERR_OUT_OF_RANGE',
56    name: 'RangeError',
57    message: 'The value of "mode" is out of range. It must be >= 0 && <= ' +
58             `4294967295. Received ${input}`
59  };
60
61  assert.rejects(promises.lchmod(f, input, () => {}), errObj);
62  assert.throws(() => fs.lchmodSync(f, input), errObj);
63});
64