Matteo Scandolo | b8c76b1 | 2016-05-11 14:53:20 -0700 | [diff] [blame] | 1 | 'use strict'; |
| 2 | var path = require('path'); |
| 3 | var helpers = require('yeoman-test'); |
| 4 | var assert = require('yeoman-assert'); |
Matteo Scandolo | b70db48 | 2016-07-06 10:39:36 -0700 | [diff] [blame] | 5 | var P = require('bluebird'); |
| 6 | var execAsync = P.promisify(require('child_process').exec); |
Matteo Scandolo | b8c76b1 | 2016-05-11 14:53:20 -0700 | [diff] [blame] | 7 | var fs = require('fs'); |
Matteo Scandolo | b70db48 | 2016-07-06 10:39:36 -0700 | [diff] [blame] | 8 | var writeFileAsync = P.promisify(fs.writeFile); |
Matteo Scandolo | b8c76b1 | 2016-05-11 14:53:20 -0700 | [diff] [blame] | 9 | const rimraf = require('rimraf'); |
| 10 | |
| 11 | const getMillisec = min => min * 60 * 1000; |
| 12 | const deleteFile = file => { |
| 13 | if(fs.existsSync(file)){ |
| 14 | // console.log(`deleting: ${file}`); |
| 15 | fs.unlinkSync(file); |
| 16 | } |
Matteo Scandolo | b70db48 | 2016-07-06 10:39:36 -0700 | [diff] [blame] | 17 | }; |
Matteo Scandolo | b8c76b1 | 2016-05-11 14:53:20 -0700 | [diff] [blame] | 18 | |
Matteo Scandolo | 4ac9a0b | 2016-05-23 15:31:25 -0700 | [diff] [blame] | 19 | // config files |
| 20 | const cfg = path.join(__dirname, `../../../env/default.js`); |
| 21 | |
Matteo Scandolo | b8c76b1 | 2016-05-11 14:53:20 -0700 | [diff] [blame] | 22 | // source files |
| 23 | const viewName = 'testDashboard'; |
| 24 | const fileName = viewName.replace(/^./, viewName[0].toUpperCase()); |
| 25 | const sourcePath = path.join(__dirname, `../../../ngXosViews/${viewName}/`); |
| 26 | |
| 27 | // dest files |
| 28 | const basePath = '../../../../xos/core/xoslib'; |
| 29 | const destHtml = path.join(__dirname, basePath + '/dashboards/xosTestDashboard.html'); |
| 30 | const destJs = path.join(__dirname, basePath + '/static/js/xosTestDashboard.js'); |
| 31 | const destVendor = path.join(__dirname, basePath + '/static/js/vendor/xosTestDashboardVendor.js'); |
| 32 | const destCss = path.join(__dirname, basePath + '/static/css/xosTestDashboard.css'); |
| 33 | |
| 34 | describe('The XOS Build script', function(){ |
| 35 | const buildCmd = 'gulp build'; |
| 36 | |
| 37 | this.timeout(getMillisec(5)); |
| 38 | |
Matteo Scandolo | b70db48 | 2016-07-06 10:39:36 -0700 | [diff] [blame] | 39 | // define timers (give a feedback while commands are running |
| 40 | let npmInstall, bowerInstall, appBuild; |
| 41 | |
Matteo Scandolo | b8c76b1 | 2016-05-11 14:53:20 -0700 | [diff] [blame] | 42 | before(done => { |
Matteo Scandolo | 4ac9a0b | 2016-05-23 15:31:25 -0700 | [diff] [blame] | 43 | // 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 Scandolo | b8c76b1 | 2016-05-11 14:53:20 -0700 | [diff] [blame] | 49 | 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 Scandolo | b70db48 | 2016-07-06 10:39:36 -0700 | [diff] [blame] | 60 | .toPromise() |
| 61 | .then(() => { |
| 62 | //.on('end', () => { |
Matteo Scandolo | b8c76b1 | 2016-05-11 14:53:20 -0700 | [diff] [blame] | 63 | process.stdout.write('Installing Node Modules'); |
Matteo Scandolo | b70db48 | 2016-07-06 10:39:36 -0700 | [diff] [blame] | 64 | npmInstall = setInterval(() => { |
Matteo Scandolo | b8c76b1 | 2016-05-11 14:53:20 -0700 | [diff] [blame] | 65 | process.stdout.write('.'); |
| 66 | }, 1000); |
Matteo Scandolo | b70db48 | 2016-07-06 10:39:36 -0700 | [diff] [blame] | 67 | 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 Scandolo | b8c76b1 | 2016-05-11 14:53:20 -0700 | [diff] [blame] | 84 | }); |
| 85 | |
| 86 | describe('when no styles or vendors are added', () => { |
| 87 | |
| 88 | before((done) => { |
| 89 | process.stdout.write('\nBuilding App'); |
Matteo Scandolo | b70db48 | 2016-07-06 10:39:36 -0700 | [diff] [blame] | 90 | appBuild = setInterval(() => { |
Matteo Scandolo | b8c76b1 | 2016-05-11 14:53:20 -0700 | [diff] [blame] | 91 | process.stdout.write('.'); |
| 92 | }, 1000); |
Matteo Scandolo | b70db48 | 2016-07-06 10:39:36 -0700 | [diff] [blame] | 93 | execAsync(buildCmd, {cwd: sourcePath}) |
| 94 | .then(() => { |
Matteo Scandolo | b8c76b1 | 2016-05-11 14:53:20 -0700 | [diff] [blame] | 95 | clearInterval(appBuild); |
Matteo Scandolo | b70db48 | 2016-07-06 10:39:36 -0700 | [diff] [blame] | 96 | done(); |
| 97 | }) |
| 98 | .catch(done); |
Matteo Scandolo | b8c76b1 | 2016-05-11 14:53:20 -0700 | [diff] [blame] | 99 | }); |
| 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 Scandolo | b70db48 | 2016-07-06 10:39:36 -0700 | [diff] [blame] | 113 | |
Matteo Scandolo | b8c76b1 | 2016-05-11 14:53:20 -0700 | [diff] [blame] | 114 | before((done) => { |
Matteo Scandolo | b70db48 | 2016-07-06 10:39:36 -0700 | [diff] [blame] | 115 | process.stdout.write('\nInstalling 3rd party library'); |
Matteo Scandolo | b8c76b1 | 2016-05-11 14:53:20 -0700 | [diff] [blame] | 116 | let bowerInstall = setInterval(() => { |
| 117 | process.stdout.write('.'); |
| 118 | }, 1000); |
Matteo Scandolo | b70db48 | 2016-07-06 10:39:36 -0700 | [diff] [blame] | 119 | |
| 120 | execAsync('bower install moment --save', {cwd: sourcePath}) |
| 121 | .then(() => { |
Matteo Scandolo | b8c76b1 | 2016-05-11 14:53:20 -0700 | [diff] [blame] | 122 | clearInterval(bowerInstall); |
| 123 | process.stdout.write('\nBuilding App'); |
Matteo Scandolo | b70db48 | 2016-07-06 10:39:36 -0700 | [diff] [blame] | 124 | appBuild = setInterval(() => { |
Matteo Scandolo | b8c76b1 | 2016-05-11 14:53:20 -0700 | [diff] [blame] | 125 | process.stdout.write('.'); |
| 126 | }, 1000); |
Matteo Scandolo | b70db48 | 2016-07-06 10:39:36 -0700 | [diff] [blame] | 127 | |
| 128 | return execAsync(buildCmd, {cwd: sourcePath}) |
| 129 | }) |
| 130 | .then(() => { |
| 131 | clearInterval(appBuild); |
| 132 | done(); |
| 133 | }) |
| 134 | .catch(done); |
Matteo Scandolo | b8c76b1 | 2016-05-11 14:53:20 -0700 | [diff] [blame] | 135 | }); |
| 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>`); |
| 143 | assert.fileContent(destHtml, `<script src="/static/js/vendor/xos${fileName}Vendor.js"></script>`); |
| 144 | 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 Scandolo | b70db48 | 2016-07-06 10:39:36 -0700 | [diff] [blame] | 158 | |
| 159 | writeFileAsync(`${sourcePath}src/sass/main.scss`, styleContent) |
| 160 | .then(() => { |
Matteo Scandolo | b8c76b1 | 2016-05-11 14:53:20 -0700 | [diff] [blame] | 161 | process.stdout.write('\nBuilding the Application'); |
Matteo Scandolo | b70db48 | 2016-07-06 10:39:36 -0700 | [diff] [blame] | 162 | appBuild = setInterval(() => { |
Matteo Scandolo | b8c76b1 | 2016-05-11 14:53:20 -0700 | [diff] [blame] | 163 | process.stdout.write('.'); |
| 164 | }, 1000); |
Matteo Scandolo | b70db48 | 2016-07-06 10:39:36 -0700 | [diff] [blame] | 165 | |
| 166 | return execAsync('bower uninstall moment --save', {cwd: sourcePath}); |
| 167 | }) |
| 168 | .then(() => { |
| 169 | return execAsync(buildCmd, { |
Matteo Scandolo | b8c76b1 | 2016-05-11 14:53:20 -0700 | [diff] [blame] | 170 | cwd: sourcePath |
Matteo Scandolo | b70db48 | 2016-07-06 10:39:36 -0700 | [diff] [blame] | 171 | }); |
| 172 | }) |
| 173 | .then(() => { |
| 174 | clearInterval(appBuild); |
| 175 | done(); |
| 176 | }) |
| 177 | .catch(done); |
Matteo Scandolo | b8c76b1 | 2016-05-11 14:53:20 -0700 | [diff] [blame] | 178 | }); |
| 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 | }); |