Skip to content

Commit

Permalink
feat: apply contract change to test
Browse files Browse the repository at this point in the history
Give the same ability to config function on test
than when running with embark run
  • Loading branch information
alaibe authored and iurimatias committed Dec 20, 2018
1 parent 3a8808e commit e3a7b74
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 37 deletions.
41 changes: 5 additions & 36 deletions src/lib/core/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ const web3 = require('web3');
const constants = require('../constants');
const {canonicalHost, defaultHost} = require('../utils/host');
const cloneDeep = require('lodash.clonedeep');
import { extendZeroAddressShorthand, replaceZeroAddressShorthand } from '../utils/addressUtils';
import { replaceZeroAddressShorthand } from '../utils/addressUtils';
import { unitRegex } from "../utils/regexConstants";
import * as utilsContractsConfig from "../utils/contractsConfig";

const DEFAULT_CONFIG_PATH = 'config/';
const unitRegex = /([0-9]+) ([a-zA-Z]+)/;

var Config = function(options) {
const self = this;
Expand Down Expand Up @@ -329,40 +330,8 @@ Config.prototype.loadContractsConfigFile = function() {
});
}

Object.keys(newContractsConfig.contracts).forEach(contractName => {

const gas = newContractsConfig.contracts[contractName].gas;
const gasPrice = newContractsConfig.contracts[contractName].gasPrice;
const address = newContractsConfig.contracts[contractName].address;
const args = newContractsConfig.contracts[contractName].args;
const onDeploy = newContractsConfig.contracts[contractName].onDeploy;

if (gas && gas.match(unitRegex)) {
newContractsConfig.contracts[contractName].gas = utils.getWeiBalanceFromString(gas, web3);
}

if (gasPrice && gasPrice.match(unitRegex)) {
newContractsConfig.contracts[contractName].gasPrice = utils.getWeiBalanceFromString(gasPrice, web3);
}

if (address) {
newContractsConfig.contracts[contractName].address = extendZeroAddressShorthand(address);
}

if (args && args.length) {
newContractsConfig.contracts[contractName].args = args.map(val => {
if (typeof val === 'string') {
return extendZeroAddressShorthand(val);
}
return val;
});
}

if (Array.isArray(onDeploy)) {
newContractsConfig.contracts[contractName].onDeploy = onDeploy.map(replaceZeroAddressShorthand);
}
});

newContractsConfig = utilsContractsConfig.prepare(newContractsConfig);

const afterDeploy = newContractsConfig.afterDeploy;

if (Array.isArray(afterDeploy)) {
Expand Down
5 changes: 4 additions & 1 deletion src/lib/modules/tests/test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import * as utilsContractsConfig from "../../utils/contractsConfig";

const async = require('async');
const AccountParser = require('../../utils/accountParser');
const EmbarkJS = require('embarkjs');
Expand Down Expand Up @@ -236,7 +238,8 @@ class Test {
async _deploy(config, callback) {
const self = this;
async.waterfall([
function getConfig(next) {
function setConfig(next) {
utilsContractsConfig.prepare(config);
self.events.request('config:contractsConfig:set',
{contracts: config.contracts, versions: self.versions_default}, next);
},
Expand Down
42 changes: 42 additions & 0 deletions src/lib/utils/contractsConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { extendZeroAddressShorthand, replaceZeroAddressShorthand } from "./addressUtils";
import { unitRegex } from "./regexConstants";

const web3 = require("web3");
const utils = require("./utils.js");

export function prepare(config: any) {
Object.keys(config.contracts).forEach((contractName) => {
const gas = config.contracts[contractName].gas;
const gasPrice = config.contracts[contractName].gasPrice;
const address = config.contracts[contractName].address;
const args = config.contracts[contractName].args;
const onDeploy = config.contracts[contractName].onDeploy;

if (gas && gas.match(unitRegex)) {
config.contracts[contractName].gas = utils.getWeiBalanceFromString(gas, web3);
}

if (gasPrice && gasPrice.match(unitRegex)) {
config.contracts[contractName].gasPrice = utils.getWeiBalanceFromString(gasPrice, web3);
}

if (address) {
config.contracts[contractName].address = extendZeroAddressShorthand(address);
}

if (args && args.length) {
config.contracts[contractName].args = args.map((val: any) => {
if (typeof val === "string") {
return extendZeroAddressShorthand(val);
}
return val;
});
}

if (Array.isArray(onDeploy)) {
config.contracts[contractName].onDeploy = onDeploy.map(replaceZeroAddressShorthand);
}
});

return config;
}
1 change: 1 addition & 0 deletions src/lib/utils/regexConstants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const unitRegex = /([0-9]+) ([a-zA-Z]+)/;

0 comments on commit e3a7b74

Please sign in to comment.