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