From a112f18d93603a0fcf8358287b45086bad31c772 Mon Sep 17 00:00:00 2001 From: Jonathan Rainville Date: Fri, 1 Jun 2018 10:05:43 -0400 Subject: [PATCH] always deploy contracts when in tests and make all test work --- lib/contracts/deploy.js | 4 +++ lib/i18n/i18n.js | 2 +- lib/tests/run_tests.js | 72 +++++++++++++++++++++++------------------ lib/tests/test.js | 18 +++++++---- package-lock.json | 31 ++++++++++++++++++ package.json | 1 + 6 files changed, 90 insertions(+), 38 deletions(-) diff --git a/lib/contracts/deploy.js b/lib/contracts/deploy.js index 462d7a81..9c6d6db0 100644 --- a/lib/contracts/deploy.js +++ b/lib/contracts/deploy.js @@ -99,6 +99,10 @@ class Deploy { return next(); } + // TODO find a better way to do that + if (process.env.isTest) { + return self.contractToDeploy(contract, params, next); + } // TODO: this should be a plugin API instead, if not existing, it should by default deploy the contract self.events.request("deploy:contract:shouldDeploy", contract, function(trackedContract) { if (!trackedContract) { diff --git a/lib/i18n/i18n.js b/lib/i18n/i18n.js index aa52a03f..ebe6562c 100644 --- a/lib/i18n/i18n.js +++ b/lib/i18n/i18n.js @@ -7,7 +7,7 @@ const supported_languages = ['en', 'pt', 'fr']; i18n.configure({ locales: supported_languages, register: global, - //updateFiles: false, + updateFiles: false, directory: path.join(__dirname, 'locales') }); diff --git a/lib/tests/run_tests.js b/lib/tests/run_tests.js index b12361ef..bd435bd9 100644 --- a/lib/tests/run_tests.js +++ b/lib/tests/run_tests.js @@ -22,28 +22,13 @@ function getFilesFromDir(filePath, cb) { module.exports = { run: function (filePath) { - const mocha = new Mocha(); + process.env.isTest = true; + let failures = 0; if (!filePath) { filePath = 'test/'; } 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) { // TODO put default config const test = new Test(); @@ -53,31 +38,56 @@ module.exports = { // TODO: this global here might not be necessary at all global.web3 = global.embark.web3; - mocha.suite.beforeEach('Wait for deploy', (done) => { - test.onReady(() => { - done(); - }); - }); global.contract = function (describeName, callback) { return Mocha.describe(describeName, callback); }; + console.info('Compiling contracts'.cyan); test.init(next); + }, + function getFiles(next) { + if (filePath.substr(-1) !== '/') { + return next(null, [filePath]); + } + getFilesFromDir(filePath, (err, files) => { + if (err) { + console.error('Error while reading the directory'); + return next(err); + } + next(null, files); + }); + }, + function executeForAllFiles(files, next) { + async.eachLimit(files, 1, (file, eachCb) => { + const mocha = new Mocha(); + mocha.addFile(file); + + mocha.suite.timeout(0); + mocha.suite.beforeEach('Wait for deploy', (done) => { + global.embark.onReady(() => { + done(); + }); + }); + + mocha.run(function(fails) { + failures += fails; + // Mocha prints the error already + eachCb(); + }); + }, next); } ], (err) => { if (err) { console.error(err); process.exit(1); } - // Run the tests. - 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(); - }); + if (failures) { + console.error(`Total number of failures: ${failures}`.red); + } + + // Clean contracts folder for next test run + fs.remove('.embark/contracts', (_err) => { + process.exit(failures); }); }); } diff --git a/lib/tests/test.js b/lib/tests/test.js index 3ac0b2d4..5269d84b 100644 --- a/lib/tests/test.js +++ b/lib/tests/test.js @@ -5,6 +5,7 @@ const Web3 = require('web3'); const utils = require('../utils/utils'); const constants = require('../constants'); const Events = require('../core/events'); +const cloneDeep = require('clone-deep'); function getSimulator() { try { @@ -63,7 +64,9 @@ class Test { } init(callback) { + const self = this; this.engine.contractsManager.build(() => { + self.builtContracts = cloneDeep(self.engine.contractsManager.contracts); callback(); }); } @@ -88,11 +91,14 @@ class Test { this.simOptions = this.options.simulatorOptions || {}; this.ready = false; + // Reset contracts + this.engine.contractsManager.contracts = cloneDeep(this.builtContracts); + this._deploy(options, (err, accounts) => { this.ready = true; this.events.emit('ready'); if (err) { - console.error(err); + console.error(err.red); return callback(err); } callback(null, accounts); @@ -131,11 +137,11 @@ class Test { }); }, function createContractObject(next) { - async.each(Object.keys(self.contracts), (contractName, eachCb) => { - if (!self.engine.contractsManager.contracts[contractName]) { - throw new Error(__('No contract with the name %s', contractName)); - } + async.each(Object.keys(self.engine.contractsManager.contracts), (contractName, eachCb) => { const contract = self.engine.contractsManager.contracts[contractName]; + if (!self.contracts[contractName]) { + self.contracts[contractName] = {}; + } Object.assign(self.contracts[contractName], new self.web3.eth.Contract(contract.abiDefinition, contract.address, {from: self.web3.eth.defaultAccount, gas: 6000000})); eachCb(); @@ -144,7 +150,7 @@ class Test { ], function (err) { if (err) { console.log(__('terminating due to error')); - throw new Error(err); + return callback(err); } callback(); }); diff --git a/package-lock.json b/package-lock.json index 8b444fe9..4b70a153 100644 --- a/package-lock.json +++ b/package-lock.json @@ -2409,6 +2409,22 @@ "resolved": "https://registry.npmjs.org/clone-buffer/-/clone-buffer-1.0.0.tgz", "integrity": "sha1-4+JbIHrE5wGvch4staFnksrD3Fg=" }, + "clone-deep": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/clone-deep/-/clone-deep-4.0.0.tgz", + "integrity": "sha512-aNJ5/7Bz2IYBb7nIj34TLGk78lBXpXUgV9qsLngtTvJ9+scsZNnlU0OX2S2N4ax/sUQt7sDBkXiGjGJEmNbXOQ==", + "requires": { + "kind-of": "6.0.2", + "shallow-clone": "3.0.0" + }, + "dependencies": { + "kind-of": { + "version": "6.0.2", + "resolved": "https://registry.npmjs.org/kind-of/-/kind-of-6.0.2.tgz", + "integrity": "sha512-s5kLOcnH0XqDO+FvuaLX8DDjZ18CGFk7VygH40QoKPUQhW4e2rvM0rwUq0t8IQDOwYSeLK01U90OjzBTme2QqA==" + } + } + }, "clone-response": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/clone-response/-/clone-response-1.0.2.tgz", @@ -11274,6 +11290,21 @@ "nan": "2.9.2" } }, + "shallow-clone": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/shallow-clone/-/shallow-clone-3.0.0.tgz", + "integrity": "sha512-Drg+nOI+ofeuslBf0nulyWLZhK1BZprqNvPJaiB4VvES+9gC6GG+qOVAfuO12zVSgxq9SKevcme7S3uDT6Be8w==", + "requires": { + "kind-of": "6.0.2" + }, + "dependencies": { + "kind-of": { + "version": "6.0.2", + "resolved": "https://registry.npmjs.org/kind-of/-/kind-of-6.0.2.tgz", + "integrity": "sha512-s5kLOcnH0XqDO+FvuaLX8DDjZ18CGFk7VygH40QoKPUQhW4e2rvM0rwUq0t8IQDOwYSeLK01U90OjzBTme2QqA==" + } + } + }, "shallow-copy": { "version": "0.0.1", "resolved": "https://registry.npmjs.org/shallow-copy/-/shallow-copy-0.0.1.tgz", diff --git a/package.json b/package.json index 05d9642c..3863fd2a 100644 --- a/package.json +++ b/package.json @@ -33,6 +33,7 @@ "bip39": "^2.5.0", "blessed": "^0.1.81", "chokidar": "^2.0.3", + "clone-deep": "^4.0.0", "colors": "^1.1.2", "commander": "^2.15.1", "css-loader": "^0.28.11",