|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +// Test that mkdir with recursive option returns appropriate error |
| 4 | +// when executed on folder it does not have permission to access. |
| 5 | +// Ref: https://github.com/nodejs/node/issues/31481 |
| 6 | + |
| 7 | +const common = require('../common'); |
| 8 | + |
| 9 | +if (!common.isWindows && process.getuid() === 0) |
| 10 | + common.skip('as this test should not be run as `root`'); |
| 11 | + |
| 12 | +if (common.isIBMi) |
| 13 | + common.skip('IBMi has a different access permission mechanism'); |
| 14 | + |
| 15 | +const tmpdir = require('../common/tmpdir'); |
| 16 | +tmpdir.refresh(); |
| 17 | + |
| 18 | +const assert = require('assert'); |
| 19 | +const fs = require('fs'); |
| 20 | +const path = require('path'); |
| 21 | + |
| 22 | +let n = 0; |
| 23 | + |
| 24 | +// Synchronous API should return an EACCESS error with path populated. |
| 25 | +{ |
| 26 | + const dir = path.join(tmpdir.path, `mkdirp_${n++}`); |
| 27 | + fs.mkdirSync(dir); |
| 28 | + fs.chmodSync(dir, '444'); |
| 29 | + let err = null; |
| 30 | + try { |
| 31 | + fs.mkdirSync(path.join(dir, '/foo'), { recursive: true }); |
| 32 | + } catch (_err) { |
| 33 | + err = _err; |
| 34 | + } |
| 35 | + assert(err); |
| 36 | + assert.strictEqual(err.code, 'EACCES'); |
| 37 | + assert(err.path); |
| 38 | +} |
| 39 | + |
| 40 | +// Asynchronous API should return an EACCESS error with path populated. |
| 41 | +{ |
| 42 | + const dir = path.join(tmpdir.path, `mkdirp_${n++}`); |
| 43 | + fs.mkdirSync(dir); |
| 44 | + fs.chmodSync(dir, '444'); |
| 45 | + fs.mkdir(path.join(dir, '/bar'), { recursive: true }, (err) => { |
| 46 | + assert(err); |
| 47 | + assert.strictEqual(err.code, 'EACCES'); |
| 48 | + assert(err.path); |
| 49 | + }); |
| 50 | +} |
0 commit comments