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