UNPKG

3.49 kBJavaScriptView Raw
1var assert = require("assert");
2var file = require('../file-system');
3var fs = require('fs');
4var path = require('path');
5
6function getPath(filepath) {
7 return path.join(__dirname, filepath);
8}
9
10describe('copy', function() {
11 var allFiles = [
12 [
13 getPath('var/copy/simple/1/demo.html'),
14 getPath('var/copy/simple/1/demo.css'),
15 getPath('var/copy/simple/1/demo.js'),
16 getPath('var/copy/simple/1/2/demo.css'),
17 getPath('var/copy/simple/1/2/demo.html'),
18 getPath('var/copy/simple/file.js/demo.css'),
19 getPath('var/copy/simple/demo.js'),
20 getPath('var/copy/simple/demo.css')
21 ]
22 ];
23
24 before(function() {
25 allFiles.forEach(function(files) {
26 files.forEach(function(item) {
27 file.writeFileSync(item, 'a');
28 });
29 });
30 });
31
32 it('copySync files with filter', function() {
33 var dirpath = getPath('var/copy/simple');
34 var destpath = getPath('var/copy/simpledest');
35
36 file.copySync(dirpath, destpath, {
37 filter: [
38 '**/*.js',
39 '1/**/*.css',
40 '1/demo.html'
41 ]
42 });
43
44 var dirDest = [
45 getPath('var/copy/simpledest/1/demo.html'),
46 getPath('var/copy/simpledest/1/demo.css'),
47 getPath('var/copy/simpledest/1/2/demo.css'),
48 getPath('var/copy/simpledest/1/demo.js'),
49 getPath('var/copy/simpledest/demo.js')
50 ];
51 var result = [];
52
53 file.recurseSync(destpath, function(filepath, relative,filename) {
54 if (!filename) return;
55
56 result.push(filepath);
57 });
58
59 assert.equal(result.length, dirDest.length);
60 });
61
62 it('copySync replace filepath', function() {
63 var dirpath = getPath('var/copy/simple');
64 var destpath = getPath('var/copy/simple-replace');
65
66 file.copySync(dirpath, destpath, {
67 process: function(contents, filepath, relative) {
68 var basename = path.basename(filepath);
69 var newpath = path.join(destpath, relative);
70
71 // Validate relative
72 assert(path.relative(dirpath, filepath), relative);
73
74 // Replace html to txt
75 newpath = newpath.replace(
76 /\.html$/,
77 '.txt'
78 );
79
80 // Move all css to rootpath of destpath
81 if (/\.css$/.test(basename)) {
82 var prefix = path.basename(path.dirname(newpath));
83 newpath = path.join(destpath, prefix + '-' + basename);
84 }
85
86 return {
87 contents: contents,
88 filepath: newpath
89 };
90 }
91 });
92
93 assert.equal(true, file.existsSync(
94 path.join(destpath, '1/demo.txt')
95 ));
96 });
97
98 it('copySync with noProcess', function() {
99 var dirpath = getPath('var/copy/simple');
100 var destpath = getPath('var/copy/simple-noprocess');
101
102 file.copySync(dirpath, destpath, {
103 filter: [
104 '**/*demo.css',
105 '!**/1/demo.css'
106 ],
107 noProcess: 'demo.css',
108 process: function(contents, filepath) {
109 return 'b';
110 }
111 });
112
113 assert.equal(true, file.existsSync(
114 path.join(destpath, 'demo.css')
115 ));
116
117 assert.equal(false, file.existsSync(
118 path.join(destpath, '1/demo.css')
119 ));
120
121 assert.equal(true, file.existsSync(
122 path.join(destpath, '1/2/demo.css')
123 ));
124
125 assert.equal(true, file.existsSync(
126 path.join(destpath, 'file.js/demo.css')
127 ));
128
129 var content = file.readFileSync(
130 path.join(destpath, 'demo.css'),
131 { encoding: 'utf8' }
132 );
133
134 assert.equal('a', content);
135 });
136
137
138 after(function() {
139 file.rmdirSync(getPath('var/copy'));
140 });
141});
\No newline at end of file