blob: e2f6b4948d44edb53ef88a0f939b4fa0a87bbb7b [file] [log] [blame]
Matteo Scandolob8c76b12016-05-11 14:53:20 -07001'use strict';
2var path = require('path');
3var helpers = require('yeoman-test');
4var assert = require('yeoman-assert');
Matteo Scandolob70db482016-07-06 10:39:36 -07005var P = require('bluebird');
6var execAsync = P.promisify(require('child_process').exec);
Matteo Scandolob8c76b12016-05-11 14:53:20 -07007var fs = require('fs');
Matteo Scandolob70db482016-07-06 10:39:36 -07008var writeFileAsync = P.promisify(fs.writeFile);
Matteo Scandolob8c76b12016-05-11 14:53:20 -07009const rimraf = require('rimraf');
10
11const getMillisec = min => min * 60 * 1000;
12const deleteFile = file => {
13 if(fs.existsSync(file)){
14 // console.log(`deleting: ${file}`);
15 fs.unlinkSync(file);
16 }
Matteo Scandolob70db482016-07-06 10:39:36 -070017};
Matteo Scandolob8c76b12016-05-11 14:53:20 -070018
Matteo Scandolo4ac9a0b2016-05-23 15:31:25 -070019// config files
20const cfg = path.join(__dirname, `../../../env/default.js`);
21
Matteo Scandolob8c76b12016-05-11 14:53:20 -070022// source files
23const viewName = 'testDashboard';
24const fileName = viewName.replace(/^./, viewName[0].toUpperCase());
25const sourcePath = path.join(__dirname, `../../../ngXosViews/${viewName}/`);
26
27// dest files
28const basePath = '../../../../xos/core/xoslib';
29const destHtml = path.join(__dirname, basePath + '/dashboards/xosTestDashboard.html');
30const destJs = path.join(__dirname, basePath + '/static/js/xosTestDashboard.js');
Matteo Scandolo195dde92016-07-25 16:43:16 -070031const destVendor = path.join(__dirname, basePath + '/static/vendor/xosTestDashboardVendor.js');
Matteo Scandolob8c76b12016-05-11 14:53:20 -070032const destCss = path.join(__dirname, basePath + '/static/css/xosTestDashboard.css');
33
34describe('The XOS Build script', function(){
35 const buildCmd = 'gulp build';
36
37 this.timeout(getMillisec(5));
38
Matteo Scandolob70db482016-07-06 10:39:36 -070039 // define timers (give a feedback while commands are running
40 let npmInstall, bowerInstall, appBuild;
41
Matteo Scandolob8c76b12016-05-11 14:53:20 -070042 before(done => {
Matteo Scandolo4ac9a0b2016-05-23 15:31:25 -070043 // if `default.js` config is not present
44 // create one (we check to avoid screwing up local envs)
45 if(!fs.existsSync(cfg)){
46 fs.writeFileSync(cfg, 'module.exports = {}');
47 }
48
Matteo Scandolob8c76b12016-05-11 14:53:20 -070049 console.log('Running generator');
50 this.generator = helpers
51 .run(require.resolve('../app'))
52 .inDir(sourcePath)
53 .withOptions({ 'skip-install': false })
54 .withPrompts({
55 name: viewName,
56 host: 'test-host',
57 token: 'test-token',
58 session: 'test-session'
59 })
Matteo Scandolob70db482016-07-06 10:39:36 -070060 .toPromise()
61 .then(() => {
62 //.on('end', () => {
Matteo Scandolob8c76b12016-05-11 14:53:20 -070063 process.stdout.write('Installing Node Modules');
Matteo Scandolob70db482016-07-06 10:39:36 -070064 npmInstall = setInterval(() => {
Matteo Scandolob8c76b12016-05-11 14:53:20 -070065 process.stdout.write('.');
66 }, 1000);
Matteo Scandolob70db482016-07-06 10:39:36 -070067 return execAsync('npm install', {cwd: sourcePath});
68 })
69 .then(() => {
70 clearInterval(npmInstall);
71 process.stdout.write('\nInstalling Bower Components');
72
73 bowerInstall = setInterval(() => {
74 process.stdout.write('.');
75 }, 1000);
76
77 return execAsync('bower install', {cwd: sourcePath});
78 })
79 .then(() => {
80 clearInterval(bowerInstall);
81 done();
82 })
83 .catch(done);
Matteo Scandolob8c76b12016-05-11 14:53:20 -070084 });
85
86 describe('when no styles or vendors are added', () => {
87
88 before((done) => {
89 process.stdout.write('\nBuilding App');
Matteo Scandolob70db482016-07-06 10:39:36 -070090 appBuild = setInterval(() => {
Matteo Scandolob8c76b12016-05-11 14:53:20 -070091 process.stdout.write('.');
92 }, 1000);
Matteo Scandolob70db482016-07-06 10:39:36 -070093 execAsync(buildCmd, {cwd: sourcePath})
94 .then(() => {
Matteo Scandolob8c76b12016-05-11 14:53:20 -070095 clearInterval(appBuild);
Matteo Scandolob70db482016-07-06 10:39:36 -070096 done();
97 })
98 .catch(done);
Matteo Scandolob8c76b12016-05-11 14:53:20 -070099 });
100
101 it('should have build the app', () => {
102 assert.file([destHtml, destJs]);
103 });
104
105 it('should include only minified files in the index', () => {
106 assert.fileContent(destHtml, `<script src="/static/js/xos${fileName}.js"></script>`);
107 assert.noFileContent(destHtml, `<!-- bower:css -->`);
108 assert.noFileContent(destHtml, `<!-- bower:js -->`);
109 });
110 });
111
112 describe('when a third party library is added', () => {
Matteo Scandolob70db482016-07-06 10:39:36 -0700113
Matteo Scandolob8c76b12016-05-11 14:53:20 -0700114 before((done) => {
Matteo Scandolob70db482016-07-06 10:39:36 -0700115 process.stdout.write('\nInstalling 3rd party library');
Matteo Scandolob8c76b12016-05-11 14:53:20 -0700116 let bowerInstall = setInterval(() => {
117 process.stdout.write('.');
118 }, 1000);
Matteo Scandolob70db482016-07-06 10:39:36 -0700119
120 execAsync('bower install moment --save', {cwd: sourcePath})
121 .then(() => {
Matteo Scandolob8c76b12016-05-11 14:53:20 -0700122 clearInterval(bowerInstall);
123 process.stdout.write('\nBuilding App');
Matteo Scandolob70db482016-07-06 10:39:36 -0700124 appBuild = setInterval(() => {
Matteo Scandolob8c76b12016-05-11 14:53:20 -0700125 process.stdout.write('.');
126 }, 1000);
Matteo Scandolob70db482016-07-06 10:39:36 -0700127
128 return execAsync(buildCmd, {cwd: sourcePath})
129 })
130 .then(() => {
131 clearInterval(appBuild);
132 done();
133 })
134 .catch(done);
Matteo Scandolob8c76b12016-05-11 14:53:20 -0700135 });
136
137 it('should have build the app with a vendor file', () => {
138 assert.file([destHtml, destJs, destVendor]);
139 });
140
141 it('should include only minified files and minified deps in the index', () => {
142 assert.fileContent(destHtml, `<script src="/static/js/xos${fileName}.js"></script>`);
Matteo Scandolo195dde92016-07-25 16:43:16 -0700143 assert.fileContent(destHtml, `<script src="/static/vendor/xos${fileName}Vendor.js"></script>`);
Matteo Scandolob8c76b12016-05-11 14:53:20 -0700144 assert.noFileContent(destHtml, `<!-- bower:css -->`);
145 assert.noFileContent(destHtml, `<!-- bower:js -->`);
146 });
147 });
148
149 describe('when some styles are added', () => {
150 before((done) => {
151 let styleContent = `
152 @import '../../../../style/sass/lib/_variables.scss';
153
154 #xosTestDashboard {
155 background: $brand-primary;
156 }
157 `;
Matteo Scandolob70db482016-07-06 10:39:36 -0700158
159 writeFileAsync(`${sourcePath}src/sass/main.scss`, styleContent)
160 .then(() => {
Matteo Scandolob8c76b12016-05-11 14:53:20 -0700161 process.stdout.write('\nBuilding the Application');
Matteo Scandolob70db482016-07-06 10:39:36 -0700162 appBuild = setInterval(() => {
Matteo Scandolob8c76b12016-05-11 14:53:20 -0700163 process.stdout.write('.');
164 }, 1000);
Matteo Scandolob70db482016-07-06 10:39:36 -0700165
166 return execAsync('bower uninstall moment --save', {cwd: sourcePath});
167 })
168 .then(() => {
169 return execAsync(buildCmd, {
Matteo Scandolob8c76b12016-05-11 14:53:20 -0700170 cwd: sourcePath
Matteo Scandolob70db482016-07-06 10:39:36 -0700171 });
172 })
173 .then(() => {
174 clearInterval(appBuild);
175 done();
176 })
177 .catch(done);
Matteo Scandolob8c76b12016-05-11 14:53:20 -0700178 });
179
180 it('should have build the app with a css file', () => {
181 assert.file([destHtml, destJs, destCss]);
182 });
183
184 it('should include only minified files and minified deps in the index', () => {
185 assert.fileContent(destHtml, `<script src="/static/js/xos${fileName}.js"></script>`);
186 assert.fileContent(destHtml, `<link rel="stylesheet" href="/static/css/xos${fileName}.css">`);
187 assert.noFileContent(destHtml, `<!-- bower:css -->`);
188 assert.noFileContent(destHtml, `<!-- bower:js -->`);
189
190 assert.fileContent(destCss, `background:#337ab7`);
191 });
192 });
193
194 after(done => {
195 // deleting the folder used for test
196 deleteFile(destHtml);
197 deleteFile(destJs);
198 deleteFile(destVendor);
199 deleteFile(destCss);
200 rimraf(sourcePath, {}, done);
201 });
202});