2018-05-29 13:24:45 +00:00
|
|
|
const async = require('async');
|
|
|
|
const fs = require('fs-extra');
|
|
|
|
const Mocha = require('mocha');
|
|
|
|
const path = require('path');
|
2018-06-01 17:33:11 +00:00
|
|
|
const Test = require('./test');
|
2017-07-02 04:27:14 +00:00
|
|
|
|
2018-05-29 13:24:45 +00:00
|
|
|
function getFilesFromDir(filePath, cb) {
|
|
|
|
fs.readdir(filePath, (err, files) => {
|
|
|
|
if (err) {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
const testFiles = files.filter((file) => {
|
|
|
|
// Only keep the .js files
|
|
|
|
// TODO: make this a configuration in embark.json
|
|
|
|
return file.substr(-3) === '.js';
|
|
|
|
}).map((file) => {
|
|
|
|
return path.join(filePath, file);
|
|
|
|
});
|
|
|
|
cb(null, testFiles);
|
|
|
|
});
|
|
|
|
}
|
2017-07-02 04:27:14 +00:00
|
|
|
|
2018-05-29 13:24:45 +00:00
|
|
|
module.exports = {
|
2018-05-30 20:17:17 +00:00
|
|
|
run: function (filePath) {
|
2018-04-18 18:56:18 +00:00
|
|
|
const mocha = new Mocha();
|
2018-05-29 13:24:45 +00:00
|
|
|
if (!filePath) {
|
|
|
|
filePath = 'test/';
|
2017-07-02 15:32:16 +00:00
|
|
|
}
|
2018-01-15 14:51:45 +00:00
|
|
|
|
2018-05-29 13:24:45 +00:00
|
|
|
async.waterfall([
|
|
|
|
function getFiles(next) {
|
|
|
|
if (filePath.substr(-1) !== '/') {
|
|
|
|
mocha.addFile(filePath);
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
getFilesFromDir(filePath, (err, files) => {
|
|
|
|
if (err) {
|
|
|
|
console.error('Error while reading the directory');
|
|
|
|
return next(err);
|
|
|
|
}
|
|
|
|
files.forEach(file => {
|
|
|
|
mocha.addFile(file);
|
|
|
|
});
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function setupGlobalNamespace(next) {
|
2018-06-01 17:33:11 +00:00
|
|
|
// TODO put default config
|
|
|
|
const test = new Test();
|
|
|
|
global.embark = test;
|
|
|
|
global.config = test.config.bind(test);
|
2018-05-29 13:24:45 +00:00
|
|
|
|
|
|
|
// TODO: this global here might not be necessary at all
|
2018-06-01 17:33:11 +00:00
|
|
|
global.web3 = global.embark.web3;
|
2017-07-03 22:15:43 +00:00
|
|
|
|
2018-05-30 20:17:17 +00:00
|
|
|
mocha.suite.beforeAll('Wait for deploy', (done) => {
|
|
|
|
test.onReady(() => {
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
global.contract = function (describeName, callback) {
|
2018-05-29 13:24:45 +00:00
|
|
|
return Mocha.describe(describeName, callback);
|
|
|
|
};
|
2018-06-01 17:35:15 +00:00
|
|
|
|
|
|
|
test.init(next);
|
2018-05-29 13:24:45 +00:00
|
|
|
}
|
|
|
|
], (err) => {
|
|
|
|
if (err) {
|
|
|
|
console.error(err);
|
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
// Run the tests.
|
2018-05-30 20:17:17 +00:00
|
|
|
mocha.run(function (failures) {
|
2018-05-29 13:24:45 +00:00
|
|
|
// Clean contracts folder for next test run
|
|
|
|
fs.remove('.embark/contracts', (_err) => {
|
|
|
|
process.on('exit', function () {
|
|
|
|
process.exit(failures); // exit with non-zero status if there were failures
|
|
|
|
});
|
|
|
|
process.exit();
|
2018-04-18 18:56:18 +00:00
|
|
|
});
|
2017-07-02 04:27:14 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2017-07-02 22:03:14 +00:00
|
|
|
};
|