UNPKG

8.6 kBJavaScriptView Raw
1var fu = require('../lib/fileUtil');
2
3describe('file-util', function() {
4 afterEach(function() {
5 fu.lookupPath('');
6 });
7
8 describe('#resolvePath', function() {
9 it('should resolve paths', function() {
10 fu.lookupPath('foo/bar');
11
12 fu.resolvePath('foo').should.equal('foo/bar/foo');
13 fu.resolvePath('foo/bar/foo').should.equal('foo/bar/foo');
14 fu.resolvePath('/foo').should.equal('/foo');
15 fu.resolvePath('c:\\foo').should.equal('c:\\foo');
16 fu.resolvePath('c:/foo').should.equal('c:/foo');
17 });
18 });
19
20 describe('#makeRelative', function() {
21 it('should make paths relative', function() {
22 fu.lookupPath('foo/bar');
23
24 fu.makeRelative('foo').should.equal('foo');
25 fu.makeRelative('foo/bar/foo').should.equal('foo');
26 fu.makeRelative('foo/baro/foo').should.equal('foo/baro/foo');
27 fu.makeRelative('/foo').should.equal('/foo');
28 fu.makeRelative('c:\\foo').should.equal('c:\\foo');
29 fu.makeRelative('c:/foo').should.equal('c:/foo');
30 });
31 });
32
33 describe('#fileList', function() {
34 it('should resolve single files', function(done) {
35 fu.fileList('test/file-util.js', function(err, files) {
36 if (err) {
37 throw err;
38 }
39
40 files.should.eql(['test/file-util.js']);
41 done();
42 });
43 });
44
45 it('should resolve immediate files', function(done) {
46 fu.lookupPath('test');
47 fu.fileList('file-util.js', function(err, files) {
48 if (err) {
49 throw err;
50 }
51
52 files.should.eql(['file-util.js']);
53 done();
54 });
55 });
56
57 it('should not filter explicit file entrys', function(done) {
58 fu.fileList('test/file-util.js', /notjason/, function(err, files) {
59 if (err) {
60 throw err;
61 }
62
63 files.should.eql(['test/file-util.js']);
64 done();
65 });
66 });
67
68 it('should mark missing files as enoent', function(done) {
69 fu.fileList('foo-bar-baz', function(err, files) {
70 files.should.eql([{src: 'foo-bar-baz', enoent: true}]);
71
72 done();
73 });
74 });
75
76 it('should return duplicates only once', function(done) {
77 fu.lookupPath('test');
78 fu.fileList(['file-util.js', 'file-util.js', 'example/lumbar.json', 'file-util.js'], function(err, files) {
79 if (err) {
80 throw err;
81 }
82
83 files.should.eql(['file-util.js', 'example/lumbar.json']);
84 done();
85 });
86 });
87
88 it('should resolve the files in a directoy', function(done) {
89 fu.lookupPath('test');
90 fu.fileList(['file-util.js', 'artifacts/js'], function(err, files) {
91 if (err) {
92 throw err;
93 }
94
95 files.should.eql([
96 'file-util.js',
97 {dir: 'artifacts/js'},
98 {src: 'artifacts/js/base.js', srcDir: 'artifacts/js'},
99 {dir: 'artifacts/js/home', srcDir: 'artifacts/js'},
100 {src: 'artifacts/js/home/home.js', srcDir: 'artifacts/js'},
101 {src: 'artifacts/js/iphone.js', srcDir: 'artifacts/js'},
102 {src: 'artifacts/js/web.js', srcDir: 'artifacts/js'}
103 ]);
104 done();
105 });
106 });
107
108 it('should filter directories by extension', function(done) {
109 fu.lookupPath('test');
110 fu.fileList(['file-util.js', 'artifacts'], /js\/.*\.js$/, function(err, files) {
111 if (err) {
112 throw err;
113 }
114
115 files.should.eql([
116 'file-util.js',
117 {dir: 'artifacts'},
118 {dir: 'artifacts/config', srcDir: 'artifacts'},
119 {dir: 'artifacts/images', srcDir: 'artifacts'},
120 {dir: 'artifacts/js', srcDir: 'artifacts'},
121 {src: 'artifacts/js/base.js', srcDir: 'artifacts'},
122 {dir: 'artifacts/js/home', srcDir: 'artifacts'},
123 {src: 'artifacts/js/home/home.js', srcDir: 'artifacts'},
124 {src: 'artifacts/js/iphone.js', srcDir: 'artifacts'},
125 {src: 'artifacts/js/web.js', srcDir: 'artifacts'},
126 {dir: 'artifacts/library', srcDir: 'artifacts'},
127 {dir: 'artifacts/node_modules', srcDir: 'artifacts'},
128 {dir: 'artifacts/node_modules/json-plugins-test-plugin', srcDir: 'artifacts'},
129 {dir: 'artifacts/styles', srcDir: 'artifacts'},
130 {dir: 'artifacts/templates', srcDir: 'artifacts'}
131 ]);
132 done();
133 });
134 });
135
136 it('should include resource flags on lookup', function(done) {
137 fu.lookupPath('test');
138 fu.fileList(['file-util.js', {src: 'artifacts', global: true}, {router: true}], /js\/.*\.js$/, function(err, files) {
139 if (err) {
140 throw err;
141 }
142
143 files.should.eql([
144 'file-util.js',
145 { dir: 'artifacts', global: true},
146 { dir: 'artifacts/config', global: true, srcDir: 'artifacts'},
147 { dir: 'artifacts/images', global: true, srcDir: 'artifacts'},
148 { dir: 'artifacts/js', global: true, srcDir: 'artifacts'},
149 { src: 'artifacts/js/base.js', global: true, srcDir: 'artifacts'},
150 { dir: 'artifacts/js/home', global: true, srcDir: 'artifacts'},
151 { src: 'artifacts/js/home/home.js', global: true, srcDir: 'artifacts'},
152 { src: 'artifacts/js/iphone.js', global: true, srcDir: 'artifacts'},
153 { src: 'artifacts/js/web.js', global: true, srcDir: 'artifacts'},
154 { dir: 'artifacts/library', global: true, srcDir: 'artifacts'},
155 { dir: 'artifacts/node_modules', global: true, srcDir: 'artifacts'},
156 { dir: 'artifacts/node_modules/json-plugins-test-plugin', global: true, srcDir: 'artifacts'},
157 { dir: 'artifacts/styles', global: true, srcDir: 'artifacts'},
158 { dir: 'artifacts/templates', global: true, srcDir: 'artifacts'},
159 {router: true}
160 ]);
161
162 done();
163 });
164 });
165
166 it('should support resource only lists', function(done) {
167 fu.lookupPath('test');
168 fu.fileList([{router: true}], /js\/.*\.js$/, function(err, files) {
169 if (err) {
170 throw err;
171 }
172
173 files.should.eql([{router: true}]);
174
175 done();
176 });
177 });
178
179 it('should support empty results', function(done) {
180 fu.lookupPath('test');
181 fu.fileList([], /js\/.*\.js$/, function(err, files) {
182 if (err) {
183 throw err;
184 }
185
186 files.should.eql([]);
187
188 done();
189 });
190 });
191 });
192
193 describe('file caches', function() {
194 var fs = require('fs'),
195 count = 0;
196 beforeEach(function() {
197 count = 0;
198 this.stub(fs, 'readFile', function(path, callback) {
199 count++;
200 callback(undefined, 'data');
201 });
202 this.stub(fs, 'readdir', function(path, callback) {
203 count++;
204 callback(undefined, 'data');
205 });
206 });
207
208 describe('#readdir', function() {
209 it('should read using cache', function() {
210 fu.resetCache();
211 fu.readFile('foo', function(err, data) {
212 data.should.equal('data');
213 });
214 count.should.equal(1);
215 fu.readFile('foo', function(err, data) {
216 data.should.equal('data');
217 });
218 count.should.equal(1);
219
220 fu.resetCache();
221 fu.readFile('foo', function(err, data) {
222 data.should.equal('data');
223 });
224 count.should.equal(2);
225 });
226 });
227
228 describe('#readdir', function() {
229 it('should read from dir cache', function() {
230 fu.resetCache();
231 fu.readdir('foo', function(err, data) {
232 data.should.equal('data');
233 });
234 count.should.equal(1);
235 fu.readdir('foo', function(err, data) {
236 data.should.equal('data');
237 });
238 count.should.equal(1);
239
240 fu.resetCache();
241 fu.readdir('foo', function(err, data) {
242 data.should.equal('data');
243 });
244 count.should.equal(2);
245 });
246 });
247
248 describe('#readFileArtifact', function() {
249 it('should maintain cached artifact', function() {
250 fu.resetCache();
251 fu.readFileArtifact('foo', 'bar', function(err, data) {
252 data.data.should.equal('data');
253 should.not.exist(data.artifact);
254 });
255 count.should.equal(1);
256 fu.setFileArtifact('foo', 'bar', 'baz');
257
258
259 fu.readFileArtifact('foo', 'bar', function(err, data) {
260 data.data.should.equal('data');
261 data.artifact.should.equal('baz');
262 });
263 count.should.equal(1);
264
265 fu.resetCache();
266 fu.readFileArtifact('foo', 'bar', function(err, data) {
267 data.data.should.equal('data');
268 should.not.exist(data.artifact);
269 });
270 count.should.equal(2);
271 });
272 });
273 });
274});