forked from jprichardson/node-fs-extra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
copy.test.js
194 lines (161 loc) · 6.24 KB
/
copy.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
'use strict'
const fs = require('fs')
const os = require('os')
const fse = require('../../')
const path = require('path')
const assert = require('assert')
const crypto = require('crypto')
/* global afterEach, beforeEach, describe, it */
const SIZE = 16 * 64 * 1024 + 7
let TEST_DIR = ''
describe('fs-extra', () => {
beforeEach(done => {
TEST_DIR = path.join(os.tmpdir(), 'fs-extra', 'copy')
fse.emptyDir(TEST_DIR, done)
})
afterEach(done => fse.remove(TEST_DIR, done))
describe('+ copy()', () => {
it('should return an error if src and dest are the same', done => {
const fileSrc = path.join(TEST_DIR, 'TEST_fs-extra_copy')
const fileDest = path.join(TEST_DIR, 'TEST_fs-extra_copy')
fse.copy(fileSrc, fileDest, err => {
assert.equal(err.message, 'Source and destination must not be the same.')
done()
})
})
describe('> when the source is a file', () => {
it('should copy the file asynchronously', done => {
const fileSrc = path.join(TEST_DIR, 'TEST_fs-extra_src')
const fileDest = path.join(TEST_DIR, 'TEST_fs-extra_copy')
fs.writeFileSync(fileSrc, crypto.randomBytes(SIZE))
const srcMd5 = crypto.createHash('md5').update(fs.readFileSync(fileSrc)).digest('hex')
let destMd5 = ''
fse.copy(fileSrc, fileDest, err => {
assert(!err)
destMd5 = crypto.createHash('md5').update(fs.readFileSync(fileDest)).digest('hex')
assert.strictEqual(srcMd5, destMd5)
done()
})
})
it('should return an error if the source file does not exist', done => {
const fileSrc = 'we-simply-assume-this-file-does-not-exist.bin'
const fileDest = path.join(TEST_DIR, 'TEST_fs-extra_copy')
fse.copy(fileSrc, fileDest, err => {
assert(err)
done()
})
})
it('should only copy files allowed by filter fn', done => {
const srcFile1 = path.join(TEST_DIR, '1.css')
fs.writeFileSync(srcFile1, '')
const destFile1 = path.join(TEST_DIR, 'dest1.css')
const filter = s => s.split('.').pop() !== 'css'
fse.copy(srcFile1, destFile1, filter, () => {
assert(!fs.existsSync(destFile1))
done()
})
})
it('accepts options object in place of filter', done => {
const srcFile1 = path.join(TEST_DIR, '1.jade')
fs.writeFileSync(srcFile1, '')
const destFile1 = path.join(TEST_DIR, 'dest1.jade')
const options = { filter: s => /.html$|.css$/i.test(s) }
fse.copy(srcFile1, destFile1, options, () => {
assert(!fs.existsSync(destFile1))
done()
})
})
it('should copy to a destination file with two \'$\' characters in name (eg: TEST_fs-extra_$$_copy)', done => {
const fileSrc = path.join(TEST_DIR, 'TEST_fs-extra_src')
const fileDest = path.join(TEST_DIR, 'TEST_fs-extra_$$_copy')
fs.writeFileSync(fileSrc, '')
fse.copy(fileSrc, fileDest, err => {
assert(!err)
fs.statSync(fileDest)
done()
})
})
describe('> when the destination dir does not exist', () => {
it('should create the destination directory and copy the file', done => {
const src = path.join(TEST_DIR, 'file.txt')
const dest = path.join(TEST_DIR, 'this/path/does/not/exist/copied.txt')
const data = 'did it copy?\n'
fs.writeFileSync(src, data, 'utf8')
fse.copy(src, dest, err => {
const data2 = fs.readFileSync(dest, 'utf8')
assert.strictEqual(data, data2)
done(err)
})
})
})
})
describe('> when the source is a directory', () => {
describe('> when the source directory does not exist', () => {
it('should return an error', done => {
const ts = path.join(TEST_DIR, 'this_dir_does_not_exist')
const td = path.join(TEST_DIR, 'this_dir_really_does_not_matter')
fse.copy(ts, td, err => {
assert(err)
done()
})
})
})
it('should copy the directory asynchronously', done => {
const FILES = 2
const src = path.join(TEST_DIR, 'src')
const dest = path.join(TEST_DIR, 'dest')
fse.mkdirs(src, err => {
assert(!err)
for (let i = 0; i < FILES; ++i) {
fs.writeFileSync(path.join(src, i.toString()), crypto.randomBytes(SIZE))
}
const subdir = path.join(src, 'subdir')
fse.mkdirs(subdir, err => {
assert(!err)
for (let i = 0; i < FILES; ++i) {
fs.writeFileSync(path.join(subdir, i.toString()), crypto.randomBytes(SIZE))
}
fse.copy(src, dest, err => {
assert.ifError(err)
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())))
}
done()
})
})
})
})
describe('> when the destination dir does not exist', () => {
it('should create the destination directory and copy the file', done => {
const src = path.join(TEST_DIR, 'data/')
fse.mkdirsSync(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')
fse.copy(src, dest, err => {
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)
done(err)
})
})
})
describe('> when src dir does not exist', () => {
it('should return an error', done => {
fse.copy('/does/not/exist', '/something/else', err => {
assert(err instanceof Error)
done()
})
})
})
})
})
})