UNPKG

20.3 kBJavaScriptView Raw
1'use strict';
2
3var Path = require('path');
4var expect = require('unexpected').clone();
5var baconize = require('../');
6var rimraf = require('rimraf');
7var when = require('when');
8var nodefn = require('when/node');
9var fs = nodefn.liftAll(require('fs'));
10
11function getPathIn(path) {
12 return Path.join(process.cwd(), 'examples/site', path || '');
13}
14
15function getPathOut(path) {
16 return Path.join(process.cwd(), 'examples/output', path || '');
17}
18
19describe('baconize', function() {
20
21 var clearDir = function(cb) {
22 rimraf(getPathOut(), cb);
23 };
24
25 describe('compile', function() {
26
27 before(clearDir);
28 after(clearDir);
29
30 it('should compile compilable files and copy all others', function () {
31 var options = {
32 exclusions: [{
33 path: 'dont-copy',
34 action: 'exclude',
35 type: 'dir',
36 },{
37 path: 'dont-compile',
38 action: 'dontCompile',
39 type: 'dir',
40 }
41 ],
42 compile: true,
43 sourcemaps: false
44 };
45 var bacon = baconize(getPathIn(), getPathOut(), options);
46
47 var dirs = [];
48 bacon.events.on('chdir', function(dir) { dirs.push(dir); });
49
50 var compiledFiles = [];
51 var lastStartedFile;
52 bacon.events.on('compile-start', function(file) {
53 lastStartedFile = file.path;
54 });
55 bacon.events.on('compile-done', function(file) {
56 if (lastStartedFile === file.path) {
57 compiledFiles.push(file.path);
58 } else {
59 expect.fail('Unexpected compile-done event');
60 }
61 });
62
63 return bacon.then(function(num) {
64 return expect.promise.all([
65 expect(num, 'to be', 9),
66 expect(dirs, 'to contain', '', 'dont-compile', 'scripts', 'styles').and('to have length', 4),
67 expect(compiledFiles, 'to contain',
68 'index.jade', 'scripts/main.coffee', 'styles/main.styl', 'scripts/log.babel.js'
69 )
70 .and('not to contain', 'about.html', 'styles/typography.css', 'styles/typography.css')
71 .and('to have length', 4)
72 ]);
73 });
74 });
75
76 describe('after compilation', function() {
77
78 it('should have all compiled files', function() {
79 return when.all([
80 fs.readFile(getPathOut('index.html')),
81 fs.readFile(getPathOut('styles/main.css')),
82 fs.readFile(getPathOut('scripts/main.js')),
83 fs.readFile(getPathOut('scripts/iterate.js')),
84 fs.readFile(getPathOut('about.html')),
85 fs.readFile(getPathOut('dont-compile/foo.coffee')),
86 fs.readFile(getPathOut('styles/typography.css'))
87 ]).then(function(results) {
88 return expect.promise.all([
89 expect(results[0].toString(), 'to contain', 'saying \'hello\'.\n'),
90 expect(results[1].toString(), 'to contain', 'body {'),
91 expect(results[2].toString(), 'to contain', 'console.log("H'),
92 expect(results[3].toString(), 'to contain', 'for (var i = 0; i < stuff.length; i++) {'),
93 expect(results[4].toString(), 'to contain', ' <head>'),
94 expect(results[5].toString(), 'to contain', 'console.log "T'),
95 expect(results[6].toString(), 'to contain', ' font-family: arial;')
96 ]);
97 });
98 });
99
100 it('should not copy pre-compiled files', function() {
101 return fs.readFile(getPathOut('index.jade'))
102 .then(function() {
103 return expect.fail('`index.jade` should not be copied');
104 }, function(err) {
105 return expect(err.code, 'to be', 'ENOENT');
106 });
107 });
108
109 it('should not compile blacklist matches', function() {
110 return fs.readFile(getPathOut('dont-compile/foo.js'))
111 .then(function() {
112 return expect.fail('`dont-compile/foo.js` should not be copied');
113 }, function(err) {
114 return expect(err.code, 'to be', 'ENOENT');
115 });
116 });
117
118 it('should not copy ignore paths', function() {
119 return fs.stat(getPathOut('dont-copy'))
120 .then(function() {
121 return expect.fail('`dont-copy` directory should not be copied');
122 }, function(err) {
123 return expect(err.code, 'to be', 'ENOENT');
124 });
125 });
126
127 });
128 });
129
130 describe('compile with sourcemaps', function() {
131
132 before(clearDir);
133 after(clearDir);
134
135 it('should compile compilable files and copy all others', function () {
136 var options = {
137 exclusions: [{
138 path: 'dont-copy',
139 action: 'exclude',
140 type: 'dir',
141 },{
142 path: 'dont-compile',
143 action: 'dontCompile',
144 type: 'dir',
145 }
146 ],
147 compile: true,
148 sourcemaps: true
149 };
150 var bacon = baconize(getPathIn(), getPathOut(), options);
151
152 var dirs = [];
153 bacon.events.on('chdir', function(dir) { dirs.push(dir); });
154
155 var compiledFiles = [];
156 var lastStartedFile;
157 bacon.events.on('compile-start', function(file) {
158 lastStartedFile = file.path;
159 });
160 bacon.events.on('compile-done', function(file) {
161 if (lastStartedFile === file.path) {
162 compiledFiles.push(file.path);
163 } else {
164 expect.fail('Unexpected compile-done event');
165 }
166 });
167
168 return bacon.then(function(num) {
169 return expect.promise.all([
170 expect(num, 'to be', 9),
171 expect(dirs, 'to contain', '', 'dont-compile', 'scripts', 'styles').and('to have length', 4),
172 expect(compiledFiles, 'to contain',
173 'index.jade', 'scripts/main.coffee', 'styles/main.styl', 'scripts/log.babel.js'
174 )
175 .and('not to contain', 'about.html', 'styles/typography.css', 'styles/typography.css')
176 .and('to have length', 4)
177 ]);
178 });
179 });
180
181 describe('after compilation', function() {
182
183 it('should have all compiled files', function() {
184 return when.all([
185 fs.readFile(getPathOut('index.html')),
186 fs.readFile(getPathOut('styles/main.css')),
187 fs.readFile(getPathOut('scripts/main.js')),
188 fs.readFile(getPathOut('scripts/iterate.js')),
189 fs.readFile(getPathOut('about.html')),
190 fs.readFile(getPathOut('dont-compile/foo.coffee')),
191 fs.readFile(getPathOut('styles/typography.css'))
192 ]).then(function(results) {
193 return expect.promise.all([
194 expect(results[0].toString(), 'to contain', 'saying \'hello\'.\n'),
195 expect(results[1].toString(), 'to contain', 'body {'),
196 expect(results[2].toString(), 'to contain', 'console.log("H'),
197 expect(results[3].toString(), 'to contain', 'for (var i = 0; i < stuff.length; i++) {'),
198 expect(results[4].toString(), 'to contain', ' <head>'),
199 expect(results[5].toString(), 'to contain', 'console.log "T'),
200 expect(results[6].toString(), 'to contain', ' font-family: arial;')
201 ]);
202 });
203 });
204
205 it('should URL link to source maps', function() {
206 return when.all([
207 fs.readFile(getPathOut('styles/main.css')),
208 fs.readFile(getPathOut('scripts/main.js')),
209 ]).then(function(results) {
210 return expect.promise.all([
211 expect(results[0].toString(), 'to contain', '/*# sourceMappingURL=main.css.map*/'),
212 expect(results[1].toString(), 'to contain', '//# sourceMappingURL=main.js.map'),
213 ]);
214 });
215 });
216
217 it('should have source maps', function() {
218 return when.all([
219 fs.readFile(getPathOut('styles/main.css.map')),
220 fs.readFile(getPathOut('scripts/main.js.map')),
221 ]).then(function(results) {
222 return expect.promise.all([
223 expect(results[0].toString(), 'to contain', '"file":"main.css"')
224 .and('to contain', '"sources":["main.styl"]'),
225 expect(results[1].toString(), 'to contain', '"file":"main.js"')
226 .and('to contain', '"sources":["main.coffee"]'),
227 ]);
228 });
229 });
230
231 it('should copy pre-compiled source files', function() {
232 return fs.readFile(getPathOut('index.jade')).then(function(results) {
233 return expect(results.toString(), 'to contain', 'html(lang="en")');
234 });
235 });
236
237 it('should not compile blacklist matches', function() {
238 return fs.readFile(getPathOut('dont-compile/foo.js'))
239 .then(function() {
240 return expect.fail('`dont-compile/foo.js` should not be copied');
241 }, function(err) {
242 return expect(err.code, 'to be', 'ENOENT');
243 });
244 });
245
246 it('should not copy ignore paths', function() {
247 return fs.stat(getPathOut('dont-copy'))
248 .then(function() {
249 return expect.fail('`dont-copy` directory should not be copied');
250 }, function(err) {
251 return expect(err.code, 'to be', 'ENOENT');
252 });
253 });
254
255 });
256 });
257
258
259 describe('compile minified', function() {
260
261 before(clearDir);
262 after(clearDir);
263
264 it('should compile compilable files and copy all others', function () {
265 var options = {
266 exclusions: [{
267 path: 'dont-copy',
268 action: 'exclude',
269 type: 'dir',
270 },{
271 path: 'dont-compile',
272 action: 'dontCompile',
273 type: 'dir',
274 }
275 ],
276 compile: true,
277 sourcemaps: false,
278 minify: true
279 };
280 var bacon = baconize(getPathIn(), getPathOut(), options);
281
282 var dirs = [];
283 bacon.events.on('chdir', function(dir) { dirs.push(dir); });
284
285 var compiledFiles = [];
286 var lastStartedFile;
287 bacon.events.on('compile-start', function(file) {
288 lastStartedFile = file.path;
289 });
290 bacon.events.on('compile-done', function(file) {
291 if (lastStartedFile === file.path) {
292 compiledFiles.push(file.path);
293 } else {
294 expect.fail('Unexpected compile-done event');
295 }
296 });
297
298
299 return bacon.then(function(num) {
300 return expect.promise.all([
301 expect(num, 'to be', 9),
302 expect(dirs, 'to contain', '', 'dont-compile', 'scripts', 'styles').and('to have length', 4),
303 expect(compiledFiles, 'to contain',
304 'about.html', 'index.jade', 'scripts/log.babel.js',
305 'styles/main.styl', 'styles/typography.css',
306 'scripts/iterate.js', 'scripts/main.coffee').and('to have length', 7),
307 ]);
308 });
309 });
310
311 describe('after compilation', function() {
312
313 it('should have all compiled files', function() {
314 return when.all([
315 fs.readFile(getPathOut('index.html')),
316 fs.readFile(getPathOut('styles/main.css')),
317 fs.readFile(getPathOut('scripts/main.js')),
318 fs.readFile(getPathOut('scripts/iterate.js')),
319 fs.readFile(getPathOut('about.html')),
320 fs.readFile(getPathOut('dont-compile/foo.coffee')),
321 fs.readFile(getPathOut('styles/typography.css'))
322 ]).then(function(results) {
323 return expect.promise.all([
324 expect(results[0].toString(), 'to contain', 'saying \'hello\'.</p>'),
325 expect(results[1].toString(), 'to contain', 'body{'),
326 expect(results[2].toString(), 'to contain', 'console.log("H'),
327 expect(results[3].toString(), 'to contain', 'for(var stuff=[1,2,3,4,5]'),
328 expect(results[4].toString(), 'to contain', '<html><head>'),
329 expect(results[5].toString(), 'to contain', 'console.log "T'),
330 expect(results[6].toString(), 'to contain', 'font-family:arial}')
331 ]);
332 });
333 });
334
335 it('should not copy pre-compiled files', function() {
336 return fs.readFile(getPathOut('index.jade'))
337 .then(function() {
338 return expect.fail('`index.jade` should not be copied');
339 }, function(err) {
340 return expect(err.code, 'to be', 'ENOENT');
341 });
342 });
343
344 it('should not compile blacklist matches', function() {
345 return fs.readFile(getPathOut('dont-compile/foo.js'))
346 .then(function() {
347 return expect.fail('`dont-compile/foo.js` should not be copied');
348 }, function(err) {
349 return expect(err.code, 'to be', 'ENOENT');
350 });
351 });
352
353 it('should not copy ignore paths', function() {
354 return fs.stat(getPathOut('dont-copy'))
355 .then(function() {
356 return expect.fail('`dont-copy` directory should not be copied');
357 }, function(err) {
358 return expect(err.code, 'to be', 'ENOENT');
359 });
360 });
361
362 });
363 });
364
365 describe('compile minified with sourcemaps', function() {
366
367 before(clearDir);
368 after(clearDir);
369
370 it('should compile compilable files and copy all others', function () {
371 var options = {
372 exclusions: [{
373 path: 'dont-copy',
374 action: 'exclude',
375 type: 'dir',
376 },{
377 path: 'dont-compile',
378 action: 'dontCompile',
379 type: 'dir',
380 }
381 ],
382 compile: true,
383 minify: true,
384 sourcemaps: true
385 };
386 var bacon = baconize(getPathIn(), getPathOut(), options);
387
388 var dirs = [];
389 bacon.events.on('chdir', function(dir) { dirs.push(dir); });
390
391 var compiledFiles = [];
392 var lastStartedFile;
393 bacon.events.on('compile-start', function(file) {
394 lastStartedFile = file.path;
395 });
396 bacon.events.on('compile-done', function(file) {
397 if (lastStartedFile === file.path) {
398 compiledFiles.push(file.path);
399 } else {
400 expect.fail('Unexpected compile-done event');
401 }
402 });
403
404
405 return bacon.then(function(num) {
406 return expect.promise.all([
407 expect(num, 'to be', 9),
408 expect(dirs, 'to contain', '', 'dont-compile', 'scripts', 'styles').and('to have length', 4),
409 expect(compiledFiles, 'to contain',
410 'about.html', 'index.jade', 'scripts/log.babel.js',
411 'styles/main.styl', 'styles/typography.css',
412 'scripts/iterate.js', 'scripts/main.coffee').and('to have length', 7),
413 ]);
414 });
415 });
416
417 describe('after compilation', function() {
418
419 it('should have all compiled files', function() {
420 return when.all([
421 fs.readFile(getPathOut('index.html')),
422 fs.readFile(getPathOut('styles/main.css')),
423 fs.readFile(getPathOut('scripts/main.js')),
424 fs.readFile(getPathOut('scripts/iterate.js')),
425 fs.readFile(getPathOut('about.html')),
426 fs.readFile(getPathOut('dont-compile/foo.coffee')),
427 fs.readFile(getPathOut('styles/typography.css'))
428 ]).then(function(results) {
429 return expect.promise.all([
430 expect(results[0].toString(), 'to contain', 'saying \'hello\'.</p>'),
431 expect(results[1].toString(), 'to contain', 'body{'),
432 expect(results[2].toString(), 'to contain', 'console.log("H'),
433 expect(results[3].toString(), 'to contain', 'for(var stuff=[1,2,3,4,5]'),
434 expect(results[4].toString(), 'to contain', '<html><head>'),
435 expect(results[5].toString(), 'to contain', 'console.log "T'),
436 expect(results[6].toString(), 'to contain', 'font-family:arial}')
437 ]);
438 });
439 });
440
441 it('should URL link to source maps', function() {
442 return when.all([
443 fs.readFile(getPathOut('styles/main.css')),
444 fs.readFile(getPathOut('scripts/main.js')),
445 fs.readFile(getPathOut('scripts/iterate.js')),
446 fs.readFile(getPathOut('styles/typography.css'))
447
448 ]).then(function(results) {
449 return expect.promise.all([
450 expect(results[0].toString(), 'to contain', '/*# sourceMappingURL=main.css.map*/'),
451 expect(results[1].toString(), 'to contain', '//# sourceMappingURL=main.js.map'),
452 expect(results[2].toString(), 'to contain', '//# sourceMappingURL=iterate.js.map'),
453 expect(results[3].toString(), 'to contain', '/*# sourceMappingURL=typography.css.map*/')
454
455 ]);
456 });
457 });
458
459 it('should have source maps', function() {
460 return when.all([
461 fs.readFile(getPathOut('styles/main.css.map')),
462 fs.readFile(getPathOut('scripts/main.js.map')),
463 fs.readFile(getPathOut('scripts/iterate.js.map')),
464 fs.readFile(getPathOut('styles/typography.css.map'))
465 ]).then(function(results) {
466 return expect.promise.all([
467 expect(results[0].toString(), 'to contain', '"file":"main.css"')
468 .and('to contain', '"sources":["main.styl"]'),
469 expect(results[1].toString(), 'to contain', '"file":"main.js"')
470 .and('to contain', '"sources":["main.coffee"]'),
471 expect(results[2].toString(), 'to contain', '"file":"iterate.js"')
472 .and('to contain', '"sources":["iterate.src.js"]'),
473 expect(results[3].toString(), 'to contain', '"file":"typography.css"')
474 .and('to contain', '"sources":["typography.src.css"]'),
475 ]);
476 });
477 });
478
479 it('should copy pre-compiled source files', function() {
480 return when.all([
481 fs.readFile(getPathOut('index.jade')),
482 fs.readFile(getPathOut('about.src.html')),
483 fs.readFile(getPathOut('scripts/iterate.src.js')),
484 fs.readFile(getPathOut('styles/typography.src.css'))
485 ]).then(function(results) {
486 return expect.promise.all([
487 expect(results[0].toString(), 'to contain', 'html(lang="en")'),
488 expect(results[1].toString(), 'to contain', ' <meta charset="utf-8">'),
489 expect(results[2].toString(), 'to contain', 'for (var i = 0; i < stuff.length; i++) {'),
490 expect(results[3].toString(), 'to contain', ' font-family: arial;')
491 ]);
492 });
493 });
494
495 it('should not compile blacklist matches', function() {
496 return fs.readFile(getPathOut('dont-compile/foo.js'))
497 .then(function() {
498 return expect.fail('`dont-compile/foo.js` should not be copied');
499 }, function(err) {
500 return expect(err.code, 'to be', 'ENOENT');
501 });
502 });
503
504 it('should not copy ignore paths', function() {
505 return fs.stat(getPathOut('dont-copy'))
506 .then(function() {
507 return expect.fail('`dont-copy` directory should not be copied');
508 }, function(err) {
509 return expect(err.code, 'to be', 'ENOENT');
510 });
511 });
512
513 });
514 });
515
516 describe('aborted compile', function() {
517
518 before(clearDir);
519 after(clearDir);
520
521 it('should abort during compile process', function () {
522 var options = {
523 exclusions: [{
524 path: 'dont-copy',
525 action: 'exclude',
526 type: 'dir',
527 },{
528 path: 'dont-compile',
529 action: 'dontCompile',
530 type: 'dir',
531 }
532 ],
533 compile: true,
534 sourcemaps: false
535 };
536 var bacon = baconize(getPathIn(), getPathOut(), options);
537
538 setTimeout(function() { bacon.abort(); }, 10);
539
540 return bacon.then(function() {
541 return expect.fail('Baconize should not have completed, should have been aborted');
542 }, function(err) {
543 return expect(err.code, 'to be', 'ABORT');
544 });
545 });
546
547 describe('after aborted compilation', function() {
548
549 it('should have removed all files', function() {
550 return when.all(fs.readdir(getPathOut())).then(function() {
551 return expect.fail('Output dir should not exist after abort');
552 }, function(err) {
553 return expect(err.code, 'to be', 'ENOENT');
554 });
555 });
556
557 });
558
559 });
560
561 describe('preflight', function() {
562 it('should work (basic test)', function() {
563 return baconize.preflight(process.cwd()).then(function(info) {
564 return expect(info, 'to have properties', {
565 empty: false,
566 exists: true,
567 nodeModules: true,
568 bowerComponents: false
569 })
570 })
571 })
572
573 });
574
575});