Matteo Scandolo | d2044a4 | 2017-08-07 16:08:28 -0700 | [diff] [blame] | 1 | |
| 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 Scandolo | b8c76b1 | 2016-05-11 14:53:20 -0700 | [diff] [blame] | 19 | 'use strict'; |
| 20 | var path = require('path'); |
| 21 | var helpers = require('yeoman-test'); |
| 22 | var assert = require('yeoman-assert'); |
Matteo Scandolo | b70db48 | 2016-07-06 10:39:36 -0700 | [diff] [blame] | 23 | var P = require('bluebird'); |
| 24 | var execAsync = P.promisify(require('child_process').exec); |
Matteo Scandolo | b8c76b1 | 2016-05-11 14:53:20 -0700 | [diff] [blame] | 25 | var fs = require('fs'); |
Matteo Scandolo | b70db48 | 2016-07-06 10:39:36 -0700 | [diff] [blame] | 26 | var writeFileAsync = P.promisify(fs.writeFile); |
Matteo Scandolo | b8c76b1 | 2016-05-11 14:53:20 -0700 | [diff] [blame] | 27 | const rimraf = require('rimraf'); |
| 28 | |
| 29 | const getMillisec = min => min * 60 * 1000; |
| 30 | const deleteFile = file => { |
| 31 | if(fs.existsSync(file)){ |
| 32 | // console.log(`deleting: ${file}`); |
| 33 | fs.unlinkSync(file); |
| 34 | } |
Matteo Scandolo | b70db48 | 2016-07-06 10:39:36 -0700 | [diff] [blame] | 35 | }; |
Matteo Scandolo | b8c76b1 | 2016-05-11 14:53:20 -0700 | [diff] [blame] | 36 | |
Matteo Scandolo | 4ac9a0b | 2016-05-23 15:31:25 -0700 | [diff] [blame] | 37 | // config files |
| 38 | const cfg = path.join(__dirname, `../../../env/default.js`); |
| 39 | |
Matteo Scandolo | b8c76b1 | 2016-05-11 14:53:20 -0700 | [diff] [blame] | 40 | // source files |
| 41 | const viewName = 'testDashboard'; |
| 42 | const fileName = viewName.replace(/^./, viewName[0].toUpperCase()); |
| 43 | const sourcePath = path.join(__dirname, `../../../ngXosViews/${viewName}/`); |
| 44 | |
| 45 | // dest files |
| 46 | const basePath = '../../../../xos/core/xoslib'; |
| 47 | const destHtml = path.join(__dirname, basePath + '/dashboards/xosTestDashboard.html'); |
| 48 | const destJs = path.join(__dirname, basePath + '/static/js/xosTestDashboard.js'); |
Matteo Scandolo | 195dde9 | 2016-07-25 16:43:16 -0700 | [diff] [blame] | 49 | const destVendor = path.join(__dirname, basePath + '/static/vendor/xosTestDashboardVendor.js'); |
Matteo Scandolo | b8c76b1 | 2016-05-11 14:53:20 -0700 | [diff] [blame] | 50 | const destCss = path.join(__dirname, basePath + '/static/css/xosTestDashboard.css'); |
| 51 | |
| 52 | describe('The XOS Build script', function(){ |
| 53 | const buildCmd = 'gulp build'; |
| 54 | |
| 55 | this.timeout(getMillisec(5)); |
| 56 | |
Matteo Scandolo | b70db48 | 2016-07-06 10:39:36 -0700 | [diff] [blame] | 57 | // define timers (give a feedback while commands are running |
| 58 | let npmInstall, bowerInstall, appBuild; |
| 59 | |
Matteo Scandolo | b8c76b1 | 2016-05-11 14:53:20 -0700 | [diff] [blame] | 60 | before(done => { |
Matteo Scandolo | 4ac9a0b | 2016-05-23 15:31:25 -0700 | [diff] [blame] | 61 | // 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 Scandolo | b8c76b1 | 2016-05-11 14:53:20 -0700 | [diff] [blame] | 67 | 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 Scandolo | b70db48 | 2016-07-06 10:39:36 -0700 | [diff] [blame] | 78 | .toPromise() |
| 79 | .then(() => { |
| 80 | //.on('end', () => { |
Matteo Scandolo | b8c76b1 | 2016-05-11 14:53:20 -0700 | [diff] [blame] | 81 | process.stdout.write('Installing Node Modules'); |
Matteo Scandolo | b70db48 | 2016-07-06 10:39:36 -0700 | [diff] [blame] | 82 | npmInstall = setInterval(() => { |
Matteo Scandolo | b8c76b1 | 2016-05-11 14:53:20 -0700 | [diff] [blame] | 83 | process.stdout.write('.'); |
| 84 | }, 1000); |
Matteo Scandolo | b70db48 | 2016-07-06 10:39:36 -0700 | [diff] [blame] | 85 | 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 Scandolo | b8c76b1 | 2016-05-11 14:53:20 -0700 | [diff] [blame] | 102 | }); |
| 103 | |
| 104 | describe('when no styles or vendors are added', () => { |
| 105 | |
| 106 | before((done) => { |
| 107 | process.stdout.write('\nBuilding App'); |
Matteo Scandolo | b70db48 | 2016-07-06 10:39:36 -0700 | [diff] [blame] | 108 | appBuild = setInterval(() => { |
Matteo Scandolo | b8c76b1 | 2016-05-11 14:53:20 -0700 | [diff] [blame] | 109 | process.stdout.write('.'); |
| 110 | }, 1000); |
Matteo Scandolo | b70db48 | 2016-07-06 10:39:36 -0700 | [diff] [blame] | 111 | execAsync(buildCmd, {cwd: sourcePath}) |
| 112 | .then(() => { |
Matteo Scandolo | b8c76b1 | 2016-05-11 14:53:20 -0700 | [diff] [blame] | 113 | clearInterval(appBuild); |
Matteo Scandolo | b70db48 | 2016-07-06 10:39:36 -0700 | [diff] [blame] | 114 | done(); |
| 115 | }) |
| 116 | .catch(done); |
Matteo Scandolo | b8c76b1 | 2016-05-11 14:53:20 -0700 | [diff] [blame] | 117 | }); |
| 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 Scandolo | b70db48 | 2016-07-06 10:39:36 -0700 | [diff] [blame] | 131 | |
Matteo Scandolo | b8c76b1 | 2016-05-11 14:53:20 -0700 | [diff] [blame] | 132 | before((done) => { |
Matteo Scandolo | b70db48 | 2016-07-06 10:39:36 -0700 | [diff] [blame] | 133 | process.stdout.write('\nInstalling 3rd party library'); |
Matteo Scandolo | b8c76b1 | 2016-05-11 14:53:20 -0700 | [diff] [blame] | 134 | let bowerInstall = setInterval(() => { |
| 135 | process.stdout.write('.'); |
| 136 | }, 1000); |
Matteo Scandolo | b70db48 | 2016-07-06 10:39:36 -0700 | [diff] [blame] | 137 | |
| 138 | execAsync('bower install moment --save', {cwd: sourcePath}) |
| 139 | .then(() => { |
Matteo Scandolo | b8c76b1 | 2016-05-11 14:53:20 -0700 | [diff] [blame] | 140 | clearInterval(bowerInstall); |
| 141 | process.stdout.write('\nBuilding App'); |
Matteo Scandolo | b70db48 | 2016-07-06 10:39:36 -0700 | [diff] [blame] | 142 | appBuild = setInterval(() => { |
Matteo Scandolo | b8c76b1 | 2016-05-11 14:53:20 -0700 | [diff] [blame] | 143 | process.stdout.write('.'); |
| 144 | }, 1000); |
Matteo Scandolo | b70db48 | 2016-07-06 10:39:36 -0700 | [diff] [blame] | 145 | |
| 146 | return execAsync(buildCmd, {cwd: sourcePath}) |
| 147 | }) |
| 148 | .then(() => { |
| 149 | clearInterval(appBuild); |
| 150 | done(); |
| 151 | }) |
| 152 | .catch(done); |
Matteo Scandolo | b8c76b1 | 2016-05-11 14:53:20 -0700 | [diff] [blame] | 153 | }); |
| 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 Scandolo | 195dde9 | 2016-07-25 16:43:16 -0700 | [diff] [blame] | 161 | assert.fileContent(destHtml, `<script src="/static/vendor/xos${fileName}Vendor.js"></script>`); |
Matteo Scandolo | b8c76b1 | 2016-05-11 14:53:20 -0700 | [diff] [blame] | 162 | 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 Scandolo | b70db48 | 2016-07-06 10:39:36 -0700 | [diff] [blame] | 176 | |
| 177 | writeFileAsync(`${sourcePath}src/sass/main.scss`, styleContent) |
| 178 | .then(() => { |
Matteo Scandolo | b8c76b1 | 2016-05-11 14:53:20 -0700 | [diff] [blame] | 179 | process.stdout.write('\nBuilding the Application'); |
Matteo Scandolo | b70db48 | 2016-07-06 10:39:36 -0700 | [diff] [blame] | 180 | appBuild = setInterval(() => { |
Matteo Scandolo | b8c76b1 | 2016-05-11 14:53:20 -0700 | [diff] [blame] | 181 | process.stdout.write('.'); |
| 182 | }, 1000); |
Matteo Scandolo | b70db48 | 2016-07-06 10:39:36 -0700 | [diff] [blame] | 183 | |
| 184 | return execAsync('bower uninstall moment --save', {cwd: sourcePath}); |
| 185 | }) |
| 186 | .then(() => { |
| 187 | return execAsync(buildCmd, { |
Matteo Scandolo | b8c76b1 | 2016-05-11 14:53:20 -0700 | [diff] [blame] | 188 | cwd: sourcePath |
Matteo Scandolo | b70db48 | 2016-07-06 10:39:36 -0700 | [diff] [blame] | 189 | }); |
| 190 | }) |
| 191 | .then(() => { |
| 192 | clearInterval(appBuild); |
| 193 | done(); |
| 194 | }) |
| 195 | .catch(done); |
Matteo Scandolo | b8c76b1 | 2016-05-11 14:53:20 -0700 | [diff] [blame] | 196 | }); |
| 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 | }); |