reformat run_test

This commit is contained in:
Jonathan Rainville 2018-05-29 09:24:45 -04:00
parent 01fc78059e
commit 68fb70f788
1 changed files with 81 additions and 67 deletions

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');
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 = {
run: function(filepath) {
const Mocha = require('mocha'),
fs = require('fs-extra'),
path = require('path');
run: function(filePath) {
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);
if (!filePath) {
filePath = 'test/';
}
} 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 = {
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) {
configOptions = utils.recursiveMerge(configOptions, config);
};
@ -58,7 +65,14 @@ module.exports = {
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
@ -75,6 +89,6 @@ module.exports = {
global.EmbarkSpec = new Test({simulatorOptions: configOptions});
global.web3 = global.EmbarkSpec.web3;
});
});
}
};