| 1 |
'use strict' |
| 2 |
|
| 3 |
const fs = require('fs') |
| 4 |
const os = require('os') |
| 5 |
const fse = require(process.cwd()) |
| 6 |
const path = require('path') |
| 7 |
const assert = require('assert') |
| 8 |
|
| 9 |
/* global afterEach, beforeEach, describe, it */ |
| 10 |
|
| 11 |
describe('mkdirp / sync', () => { |
| 12 |
let TEST_DIR |
| 13 |
let file |
| 14 |
|
| 15 |
beforeEach(done => { |
| 16 |
TEST_DIR = path.join(os.tmpdir(), 'fs-extra', 'mkdirp-sync') |
| 17 |
fse.emptyDir(TEST_DIR, err => { |
| 18 |
assert.ifError(err) |
| 19 |
|
| 20 |
const x = Math.floor(Math.random() * Math.pow(16, 4)).toString(16) |
| 21 |
const y = Math.floor(Math.random() * Math.pow(16, 4)).toString(16) |
| 22 |
const z = Math.floor(Math.random() * Math.pow(16, 4)).toString(16) |
| 23 |
|
| 24 |
file = path.join(TEST_DIR, x, y, z) |
| 25 |
|
| 26 |
done() |
| 27 |
}) |
| 28 |
}) |
| 29 |
|
| 30 |
afterEach(done => fse.remove(TEST_DIR, done)) |
| 31 |
|
| 32 |
it('should', done => { |
| 33 |
try { |
| 34 |
fse.mkdirpSync(file, 0o755) |
| 35 |
} catch (err) { |
| 36 |
assert.fail(err) |
| 37 |
} |
| 38 |
|
| 39 |
fse.pathExists(file, (err, ex) => { |
| 40 |
assert.ifError(err) |
| 41 |
assert.ok(ex, 'file created') |
| 42 |
fs.stat(file, (err, stat) => { |
| 43 |
assert.ifError(err) |
| 44 |
// http://stackoverflow.com/questions/592448/c-how-to-set-file-permissions-cross-platform |
| 45 |
if (os.platform().indexOf('win') === 0) { |
| 46 |
assert.strictEqual(stat.mode & 0o777, 0o666) |
| 47 |
} else { |
| 48 |
assert.strictEqual(stat.mode & 0o777, 0o755) |
| 49 |
} |
| 50 |
|
| 51 |
assert.ok(stat.isDirectory(), 'target not a directory') |
| 52 |
done() |
| 53 |
}) |
| 54 |
}) |
| 55 |
}) |
| 56 |
}) |