Matteo Scandolo | 9069d59 | 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'); |
| 5 | var exec = require('child_process').exec; |
| 6 | var fs = require('fs'); |
| 7 | const rimraf = require('rimraf'); |
| 8 | |
| 9 | const getMillisec = min => min * 60 * 1000; |
| 10 | const deleteFile = file => { |
| 11 | if(fs.existsSync(file)){ |
| 12 | // console.log(`deleting: ${file}`); |
| 13 | fs.unlinkSync(file); |
| 14 | } |
| 15 | } |
| 16 | |
Matteo Scandolo | 881fed5 | 2016-05-23 15:31:25 -0700 | [diff] [blame] | 17 | // config files |
| 18 | const cfg = path.join(__dirname, `../../../env/default.js`); |
| 19 | |
Matteo Scandolo | 9069d59 | 2016-05-11 14:53:20 -0700 | [diff] [blame] | 20 | // source files |
| 21 | const viewName = 'testDashboard'; |
| 22 | const fileName = viewName.replace(/^./, viewName[0].toUpperCase()); |
| 23 | const sourcePath = path.join(__dirname, `../../../ngXosViews/${viewName}/`); |
| 24 | |
| 25 | // dest files |
| 26 | const basePath = '../../../../xos/core/xoslib'; |
| 27 | const destHtml = path.join(__dirname, basePath + '/dashboards/xosTestDashboard.html'); |
| 28 | const destJs = path.join(__dirname, basePath + '/static/js/xosTestDashboard.js'); |
| 29 | const destVendor = path.join(__dirname, basePath + '/static/js/vendor/xosTestDashboardVendor.js'); |
| 30 | const destCss = path.join(__dirname, basePath + '/static/css/xosTestDashboard.css'); |
| 31 | |
| 32 | describe('The XOS Build script', function(){ |
| 33 | const buildCmd = 'gulp build'; |
| 34 | |
| 35 | this.timeout(getMillisec(5)); |
| 36 | |
| 37 | before(done => { |
Matteo Scandolo | 881fed5 | 2016-05-23 15:31:25 -0700 | [diff] [blame] | 38 | // if `default.js` config is not present |
| 39 | // create one (we check to avoid screwing up local envs) |
| 40 | if(!fs.existsSync(cfg)){ |
| 41 | fs.writeFileSync(cfg, 'module.exports = {}'); |
| 42 | } |
| 43 | |
Matteo Scandolo | 9069d59 | 2016-05-11 14:53:20 -0700 | [diff] [blame] | 44 | console.log('Running generator'); |
| 45 | this.generator = helpers |
| 46 | .run(require.resolve('../app')) |
| 47 | .inDir(sourcePath) |
| 48 | .withOptions({ 'skip-install': false }) |
| 49 | .withPrompts({ |
| 50 | name: viewName, |
| 51 | host: 'test-host', |
| 52 | token: 'test-token', |
| 53 | session: 'test-session' |
| 54 | }) |
| 55 | .on('end', () => { |
| 56 | process.stdout.write('Installing Node Modules'); |
| 57 | let npmInstall = setInterval(() => { |
| 58 | process.stdout.write('.'); |
| 59 | }, 1000); |
| 60 | exec('npm install', { |
| 61 | cwd: sourcePath |
| 62 | }, (err) => { |
| 63 | clearInterval(npmInstall); |
| 64 | process.stdout.write('\nInstalling Bower Components'); |
| 65 | let bowerInstall = setInterval(() => { |
| 66 | process.stdout.write('.'); |
| 67 | }, 1000); |
| 68 | exec('bower install', { |
| 69 | cwd: sourcePath |
| 70 | }, (err) => { |
| 71 | clearInterval(bowerInstall); |
| 72 | done(err); |
| 73 | }); |
| 74 | }); |
| 75 | }); |
| 76 | }); |
| 77 | |
| 78 | describe('when no styles or vendors are added', () => { |
| 79 | |
| 80 | before((done) => { |
| 81 | process.stdout.write('\nBuilding App'); |
| 82 | let appBuild = setInterval(() => { |
| 83 | process.stdout.write('.'); |
| 84 | }, 1000); |
| 85 | exec(buildCmd, { |
| 86 | cwd: sourcePath |
| 87 | }, (err) => { |
| 88 | console.log(err); |
| 89 | clearInterval(appBuild); |
| 90 | done(err); |
| 91 | }); |
| 92 | }); |
| 93 | |
| 94 | it('should have build the app', () => { |
| 95 | assert.file([destHtml, destJs]); |
| 96 | }); |
| 97 | |
| 98 | it('should include only minified files in the index', () => { |
| 99 | assert.fileContent(destHtml, `<script src="/static/js/xos${fileName}.js"></script>`); |
| 100 | assert.noFileContent(destHtml, `<!-- bower:css -->`); |
| 101 | assert.noFileContent(destHtml, `<!-- bower:js -->`); |
| 102 | }); |
| 103 | }); |
| 104 | |
| 105 | describe('when a third party library is added', () => { |
| 106 | before((done) => { |
| 107 | process.stdout.write('\nInstalling 3rd party library'); |
| 108 | let bowerInstall = setInterval(() => { |
| 109 | process.stdout.write('.'); |
| 110 | }, 1000); |
| 111 | exec('bower install d3 --save', { |
| 112 | cwd: sourcePath |
| 113 | }, (err, out) => { |
| 114 | clearInterval(bowerInstall); |
| 115 | process.stdout.write('\nBuilding App'); |
| 116 | let appBuild = setInterval(() => { |
| 117 | process.stdout.write('.'); |
| 118 | }, 1000); |
| 119 | exec(buildCmd, { |
| 120 | cwd: sourcePath |
| 121 | }, (err) => { |
| 122 | console.log(err); |
| 123 | clearInterval(appBuild); |
| 124 | done(err); |
| 125 | }); |
| 126 | }); |
| 127 | }); |
| 128 | |
| 129 | it('should have build the app with a vendor file', () => { |
| 130 | assert.file([destHtml, destJs, destVendor]); |
| 131 | }); |
| 132 | |
| 133 | it('should include only minified files and minified deps in the index', () => { |
| 134 | assert.fileContent(destHtml, `<script src="/static/js/xos${fileName}.js"></script>`); |
| 135 | assert.fileContent(destHtml, `<script src="/static/js/vendor/xos${fileName}Vendor.js"></script>`); |
| 136 | assert.noFileContent(destHtml, `<!-- bower:css -->`); |
| 137 | assert.noFileContent(destHtml, `<!-- bower:js -->`); |
| 138 | }); |
| 139 | }); |
| 140 | |
| 141 | describe('when some styles are added', () => { |
| 142 | before((done) => { |
| 143 | let styleContent = ` |
| 144 | @import '../../../../style/sass/lib/_variables.scss'; |
| 145 | |
| 146 | #xosTestDashboard { |
| 147 | background: $brand-primary; |
| 148 | } |
| 149 | `; |
| 150 | |
| 151 | fs.writeFile(`${sourcePath}src/sass/main.scss`, styleContent, function(err) { |
| 152 | process.stdout.write('\nBuilding the Application'); |
| 153 | let appBuild = setInterval(() => { |
| 154 | process.stdout.write('.'); |
| 155 | }, 1000); |
| 156 | exec('bower uninstall d3 --save', { |
| 157 | cwd: sourcePath |
| 158 | }, (err, out) => { |
| 159 | exec(buildCmd, { |
| 160 | cwd: sourcePath |
| 161 | }, (err, out) => { |
| 162 | clearInterval(appBuild); |
| 163 | done(); |
| 164 | }) |
| 165 | }) |
| 166 | }); |
| 167 | }); |
| 168 | |
| 169 | it('should have build the app with a css file', () => { |
| 170 | assert.file([destHtml, destJs, destCss]); |
| 171 | }); |
| 172 | |
| 173 | it('should include only minified files and minified deps in the index', () => { |
| 174 | assert.fileContent(destHtml, `<script src="/static/js/xos${fileName}.js"></script>`); |
| 175 | assert.fileContent(destHtml, `<link rel="stylesheet" href="/static/css/xos${fileName}.css">`); |
| 176 | assert.noFileContent(destHtml, `<!-- bower:css -->`); |
| 177 | assert.noFileContent(destHtml, `<!-- bower:js -->`); |
| 178 | |
| 179 | assert.fileContent(destCss, `background:#337ab7`); |
| 180 | }); |
| 181 | }); |
| 182 | |
| 183 | after(done => { |
| 184 | // deleting the folder used for test |
| 185 | deleteFile(destHtml); |
| 186 | deleteFile(destJs); |
| 187 | deleteFile(destVendor); |
| 188 | deleteFile(destCss); |
| 189 | rimraf(sourcePath, {}, done); |
| 190 | }); |
| 191 | }); |