From e3a7b74284e20f12ddcf27500d9c0920d7a769c6 Mon Sep 17 00:00:00 2001 From: Anthony Laibe Date: Wed, 19 Dec 2018 10:14:32 +0000 Subject: [PATCH] feat: apply contract change to test Give the same ability to config function on test than when running with embark run --- src/lib/core/config.js | 41 ++++--------------------------- src/lib/modules/tests/test.js | 5 +++- src/lib/utils/contractsConfig.ts | 42 ++++++++++++++++++++++++++++++++ src/lib/utils/regexConstants.ts | 1 + 4 files changed, 52 insertions(+), 37 deletions(-) create mode 100644 src/lib/utils/contractsConfig.ts create mode 100644 src/lib/utils/regexConstants.ts diff --git a/src/lib/core/config.js b/src/lib/core/config.js index 43c70d35b..49f2a78af 100644 --- a/src/lib/core/config.js +++ b/src/lib/core/config.js @@ -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; @@ -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)) { diff --git a/src/lib/modules/tests/test.js b/src/lib/modules/tests/test.js index de24a1e80..8f88f0236 100644 --- a/src/lib/modules/tests/test.js +++ b/src/lib/modules/tests/test.js @@ -1,3 +1,5 @@ +import * as utilsContractsConfig from "../../utils/contractsConfig"; + const async = require('async'); const AccountParser = require('../../utils/accountParser'); const EmbarkJS = require('embarkjs'); @@ -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); }, diff --git a/src/lib/utils/contractsConfig.ts b/src/lib/utils/contractsConfig.ts new file mode 100644 index 000000000..e12b924a2 --- /dev/null +++ b/src/lib/utils/contractsConfig.ts @@ -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; +} diff --git a/src/lib/utils/regexConstants.ts b/src/lib/utils/regexConstants.ts new file mode 100644 index 000000000..40be97c0a --- /dev/null +++ b/src/lib/utils/regexConstants.ts @@ -0,0 +1 @@ +export const unitRegex = /([0-9]+) ([a-zA-Z]+)/;