You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
158 lines
5.9 KiB
158 lines
5.9 KiB
6 years ago
|
import async from 'async'
|
||
6 years ago
|
import fs from './fileSystem'
|
||
6 years ago
|
import { runTest } from './testRunner'
|
||
5 years ago
|
import { TestResultInterface, ResultsInterface, compilationInterface, ASTInterface, Options, AstNode } from './types'
|
||
6 years ago
|
import colors from 'colors'
|
||
5 years ago
|
import Web3 from 'web3';
|
||
6 years ago
|
|
||
6 years ago
|
import { compileFileOrFiles } from './compiler'
|
||
|
import { deployAll } from './deployer'
|
||
6 years ago
|
|
||
5 years ago
|
/**
|
||
|
* @dev run test contract files (used for CLI)
|
||
|
* @param filepath Path of file
|
||
|
* @param isDirectory True, if path is a directory
|
||
|
* @param web3 Web3
|
||
5 years ago
|
* @param finalCallback optional callback to run finally
|
||
5 years ago
|
* @param opts Options
|
||
|
*/
|
||
|
|
||
5 years ago
|
export function runTestFiles(filepath: string, isDirectory: boolean, web3: Web3, finalCallback: any = () => {}, opts?: Options) {
|
||
6 years ago
|
opts = opts || {}
|
||
5 years ago
|
const sourceASTs: any = {}
|
||
6 years ago
|
const { Signale } = require('signale')
|
||
|
// signale configuration
|
||
|
const options = {
|
||
|
types: {
|
||
|
result: {
|
||
|
badge: '\t✓',
|
||
|
label: '',
|
||
|
color: 'greenBright'
|
||
|
},
|
||
|
name: {
|
||
|
badge: '\n\t◼',
|
||
|
label: '',
|
||
|
color: 'white'
|
||
|
},
|
||
|
error: {
|
||
|
badge: '\t✘',
|
||
|
label: '',
|
||
|
color: 'redBright'
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
const signale = new Signale(options)
|
||
6 years ago
|
let accounts = opts['accounts'] || null
|
||
6 years ago
|
async.waterfall([
|
||
6 years ago
|
function getAccountList (next: Function) {
|
||
6 years ago
|
if (accounts) return next(null)
|
||
6 years ago
|
web3.eth.getAccounts((_err: Error | null | undefined, _accounts) => {
|
||
6 years ago
|
accounts = _accounts
|
||
|
next(null)
|
||
|
})
|
||
|
},
|
||
6 years ago
|
function compile(next: Function) {
|
||
|
compileFileOrFiles(filepath, isDirectory, { accounts }, next)
|
||
6 years ago
|
},
|
||
5 years ago
|
function deployAllContracts (compilationResult: compilationInterface, asts: ASTInterface, next: Function) {
|
||
5 years ago
|
// Extract AST of test contract file source
|
||
5 years ago
|
for(const filename in asts) {
|
||
5 years ago
|
if(filename.endsWith('_test.sol'))
|
||
5 years ago
|
sourceASTs[filename] = asts[filename].ast
|
||
|
}
|
||
5 years ago
|
deployAll(compilationResult, web3, false, (err, contracts) => {
|
||
6 years ago
|
if (err) {
|
||
|
next(err)
|
||
|
}
|
||
|
next(null, compilationResult, contracts)
|
||
|
})
|
||
|
},
|
||
5 years ago
|
function determineTestContractsToRun (compilationResult: compilationInterface, contracts: any, next: Function) {
|
||
|
let contractsToTest: string[] = []
|
||
6 years ago
|
let contractsToTestDetails: any[] = []
|
||
6 years ago
|
const gatherContractsFrom = function(filename: string) {
|
||
5 years ago
|
if (!filename.endsWith('_test.sol')) {
|
||
6 years ago
|
return
|
||
|
}
|
||
6 years ago
|
try {
|
||
5 years ago
|
Object.keys(compilationResult[filename]).forEach(contractName => {
|
||
|
contractsToTest.push(contractName)
|
||
|
contractsToTestDetails.push(compilationResult[filename][contractName])
|
||
|
})
|
||
6 years ago
|
} catch (e) {
|
||
|
console.error(e)
|
||
|
}
|
||
6 years ago
|
}
|
||
|
if (isDirectory) {
|
||
6 years ago
|
fs.walkSync(filepath, (foundpath: string) => {
|
||
6 years ago
|
gatherContractsFrom(foundpath)
|
||
|
})
|
||
|
} else {
|
||
|
gatherContractsFrom(filepath)
|
||
|
}
|
||
|
next(null, contractsToTest, contractsToTestDetails, contracts)
|
||
|
},
|
||
5 years ago
|
function runTests(contractsToTest: string[], contractsToTestDetails: any[], contracts: any, next: Function) {
|
||
6 years ago
|
let totalPassing: number = 0
|
||
|
let totalFailing: number = 0
|
||
|
let totalTime: number = 0
|
||
6 years ago
|
let errors: any[] = []
|
||
|
|
||
6 years ago
|
const _testCallback = function (err: Error | null | undefined, result: TestResultInterface) {
|
||
6 years ago
|
if(err) throw err;
|
||
6 years ago
|
if (result.type === 'contract') {
|
||
|
signale.name(result.value.white)
|
||
|
} else if (result.type === 'testPass') {
|
||
|
signale.result(result.value)
|
||
|
} else if (result.type === 'testFailure') {
|
||
|
signale.result(result.value.red)
|
||
|
errors.push(result)
|
||
|
}
|
||
|
}
|
||
6 years ago
|
const _resultsCallback = (_err: Error | null | undefined, result: ResultsInterface, cb) => {
|
||
6 years ago
|
totalPassing += result.passingNum
|
||
|
totalFailing += result.failureNum
|
||
|
totalTime += result.timePassed
|
||
|
cb()
|
||
|
}
|
||
|
|
||
6 years ago
|
async.eachOfLimit(contractsToTest, 1, (contractName: string, index, cb) => {
|
||
|
try {
|
||
5 years ago
|
const fileAST: AstNode = sourceASTs[contracts[contractName]['filename']]
|
||
5 years ago
|
runTest(contractName, contracts[contractName], contractsToTestDetails[index], fileAST, { accounts }, _testCallback, (err, result) => {
|
||
6 years ago
|
if (err) {
|
||
6 years ago
|
console.log(err)
|
||
6 years ago
|
return cb(err)
|
||
|
}
|
||
6 years ago
|
_resultsCallback(null, result, cb)
|
||
6 years ago
|
})
|
||
6 years ago
|
} catch(e) {
|
||
|
console.error(e)
|
||
|
}
|
||
6 years ago
|
}, function (err) {
|
||
|
if (err) {
|
||
|
return next(err)
|
||
|
}
|
||
|
|
||
|
console.log('\n')
|
||
|
if (totalPassing > 0) {
|
||
6 years ago
|
console.log(colors.green(totalPassing + ' passing ') + colors.grey('(' + totalTime + 's)'))
|
||
6 years ago
|
}
|
||
|
if (totalFailing > 0) {
|
||
6 years ago
|
console.log(colors.red(totalFailing + ' failing'))
|
||
6 years ago
|
}
|
||
|
console.log('')
|
||
|
|
||
|
errors.forEach((error, index) => {
|
||
|
console.log(' ' + (index + 1) + ') ' + error.context + ' ' + error.value)
|
||
|
console.log('')
|
||
6 years ago
|
console.log(colors.red('\t error: ' + error.errMsg))
|
||
6 years ago
|
})
|
||
|
console.log('')
|
||
|
|
||
|
next()
|
||
|
})
|
||
|
}
|
||
5 years ago
|
], finalCallback)
|
||
6 years ago
|
}
|