make require and test work

This commit is contained in:
Jonathan Rainville 2018-05-29 16:41:55 -04:00
parent 74d76585db
commit 89e0d39742
5 changed files with 84 additions and 53 deletions

View File

@ -19,11 +19,7 @@ class DeployManager {
this.onlyCompile = options.onlyCompile !== undefined ? options.onlyCompile : false; this.onlyCompile = options.onlyCompile !== undefined ? options.onlyCompile : false;
} }
deployContracts(compileOnlyIfNeeded, done) { deployContracts(done) {
if (typeof compileOnlyIfNeeded === 'function') {
done = compileOnlyIfNeeded;
compileOnlyIfNeeded = false;
}
let self = this; let self = this;
if (self.blockchainConfig === {} || self.blockchainConfig.enabled === false) { if (self.blockchainConfig === {} || self.blockchainConfig.enabled === false) {
@ -34,13 +30,6 @@ class DeployManager {
async.waterfall([ async.waterfall([
function buildContracts(callback) { function buildContracts(callback) {
if (compileOnlyIfNeeded) {
if (self.contractsManager.compiledContracts && Object.keys(self.contractsManager.compiledContracts).length) {
// Contracts have already been compiled
return callback();
}
}
console.log('compiling');
self.contractsManager.deployOnlyOnConfig = self.deployOnlyOnConfig; // temporary, should refactor self.contractsManager.deployOnlyOnConfig = self.deployOnlyOnConfig; // temporary, should refactor
self.contractsManager.build(() => { self.contractsManager.build(() => {
callback(); callback();

View File

@ -56,7 +56,8 @@ module.exports = {
global.contract = function(describeName, callback) { global.contract = function(describeName, callback) {
return Mocha.describe(describeName, callback); return Mocha.describe(describeName, callback);
}; };
next();
test.init(next);
} }
], (err) => { ], (err) => {
if (err) { if (err) {

View File

@ -27,6 +27,8 @@ class Test {
constructor(options) { constructor(options) {
this.options = options || {}; this.options = options || {};
this.simOptions = this.options.simulatorOptions || {}; this.simOptions = this.options.simulatorOptions || {};
this.contracts = {};
this.web3 = new Web3(); this.web3 = new Web3();
if (this.simOptions.node) { if (this.simOptions.node) {
this.web3.setProvider(new this.web3.providers.HttpProvider(this.simOptions.node)); this.web3.setProvider(new this.web3.providers.HttpProvider(this.simOptions.node));
@ -57,6 +59,12 @@ class Test {
this.engine.startService("codeGenerator"); this.engine.startService("codeGenerator");
} }
init(callback) {
this.engine.contractsManager.build(() => {
callback();
});
}
config(options, callback) { config(options, callback) {
this.options = utils.recursiveMerge(this.options, options); this.options = utils.recursiveMerge(this.options, options);
this.simOptions = this.options.simulatorOptions || {}; this.simOptions = this.options.simulatorOptions || {};
@ -70,45 +78,76 @@ class Test {
}); });
} }
_deploy(config, cb) { _deploy(config, callback) {
const self = this; const self = this;
async.waterfall([ async.waterfall([
function getConfig(callback) { function getConfig(next) {
let _versions_default = self.engine.config.contractsConfig.versions; let _versions_default = self.engine.config.contractsConfig.versions;
self.engine.config.contractsConfig = {contracts: config.contracts, versions: _versions_default}; self.engine.config.contractsConfig = {contracts: config.contracts, versions: _versions_default};
callback();
},
function reloadConfig(callback) {
self.engine.events.emit(constants.events.contractConfigChanged, self.engine.config.contractsConfig); self.engine.events.emit(constants.events.contractConfigChanged, self.engine.config.contractsConfig);
callback(); next();
}, },
function deploy(callback) { function deploy(next) {
// self.engine.events.on('code-generator-ready', function () {
// self.engine.events.request('code-generator:contract:vanilla', function(vanillaABI) {
// console.log(vanillaABI);
// });
// });
self.engine.deployManager.gasLimit = 6000000; self.engine.deployManager.gasLimit = 6000000;
self.engine.contractsManager.gasLimit = 6000000; self.engine.contractsManager.gasLimit = 6000000;
self.engine.deployManager.fatalErrors = true; self.engine.deployManager.fatalErrors = true;
self.engine.deployManager.deployOnlyOnConfig = true; self.engine.deployManager.deployOnlyOnConfig = true;
self.engine.deployManager.deployContracts(true, function(err, _result) { self.engine.deployManager.deployContracts(function (err) {
if (err) { if (err) {
callback(err); return next(err);
}
next();
});
},
function getAccounts(next) {
self.web3.eth.getAccounts(function(err, accounts) {
if (err) {
return next(err);
}
self.accounts = accounts;
self.web3.eth.defaultAccount = accounts[0];
next();
});
},
function createContractObject(next) {
async.each(Object.keys(self.contracts), (contractName, eachCb) => {
const contract = self.engine.contractsManager.contracts[contractName];
self.contracts[contractName].contract = new self.web3.eth.Contract(contract.abiDefinition, contract.address,
{from: self.web3.defaultAccount, gas: 6000000});
eachCb();
}, next);
}
], function (err) {
if (err) {
console.log(__('terminating due to error'));
throw new Error(err);
} }
callback(); callback();
}); });
} }
], function(err) {
if (err) { require(module) {
console.log(__('terminating due to error')); if (module.startsWith('contracts/')) {
cb(err); const contractName = module.substr(10);
if (!this.engine.contractsManager.contracts[contractName]) {
throw new Error(__('No contract with the name %s', contractName));
} }
// this should be part of the waterfall and not just something done at the if (this.contracts[contractName]) {
// end return this.contracts[contractName];
self.web3.eth.getAccounts(function(err, accounts) {
if (err) {
throw new Error(err);
} }
self.web3.eth.defaultAccount = accounts[0]; const contract = this.engine.contractsManager.contracts[contractName];
cb(null, accounts); this.contracts[contractName] = {};
}); // TODO find better way to update contract
}); this.contracts[contractName].contract = new this.web3.eth.Contract(contract.abiDefinition, contract.address);
return this.contracts[contractName];
}
throw new Error(__('Unknown module %s', module));
} }
} }

View File

@ -1,31 +1,33 @@
/*global contract, before, it, embark, web3*/
const assert = require('assert');
const SimpleStorage = embark.require('contracts/SimpleStorage');
contract("SimpleStorage", function() { contract("SimpleStorage", function () {
this.timeout(0);
before(function(done) {
this.timeout(0); this.timeout(0);
//config({ before(function (done) {
// node: "http://localhost:8545" const contractsConfig = {
//}); contracts: {
var contractsConfig = {
"SimpleStorage": { "SimpleStorage": {
args: [100] args: [100]
} }
}
}; };
EmbarkSpec.deployAll(contractsConfig, () => { done() }); embark.config(contractsConfig, () => {
done();
});
}); });
it("should set constructor value", async function() { it("should set constructor value", async function () {
let result = await SimpleStorage.methods.storedData().call(); let result = await SimpleStorage.contract.methods.storedData().call();
assert.equal(result, 100); assert.strictEqual(parseInt(result, 10), 100);
}); });
it("set storage value", async function() { it("set storage value", async function () {
await SimpleStorage.methods.set(150).send(); // TODO Solve from
let result = await SimpleStorage.methods.get().call(); await SimpleStorage.contract.methods.set(150).send({from: web3.eth.defaultAccount});
assert.equal(result, 499650); let result = await SimpleStorage.contract.methods.get().call();
assert.strictEqual(parseInt(result, 10), 499650);
}); });
}); });