blob: dd9dc93cff8f1323bc5d9131f9b1a9d023d7c7e8 [file] [log] [blame]
Matteo Scandolod2044a42017-08-07 16:08:28 -07001
2/*
3 * Copyright 2017-present Open Networking Foundation
4
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8
9 * http://www.apache.org/licenses/LICENSE-2.0
10
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18
Matteo Scandolob8c76b12016-05-11 14:53:20 -070019'use strict';
20var path = require('path');
21var helpers = require('yeoman-test');
22var assert = require('yeoman-assert');
Matteo Scandolob70db482016-07-06 10:39:36 -070023var P = require('bluebird');
24var execAsync = P.promisify(require('child_process').exec);
Matteo Scandolob8c76b12016-05-11 14:53:20 -070025var fs = require('fs');
Matteo Scandolob70db482016-07-06 10:39:36 -070026var writeFileAsync = P.promisify(fs.writeFile);
Matteo Scandolob8c76b12016-05-11 14:53:20 -070027const rimraf = require('rimraf');
28
29const getMillisec = min => min * 60 * 1000;
30const deleteFile = file => {
31 if(fs.existsSync(file)){
32 // console.log(`deleting: ${file}`);
33 fs.unlinkSync(file);
34 }
Matteo Scandolob70db482016-07-06 10:39:36 -070035};
Matteo Scandolob8c76b12016-05-11 14:53:20 -070036
Matteo Scandolo4ac9a0b2016-05-23 15:31:25 -070037// config files
38const cfg = path.join(__dirname, `../../../env/default.js`);
39
Matteo Scandolob8c76b12016-05-11 14:53:20 -070040// source files
41const viewName = 'testDashboard';
42const fileName = viewName.replace(/^./, viewName[0].toUpperCase());
43const sourcePath = path.join(__dirname, `../../../ngXosViews/${viewName}/`);
44
45// dest files
46const basePath = '../../../../xos/core/xoslib';
47const destHtml = path.join(__dirname, basePath + '/dashboards/xosTestDashboard.html');
48const destJs = path.join(__dirname, basePath + '/static/js/xosTestDashboard.js');
Matteo Scandolo195dde92016-07-25 16:43:16 -070049const destVendor = path.join(__dirname, basePath + '/static/vendor/xosTestDashboardVendor.js');
Matteo Scandolob8c76b12016-05-11 14:53:20 -070050const destCss = path.join(__dirname, basePath + '/static/css/xosTestDashboard.css');
51
52describe('The XOS Build script', function(){
53 const buildCmd = 'gulp build';
54
55 this.timeout(getMillisec(5));
56
Matteo Scandolob70db482016-07-06 10:39:36 -070057 // define timers (give a feedback while commands are running
58 let npmInstall, bowerInstall, appBuild;
59
Matteo Scandolob8c76b12016-05-11 14:53:20 -070060 before(done => {
Matteo Scandolo4ac9a0b2016-05-23 15:31:25 -070061 // if `default.js` config is not present
62 // create one (we check to avoid screwing up local envs)
63 if(!fs.existsSync(cfg)){
64 fs.writeFileSync(cfg, 'module.exports = {}');
65 }
66
Matteo Scandolob8c76b12016-05-11 14:53:20 -070067 console.log('Running generator');
68 this.generator = helpers
69 .run(require.resolve('../app'))
70 .inDir(sourcePath)
71 .withOptions({ 'skip-install': false })
72 .withPrompts({
73 name: viewName,
74 host: 'test-host',
75 token: 'test-token',
76 session: 'test-session'
77 })
Matteo Scandolob70db482016-07-06 10:39:36 -070078 .toPromise()
79 .then(() => {
80 //.on('end', () => {
Matteo Scandolob8c76b12016-05-11 14:53:20 -070081 process.stdout.write('Installing Node Modules');
Matteo Scandolob70db482016-07-06 10:39:36 -070082 npmInstall = setInterval(() => {
Matteo Scandolob8c76b12016-05-11 14:53:20 -070083 process.stdout.write('.');
84 }, 1000);
Matteo Scandolob70db482016-07-06 10:39:36 -070085 return execAsync('npm install', {cwd: sourcePath});
86 })
87 .then(() => {
88 clearInterval(npmInstall);
89 process.stdout.write('\nInstalling Bower Components');
90
91 bowerInstall = setInterval(() => {
92 process.stdout.write('.');
93 }, 1000);
94
95 return execAsync('bower install', {cwd: sourcePath});
96 })
97 .then(() => {
98 clearInterval(bowerInstall);
99 done();
100 })
101 .catch(done);
Matteo Scandolob8c76b12016-05-11 14:53:20 -0700102 });
103
104 describe('when no styles or vendors are added', () => {
105
106 before((done) => {
107 process.stdout.write('\nBuilding App');
Matteo Scandolob70db482016-07-06 10:39:36 -0700108 appBuild = setInterval(() => {
Matteo Scandolob8c76b12016-05-11 14:53:20 -0700109 process.stdout.write('.');
110 }, 1000);
Matteo Scandolob70db482016-07-06 10:39:36 -0700111 execAsync(buildCmd, {cwd: sourcePath})
112 .then(() => {
Matteo Scandolob8c76b12016-05-11 14:53:20 -0700113 clearInterval(appBuild);
Matteo Scandolob70db482016-07-06 10:39:36 -0700114 done();
115 })
116 .catch(done);
Matteo Scandolob8c76b12016-05-11 14:53:20 -0700117 });
118
119 it('should have build the app', () => {
120 assert.file([destHtml, destJs]);
121 });
122
123 it('should include only minified files in the index', () => {
124 assert.fileContent(destHtml, `<script src="/static/js/xos${fileName}.js"></script>`);
125 assert.noFileContent(destHtml, `<!-- bower:css -->`);
126 assert.noFileContent(destHtml, `<!-- bower:js -->`);
127 });
128 });
129
130 describe('when a third party library is added', () => {
Matteo Scandolob70db482016-07-06 10:39:36 -0700131
Matteo Scandolob8c76b12016-05-11 14:53:20 -0700132 before((done) => {
Matteo Scandolob70db482016-07-06 10:39:36 -0700133 process.stdout.write('\nInstalling 3rd party library');
Matteo Scandolob8c76b12016-05-11 14:53:20 -0700134 let bowerInstall = setInterval(() => {
135 process.stdout.write('.');
136 }, 1000);
Matteo Scandolob70db482016-07-06 10:39:36 -0700137
138 execAsync('bower install moment --save', {cwd: sourcePath})
139 .then(() => {
Matteo Scandolob8c76b12016-05-11 14:53:20 -0700140 clearInterval(bowerInstall);
141 process.stdout.write('\nBuilding App');
Matteo Scandolob70db482016-07-06 10:39:36 -0700142 appBuild = setInterval(() => {
Matteo Scandolob8c76b12016-05-11 14:53:20 -0700143 process.stdout.write('.');
144 }, 1000);
Matteo Scandolob70db482016-07-06 10:39:36 -0700145
146 return execAsync(buildCmd, {cwd: sourcePath})
147 })
148 .then(() => {
149 clearInterval(appBuild);
150 done();
151 })
152 .catch(done);
Matteo Scandolob8c76b12016-05-11 14:53:20 -0700153 });
154
155 it('should have build the app with a vendor file', () => {
156 assert.file([destHtml, destJs, destVendor]);
157 });
158
159 it('should include only minified files and minified deps in the index', () => {
160 assert.fileContent(destHtml, `<script src="/static/js/xos${fileName}.js"></script>`);
Matteo Scandolo195dde92016-07-25 16:43:16 -0700161 assert.fileContent(destHtml, `<script src="/static/vendor/xos${fileName}Vendor.js"></script>`);
Matteo Scandolob8c76b12016-05-11 14:53:20 -0700162 assert.noFileContent(destHtml, `<!-- bower:css -->`);
163 assert.noFileContent(destHtml, `<!-- bower:js -->`);
164 });
165 });
166
167 describe('when some styles are added', () => {
168 before((done) => {
169 let styleContent = `
170 @import '../../../../style/sass/lib/_variables.scss';
171
172 #xosTestDashboard {
173 background: $brand-primary;
174 }
175 `;
Matteo Scandolob70db482016-07-06 10:39:36 -0700176
177 writeFileAsync(`${sourcePath}src/sass/main.scss`, styleContent)
178 .then(() => {
Matteo Scandolob8c76b12016-05-11 14:53:20 -0700179 process.stdout.write('\nBuilding the Application');
Matteo Scandolob70db482016-07-06 10:39:36 -0700180 appBuild = setInterval(() => {
Matteo Scandolob8c76b12016-05-11 14:53:20 -0700181 process.stdout.write('.');
182 }, 1000);
Matteo Scandolob70db482016-07-06 10:39:36 -0700183
184 return execAsync('bower uninstall moment --save', {cwd: sourcePath});
185 })
186 .then(() => {
187 return execAsync(buildCmd, {
Matteo Scandolob8c76b12016-05-11 14:53:20 -0700188 cwd: sourcePath
Matteo Scandolob70db482016-07-06 10:39:36 -0700189 });
190 })
191 .then(() => {
192 clearInterval(appBuild);
193 done();
194 })
195 .catch(done);
Matteo Scandolob8c76b12016-05-11 14:53:20 -0700196 });
197
198 it('should have build the app with a css file', () => {
199 assert.file([destHtml, destJs, destCss]);
200 });
201
202 it('should include only minified files and minified deps in the index', () => {
203 assert.fileContent(destHtml, `<script src="/static/js/xos${fileName}.js"></script>`);
204 assert.fileContent(destHtml, `<link rel="stylesheet" href="/static/css/xos${fileName}.css">`);
205 assert.noFileContent(destHtml, `<!-- bower:css -->`);
206 assert.noFileContent(destHtml, `<!-- bower:js -->`);
207
208 assert.fileContent(destCss, `background:#337ab7`);
209 });
210 });
211
212 after(done => {
213 // deleting the folder used for test
214 deleteFile(destHtml);
215 deleteFile(destJs);
216 deleteFile(destVendor);
217 deleteFile(destCss);
218 rimraf(sourcePath, {}, done);
219 });
220});