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;
}
deployContracts(compileOnlyIfNeeded, done) {
if (typeof compileOnlyIfNeeded === 'function') {
done = compileOnlyIfNeeded;
compileOnlyIfNeeded = false;
}
deployContracts(done) {
let self = this;
if (self.blockchainConfig === {} || self.blockchainConfig.enabled === false) {
@ -34,13 +30,6 @@ class DeployManager {
async.waterfall([
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.build(() => {
callback();

View File

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

View File

@ -27,6 +27,8 @@ class Test {
constructor(options) {
this.options = options || {};
this.simOptions = this.options.simulatorOptions || {};
this.contracts = {};
this.web3 = new Web3();
if (this.simOptions.node) {
this.web3.setProvider(new this.web3.providers.HttpProvider(this.simOptions.node));
@ -57,6 +59,12 @@ class Test {
this.engine.startService("codeGenerator");
}
init(callback) {
this.engine.contractsManager.build(() => {
callback();
});
}
config(options, callback) {
this.options = utils.recursiveMerge(this.options, options);
this.simOptions = this.options.simulatorOptions || {};
@ -70,46 +78,77 @@ class Test {
});
}
_deploy(config, cb) {
_deploy(config, callback) {
const self = this;
async.waterfall([
function getConfig(callback) {
function getConfig(next) {
let _versions_default = self.engine.config.contractsConfig.versions;
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);
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.contractsManager.gasLimit = 6000000;
self.engine.deployManager.fatalErrors = true;
self.engine.deployManager.deployOnlyOnConfig = true;
self.engine.deployManager.deployContracts(true, function(err, _result) {
self.engine.deployManager.deployContracts(function (err) {
if (err) {
callback(err);
return next(err);
}
callback();
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) {
], function (err) {
if (err) {
console.log(__('terminating due to error'));
cb(err);
throw new Error(err);
}
// this should be part of the waterfall and not just something done at the
// end
self.web3.eth.getAccounts(function(err, accounts) {
if (err) {
throw new Error(err);
}
self.web3.eth.defaultAccount = accounts[0];
cb(null, accounts);
});
callback();
});
}
require(module) {
if (module.startsWith('contracts/')) {
const contractName = module.substr(10);
if (!this.engine.contractsManager.contracts[contractName]) {
throw new Error(__('No contract with the name %s', contractName));
}
if (this.contracts[contractName]) {
return this.contracts[contractName];
}
const contract = this.engine.contractsManager.contracts[contractName];
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));
}
}
module.exports = Test;

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);
//config({
// node: "http://localhost:8545"
//});
var contractsConfig = {
"SimpleStorage": {
args: [100]
before(function (done) {
const contractsConfig = {
contracts: {
"SimpleStorage": {
args: [100]
}
}
};
EmbarkSpec.deployAll(contractsConfig, () => { done() });
embark.config(contractsConfig, () => {
done();
});
});
it("should set constructor value", async function() {
let result = await SimpleStorage.methods.storedData().call();
assert.equal(result, 100);
it("should set constructor value", async function () {
let result = await SimpleStorage.contract.methods.storedData().call();
assert.strictEqual(parseInt(result, 10), 100);
});
it("set storage value", async function() {
await SimpleStorage.methods.set(150).send();
let result = await SimpleStorage.methods.get().call();
assert.equal(result, 499650);
it("set storage value", async function () {
// TODO Solve from
await SimpleStorage.contract.methods.set(150).send({from: web3.eth.defaultAccount});
let result = await SimpleStorage.contract.methods.get().call();
assert.strictEqual(parseInt(result, 10), 499650);
});
});