reformat run_test

This commit is contained in:
Jonathan Rainville 2018-05-29 09:24:45 -04:00
parent 4669922090
commit 723a4ae805

View File

@ -1,50 +1,57 @@
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');
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);
});
}
module.exports = { module.exports = {
run: function(filepath) { run: function(filePath) {
const Mocha = require('mocha'),
fs = require('fs-extra'),
path = require('path');
const mocha = new Mocha(); const mocha = new Mocha();
if (!filePath) {
if (filepath) { filePath = 'test/';
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)
);
});
}
let Test = require('./test.js');
global.assert = require('assert');
let configOptions = { let configOptions = {
gasPrice: 1 gasPrice: 1
}; };
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) {
// ---------------- Deprecated code ------------------------------------------------------------
global.assert = require('assert');
global.config = function(config) { global.config = function(config) {
configOptions = utils.recursiveMerge(configOptions, config); configOptions = utils.recursiveMerge(configOptions, config);
}; };
@ -58,7 +65,14 @@ module.exports = {
global.contract = function(describeName, callback) { global.contract = function(describeName, callback) {
return Mocha.describe(describeName, callback); return Mocha.describe(describeName, callback);
}; };
next();
// ---------------- Deprecated code ------------------------------------------------------------
}
], (err) => {
if (err) {
console.error(err);
process.exit(1);
}
// Run the tests. // Run the tests.
let runner = mocha.run(function(failures) { let runner = mocha.run(function(failures) {
// Clean contracts folder for next test run // Clean contracts folder for next test run
@ -75,6 +89,6 @@ module.exports = {
global.EmbarkSpec = new Test({simulatorOptions: configOptions}); global.EmbarkSpec = new Test({simulatorOptions: configOptions});
global.web3 = global.EmbarkSpec.web3; global.web3 = global.EmbarkSpec.web3;
}); });
});
} }
}; };