/
test.js
357 lines (283 loc) · 11 KB
/
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
import path from 'path';
import fs from 'fs';
import crypto from 'crypto';
import rimraf from 'rimraf';
import test from 'ava';
import tempfile from 'tempfile';
import CpyError from './cpy-error';
import cpy from '.';
const read = (...args) => fs.readFileSync(path.join(...args), 'utf8');
test.beforeEach(t => {
t.context.tmp = tempfile();
t.context.EPERM = tempfile('EPERM');
fs.mkdirSync(t.context.EPERM, 0);
});
test.afterEach(t => {
rimraf.sync(t.context.tmp);
rimraf.sync(t.context.EPERM);
});
test('reject Errors on missing `source`', async t => {
const error1 = await t.throwsAsync(cpy, /`source`/);
t.true(error1 instanceof CpyError);
const error2 = await t.throwsAsync(cpy(null, 'destination'), /`source`/);
t.true(error2 instanceof CpyError);
const error3 = await t.throwsAsync(cpy([], 'destination'), /`source`/);
t.true(error3 instanceof CpyError);
});
test('reject Errors on missing `destination`', async t => {
const error = await t.throwsAsync(cpy('TARGET'), /`destination`/);
t.true(error instanceof CpyError);
});
test('copy single file', async t => {
await cpy('license', t.context.tmp);
t.is(read('license'), read(t.context.tmp, 'license'));
});
test('copy array of files', async t => {
await cpy(['license', 'package.json'], t.context.tmp);
t.is(read('license'), read(t.context.tmp, 'license'));
t.is(read('package.json'), read(t.context.tmp, 'package.json'));
});
test('throws on invalid concurrency value', async t => {
await t.throwsAsync(cpy(['license', 'package.json'], t.context.tmp, {concurrency: -2}));
await t.throwsAsync(cpy(['license', 'package.json'], t.context.tmp, {concurrency: 'foo'}));
});
test('copy array of files with filter', async t => {
await cpy(['license', 'package.json'], t.context.tmp, {
filter: file => {
if (file.path.endsWith('/license')) {
t.is(file.path, path.join(process.cwd(), 'license'));
t.is(file.relativePath, 'license');
t.is(file.name, 'license');
t.is(file.nameWithoutExtension, 'license');
t.is(file.extension, '');
} else if (file.path.endsWith('/package.json')) {
t.is(file.path, path.join(process.cwd(), 'package.json'));
t.is(file.relativePath, 'package.json');
t.is(file.name, 'package.json');
t.is(file.nameWithoutExtension, 'package');
t.is(file.extension, 'json');
}
return !file.path.endsWith('/license');
}
});
t.false(fs.existsSync(path.join(t.context.tmp, 'license')));
t.is(read('package.json'), read(t.context.tmp, 'package.json'));
});
test('copy array of files with async filter', async t => {
await cpy(['license', 'package.json'], t.context.tmp, {
filter: async file => {
if (file.path.endsWith('/license')) {
t.is(file.path, path.join(process.cwd(), 'license'));
t.is(file.relativePath, 'license');
t.is(file.name, 'license');
t.is(file.nameWithoutExtension, 'license');
t.is(file.extension, '');
} else if (file.path.endsWith('/package.json')) {
t.is(file.path, path.join(process.cwd(), 'package.json'));
t.is(file.relativePath, 'package.json');
t.is(file.name, 'package.json');
t.is(file.nameWithoutExtension, 'package');
t.is(file.extension, 'json');
}
return !file.path.endsWith('/license');
}
});
t.false(fs.existsSync(path.join(t.context.tmp, 'license')));
t.is(read('package.json'), read(t.context.tmp, 'package.json'));
});
test('cwd', async t => {
fs.mkdirSync(t.context.tmp);
fs.mkdirSync(path.join(t.context.tmp, 'cwd'));
fs.writeFileSync(path.join(t.context.tmp, 'cwd/hello.js'), 'console.log("hello");');
await cpy(['hello.js'], 'destination', {cwd: path.join(t.context.tmp, 'cwd')});
t.is(read(t.context.tmp, 'cwd/hello.js'), read(t.context.tmp, 'cwd/destination/hello.js'));
});
test('do not overwrite', async t => {
fs.mkdirSync(t.context.tmp);
fs.writeFileSync(path.join(t.context.tmp, 'license'), '');
await cpy(['license'], t.context.tmp, {overwrite: false});
t.is(read(t.context.tmp, 'license'), '');
});
test('do not keep path structure', async t => {
fs.mkdirSync(t.context.tmp);
fs.mkdirSync(path.join(t.context.tmp, 'cwd'));
fs.writeFileSync(path.join(t.context.tmp, 'cwd/hello.js'), 'console.log("hello");');
await cpy([path.join(t.context.tmp, 'cwd/hello.js')], t.context.tmp);
t.is(read(t.context.tmp, 'cwd/hello.js'), read(t.context.tmp, 'hello.js'));
});
test('path structure', async t => {
fs.mkdirSync(t.context.tmp);
fs.mkdirSync(path.join(t.context.tmp, 'cwd'));
fs.writeFileSync(path.join(t.context.tmp, 'cwd/hello.js'), 'console.log("hello");');
await cpy([path.join(t.context.tmp, 'cwd/hello.js')], t.context.tmp, {parents: true});
t.is(read(t.context.tmp, 'cwd/hello.js'), read(t.context.tmp, t.context.tmp, 'cwd/hello.js'));
});
test('rename filenames but not filepaths', async t => {
fs.mkdirSync(t.context.tmp);
fs.mkdirSync(path.join(t.context.tmp, 'source'));
fs.writeFileSync(path.join(t.context.tmp, 'hello.js'), 'console.log("hello");');
fs.writeFileSync(path.join(t.context.tmp, 'source/hello.js'), 'console.log("hello");');
await cpy(['hello.js', 'source/hello.js'], 'destination/subdir', {
cwd: t.context.tmp,
parents: true,
rename: 'hi.js'
});
t.is(read(t.context.tmp, 'hello.js'), read(t.context.tmp, 'destination/subdir/hi.js'));
t.is(read(t.context.tmp, 'source/hello.js'), read(t.context.tmp, 'destination/subdir/source/hi.js'));
});
test('rename filenames using a function', async t => {
fs.mkdirSync(t.context.tmp);
fs.mkdirSync(path.join(t.context.tmp, 'source'));
fs.writeFileSync(path.join(t.context.tmp, 'foo.js'), 'console.log("foo");');
fs.writeFileSync(path.join(t.context.tmp, 'source/bar.js'), 'console.log("bar");');
await cpy(['foo.js', 'source/bar.js'], 'destination/subdir', {
cwd: t.context.tmp,
parents: true,
rename: basename => `prefix-${basename}`
});
t.is(read(t.context.tmp, 'foo.js'), read(t.context.tmp, 'destination/subdir/prefix-foo.js'));
t.is(read(t.context.tmp, 'source/bar.js'), read(t.context.tmp, 'destination/subdir/source/prefix-bar.js'));
});
test('cp-file errors are not glob errors', async t => {
const error = await t.throwsAsync(cpy('license', t.context.EPERM), /EPERM/);
t.notRegex(error.message, /glob/);
});
test('cp-file errors are CpyErrors', async t => {
const error = await t.throwsAsync(cpy('license', t.context.EPERM), /EPERM/);
t.true(error instanceof CpyError);
});
test('glob errors are CpyErrors', async t => {
const error = await t.throwsAsync(cpy(t.context.EPERM + '/**', t.context.tmp), /EPERM/);
t.true(error instanceof CpyError);
});
test('throws on non-existing file', async t => {
fs.mkdirSync(t.context.tmp);
await t.throwsAsync(cpy(['no-file'], t.context.tmp), {
instanceOf: CpyError
});
});
test('throws on multiple non-existing files', async t => {
fs.mkdirSync(t.context.tmp);
await t.throwsAsync(cpy(['no-file1', 'no-file2'], t.context.tmp), {
instanceOf: CpyError
});
});
test('does not throw when not matching any file on glob pattern', async t => {
fs.mkdirSync(t.context.tmp);
await t.notThrowsAsync(cpy(['*.nonexistent'], t.context.tmp));
});
test('junk files are ignored', async t => {
fs.mkdirSync(t.context.tmp);
fs.mkdirSync(path.join(t.context.tmp, 'cwd'));
fs.writeFileSync(path.join(t.context.tmp, 'cwd/Thumbs.db'), 'lorem ipsum');
fs.writeFileSync(path.join(t.context.tmp, 'cwd/foo'), 'lorem ipsum');
let report;
await cpy('*', t.context.tmp, {cwd: path.join(t.context.tmp, 'cwd'), ignoreJunk: true})
.on('progress', event => {
report = event;
});
t.not(report, undefined);
t.is(report.totalFiles, 1);
t.is(report.completedFiles, 1);
t.is(report.completedSize, 11);
t.is(report.percent, 1);
});
test('junk files are copied', async t => {
fs.mkdirSync(t.context.tmp);
fs.mkdirSync(path.join(t.context.tmp, 'cwd'));
fs.writeFileSync(path.join(t.context.tmp, 'cwd/Thumbs.db'), 'lorem ipsum');
fs.writeFileSync(path.join(t.context.tmp, 'cwd/foo'), 'lorem ipsum');
let report;
await cpy('*', t.context.tmp, {cwd: path.join(t.context.tmp, 'cwd'), ignoreJunk: false})
.on('progress', event => {
report = event;
});
t.not(report, undefined);
t.is(report.totalFiles, 2);
t.is(report.completedFiles, 2);
t.is(report.completedSize, 22);
t.is(report.percent, 1);
});
test('nested junk files are ignored', async t => {
fs.mkdirSync(t.context.tmp);
fs.mkdirSync(path.join(t.context.tmp, 'cwd'));
fs.writeFileSync(path.join(t.context.tmp, 'cwd/Thumbs.db'), 'lorem ispum');
fs.writeFileSync(path.join(t.context.tmp, 'cwd/test'), 'lorem ispum');
let report;
await cpy(['cwd/*'], t.context.tmp, {cwd: t.context.tmp, ignoreJunk: true})
.on('progress', event => {
report = event;
});
t.not(report, undefined);
t.is(report.totalFiles, 1);
t.is(report.completedFiles, 1);
t.is(report.completedSize, 11);
t.is(report.percent, 1);
});
test('reports copy progress of single file', async t => {
fs.mkdirSync(t.context.tmp);
fs.mkdirSync(path.join(t.context.tmp, 'cwd'));
fs.writeFileSync(path.join(t.context.tmp, 'cwd/foo'), 'lorem ipsum');
let report;
await cpy(['foo'], t.context.tmp, {cwd: path.join(t.context.tmp, 'cwd')})
.on('progress', event => {
report = event;
});
t.not(report, undefined);
t.is(report.totalFiles, 1);
t.is(report.completedFiles, 1);
t.is(report.completedSize, 11);
t.is(report.percent, 1);
});
test('reports copy progress of multiple files', async t => {
fs.mkdirSync(t.context.tmp);
fs.mkdirSync(path.join(t.context.tmp, 'cwd'));
fs.writeFileSync(path.join(t.context.tmp, 'cwd/foo'), 'lorem ipsum');
fs.writeFileSync(path.join(t.context.tmp, 'cwd/bar'), 'dolor sit amet');
let report;
await cpy(['foo', 'bar'], t.context.tmp, {cwd: path.join(t.context.tmp, 'cwd')})
.on('progress', event => {
report = event;
});
t.not(report, undefined);
t.is(report.totalFiles, 2);
t.is(report.completedFiles, 2);
t.is(report.completedSize, 25);
t.is(report.percent, 1);
});
test('reports correct completedSize', async t => {
const ONE_MEGABYTE = (1 * 1024 * 1024) + 1;
const buf = crypto.randomBytes(ONE_MEGABYTE);
fs.mkdirSync(t.context.tmp);
fs.mkdirSync(path.join(t.context.tmp, 'cwd'));
fs.writeFileSync(path.join(t.context.tmp, 'cwd/fatfile'), buf);
let report;
let chunkCount = 0;
await cpy(['fatfile'], t.context.tmp, {cwd: path.join(t.context.tmp, 'cwd')})
.on('progress', event => {
chunkCount++;
report = event;
});
t.not(report, undefined);
t.is(report.totalFiles, 1);
t.is(report.completedFiles, 1);
t.is(report.completedSize, ONE_MEGABYTE);
t.true(chunkCount > 1);
t.is(report.percent, 1);
});
test('returns the event emitter on early rejection', t => {
const rejectedPromise = cpy(null, null);
t.is(typeof rejectedPromise.on, 'function');
rejectedPromise.catch(() => {});
});
test('returns destination path', async t => {
fs.mkdirSync(t.context.tmp);
fs.mkdirSync(path.join(t.context.tmp, 'cwd'));
fs.writeFileSync(path.join(t.context.tmp, 'cwd/foo'), 'lorem ipsum');
fs.writeFileSync(path.join(t.context.tmp, 'cwd/bar'), 'dolor sit amet');
const to = await cpy(['foo', 'bar'], t.context.tmp, {cwd: path.join(t.context.tmp, 'cwd')});
t.deepEqual(to, [
path.join(t.context.tmp, 'foo'),
path.join(t.context.tmp, 'bar')
]);
});