ncp-test.js
2.55 KB
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
var assert = require('assert'),
path = require('path'),
rimraf = require('rimraf'),
vows = require('vows'),
readDirFiles = require('read-dir-files'),
ncp = require('../').ncp;
var fixtures = path.join(__dirname, 'fixtures'),
src = path.join(fixtures, 'src'),
out = path.join(fixtures, 'out');
vows.describe('ncp').addBatch({
'When copying a directory of files': {
topic: function () {
var cb = this.callback;
rimraf(out, function () {
ncp(src, out, cb);
});
},
'files should be copied': {
topic: function () {
var cb = this.callback;
readDirFiles(src, 'utf8', function (srcErr, srcFiles) {
readDirFiles(out, 'utf8', function (outErr, outFiles) {
cb(outErr, srcFiles, outFiles);
});
});
},
'and the destination should match the source': function (err, srcFiles, outFiles) {
assert.isNull(err);
assert.deepEqual(srcFiles, outFiles);
}
}
}
}).addBatch({
'When copying files using filter': {
topic: function() {
var cb = this.callback;
var filter = function(name) {
return name.substr(name.length - 1) != 'a'
}
rimraf(out, function () {
ncp(src, out, {filter: filter}, cb);
});
},
'it should copy files': {
topic: function () {
var cb = this.callback;
readDirFiles(src, 'utf8', function (srcErr, srcFiles) {
function filter(files) {
for (var fileName in files) {
var curFile = files[fileName];
if (curFile instanceof Object)
return filter(curFile);
if (fileName.substr(fileName.length - 1) == 'a')
delete files[fileName];
}
}
filter(srcFiles);
readDirFiles(out, 'utf8', function (outErr, outFiles) {
cb(outErr, srcFiles, outFiles);
});
});
},
'and destination files should match source files that pass filter': function (err, srcFiles, outFiles) {
assert.isNull(err);
assert.deepEqual(srcFiles, outFiles);
}
}
}
}).addBatch({
'When copying files using transform': {
'it should pass file descriptors along to transform functions': function() {
ncp(src, out, {
transform: function(read,write,file) {
assert.notEqual(file.name, undefined);
assert.strictEqual(typeof file.mode,'number');
read.pipe(write);
}
}, function(){});
}
}
}).export(module);