| 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 |
|
| 10 |
|
| 11 |
describe('fs-extra', () => { |
| 12 |
let TEST_DIR |
| 13 |
|
| 14 |
beforeEach(done => { |
| 15 |
TEST_DIR = path.join(os.tmpdir(), 'fs-extra', 'ensure') |
| 16 |
fse.emptyDir(TEST_DIR, done) |
| 17 |
}) |
| 18 |
|
| 19 |
afterEach(done => fse.remove(TEST_DIR, done)) |
| 20 |
|
| 21 |
describe('+ ensureFile()', () => { |
| 22 |
describe('> when file exists', () => { |
| 23 |
it('should not do anything', done => { |
| 24 |
const file = path.join(TEST_DIR, 'file.txt') |
| 25 |
fs.writeFileSync(file, 'blah') |
| 26 |
|
| 27 |
assert(fs.existsSync(file)) |
| 28 |
fse.ensureFile(file, err => { |
| 29 |
assert.ifError(err) |
| 30 |
assert(fs.existsSync(file)) |
| 31 |
done() |
| 32 |
}) |
| 33 |
}) |
| 34 |
}) |
| 35 |
|
| 36 |
describe('> when file does not exist', () => { |
| 37 |
it('should create the file', done => { |
| 38 |
const file = path.join(TEST_DIR, 'dir/that/does/not/exist', 'file.txt') |
| 39 |
|
| 40 |
assert(!fs.existsSync(file)) |
| 41 |
fse.ensureFile(file, err => { |
| 42 |
assert.ifError(err) |
| 43 |
assert(fs.existsSync(file)) |
| 44 |
done() |
| 45 |
}) |
| 46 |
}) |
| 47 |
}) |
| 48 |
|
| 49 |
describe('> when there is a directory at that path', () => { |
| 50 |
it('should error', done => { |
| 51 |
const p = path.join(TEST_DIR, 'somedir') |
| 52 |
fs.mkdirSync(p) |
| 53 |
|
| 54 |
fse.ensureFile(p, err => { |
| 55 |
assert(err) |
| 56 |
assert.strictEqual(err.code, 'EISDIR') |
| 57 |
done() |
| 58 |
}) |
| 59 |
}) |
| 60 |
}) |
| 61 |
}) |
| 62 |
|
| 63 |
describe('+ ensureFileSync()', () => { |
| 64 |
describe('> when file exists', () => { |
| 65 |
it('should not do anything', () => { |
| 66 |
const file = path.join(TEST_DIR, 'file.txt') |
| 67 |
fs.writeFileSync(file, 'blah') |
| 68 |
|
| 69 |
assert(fs.existsSync(file)) |
| 70 |
fse.ensureFileSync(file) |
| 71 |
assert(fs.existsSync(file)) |
| 72 |
}) |
| 73 |
}) |
| 74 |
|
| 75 |
describe('> when file does not exist', () => { |
| 76 |
it('should create the file', () => { |
| 77 |
const file = path.join(TEST_DIR, 'dir/that/does/not/exist', 'file.txt') |
| 78 |
|
| 79 |
assert(!fs.existsSync(file)) |
| 80 |
fse.ensureFileSync(file) |
| 81 |
assert(fs.existsSync(file)) |
| 82 |
}) |
| 83 |
}) |
| 84 |
|
| 85 |
describe('> when there is a directory at that path', () => { |
| 86 |
it('should error', () => { |
| 87 |
const p = path.join(TEST_DIR, 'somedir2') |
| 88 |
fs.mkdirSync(p) |
| 89 |
|
| 90 |
assert.throws(() => { |
| 91 |
try { |
| 92 |
fse.ensureFileSync(p) |
| 93 |
} catch (e) { |
| 94 |
assert.strictEqual(e.code, 'EISDIR') |
| 95 |
throw e |
| 96 |
} |
| 97 |
}) |
| 98 |
}) |
| 99 |
}) |
| 100 |
}) |
| 101 |
|
| 102 |
describe('+ ensureDir()', () => { |
| 103 |
describe('> when dir exists', () => { |
| 104 |
it('should not do anything', done => { |
| 105 |
const dir = path.join(TEST_DIR, 'dir/does/not/exist') |
| 106 |
fse.mkdirpSync(dir) |
| 107 |
|
| 108 |
assert(fs.existsSync(dir)) |
| 109 |
fse.ensureDir(dir, err => { |
| 110 |
assert.ifError(err) |
| 111 |
assert(fs.existsSync(dir)) |
| 112 |
done() |
| 113 |
}) |
| 114 |
}) |
| 115 |
}) |
| 116 |
|
| 117 |
describe('> when dir does not exist', () => { |
| 118 |
it('should create the dir', done => { |
| 119 |
const dir = path.join(TEST_DIR, 'dir/that/does/not/exist') |
| 120 |
|
| 121 |
assert(!fs.existsSync(dir)) |
| 122 |
fse.ensureDir(dir, err => { |
| 123 |
assert.ifError(err) |
| 124 |
assert(fs.existsSync(dir)) |
| 125 |
done() |
| 126 |
}) |
| 127 |
}) |
| 128 |
}) |
| 129 |
}) |
| 130 |
|
| 131 |
describe('+ ensureDirSync()', () => { |
| 132 |
describe('> when dir exists', () => { |
| 133 |
it('should not do anything', () => { |
| 134 |
const dir = path.join(TEST_DIR, 'dir/does/not/exist') |
| 135 |
fse.mkdirpSync(dir) |
| 136 |
|
| 137 |
assert(fs.existsSync(dir)) |
| 138 |
fse.ensureDirSync(dir) |
| 139 |
assert(fs.existsSync(dir)) |
| 140 |
}) |
| 141 |
}) |
| 142 |
|
| 143 |
describe('> when dir does not exist', () => { |
| 144 |
it('should create the dir', () => { |
| 145 |
const dir = path.join(TEST_DIR, 'dir/that/does/not/exist') |
| 146 |
|
| 147 |
assert(!fs.existsSync(dir)) |
| 148 |
fse.ensureDirSync(dir) |
| 149 |
assert(fs.existsSync(dir)) |
| 150 |
}) |
| 151 |
}) |
| 152 |
}) |
| 153 |
}) |