-
Notifications
You must be signed in to change notification settings - Fork 775
/
ncp.test.js
191 lines (165 loc) · 5.12 KB
/
ncp.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
'use strict'
const fs = require('fs')
const { copy: ncp } = require('../../')
const path = require('path')
const rimraf = require('rimraf')
const assert = require('assert')
const readDirFiles = require('read-dir-files').read // temporary, will remove
/* eslint-env mocha */
const fixturesDir = path.join(__dirname, 'fixtures')
describe('ncp', () => {
describe('regular files and directories', () => {
const fixtures = path.join(fixturesDir, 'regular-fixtures')
const src = path.join(fixtures, 'src')
const out = path.join(fixtures, 'out')
before(cb => rimraf(out, () => ncp(src, out, cb)))
describe('when copying a directory of files', () => {
it('files are copied correctly', cb => {
readDirFiles(src, 'utf8', (srcErr, srcFiles) => {
readDirFiles(out, 'utf8', (outErr, outFiles) => {
assert.ifError(srcErr)
assert.deepStrictEqual(srcFiles, outFiles)
cb()
})
})
})
})
describe('when copying files using filter', () => {
before(cb => {
const filter = name => name.slice(-1) !== 'a'
rimraf(out, () => ncp(src, out, { filter }, cb))
})
it('files are copied correctly', cb => {
readDirFiles(src, 'utf8', (srcErr, srcFiles) => {
function filter (files) {
for (const fileName in files) {
const curFile = files[fileName]
if (curFile instanceof Object) {
filter(curFile)
} else if (fileName.slice(-1) === 'a') {
delete files[fileName]
}
}
}
filter(srcFiles)
readDirFiles(out, 'utf8', (outErr, outFiles) => {
assert.ifError(outErr)
assert.deepStrictEqual(srcFiles, outFiles)
cb()
})
})
})
})
describe('when using overwrite=true', () => {
before(function () {
this.originalCreateReadStream = fs.createReadStream
})
after(function () {
fs.createReadStream = this.originalCreateReadStream
})
it('the copy is complete after callback', done => {
ncp(src, out, { overwrite: true }, err => {
fs.createReadStream = () => done(new Error('createReadStream after callback'))
assert.ifError(err)
process.nextTick(done)
})
})
})
describe('when using overwrite=false', () => {
beforeEach(done => rimraf(out, done))
it('works', cb => {
ncp(src, out, { overwrite: false }, err => {
assert.ifError(err)
cb()
})
})
it('should not error if files exist', cb => {
ncp(src, out, () => {
ncp(src, out, { overwrite: false }, err => {
assert.ifError(err)
cb()
})
})
})
it('should error if errorOnExist and file exists', cb => {
ncp(src, out, () => {
ncp(src, out, {
overwrite: false,
errorOnExist: true
}, err => {
assert(err)
cb()
})
})
})
})
describe('clobber', () => {
beforeEach(done => rimraf(out, done))
it('is an alias for overwrite', cb => {
ncp(src, out, () => {
ncp(src, out, {
clobber: false,
errorOnExist: true
}, err => {
assert(err)
cb()
})
})
})
})
describe('when using transform', () => {
it('file descriptors are passed correctly', cb => {
ncp(src, out, {
transform: (read, write, file) => {
assert.notStrictEqual(file.name, undefined)
assert.strictEqual(typeof file.mode, 'number')
read.pipe(write)
}
}, cb)
})
})
})
// see https://github.com/AvianFlu/ncp/issues/71
describe('Issue 71: Odd Async Behaviors', () => {
const fixtures = path.join(__dirname, 'fixtures', 'regular-fixtures')
const src = path.join(fixtures, 'src')
const out = path.join(fixtures, 'out')
let totalCallbacks = 0
function copyAssertAndCount (callback) {
// rimraf(out, function() {
ncp(src, out, err => {
assert(!err)
totalCallbacks += 1
readDirFiles(src, 'utf8', (srcErr, srcFiles) => {
readDirFiles(out, 'utf8', (outErr, outFiles) => {
assert.ifError(srcErr)
assert.deepStrictEqual(srcFiles, outFiles)
callback()
})
})
})
// })
}
describe('when copying a directory of files without cleaning the destination', () => {
it('callback fires once per run and directories are equal', done => {
const expected = 10
let count = 10
function next () {
if (count > 0) {
setTimeout(() => {
copyAssertAndCount(() => {
count -= 1
next()
})
}, 100)
} else {
// console.log('Total callback count is', totalCallbacks)
assert.strictEqual(totalCallbacks, expected)
done()
}
}
next()
})
})
})
})