embark-area-51/lib/tests/run_tests.js

224 lines
6.9 KiB
JavaScript
Raw Normal View History

2018-05-29 13:24:45 +00:00
const async = require('async');
const Mocha = require('mocha');
const path = require('path');
2018-09-13 18:30:15 +00:00
const {runCmd} = require('../utils/utils');
const fs = require('../core/fs');
2018-06-06 15:00:30 +00:00
const assert = require('assert');
2018-06-01 17:33:11 +00:00
const Test = require('./test');
2018-06-28 18:01:44 +00:00
const EmbarkSpec = require('./reporter');
const SolcTest = require('./solc_test');
2017-07-02 04:27:14 +00:00
2018-05-29 13:24:45 +00:00
function getFilesFromDir(filePath, cb) {
fs.stat(filePath, (err, fileStat) => {
const errorMessage = `File "${filePath}" doesn't exist or you don't have permission to it`.red;
2018-05-29 13:24:45 +00:00
if (err) {
return cb(errorMessage);
}
let isDirectory = fileStat.isDirectory();
if (isDirectory) {
return fs.readdir(filePath, (err, files) => {
if (err) {
return cb(err);
}
async.map(files, (file, _cb) => {
getFilesFromDir(path.join(filePath, file), _cb);
}, (err, arr) => {
if (err) {
return cb(errorMessage);
}
cb(null, arr.reduce((a,b) => a.concat(b), []));
});
});
}
cb(null, [filePath]);
});
}
2018-05-29 13:24:45 +00:00
function runJSTests(files, options, cb) {
const loglevel = options.loglevel || 'warn';
async.waterfall([
function setupGlobalNamespace(next) {
// TODO put default config
const test = new Test({loglevel, node: options.node, coverage: options.coverage});
global.embark = test;
global.assert = assert;
global.config = test.config.bind(test);
2018-06-07 15:38:18 +00:00
let deprecatedWarning = function () {
console.error(__('%s are not supported anymore', 'EmbarkSpec & deployAll').red);
console.info(__('You can learn about the new revamped tests here: %s', 'https://embark.status.im/docs/testing.html'.underline));
process.exit();
};
2018-06-18 16:46:39 +00:00
global.deployAll = deprecatedWarning;
global.EmbarkSpec = {};
global.EmbarkSpec.deployAll = deprecatedWarning;
2018-06-07 20:14:42 +00:00
// Override require to enable `require('Embark/contracts/contractName');`
const Module = require('module');
const originalRequire = require('module').prototype.require;
Module.prototype.require = function (requireName) {
if (requireName.startsWith('Embark')) {
return test.require(...arguments);
}
return originalRequire.apply(this, arguments);
};
2017-07-03 22:15:43 +00:00
// TODO: this global here might not be necessary at all
global.web3 = global.embark.web3;
2018-06-01 17:43:43 +00:00
global.contract = function (describeName, callback) {
return Mocha.describe(describeName, callback);
};
test.init((err) => {
next(err, files);
});
},
function executeForAllFiles(files, next) {
let fns = files.map((file) => {
return (cb) => {
2018-06-01 17:44:49 +00:00
const mocha = new Mocha();
2018-06-28 20:14:34 +00:00
mocha.reporter(EmbarkSpec, {
events: global.embark.engine.events,
gasDetails: options.gasDetails,
2018-07-27 17:57:39 +00:00
gasLimit: 6000000
2018-06-28 20:14:34 +00:00
});
2018-09-17 13:12:20 +00:00
2018-06-01 17:44:49 +00:00
mocha.addFile(file);
mocha.suite.timeout(0);
mocha.suite.beforeAll('Wait for deploy', (done) => {
if (global.embark.needConfig) {
global.config({});
}
global.embark.onReady((err) => {
done(err);
2018-06-01 17:44:49 +00:00
});
});
mocha.run(function (fails) {
2018-06-28 19:18:28 +00:00
mocha.suite.removeAllListeners();
2018-06-01 17:44:49 +00:00
// Mocha prints the error already
cb(null, fails);
2018-06-01 17:44:49 +00:00
});
};
});
async.series(fns, next);
}
], (err, runs) => {
if(err) {
return cb(err);
}
let failures = runs.reduce((acc, val) => acc + val, 0);
fs.remove('.embark/contracts', (_err) => {
cb(null, {failures});
});
});
}
function runSolidityTests(files, options, cb) {
console.log('Running solc tests');
const loglevel = options.loglevel || 'warn';
let solcTest = new SolcTest({loglevel, node: options.node});
global.embark = solcTest;
async.waterfall([
function initEngine(next) {
solcTest.init(next);
},
function setupTests(next) {
solcTest.setupTests(files, next);
},
function runTests(_reciepts ,cb) {
let fns = files.map((file) => {
return (cb) => {
return solcTest.runTests(file, cb);
};
});
async.series(fns, cb);
}
], (err, results) => {
if(err) return cb(err);
let totalPass = 0;
let totalFailures = 0;
results.forEach((result) => {
result.forEach((r) => {
totalPass = totalPass + r.passingNum;
totalFailures = totalFailures + r.failureNum;
});
});
cb(null, {failures: totalFailures, pass: totalPass});
});
}
module.exports = {
run: function (options) {
let filePath = options.file;
if (!filePath) {
filePath = 'test';
}
async.waterfall([
function getFiles(next) {
getFilesFromDir(filePath, next);
},
function groupFiles(files, next) {
let jsFiles = files.filter((filename) => filename.substr(-3) === '.js');
let solidityFiles = files.filter((filename) => filename.indexOf('_test.sol') > 0);
next(null, {jsFiles, solidityFiles});
},
function runTests(files, next) {
const fns = [];
if (!options.solc && files.jsFiles.length > 0) {
let fn = (callback) => {
runJSTests(files.jsFiles, options, callback);
};
fns.push(fn);
}
if(files.solidityFiles.length > 0) {
let fn = (callback) => {
runSolidityTests(files.solidityFiles, options, callback);
};
fns.push(fn);
}
if(fns.length === 0){
return next('No tests to run');
}
async.series(fns, next);
2018-09-13 18:30:15 +00:00
},
function runCoverage(results, next) {
2018-09-13 18:30:15 +00:00
if (!options.coverage) {
return next(null, results);
2018-09-13 18:30:15 +00:00
}
global.embark.events.emit('tests:finished', function() {
runCmd(`${fs.embarkPath('node_modules/.bin/istanbul')} report --root .embark --format html`,
{silent: false, exitOnError: false}, (err) => {
if (err) {
return next(err);
}
2018-09-17 19:25:00 +00:00
console.log(`Coverage report created. You can find it here: ${fs.dappPath('coverage/__root__/index.html')}\n`);
const opn = require('opn');
const _next = () => { next(null, results); };
if (options.noBrowser) {
return next(null, results);
}
2018-09-17 14:37:24 +00:00
opn(fs.dappPath('coverage/__root__/index.html'), {wait: false})
.then(() => new Promise(resolve => setTimeout(resolve, 1000)))
.then(_next, _next);
});
});
2018-05-29 13:24:45 +00:00
}
], (err, results) => {
2018-05-29 13:24:45 +00:00
if (err) {
console.error(err);
process.exit(1);
}
let totalFailures = results.reduce((acc, result) => acc + result.failures, 0);
if (totalFailures) {
console.error(` > Total number of failures: ${totalFailures}`.red.bold);
2018-06-01 14:48:29 +00:00
} else {
console.log(' > All tests passed'.green.bold);
2018-06-01 17:44:49 +00:00
}
process.exit(totalFailures);
2017-07-02 04:27:14 +00:00
});
}
2017-07-02 22:03:14 +00:00
};