/
copy-sync-dir.test.js
230 lines (173 loc) · 6.75 KB
/
copy-sync-dir.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
'use strict'
const fs = require('../../')
const os = require('os')
const path = require('path')
const assert = require('assert')
const crypto = require('crypto')
/* global beforeEach, afterEach, describe, it */
describe('+ copySync() / dir', () => {
const SIZE = 16 * 64 * 1024 + 7
let TEST_DIR
let src, dest
beforeEach(done => {
TEST_DIR = path.join(os.tmpdir(), 'fs-extra', 'copy-sync-dir')
src = path.join(TEST_DIR, 'src')
dest = path.join(TEST_DIR, 'dest')
fs.emptyDir(TEST_DIR, done)
})
afterEach(done => fs.remove(TEST_DIR, done))
describe('> when src is a directory', () => {
describe('> when dest exists and is a file', () => {
it('should throw error', () => {
const src = path.join(TEST_DIR, 'src')
const dest = path.join(TEST_DIR, 'file.txt')
fs.mkdirSync(src)
fs.ensureFileSync(dest)
try {
fs.copySync(src, dest)
} catch (err) {
assert.strictEqual(err.message, `Cannot overwrite non-directory '${dest}' with directory '${src}'.`)
}
})
})
it('should copy the directory synchronously', () => {
const FILES = 2
src = path.join(TEST_DIR, 'src')
dest = path.join(TEST_DIR, 'dest')
fs.mkdirSync(src)
for (let i = 0; i < FILES; ++i) {
fs.writeFileSync(path.join(src, i.toString()), crypto.randomBytes(SIZE))
}
const subdir = path.join(src, 'subdir')
fs.mkdirSync(subdir)
for (let i = 0; i < FILES; ++i) {
fs.writeFileSync(path.join(subdir, i.toString()), crypto.randomBytes(SIZE))
}
fs.copySync(src, dest)
assert(fs.existsSync(dest))
for (let i = 0; i < FILES; ++i) {
assert(fs.existsSync(path.join(dest, i.toString())))
}
const destSub = path.join(dest, 'subdir')
for (let j = 0; j < FILES; ++j) {
assert(fs.existsSync(path.join(destSub, j.toString())))
}
})
it('should preserve symbolic links', () => {
const srcTarget = path.join(TEST_DIR, 'destination')
fs.mkdirSync(src)
fs.mkdirSync(srcTarget)
fs.symlinkSync(srcTarget, path.join(src, 'symlink'), 'dir')
fs.copySync(src, dest)
const link = fs.readlinkSync(path.join(dest, 'symlink'))
assert.strictEqual(link, srcTarget)
})
describe('> when the destination dir does not exist', () => {
it('should create the destination directory and copy the file', () => {
const src = path.join(TEST_DIR, 'data/')
fs.mkdirSync(src)
const d1 = 'file1'
const d2 = 'file2'
fs.writeFileSync(path.join(src, 'f1.txt'), d1)
fs.writeFileSync(path.join(src, 'f2.txt'), d2)
const dest = path.join(TEST_DIR, 'this/path/does/not/exist/outputDir')
fs.copySync(src, dest)
const o1 = fs.readFileSync(path.join(dest, 'f1.txt'), 'utf8')
const o2 = fs.readFileSync(path.join(dest, 'f2.txt'), 'utf8')
assert.strictEqual(d1, o1)
assert.strictEqual(d2, o2)
})
})
})
describe('> when filter is used', () => {
it('should do nothing if filter fails', () => {
const srcDir = path.join(TEST_DIR, 'src')
const srcFile = path.join(srcDir, 'srcfile.css')
fs.outputFileSync(srcFile, 'src contents')
const destDir = path.join(TEST_DIR, 'dest')
const destFile = path.join(destDir, 'destfile.css')
const filter = s => path.extname(s) !== '.css' && !fs.statSync(s).isDirectory()
fs.copySync(srcFile, destFile, filter)
assert(!fs.existsSync(destDir))
})
it('should should apply filter recursively', () => {
const FILES = 2
// Don't match anything that ends with a digit higher than 0:
const filter = s => /(0|\D)$/i.test(s)
fs.mkdirSync(src)
for (let i = 0; i < FILES; ++i) {
fs.writeFileSync(path.join(src, i.toString()), crypto.randomBytes(SIZE))
}
const subdir = path.join(src, 'subdir')
fs.mkdirSync(subdir)
for (let i = 0; i < FILES; ++i) {
fs.writeFileSync(path.join(subdir, i.toString()), crypto.randomBytes(SIZE))
}
fs.copySync(src, dest, filter)
assert(fs.existsSync(dest))
assert(FILES > 1)
for (let i = 0; i < FILES; ++i) {
if (i === 0) {
assert(fs.existsSync(path.join(dest, i.toString())))
} else {
assert(!fs.existsSync(path.join(dest, i.toString())))
}
}
const destSub = path.join(dest, 'subdir')
for (let j = 0; j < FILES; ++j) {
if (j === 0) {
assert(fs.existsSync(path.join(destSub, j.toString())))
} else {
assert(!fs.existsSync(path.join(destSub, j.toString())))
}
}
})
it('should apply the filter to directory names', () => {
const IGNORE = 'ignore'
const filter = p => !~p.indexOf(IGNORE)
fs.mkdirSync(src)
const ignoreDir = path.join(src, IGNORE)
fs.mkdirSync(ignoreDir)
fs.writeFileSync(path.join(ignoreDir, 'file'), crypto.randomBytes(SIZE))
fs.copySync(src, dest, filter)
assert(!fs.existsSync(path.join(dest, IGNORE)), 'directory was not ignored')
assert(!fs.existsSync(path.join(dest, IGNORE, 'file')), 'file was not ignored')
})
it('should apply filter when it is applied only to dest', done => {
const timeCond = new Date().getTime()
const filter = (s, d) => fs.statSync(d).birthtime.getTime() < timeCond
const dest = path.join(TEST_DIR, 'dest')
setTimeout(() => {
fs.outputFileSync(path.join(src, 'somefile.html'), 'some data')
fs.mkdirSync(dest)
fs.copySync(src, dest, filter)
assert(!fs.existsSync(path.join(dest, 'somefile.html')))
done()
}, 1000)
})
it('should apply filter when it is applied to both src and dest', done => {
const timeCond = new Date().getTime()
const filter = (s, d) => s.split('.').pop() !== 'css' && fs.statSync(path.dirname(d)).birthtime.getTime() > timeCond
const dest = path.join(TEST_DIR, 'dest')
setTimeout(() => {
const srcFile1 = path.join(TEST_DIR, '1.html')
const srcFile2 = path.join(TEST_DIR, '2.css')
const srcFile3 = path.join(TEST_DIR, '3.jade')
fs.writeFileSync(srcFile1, '')
fs.writeFileSync(srcFile2, '')
fs.writeFileSync(srcFile3, '')
const destFile1 = path.join(dest, 'dest1.html')
const destFile2 = path.join(dest, 'dest2.css')
const destFile3 = path.join(dest, 'dest3.jade')
fs.mkdirSync(dest)
fs.copySync(srcFile1, destFile1, filter)
fs.copySync(srcFile2, destFile2, filter)
fs.copySync(srcFile3, destFile3, filter)
assert(fs.existsSync(destFile1))
assert(!fs.existsSync(destFile2))
assert(fs.existsSync(destFile3))
done()
}, 1000)
})
})
})