• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2
3const common = require('../common');
4const tmpdir = require('../common/tmpdir');
5const assert = require('assert');
6const fs = require('fs');
7const path = require('path');
8const { promises } = fs;
9
10// Validate the path argument.
11[false, 1, {}, [], null, undefined].forEach((i) => {
12  const err = { name: 'TypeError', code: 'ERR_INVALID_ARG_TYPE' };
13
14  assert.throws(() => fs.lchown(i, 1, 1, common.mustNotCall()), err);
15  assert.throws(() => fs.lchownSync(i, 1, 1), err);
16  promises.lchown(false, 1, 1)
17    .then(common.mustNotCall())
18    .catch(common.expectsError(err));
19});
20
21// Validate the uid and gid arguments.
22[false, 'test', {}, [], null, undefined].forEach((i) => {
23  const err = { name: 'TypeError', code: 'ERR_INVALID_ARG_TYPE' };
24
25  assert.throws(
26    () => fs.lchown('not_a_file_that_exists', i, 1, common.mustNotCall()),
27    err
28  );
29  assert.throws(
30    () => fs.lchown('not_a_file_that_exists', 1, i, common.mustNotCall()),
31    err
32  );
33  assert.throws(() => fs.lchownSync('not_a_file_that_exists', i, 1), err);
34  assert.throws(() => fs.lchownSync('not_a_file_that_exists', 1, i), err);
35
36  promises.lchown('not_a_file_that_exists', i, 1)
37    .then(common.mustNotCall())
38    .catch(common.expectsError(err));
39
40  promises.lchown('not_a_file_that_exists', 1, i)
41    .then(common.mustNotCall())
42    .catch(common.expectsError(err));
43});
44
45// Validate the callback argument.
46[false, 1, 'test', {}, [], null, undefined].forEach((i) => {
47  assert.throws(() => fs.lchown('not_a_file_that_exists', 1, 1, i), {
48    name: 'TypeError',
49    code: 'ERR_INVALID_CALLBACK'
50  });
51});
52
53if (!common.isWindows) {
54  const testFile = path.join(tmpdir.path, path.basename(__filename));
55  const uid = process.geteuid();
56  const gid = process.getegid();
57
58  tmpdir.refresh();
59  fs.copyFileSync(__filename, testFile);
60  fs.lchownSync(testFile, uid, gid);
61  fs.lchown(testFile, uid, gid, common.mustSucceed(async (err) => {
62    await promises.lchown(testFile, uid, gid);
63  }));
64}
65