add more tests; fix issue with selecting selecting non-default environment contract configuration

This commit is contained in:
Iuri Matias 2016-10-15 15:54:19 -04:00
parent 5a6a9e8b94
commit 23a7f03cc4
10 changed files with 246 additions and 13 deletions

View File

@ -1,17 +1,19 @@
var fs = require('fs');
var grunt = require('grunt');
var merge = require('merge');
// TODO: add wrapper for fs so it can also work in the browser
// can work with both read and save
var Config = function(env) {
this.env = env;
var Config = function(options) {
this.env = options.env;
this.blockchainConfig = {};
this.contractsConfig = {};
this.pipelineConfig = {};
this.chainTracker = {};
this.assetFiles = {};
this.contractsFiles = [];
this.configDir = 'config/';
this.configDir = options.configDir || 'config/';
this.chainsFile = options.chainsFile || './chains.json';
};
Config.prototype.loadConfigFiles = function(options) {
@ -31,17 +33,17 @@ Config.prototype.reloadConfig = function() {
};
Config.prototype.loadBlockchainConfigFile = function() {
//var defaultBlockchainConfig = JSON.parse(fs.readFileSync(this.configDir + this.env + "/blockchain.json"))[this.env];
var defaultBlockchainConfig = JSON.parse(fs.readFileSync(this.configDir + "blockchain.json"))[this.env];
this.blockchainConfig = defaultBlockchainConfig;
};
Config.prototype.loadContractsConfigFile = function() {
var defaultContractsConfig = JSON.parse(fs.readFileSync(this.configDir + "contracts.json"))['default'];
//var envContractsConfig = JSON.parse(fs.readFileSync(this.configDir + this.env + "/contracts.json"))[this.env];
var contractsConfig = JSON.parse(fs.readFileSync(this.configDir + "contracts.json"));
var defaultContractsConfig = contractsConfig['default'];
var envContractsConfig = contractsConfig[this.env];
//merge.recursive(defaultContractsConfig, envContractsConfig);
this.contractsConfig = defaultContractsConfig;
var mergedConfig = merge.recursive(defaultContractsConfig, envContractsConfig);
this.contractsConfig = mergedConfig;
};
Config.prototype.loadPipelineConfigFile = function() {
@ -61,12 +63,12 @@ Config.prototype.loadChainTrackerFile = function() {
//var self = this;
var chainTracker;
try {
chainTracker = JSON.parse(fs.readFileSync("./chains.json"));
chainTracker = JSON.parse(fs.readFileSync(this.chainsFile));
}
catch(err) {
//self.logger.info('chains.json file not found, creating it...');
//self.logger.info(this.chainsFile + ' file not found, creating it...');
chainTracker = {};
fs.writeFileSync('./chains.json', '{}');
fs.writeFileSync(this.chainsFile, '{}');
}
this.chainTracker = chainTracker;
};

View File

@ -37,7 +37,7 @@ var Embark = {
},
initConfig: function(env, options) {
this.config = new Config(env);
this.config = new Config({env: env});
this.config.loadConfigFiles(options);
this.logger = new Logger({logLevel: 'debug'});

View File

@ -29,7 +29,7 @@ Test.prototype.deployAll = function(contractsConfig, cb) {
async.waterfall([
function buildContracts(callback) {
var config = new Config('test');
var config = new Config({env: 'test'});
config.contractsFiles = config.loadFiles(["app/contracts/**"]);
config.contractsConfig = {contracts: contractsConfig} ;

View File

@ -23,6 +23,7 @@
"finalhandler": "^0.5.0",
"grunt": "^0.4.5",
"json-honey": "^0.4.1",
"merge": "^1.2.0",
"meteor-build-client": "^0.1.6",
"mkdirp": "^0.5.1",
"read-yaml": "^1.0.0",

30
test/compiler.js Normal file

File diff suppressed because one or more lines are too long

57
test/config.js Normal file
View File

@ -0,0 +1,57 @@
/*globals describe, it*/
var Config = require('../lib/config.js');
var assert = require('assert');
var fs = require('fs');
describe('embark.Config', function() {
var config = new Config({
env: 'myenv',
configDir: './test/test1/config/'
});
describe('#loadBlockchainConfigFile', function() {
it('should load blockchain config correctly', function() {
config.loadBlockchainConfigFile();
var expectedConfig = {
"networkType": "custom",
"genesisBlock": "config/development/genesis.json",
"datadir": ".embark/development/datadir",
"mineWhenNeeded": true,
"nodiscover": true,
"rpcHost": "localhost",
"rpcPort": 8545,
"rpcCorsDomain": "http://localhost:8000",
"account": {
"password": "config/development/password"
}
};
assert.deepEqual(config.blockchainConfig, expectedConfig);
});
});
describe('#loadContractsConfigFile', function() {
it('should load contract config correctly', function() {
config.loadContractsConfigFile();
var expectedConfig = {
"gas": "auto",
"contracts": {
"SimpleStorage": {
"args": [
100
],
"gas": 123456
},
"Token": {
"args": [
200
]
}
}
};
assert.deepEqual(config.contractsConfig, expectedConfig);
});
});
});

View File

@ -0,0 +1,17 @@
contract SimpleStorage {
uint public storedData;
function SimpleStorage(uint initialValue) {
storedData = initialValue;
}
function set(uint x) {
storedData = x;
}
function get() constant returns (uint retVal) {
return storedData;
}
}

64
test/contracts/token.sol Normal file
View File

@ -0,0 +1,64 @@
// https://github.com/nexusdev/erc20/blob/master/contracts/base.sol
contract Token {
event Transfer(address indexed from, address indexed to, uint value);
event Approval( address indexed owner, address indexed spender, uint value);
mapping( address => uint ) _balances;
mapping( address => mapping( address => uint ) ) _approvals;
uint _supply;
function Token( uint initial_balance ) {
_balances[msg.sender] = initial_balance;
_supply = initial_balance;
}
function totalSupply() constant returns (uint supply) {
return _supply;
}
function balanceOf( address who ) constant returns (uint value) {
return _balances[who];
}
function transfer( address to, uint value) returns (bool ok) {
if( _balances[msg.sender] < value ) {
throw;
}
if( !safeToAdd(_balances[to], value) ) {
throw;
}
_balances[msg.sender] -= value;
_balances[to] += value;
Transfer( msg.sender, to, value );
return true;
}
function transferFrom( address from, address to, uint value) returns (bool ok) {
// if you don't have enough balance, throw
if( _balances[from] < value ) {
throw;
}
// if you don't have approval, throw
if( _approvals[from][msg.sender] < value ) {
throw;
}
if( !safeToAdd(_balances[to], value) ) {
throw;
}
// transfer and return true
_approvals[from][msg.sender] -= value;
_balances[from] -= value;
_balances[to] += value;
Transfer( from, to, value );
return true;
}
function approve(address spender, uint value) returns (bool ok) {
// TODO: should increase instead
_approvals[msg.sender][spender] = value;
Approval( msg.sender, spender, value );
return true;
}
function allowance(address owner, address spender) constant returns (uint _allowance) {
return _approvals[owner][spender];
}
function safeToAdd(uint a, uint b) internal returns (bool) {
return (a + b >= a);
}
}

View File

@ -0,0 +1,37 @@
{
"myenv": {
"networkType": "custom",
"genesisBlock": "config/development/genesis.json",
"datadir": ".embark/development/datadir",
"mineWhenNeeded": true,
"nodiscover": true,
"rpcHost": "localhost",
"rpcPort": 8545,
"rpcCorsDomain": "http://localhost:8000",
"account": {
"password": "config/development/password"
}
},
"testnet": {
"networkType": "testnet",
"rpcHost": "localhost",
"rpcPort": 8545
},
"livenet": {
"networkType": "livenet",
"rpcHost": "localhost",
"rpcPort": 8545,
"rpcCorsDomain": "http://localhost:8000",
"account": {
"password": "config/production/password"
}
},
"privatenet": {
"networkType": "custom",
"rpcHost": "localhost",
"rpcPort": 8545,
"datadir": "yourdatadir",
"networkId": "123",
"nodes": []
}
}

View File

@ -0,0 +1,25 @@
{
"default": {
"gas": "auto",
"contracts": {
"SimpleStorage": {
"args": [
100
]
}
}
},
"myenv": {
"gas": "auto",
"contracts": {
"SimpleStorage": {
"gas": 123456
},
"Token": {
"args": [
200
]
}
}
}
}