reformat run_test

This commit is contained in:
Jonathan Rainville 2018-05-29 09:24:45 -04:00
parent 836fbfbc47
commit 67b8f76384

View File

@ -1,80 +1,94 @@
const async = require('async');
const fs = require('fs-extra');
const Mocha = require('mocha');
const path = require('path');
const Test = require('./test.js');
const utils = require('../utils/utils.js'); const utils = require('../utils/utils.js');
module.exports = { function getFilesFromDir(filePath, cb) {
run: function(filepath) { fs.readdir(filePath, (err, files) => {
const Mocha = require('mocha'), if (err) {
fs = require('fs-extra'), return cb(err);
path = require('path');
const mocha = new Mocha();
if (filepath) {
if (filepath.substr(-1) === '/') {
// Add each .js file to the mocha instance
fs.readdirSync(filepath).filter(function(file){
// Only keep the .js files
// TODO: make this a configuration in embark.json
return file.substr(-3) === '.js';
}).forEach(function(file){
mocha.addFile(
path.join(filepath, file)
);
});
} else {
mocha.addFile(filepath);
}
} else {
var testDir = 'test/';
// Add each .js file to the mocha instance
fs.readdirSync(testDir).filter(function(file){
// Only keep the .js files
// TODO: make this a configuration in embark.json
return file.substr(-3) === '.js';
}).forEach(function(file){
mocha.addFile(
path.join(testDir, file)
);
});
} }
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);
});
}
let Test = require('./test.js'); module.exports = {
run: function(filePath) {
global.assert = require('assert'); const mocha = new Mocha();
if (!filePath) {
filePath = 'test/';
}
let configOptions = { let configOptions = {
gasPrice: 1 gasPrice: 1
}; };
global.config = function(config) {
configOptions = utils.recursiveMerge(configOptions, config);
};
// TODO: check how to pass the options
//global.EmbarkSpec = new Test(options);
// TODO: this global here might not be necessary at all async.waterfall([
global.EmbarkSpec = new Test({}); function getFiles(next) {
global.web3 = global.EmbarkSpec.web3; if (filePath.substr(-1) !== '/') {
mocha.addFile(filePath);
global.contract = function(describeName, callback) { return next();
return Mocha.describe(describeName, callback); }
}; getFilesFromDir(filePath, (err, files) => {
if (err) {
// Run the tests. console.error('Error while reading the directory');
let runner = mocha.run(function(failures) { return next(err);
// Clean contracts folder for next test run }
fs.remove('.embark/contracts', (_err) => { files.forEach(file => {
process.on('exit', function () { mocha.addFile(file);
process.exit(failures); // exit with non-zero status if there were failures });
next();
}); });
process.exit(); },
function setupGlobalNamespace(next) {
// ---------------- Deprecated code ------------------------------------------------------------
global.assert = require('assert');
global.config = function(config) {
configOptions = utils.recursiveMerge(configOptions, config);
};
// TODO: check how to pass the options
//global.EmbarkSpec = new Test(options);
// TODO: this global here might not be necessary at all
global.EmbarkSpec = new Test({});
global.web3 = global.EmbarkSpec.web3;
global.contract = function(describeName, callback) {
return Mocha.describe(describeName, callback);
};
next();
// ---------------- Deprecated code ------------------------------------------------------------
}
], (err) => {
if (err) {
console.error(err);
process.exit(1);
}
// Run the tests.
let runner = mocha.run(function(failures) {
// 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();
});
});
runner.on('suite', function() {
global.assert = require('assert');
global.EmbarkSpec = new Test({simulatorOptions: configOptions});
global.web3 = global.EmbarkSpec.web3;
}); });
}); });
runner.on('suite', function() {
global.assert = require('assert');
global.EmbarkSpec = new Test({simulatorOptions: configOptions});
global.web3 = global.EmbarkSpec.web3;
});
} }
}; };