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

@ -1,22 +1,21 @@
const commander = require('commander'); const commander = require('commander')
const Web3 = require('web3'); const Web3 = require('web3')
const RemixTests = require('./index.js'); const RemixTests = require('./index.js')
const fs = require('fs'); const fs = require('fs')
commander.action(function (filename) { commander.action(function (filename) {
let web3 = new Web3(); let web3 = new Web3()
web3.setProvider(new web3.providers.HttpProvider('http://localhost:8545')); web3.setProvider(new web3.providers.HttpProvider('http://localhost:8545'))
if (fs.lstatSync(filename).isDirectory()) { if (fs.lstatSync(filename).isDirectory()) {
RemixTests.runTestFiles(filename, web3); RemixTests.runTestFiles(filename, web3)
} else { } else {
RemixTests.runTestFile(filename, web3); RemixTests.runTestFile(filename, web3)
} }
}); })
if (!process.argv.slice(2).length) { 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'); let fs = require('fs')
var async = require('async'); var async = require('async')
var path = require('path'); var path = require('path')
let remixLib = require('remix-lib'); let remixLib = require('remix-lib')
let compilerInput = remixLib.helpers.compiler; let compilerInput = remixLib.helpers.compiler
let RemixCompiler = require('remix-solidity').Compiler; let RemixCompiler = require('remix-solidity').Compiler
// TODO: replace this with remix's own compiler code // TODO: replace this with remix's own compiler code
function compileFile(filename, cb) { function compileFile (filename, cb) {
let compiler; let compiler
const sources = { 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 // TODO: for now assumes filepath dir contains all tests, later all this
// should be replaced with remix's & browser solidity compiler code // 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 => { fs.readdirSync(filepath).forEach(file => {
sources[file] = {content: fs.readFileSync(path.join(filepath, file)).toString()} sources[file] = {content: fs.readFileSync(path.join(filepath, file)).toString()}
}); })
async.waterfall([ async.waterfall([
function loadCompiler(next) { function loadCompiler (next) {
compiler = new RemixCompiler(); compiler = new RemixCompiler()
compiler.onInternalCompilerLoaded(); compiler.onInternalCompilerLoaded()
//compiler.event.register('compilerLoaded', this, function (version) { // compiler.event.register('compilerLoaded', this, function (version) {
next(); next()
//}); // });
}, },
function doCompilation(next) { function doCompilation (next) {
compiler.event.register('compilationFinished', this, function (success, data, source) { compiler.event.register('compilationFinished', this, function (success, data, source) {
next(null, data); next(null, data)
}); })
compiler.compile(sources, filepath); compiler.compile(sources, filepath)
} }
], function(err, result) { ], function (err, result) {
cb(err, result.contracts); cb(err, result.contracts)
}); })
} }
function compileFiles(directory, cb) { function compileFiles (directory, cb) {
let compiler; let compiler
const sources = { 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 // TODO: for now assumes filepath dir contains all tests, later all this
// should be replaced with remix's & browser solidity compiler code // should be replaced with remix's & browser solidity compiler code
fs.readdirSync(directory).forEach(file => { fs.readdirSync(directory).forEach(file => {
sources[file] = {content: fs.readFileSync(path.join(directory, file)).toString()} sources[file] = {content: fs.readFileSync(path.join(directory, file)).toString()}
}); })
async.waterfall([ async.waterfall([
function loadCompiler(next) { function loadCompiler (next) {
compiler = new RemixCompiler(); compiler = new RemixCompiler()
compiler.onInternalCompilerLoaded(); compiler.onInternalCompilerLoaded()
//compiler.event.register('compilerLoaded', this, function (version) { // compiler.event.register('compilerLoaded', this, function (version) {
next(); next()
//}); // });
}, },
function doCompilation(next) { function doCompilation (next) {
compiler.event.register('compilationFinished', this, function (success, data, source) { compiler.event.register('compilationFinished', this, function (success, data, source) {
next(null, data); next(null, data)
}); })
compiler.compile(sources, directory); compiler.compile(sources, directory)
} }
], function(err, result) { ], function (err, result) {
cb(err, result.contracts); cb(err, result.contracts)
}); })
} }
module.exports = { module.exports = {
compileFile: compileFile, compileFile: compileFile,
compileFiles: compileFiles compileFiles: compileFiles
} }

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

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

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

Loading…
Cancel
Save