1 | var Lab = require('lab');
|
2 | var async = require('async');
|
3 | var os = require('os');
|
4 | var fs = require('fs');
|
5 | var path = require('path');
|
6 | var crypto = require('crypto');
|
7 | var Moonboots = require('..');
|
8 | var moonboots;
|
9 |
|
10 | Lab.experiment('files get read from buildDirectory', function () {
|
11 | var tmpHash = crypto.randomBytes(16).toString('hex');
|
12 | var buildDir = path.join(os.tmpdir(), tmpHash);
|
13 | Lab.before(function (done) {
|
14 | async.series([
|
15 | function (next) {
|
16 | fs.mkdir(buildDir, next);
|
17 | },
|
18 | function (next) {
|
19 | fs.writeFile(path.join(buildDir, 'app.deadbeef.min.js'), 'javascript!' + tmpHash, next);
|
20 | },
|
21 | function (next) {
|
22 | fs.writeFile(path.join(buildDir, 'app.deadbeef.min.css'), 'cascading stylesheets!' + tmpHash, next);
|
23 | },
|
24 | function (next) {
|
25 | fs.writeFile(path.join(buildDir, 'readme.md'), '# this file will be ignored in the builddir', next);
|
26 | }
|
27 | ], function () {
|
28 | var options = {
|
29 | main: __dirname + '/../fixtures/app/app.js',
|
30 | jsFileName: 'app',
|
31 | cssFileName: 'app',
|
32 | buildDirectory: buildDir,
|
33 | stylesheets: [
|
34 | __dirname + '/../fixtures/stylesheets/style.css'
|
35 | ]
|
36 | };
|
37 | moonboots = new Moonboots(options);
|
38 | moonboots.on('ready', done);
|
39 | });
|
40 | });
|
41 | Lab.after(function (done) {
|
42 | async.series([
|
43 | function (next) {
|
44 | fs.unlink(path.join(buildDir, 'app.deadbeef.min.js'), next);
|
45 | },
|
46 | function (next) {
|
47 | fs.unlink(path.join(buildDir, 'app.deadbeef.min.css'), next);
|
48 | },
|
49 | function (next) {
|
50 | fs.unlink(path.join(buildDir, 'readme.md'), next);
|
51 | },
|
52 | function (next) {
|
53 | fs.rmdir(buildDir, next);
|
54 | }
|
55 | ], function (err) {
|
56 | if (err) {throw err; }
|
57 | done();
|
58 | });
|
59 | });
|
60 | Lab.test('htmlContext', function (done) {
|
61 | var context = moonboots.htmlContext();
|
62 | Lab.expect(context).to.have.keys('jsFileName', 'cssFileName');
|
63 | Lab.expect(context.jsFileName).to.equal('app.deadbeef.min.js');
|
64 | Lab.expect(context.cssFileName).to.equal('app.deadbeef.min.css');
|
65 | done();
|
66 | });
|
67 | Lab.test('js', function (done) {
|
68 | moonboots.jsSource(function (err, js) {
|
69 | Lab.expect(js).to.equal('javascript!' + tmpHash);
|
70 | done();
|
71 | });
|
72 | });
|
73 | Lab.test('css', function (done) {
|
74 | moonboots.cssSource(function (err, css) {
|
75 | Lab.expect(css).to.equal('cascading stylesheets!' + tmpHash);
|
76 | done();
|
77 | });
|
78 | });
|
79 | });
|
80 |
|
81 | Lab.experiment('Files get written to build directory', function () {
|
82 | var tmpHash = crypto.randomBytes(16).toString('hex');
|
83 | var buildDir = path.join(os.tmpdir(), tmpHash);
|
84 | Lab.before(function (done) {
|
85 | fs.mkdir(buildDir, function () {
|
86 | var options = {
|
87 | main: __dirname + '/../fixtures/app/app.js',
|
88 | jsFileName: 'app',
|
89 | cssFileName: 'app',
|
90 | buildDirectory: buildDir,
|
91 | stylesheets: [
|
92 | __dirname + '/../fixtures/stylesheets/style.css'
|
93 | ],
|
94 | libraries: [
|
95 | __dirname + '/../fixtures/libraries/iife-no-semicolon.js',
|
96 | __dirname + '/../fixtures/libraries/lib.js'
|
97 | ]
|
98 | };
|
99 | moonboots = new Moonboots(options);
|
100 | moonboots.on('ready', done);
|
101 | });
|
102 | });
|
103 | Lab.after(function (done) {
|
104 | async.series([
|
105 | function (next) {
|
106 | fs.unlink(path.join(buildDir, 'app.3adb4850.min.js'), next);
|
107 | },
|
108 | function (next) {
|
109 | fs.unlink(path.join(buildDir, 'app.38ea6c96.min.css'), next);
|
110 | },
|
111 | function (next) {
|
112 | fs.rmdir(buildDir, next);
|
113 | }
|
114 | ], function (err) {
|
115 | if (err) {throw err; }
|
116 | done();
|
117 | });
|
118 | });
|
119 | Lab.test('js file was written', function (done) {
|
120 | var jsFileName = moonboots.jsFileName();
|
121 | var filePath = path.join(buildDir, jsFileName);
|
122 | Lab.expect(jsFileName).to.equal('app.3adb4850.min.js');
|
123 | fs.readFile(filePath, 'utf8', function (err) {
|
124 | Lab.expect(err).to.not.be.ok;
|
125 |
|
126 |
|
127 | Lab.expect(function () { require(filePath); }).to.not.throw();
|
128 | done();
|
129 | });
|
130 | });
|
131 | Lab.test('css file was written', function (done) {
|
132 | var cssFileName = moonboots.cssFileName();
|
133 | Lab.expect(cssFileName).to.equal('app.38ea6c96.min.css');
|
134 | fs.readFile(path.join(buildDir, cssFileName), 'utf8', function (err) {
|
135 | Lab.expect(err).to.not.be.ok;
|
136 | done();
|
137 | });
|
138 | });
|
139 | });
|