refactor blockchain lib + tests

This commit is contained in:
Iuri Matias 2015-07-03 08:53:42 -04:00
parent 1a5ddea9a8
commit e43c0a38d6
2 changed files with 93 additions and 28 deletions

View File

@ -1,12 +1,13 @@
var Deploy;
var Config = require('./config/config.js');
startChain = function(env) { Blockchain = function(blockchainConfig) {
config = (new Config.Blockchain()).loadConfigFile('config/blockchain.yml').config(env); this.config = blockchainConfig;
}
address = config.account.address; Blockchain.prototype.generate_basic_command = function() {
var config = this.config;
var address = config.account.address;
cmd = "geth "; var cmd = "geth ";
if (config.datadir !== "default") { if (config.datadir !== "default") {
cmd += "--datadir=\"" + config.datadir + "\" "; cmd += "--datadir=\"" + config.datadir + "\" ";
@ -32,21 +33,20 @@ startChain = function(env) {
cmd += "--password " + config.account.password + " "; cmd += "--password " + config.account.password + " ";
} }
if (config.account.init) { return cmd;
console.log("=== initializating account"); }
console.log("running: " + cmd + " account list");
result = exec(cmd + "account list"); Blockchain.prototype.list_command = function() {
console.log("finished"); return this.generate_basic_command() + "account list ";
console.log("=== output is " + result.output); }
if (result.output.indexOf("Fatal") < 0) {
console.log("=== already initialized"); Blockchain.prototype.init_command = function() {
address = result.output.match(/{(\w+)}/)[1]; return this.generate_basic_command() + "account new ";
} else { }
console.log("running: " + cmd + " account new");
output = exec(cmd + " account new"); Blockchain.prototype.run_command = function(address) {
address = output.output.match(/{(\w+)}/)[1]; var cmd = this.generate_basic_command();
} var config = this.config;
}
if (address !== void 0) { if (address !== void 0) {
cmd += "--unlock " + address + " "; cmd += "--unlock " + address + " ";
@ -60,15 +60,34 @@ startChain = function(env) {
cmd += "js node_modules/embark-framework/js/mine.js"; cmd += "js node_modules/embark-framework/js/mine.js";
} }
console.log("running: " + cmd); return cmd;
console.log("=== running geth");
exec(cmd);
} }
Blockchain = { Blockchain.prototype.get_address = function() {
startChain: startChain var config = this.config;
var address = null;
if (config.account.init) {
console.log("running: " + this.list_command());
result = exec(this.list_command());
if (result.output.indexOf("Fatal") < 0) {
console.log("=== already initialized");
address = result.output.match(/{(\w+)}/)[1];
} else {
console.log("running: " + this.init_command());
result = exec(this.init_command());
address = output.output.match(/{(\w+)}/)[1];
}
}
return address;
}
Blockchain.prototype.startChain = function() {
var address = this.get_address();
console.log("running: " + this.run_command(address));
exec(this.run_command(address));
} }
module.exports = Blockchain module.exports = Blockchain

46
test/blockchain.js Normal file
View File

@ -0,0 +1,46 @@
var Config = require('../lib/config/config.js');
var Blockchain = require('../lib/blockchain.js');
var assert = require('assert');
var sinon = require('sinon');
describe('embark.blockchain', function() {
var blockchainConfig = (new Config.Blockchain()).loadConfigFile('test/support/blockchain.yml').config("development");
describe('#generate_basic_command', function() {
var blockchain = new Blockchain(blockchainConfig);
it('should return correct cmd', function() {
assert.strictEqual(blockchain.generate_basic_command(), "geth --datadir=\"/tmp/embark\" --logfile=\"/tmp/embark.log\" --port 30303 --rpc --rpcport 8101 --networkid "+blockchainConfig.networkId+" --rpccorsdomain \"*\" --minerthreads \"1\" --mine --maxpeers 0 --password config/password ");
});
});
describe('#list_command', function() {
var blockchain = new Blockchain(blockchainConfig);
blockchain.generate_basic_command = sinon.stub().returns("geth ");
it('should generate command to list accounts', function() {
assert.equal(blockchain.list_command(), "geth account list ");
});
});
describe('#init_command', function() {
var blockchain = new Blockchain(blockchainConfig);
blockchain.generate_basic_command = sinon.stub().returns("geth ");
it('should generate command to create an account', function() {
assert.equal(blockchain.init_command(), "geth account new ");
});
});
describe('#run_command', function() {
describe('with mine when needed config set', function() {
var blockchain = new Blockchain(blockchainConfig);
blockchain.generate_basic_command = sinon.stub().returns("geth ");
it('should generate run command with script ', function() {
assert.equal(blockchain.run_command(), "geth js node_modules/embark-framework/js/mine.js");
});
});
});
});