Merge pull request #5698 from NodeBB/build-tests

Tests for `./nodebb build`
v1.18.x
Barış Soner Uşaklı 8 years ago committed by GitHub
commit b2cef4846c

@ -17,3 +17,4 @@ logs/
/coverage /coverage
/build /build
.eslintrc .eslintrc
test/files

@ -8,6 +8,8 @@ var initWorker;
var incomplete = []; var incomplete = [];
var running = 0; var running = 0;
env.NODE_ENV = env.NODE_ENV || 'development';
module.exports = function (grunt) { module.exports = function (grunt) {
var args = []; var args = [];
var initArgs = ['--build']; var initArgs = ['--build'];

@ -180,6 +180,11 @@ function build(targets, callback) {
async.series([ async.series([
beforeBuild, beforeBuild,
function (next) { function (next) {
var threads = parseInt(nconf.get('threads'), 10);
if (threads) {
require('./minifier').maxThreads = threads - 1;
}
var parallel = !nconf.get('series'); var parallel = !nconf.get('series');
if (parallel) { if (parallel) {
winston.info('[build] Building in parallel mode'); winston.info('[build] Building in parallel mode');

@ -41,9 +41,21 @@ function setupDebugging() {
var pool = []; var pool = [];
var free = []; var free = [];
Minifier.maxThreads = os.cpus().length - 1; var maxThreads = 0;
Object.defineProperty(Minifier, 'maxThreads', {
get: function () {
return maxThreads;
},
set: function (val) {
maxThreads = val;
winston.verbose('[minifier] utilizing a maximum of ' + maxThreads + ' additional threads');
},
configurable: true,
enumerable: true,
});
winston.verbose('[minifier] utilizing a maximum of ' + Minifier.maxThreads + ' additional threads'); Minifier.maxThreads = os.cpus().length - 1;
Minifier.killAll = function () { Minifier.killAll = function () {
pool.forEach(function (child) { pool.forEach(function (child) {
@ -156,7 +168,7 @@ function concat(data, callback) {
return callback(err); return callback(err);
} }
var output = files.join(os.EOL + ';'); var output = files.join('\n;');
callback(null, { code: output }); callback(null, { code: output });
}); });

@ -1,15 +1,195 @@
'use strict'; 'use strict';
var string = require('string');
var path = require('path');
var fs = require('fs');
var assert = require('assert'); var assert = require('assert');
var mkdirp = require('mkdirp');
var rimraf = require('rimraf');
var db = require('./mocks/databasemock'); var db = require('./mocks/databasemock');
var file = require('../src/file');
describe('Build', function () { describe('minifier', function () {
it('should build all assets', function (done) { before(function (done) {
this.timeout(50000); mkdirp(path.join(__dirname, '../build/test'), done);
var build = require('../src/meta/build'); });
build.buildAll(function (err) {
var minifier = require('../src/meta/minifier');
var scripts = [
path.resolve(__dirname, './files/1.js'),
path.resolve(__dirname, './files/2.js'),
];
it('.js.bundle() should concat scripts', function (done) {
minifier.js.bundle(scripts, false, false, function (err, bundle) {
assert.ifError(err);
assert.strictEqual(
bundle.code,
'(function (window, document) {' +
'\n\twindow.doStuff = function () {' +
'\n\t\tdocument.body.innerHTML = \'Stuff has been done\';' +
'\n\t};' +
'\n})(window, document);' +
'\n' +
'\n;function foo(name, age) {' +
'\n\treturn \'The person known as "\' + name + \'" is \' + age + \' years old\';' +
'\n}' +
'\n'
);
done();
});
});
it('.js.bundle() should minify scripts', function (done) {
minifier.js.bundle(scripts, true, false, function (err, bundle) {
assert.ifError(err);
assert.strictEqual(
bundle.code,
'(function(n,o){n.doStuff=function(){o.body.innerHTML="Stuff has been done"}})(window,document);function foo(n,o){return\'The person known as "\'+n+\'" is \'+o+" years old"}'
);
done();
});
});
it('.js.minifyBatch() should minify each script', function (done) {
var s = scripts.map(function (script) {
return {
srcPath: script,
destPath: path.resolve(__dirname, '../build/test', path.basename(script)),
};
});
minifier.js.minifyBatch(s, false, function (err) {
assert.ifError(err);
assert(file.existsSync(s[0].destPath));
assert(file.existsSync(s[1].destPath));
fs.readFile(s[0].destPath, function (err, buffer) {
assert.ifError(err);
assert.strictEqual(
buffer.toString(),
'(function(n,o){n.doStuff=function(){o.body.innerHTML="Stuff has been done"}})(window,document);'
);
done();
});
});
});
var styles = [
'@import (inline) "./1.css";',
'@import "./2.less";',
].join('\n');
var paths = [
path.resolve(__dirname, './files'),
];
it('.css.bundle() should concat styles', function (done) {
minifier.css.bundle(styles, paths, false, false, function (err, bundle) {
assert.ifError(err);
assert.strictEqual(bundle.code, '.help { margin: 10px; } .yellow { background: yellow; }\n.help {\n display: block;\n}\n.help .blue {\n background: blue;\n}\n');
done();
});
});
it('.css.bundle() should minify styles', function (done) {
minifier.css.bundle(styles, paths, true, false, function (err, bundle) {
assert.ifError(err);
assert.strictEqual(bundle.code, '.help{margin:10px;display:block}.yellow{background:#ff0}.help .blue{background:#00f}');
done();
});
});
});
describe('Build', function (done) {
var build = require('../src/meta/build');
before(function (done) {
rimraf(path.join(__dirname, '../build/public'), done);
});
it('should build plugin static dirs', function (done) {
build.build(['plugin static dirs'], function (err) {
assert.ifError(err);
assert(file.existsSync(path.join(__dirname, '../build/public/plugins/nodebb-plugin-dbsearch/dbsearch')));
done();
});
});
it('should build requirejs modules', function (done) {
build.build(['requirejs modules'], function (err) {
assert.ifError(err);
var filename = path.join(__dirname, '../build/public/src/modules/Chart.js');
assert(file.existsSync(filename));
assert(fs.readFileSync(filename).toString().startsWith('/*!\n * Chart.js'));
done();
});
});
it('should build client js bundle', function (done) {
build.build(['client js bundle'], function (err) {
assert.ifError(err);
var filename = path.join(__dirname, '../build/public/nodebb.min.js');
assert(file.existsSync(filename));
assert(fs.readFileSync(filename).length > 1000);
done();
});
});
it('should build admin js bundle', function (done) {
build.build(['admin js bundle'], function (err) {
assert.ifError(err);
var filename = path.join(__dirname, '../build/public/acp.min.js');
assert(file.existsSync(filename));
assert(fs.readFileSync(filename).length > 1000);
done();
});
});
it('should build client side styles', function (done) {
build.build(['client side styles'], function (err) {
assert.ifError(err);
var filename = path.join(__dirname, '../build/public/stylesheet.css');
assert(file.existsSync(filename));
assert(fs.readFileSync(filename).toString().startsWith('/*! normalize.css'));
done();
});
});
it('should build admin control panel styles', function (done) {
build.build(['admin control panel styles'], function (err) {
assert.ifError(err);
var filename = path.join(__dirname, '../build/public/admin.css');
assert(file.existsSync(filename));
assert(fs.readFileSync(filename).toString().startsWith('@charset "UTF-8";'));
done();
});
});
it('should build templates', function (done) {
build.build(['templates'], function (err) {
assert.ifError(err);
var filename = path.join(__dirname, '../build/public/templates/admin/header.tpl');
assert(file.existsSync(filename));
assert(fs.readFileSync(filename).toString().startsWith('<!DOCTYPE html>'));
done();
});
});
it('should build languages', function (done) {
build.build(['languages'], function (err) {
assert.ifError(err);
var filename = path.join(__dirname, '../build/public/language/en-GB/global.json');
assert(file.existsSync(filename));
var global = fs.readFileSync(filename).toString();
assert.strictEqual(JSON.parse(global).home, 'Home');
done();
});
});
it('should build sounds', function (done) {
build.build(['sounds'], function (err) {
assert.ifError(err); assert.ifError(err);
var filename = path.join(__dirname, '../build/public/sounds/fileMap.json');
assert(file.existsSync(filename));
done(); done();
}); });
}); });

@ -0,0 +1 @@
.help { margin: 10px; } .yellow { background: yellow; }

@ -0,0 +1,5 @@
(function (window, document) {
window.doStuff = function () {
document.body.innerHTML = 'Stuff has been done';
};
})(window, document);

@ -0,0 +1,3 @@
function foo(name, age) {
return 'The person known as "' + name + '" is ' + age + ' years old';
}

@ -0,0 +1 @@
.help { display: block; .blue { background: blue; } }
Loading…
Cancel
Save