comply with linter

pull/7/head
Iuri Matias 7 years ago
parent 5ceb83a597
commit 8a7e58c366
  1. 114
      index.js
  2. 23
      run.js
  3. 87
      src/compiler.js
  4. 86
      src/deployer.js
  5. 54
      src/testRunner.js
  6. 119
      tests/testRunner.js

@ -1,120 +1,120 @@
const async = require('async');
const path = require('path');
const fs = require('fs');
require('colors');
const async = require('async')
const path = require('path')
const fs = require('fs')
require('colors')
let Compiler = require('./src/compiler.js');
let Deployer = require('./src/deployer.js');
let TestRunner = require('./src/testRunner.js');
let Compiler = require('./src/compiler.js')
let Deployer = require('./src/deployer.js')
let TestRunner = require('./src/testRunner.js')
var runTest = function(contractName, contractObj, testCallback, resultsCallback) {
TestRunner.runTest(contractName, contractObj, testCallback, resultsCallback);
var runTest = function (contractName, contractObj, testCallback, resultsCallback) {
TestRunner.runTest(contractName, contractObj, testCallback, resultsCallback)
}
var runTestFile = function(filename, web3) {
var runTestFile = function (filename, web3) {
async.waterfall([
function compile(next) {
Compiler.compileFile(filename, next);
function compile (next) {
Compiler.compileFile(filename, next)
},
function deployAllContracts(compilationResult, next) {
Deployer.deployAll(compilationResult, web3, function(err, contracts) {
function deployAllContracts (compilationResult, next) {
Deployer.deployAll(compilationResult, web3, function (err, contracts) {
if (err) {
next(err);
next(err)
}
let contractsToTest = Object.keys(compilationResult[path.basename(filename)]);
next(null, contractsToTest, contracts);
});
let contractsToTest = Object.keys(compilationResult[path.basename(filename)])
next(null, contractsToTest, contracts)
})
},
function runTests(contractsToTest, contracts, next) {
var testCallback = function(result) {
function runTests (contractsToTest, contracts, next) {
var testCallback = function (result) {
if (result.type === 'contract') {
console.log(("\t " + result.value).green);
console.log(('\t ' + result.value).green)
} else if (result.type === 'testPass') {
console.log("\t✓ ".green.bold + result.value.grey);
console.log('\t✓ '.green.bold + result.value.grey)
} else if (result.type === 'testFailure') {
console.log("\t✘ ".bold.red + result.value.red);
console.log('\t✘ '.bold.red + result.value.red)
}
}
var resultsCallback = function(err, result) {
var resultsCallback = function (err, result) {
if (err) {
return cb(err);
return cb(err)
}
if (result.passingNum > 0) {
console.log((result.passingNum + " passing").green);
console.log((result.passingNum + ' passing').green)
}
if (result.failureNum > 0) {
console.log((result.failureNum + " failing").red);
console.log((result.failureNum + ' failing').red)
}
}
async.eachLimit(contractsToTest, 1, (contractName, cb) => {
runTest(contractName, contracts[contractName], testCallback, resultsCallback);
}, next);
runTest(contractName, contracts[contractName], testCallback, resultsCallback)
}, next)
}
], function() {
});
], function () {
})
}
var runTestFiles = function(directory, web3) {
var runTestFiles = function (directory, web3) {
async.waterfall([
function compile(next) {
Compiler.compileFiles(directory, next);
function compile (next) {
Compiler.compileFiles(directory, next)
},
function deployAllContracts(compilationResult, next) {
Deployer.deployAll(compilationResult, web3, function(err, contracts) {
function deployAllContracts (compilationResult, next) {
Deployer.deployAll(compilationResult, web3, function (err, contracts) {
if (err) {
next(err);
next(err)
}
let contractsToTest = [];
let contractsToTest = []
fs.readdirSync(directory).forEach(filename => {
if (filename.indexOf('_test.sol') < 0) {
return;
return
}
Object.keys(compilationResult[path.basename(filename)]).forEach(contractName => {
contractsToTest.push(contractName)
})
})
next(null, contractsToTest, contracts);
});
next(null, contractsToTest, contracts)
})
},
function runTests(contractsToTest, contracts, next) {
var testCallback = function(result) {
function runTests (contractsToTest, contracts, next) {
var testCallback = function (result) {
if (result.type === 'contract') {
console.log("\n " + result.value);
console.log('\n ' + result.value)
} else if (result.type === 'testPass') {
console.log("\t✓ ".green.bold + result.value.grey);
console.log('\t✓ '.green.bold + result.value.grey)
} else if (result.type === 'testFailure') {
console.log("\t✘ ".bold.red + result.value.red);
console.log('\t✘ '.bold.red + result.value.red)
}
}
var resultsCallback = function(_err, result, cb) {
var resultsCallback = function (_err, result, cb) {
if (result.passingNum > 0) {
console.log((result.passingNum + " passing").green);
console.log((result.passingNum + ' passing').green)
}
if (result.failureNum > 0) {
console.log((result.failureNum + " failing").red);
console.log((result.failureNum + ' failing').red)
}
cb();
cb()
}
async.eachOfLimit(contractsToTest, 1, (contractName, index, cb) => {
runTest(contractName, contracts[contractName], testCallback, (err, result) => {
if (err) {
return cb(err);
return cb(err)
}
resultsCallback(null, result, cb);
});
}, next);
resultsCallback(null, result, cb)
})
}, next)
}
], function() {
});
], function () {
})
}
module.exports = {
runTestFile: runTestFile,
runTestFiles: runTestFiles,
runTest: runTest
};
}

@ -1,22 +1,21 @@
const commander = require('commander');
const Web3 = require('web3');
const RemixTests = require('./index.js');
const fs = require('fs');
const commander = require('commander')
const Web3 = require('web3')
const RemixTests = require('./index.js')
const fs = require('fs')
commander.action(function (filename) {
let web3 = new Web3();
web3.setProvider(new web3.providers.HttpProvider('http://localhost:8545'));
let web3 = new Web3()
web3.setProvider(new web3.providers.HttpProvider('http://localhost:8545'))
if (fs.lstatSync(filename).isDirectory()) {
RemixTests.runTestFiles(filename, web3);
RemixTests.runTestFiles(filename, web3)
} else {
RemixTests.runTestFile(filename, web3);
RemixTests.runTestFile(filename, web3)
}
});
})
if (!process.argv.slice(2).length) {
console.log("please specify filename");
console.log('please specify filename')
}
commander.parse(process.argv);
commander.parse(process.argv)

@ -1,82 +1,77 @@
let fs = require('fs');
var async = require('async');
var path = require('path');
let fs = require('fs')
var async = require('async')
var path = require('path')
let remixLib = require('remix-lib');
let compilerInput = remixLib.helpers.compiler;
let RemixCompiler = require('remix-solidity').Compiler;
let remixLib = require('remix-lib')
let compilerInput = remixLib.helpers.compiler
let RemixCompiler = require('remix-solidity').Compiler
// TODO: replace this with remix's own compiler code
function compileFile(filename, cb) {
let compiler;
function compileFile (filename, cb) {
let compiler
const sources = {
"tests.sol": {content: fs.readFileSync("sol/tests.sol").toString()},
'tests.sol': {content: fs.readFileSync('sol/tests.sol').toString()}
}
// TODO: for now assumes filepath dir contains all tests, later all this
// should be replaced with remix's & browser solidity compiler code
let filepath = path.dirname(filename);
let filepath = path.dirname(filename)
fs.readdirSync(filepath).forEach(file => {
sources[file] = {content: fs.readFileSync(path.join(filepath, file)).toString()}
});
})
async.waterfall([
function loadCompiler(next) {
compiler = new RemixCompiler();
compiler.onInternalCompilerLoaded();
//compiler.event.register('compilerLoaded', this, function (version) {
next();
//});
function loadCompiler (next) {
compiler = new RemixCompiler()
compiler.onInternalCompilerLoaded()
// compiler.event.register('compilerLoaded', this, function (version) {
next()
// });
},
function doCompilation(next) {
function doCompilation (next) {
compiler.event.register('compilationFinished', this, function (success, data, source) {
next(null, data);
});
compiler.compile(sources, filepath);
next(null, data)
})
compiler.compile(sources, filepath)
}
], function(err, result) {
cb(err, result.contracts);
});
], function (err, result) {
cb(err, result.contracts)
})
}
function compileFiles(directory, cb) {
let compiler;
function compileFiles (directory, cb) {
let compiler
const sources = {
"tests.sol": {content: fs.readFileSync("sol/tests.sol").toString()},
'tests.sol': {content: fs.readFileSync('sol/tests.sol').toString()}
}
// TODO: for now assumes filepath dir contains all tests, later all this
// should be replaced with remix's & browser solidity compiler code
fs.readdirSync(directory).forEach(file => {
sources[file] = {content: fs.readFileSync(path.join(directory, file)).toString()}
});
})
async.waterfall([
function loadCompiler(next) {
compiler = new RemixCompiler();
compiler.onInternalCompilerLoaded();
//compiler.event.register('compilerLoaded', this, function (version) {
next();
//});
function loadCompiler (next) {
compiler = new RemixCompiler()
compiler.onInternalCompilerLoaded()
// compiler.event.register('compilerLoaded', this, function (version) {
next()
// });
},
function doCompilation(next) {
function doCompilation (next) {
compiler.event.register('compilationFinished', this, function (success, data, source) {
next(null, data);
});
compiler.compile(sources, directory);
next(null, data)
})
compiler.compile(sources, directory)
}
], function(err, result) {
cb(err, result.contracts);
});
], function (err, result) {
cb(err, result.contracts)
})
}
module.exports = {
compileFile: compileFile,
compileFiles: compileFiles
}

@ -1,80 +1,78 @@
var async = require('async');
var async = require('async')
// TODO: replace this with remix's own deployer code
function deployAll(compileResult, web3, callback) {
let compiledObject = {}, contracts = {}, accounts;
function deployAll (compileResult, web3, callback) {
let compiledObject = {}, contracts = {}, accounts
async.waterfall([
function getAccountList(next) {
function getAccountList (next) {
web3.eth.getAccounts((err, _accounts) => {
accounts = _accounts;
next();
});
accounts = _accounts
next()
})
},
function getContractData(next) {
function getContractData (next) {
for (let contractFile in compileResult) {
for (let contractName in compileResult[contractFile]) {
let contract = compileResult[contractFile][contractName];
let contract = compileResult[contractFile][contractName]
const className = contractName;
const filename = contractFile;
const className = contractName
const filename = contractFile
let abi = contract.abi
let code = contract.evm.bytecode.object
let abi = contract.abi;
let code = contract.evm.bytecode.object;
compiledObject[className] = {};
compiledObject[className].abi = abi;
compiledObject[className].code = code;
compiledObject[className].filename = filename;
compiledObject[className].className = className;
compiledObject[className] = {}
compiledObject[className].abi = abi
compiledObject[className].code = code
compiledObject[className].filename = filename
compiledObject[className].className = className
}
}
next();
next()
},
function deployContracts(next) {
async.eachOfLimit(compiledObject, 1, function(contract, contractName, nextEach) {
let contractObject = new web3.eth.Contract(contract.abi);
function deployContracts (next) {
async.eachOfLimit(compiledObject, 1, function (contract, contractName, nextEach) {
let contractObject = new web3.eth.Contract(contract.abi)
let contractCode = "0x" + contract.code;
let contractCode = '0x' + contract.code
// TODO: temporary code, and terrible if the contracts are not in order...
for (let name in compiledObject) {
let contractObj = compiledObject[name];
let linkReference = '__' + contractObj.filename + ":" + contractObj.className;
let toReplace = linkReference + "_".repeat(40 - linkReference.length);
let contractObj = compiledObject[name]
let linkReference = '__' + contractObj.filename + ':' + contractObj.className
let toReplace = linkReference + '_'.repeat(40 - linkReference.length)
if (contractCode.indexOf(linkReference) < 0) {
continue
}
if (!contractObj.deployedAddress) {
throw new Error("linking not found for " + name + " when deploying " + contractName);
throw new Error('linking not found for ' + name + ' when deploying ' + contractName)
}
contractCode = contractCode.replace(new RegExp(toReplace, "g"), contractObj.deployedAddress)
contractCode = contractCode.replace(new RegExp(toReplace, 'g'), contractObj.deployedAddress)
}
contractObject.deploy({arguments: [], data: contractCode}).send({
from: accounts[0],
gas: 4000 * 1000
}).on('receipt', function(receipt) {
contractObject.options.address = receipt.contractAddress;
contractObject.options.from = accounts[0];
contractObject.options.gas = 4000*1000;
compiledObject[contractName].deployedAddress = receipt.contractAddress;
contracts[contractName] = contractObject;
nextEach();
});
}, function() {
next(null, contracts);
});
}).on('receipt', function (receipt) {
contractObject.options.address = receipt.contractAddress
contractObject.options.from = accounts[0]
contractObject.options.gas = 4000 * 1000
compiledObject[contractName].deployedAddress = receipt.contractAddress
contracts[contractName] = contractObject
nextEach()
})
}, function () {
next(null, contracts)
})
}
], callback);
], callback)
}
module.exports = {

@ -1,50 +1,50 @@
var async = require('async');
var changeCase = require('change-case');
var async = require('async')
var changeCase = require('change-case')
function runTest(testName, testObject, testCallback, resultsCallback) {
let runList = [];
let specialFunctions = ['beforeAll', 'beforeEach'];
let availableFunctions = testObject._jsonInterface.reverse().filter((x) => x.type === 'function').map((x) => x.name);
let testFunctions = testObject._jsonInterface.filter((x) => specialFunctions.indexOf(x.name) < 0 && x.type === 'function');
function runTest (testName, testObject, testCallback, resultsCallback) {
let runList = []
let specialFunctions = ['beforeAll', 'beforeEach']
let availableFunctions = testObject._jsonInterface.reverse().filter((x) => x.type === 'function').map((x) => x.name)
let testFunctions = testObject._jsonInterface.filter((x) => specialFunctions.indexOf(x.name) < 0 && x.type === 'function')
if (availableFunctions.indexOf("beforeAll") >= 0) {
runList.push({name: 'beforeAll', type: 'internal', constant: false});
if (availableFunctions.indexOf('beforeAll') >= 0) {
runList.push({name: 'beforeAll', type: 'internal', constant: false})
}
for (let func of testFunctions) {
if (availableFunctions.indexOf("beforeEach") >= 0) {
runList.push({name: 'beforeEach', type: 'internal', constant: false});
if (availableFunctions.indexOf('beforeEach') >= 0) {
runList.push({name: 'beforeEach', type: 'internal', constant: false})
}
runList.push({name: func.name, type: 'test', constant: func.constant});
runList.push({name: func.name, type: 'test', constant: func.constant})
}
let passingNum = 0, failureNum = 0;
let passingNum = 0, failureNum = 0
testCallback({type: "contract", value: testName});
async.eachOfLimit(runList, 1, function(func, index, next) {
let method = testObject.methods[func.name].apply(testObject.methods[func.name], []);
testCallback({type: 'contract', value: testName})
async.eachOfLimit(runList, 1, function (func, index, next) {
let method = testObject.methods[func.name].apply(testObject.methods[func.name], [])
if (func.constant) {
method.call().then((result) => {
if (result) {
testCallback({type: "testPass", value: changeCase.sentenceCase(func.name)});
passingNum += 1;
testCallback({type: 'testPass', value: changeCase.sentenceCase(func.name)})
passingNum += 1
} else {
testCallback({type: "testFailure", value: changeCase.sentenceCase(func.name)});
failureNum += 1;
testCallback({type: 'testFailure', value: changeCase.sentenceCase(func.name)})
failureNum += 1
}
next();
});
next()
})
} else {
method.send().then(() => {
next();
});
next()
})
}
}, function() {
}, function () {
resultsCallback(null, {
passingNum: passingNum,
failureNum: failureNum
});
});
})
})
}
module.exports = {

@ -1,96 +1,93 @@
const async = require('async');
const Web3 = require('web3');
const assert = require('assert');
const async = require('async')
const Web3 = require('web3')
const assert = require('assert')
let Compiler = require('../src/compiler.js');
let Deployer = require('../src/deployer.js');
let TestRunner = require('../src/testRunner.js');
let Compiler = require('../src/compiler.js')
let Deployer = require('../src/deployer.js')
let TestRunner = require('../src/testRunner.js')
function compileAndDeploy(filename, callback) {
let web3 = new Web3();
web3.setProvider(new web3.providers.HttpProvider('http://localhost:8545'));
function compileAndDeploy (filename, callback) {
let web3 = new Web3()
web3.setProvider(new web3.providers.HttpProvider('http://localhost:8545'))
async.waterfall([
function compile(next) {
Compiler.compileFile(filename, next);
function compile (next) {
Compiler.compileFile(filename, next)
},
function deployAllContracts(compilationResult, next) {
Deployer.deployAll(compilationResult, web3, next);
function deployAllContracts (compilationResult, next) {
Deployer.deployAll(compilationResult, web3, next)
}
], function(_err, contracts) {
callback(null, contracts);
});
], function (_err, contracts) {
callback(null, contracts)
})
}
describe('testRunner', function() {
describe("test with beforeAll", function() {
describe('testRunner', function () {
describe('test with beforeAll', function () {
let filename = 'tests/examples_1/simple_storage_test.sol'
let tests = [], results = {};
let tests = [], results = {}
before(function(done) {
compileAndDeploy(filename, function(_err, contracts) {
var testCallback = function(test) {
tests.push(test);
before(function (done) {
compileAndDeploy(filename, function (_err, contracts) {
var testCallback = function (test) {
tests.push(test)
}
var resultsCallback = function(_err, _results) {
results = _results;
done();
var resultsCallback = function (_err, _results) {
results = _results
done()
}
TestRunner.runTest('MyTest', contracts.MyTest, testCallback, resultsCallback);
});
});
TestRunner.runTest('MyTest', contracts.MyTest, testCallback, resultsCallback)
})
})
it('should 1 passing test', function() {
it('should 1 passing test', function () {
assert.equal(results.passingNum, 1)
});
})
it('should 1 failing test', function() {
it('should 1 failing test', function () {
assert.equal(results.failureNum, 1)
});
})
it('should returns 3 messages', function() {
it('should returns 3 messages', function () {
assert.deepEqual(tests, [
{ type: 'contract', value: 'MyTest' },
{ type: 'testPass', value: 'Initial value should be100' },
{ type: 'testFailure', value: 'Initial value should be200' }
]);
});
])
})
})
});
describe("test with beforeEach", function() {
describe('test with beforeEach', function () {
let filename = 'tests/examples_2/simple_storage_test.sol'
let tests = [], results = {};
let tests = [], results = {}
before(function(done) {
compileAndDeploy(filename, function(_err, contracts) {
var testCallback = function(test) {
tests.push(test);
before(function (done) {
compileAndDeploy(filename, function (_err, contracts) {
var testCallback = function (test) {
tests.push(test)
}
var resultsCallback = function(_err, _results) {
results = _results;
done();
var resultsCallback = function (_err, _results) {
results = _results
done()
}
TestRunner.runTest("MyTest", contracts.MyTest, testCallback, resultsCallback);
});
});
TestRunner.runTest('MyTest', contracts.MyTest, testCallback, resultsCallback)
})
})
it('should 2 passing tests', function() {
it('should 2 passing tests', function () {
assert.equal(results.passingNum, 2)
});
})
it('should 0 failing tests', function() {
it('should 0 failing tests', function () {
assert.equal(results.failureNum, 0)
});
})
it('should returns 3 messages', function() {
it('should returns 3 messages', function () {
assert.deepEqual(tests, [
{ type: 'contract', value: 'MyTest' },
{ type: 'testPass', value: 'Initial value should be100' },
{ type: 'testPass', value: 'Initial value should be200' }
]);
});
});
});
])
})
})
})

Loading…
Cancel
Save