diff --git a/remix-tests/examples/simple_storage.sol b/remix-tests/examples/simple_storage.sol index bc74b55c00..278155d1f1 100644 --- a/remix-tests/examples/simple_storage.sol +++ b/remix-tests/examples/simple_storage.sol @@ -13,5 +13,4 @@ contract SimpleStorage { function get() public view returns (uint retVal) { return storedData; } - } diff --git a/remix-tests/package.json b/remix-tests/package.json index 24f876a21f..7285b8970f 100644 --- a/remix-tests/package.json +++ b/remix-tests/package.json @@ -38,6 +38,7 @@ "homepage": "https://github.com/ethereum/remix-tests#readme", "dependencies": { "@types/async": "^2.4.0", + "@types/colors": "^1.2.1", "@types/web3": "^1.0.18", "async": "^2.6.0", "change-case": "^3.0.1", @@ -54,6 +55,7 @@ "yo-yoify": "latest" }, "devDependencies": { + "@types/commander": "^2.12.2", "@types/mocha": "^5.2.5", "@types/node": "^10.12.21", "babel-preset-es2017": "^6.24.1", diff --git a/remix-tests/src/run.ts b/remix-tests/src/run.ts index 217a43e843..b4cd97fd15 100644 --- a/remix-tests/src/run.ts +++ b/remix-tests/src/run.ts @@ -1,17 +1,15 @@ -#!/usr/bin/env ts-node - -const commander = require('commander') +import commander from 'commander' import Web3 from 'web3' import { runTestFiles } from './runTestFiles' import fs from './fileSystem' const Provider = require('remix-simulator').Provider -import Log = require('./logger') +import Log from './logger' const logger = new Log() const log = logger.logger -require('colors') +import colors from 'colors' // parse verbosity -function mapVerbosity (v) { +function mapVerbosity (v: number) { const levels = { 0: 'error', 1: 'warn', @@ -39,7 +37,7 @@ commander .option('-v, --verbose ', 'run with verbosity', mapVerbosity) .action(function (filename) { // Console message - console.log(('\n\tšŸ‘ :: Running remix-tests - Unit testing for solidity :: šŸ‘\t\n'),'color: white') + console.log(colors.white('\n\tšŸ‘\t:: Running remix-tests - Unit testing for solidity ::\tšŸ‘\n')) // set logger verbosity if (commander.verbose) { logger.setVerbosity(commander.verbose) diff --git a/remix-tests/src/runTestFiles.ts b/remix-tests/src/runTestFiles.ts index b6ea60ee3f..4b08d94595 100644 --- a/remix-tests/src/runTestFiles.ts +++ b/remix-tests/src/runTestFiles.ts @@ -1,13 +1,13 @@ -import async = require('async') -import path = require('path') +import async from 'async' import fs from './fileSystem' -import { runTest } from './testRunner' -require('colors') +import { runTest, TestResultInterface, ResultsInterface } from './testRunner' +import colors from 'colors' +import Web3 from 'web3' -import Compiler = require('./compiler') -import Deployer = require('./deployer') +import { compileFileOrFiles } from './compiler' +import { deployAll } from './deployer' -export function runTestFiles(filepath, isDirectory, web3, opts = {}) { +export function runTestFiles(filepath: string, isDirectory: boolean, web3: Web3, opts?: object) { opts = opts || {} const { Signale } = require('signale') // signale configuration @@ -33,38 +33,42 @@ export function runTestFiles(filepath, isDirectory, web3, opts = {}) { const signale = new Signale(options) let accounts = opts['accounts'] || null async.waterfall([ - function getAccountList (next) { + function getAccountList (next: Function) { if (accounts) return next(null) - web3.eth.getAccounts((_err, _accounts) => { + web3.eth.getAccounts((_err: Error | null | undefined, _accounts) => { accounts = _accounts next(null) }) }, - function compile (next) { - Compiler.compileFileOrFiles(filepath, isDirectory, { accounts }, next) + function compile(next: Function) { + compileFileOrFiles(filepath, isDirectory, { accounts }, next) }, - function deployAllContracts (compilationResult, next) { - Deployer.deployAll(compilationResult, web3, function (err, contracts) { + function deployAllContracts (compilationResult, next: Function) { + deployAll(compilationResult, web3, (err, contracts) => { if (err) { next(err) } next(null, compilationResult, contracts) }) }, - function determineTestContractsToRun (compilationResult, contracts, next) { + function determineTestContractsToRun (compilationResult, contracts, next: Function) { let contractsToTest: any[] = [] let contractsToTestDetails: any[] = [] - const gatherContractsFrom = (filename) => { + const gatherContractsFrom = function(filename: string) { if (filename.indexOf('_test.sol') < 0) { return } - Object.keys(compilationResult[path.basename(filename)]).forEach(contractName => { - contractsToTest.push(contractName) - contractsToTestDetails.push(compilationResult[path.basename(filename)][contractName]) - }) + try { + Object.keys(compilationResult[filename]).forEach(contractName => { + contractsToTest.push(contractName) + contractsToTestDetails.push(compilationResult[filename][contractName]) + }) + } catch (e) { + console.error(e) + } } if (isDirectory) { - fs.walkSync(filepath, foundpath => { + fs.walkSync(filepath, (foundpath: string) => { gatherContractsFrom(foundpath) }) } else { @@ -72,13 +76,14 @@ export function runTestFiles(filepath, isDirectory, web3, opts = {}) { } next(null, contractsToTest, contractsToTestDetails, contracts) }, - function runTests (contractsToTest, contractsToTestDetails, contracts, next) { - let totalPassing = 0 - let totalFailing = 0 - let totalTime = 0 + function runTests(contractsToTest, contractsToTestDetails, contracts, next: Function) { + let totalPassing: number = 0 + let totalFailing: number = 0 + let totalTime: number = 0 let errors: any[] = [] - var testCallback = function (result) { + var _testCallback = function (err: Error | null | undefined, result: TestResultInterface) { + if(err) throw err; if (result.type === 'contract') { signale.name(result.value.white) } else if (result.type === 'testPass') { @@ -88,20 +93,25 @@ export function runTestFiles(filepath, isDirectory, web3, opts = {}) { errors.push(result) } } - var resultsCallback = function (_err, result, cb) { + var _resultsCallback = (_err: Error | null | undefined, result: ResultsInterface, cb) => { totalPassing += result.passingNum totalFailing += result.failureNum totalTime += result.timePassed cb() } - async.eachOfLimit(contractsToTest, 1, (contractName, index, cb) => { - runTest(contractName, contracts(contractName), contractsToTestDetails[index], { accounts }, testCallback, (err, result) => { + async.eachOfLimit(contractsToTest, 1, (contractName: string, index, cb) => { + try { + runTest(contractName, contracts[contractName], contractsToTestDetails[index], { accounts }, _testCallback, (err, result) => { if (err) { + console.log(err) return cb(err) } - resultsCallback(null, result, cb) + _resultsCallback(null, result, cb) }) + } catch(e) { + console.error(e) + } }, function (err) { if (err) { return next(err) @@ -109,23 +119,23 @@ export function runTestFiles(filepath, isDirectory, web3, opts = {}) { console.log('\n') if (totalPassing > 0) { - console.log(('%c ' + totalPassing + ' passing ') + ('%c(' + totalTime + 's)'),'color: green','color: grey') + console.log(colors.green(totalPassing + ' passing ') + colors.grey('(' + totalTime + 's)')) } if (totalFailing > 0) { - console.log(('%c ' + totalFailing + ' failing'),'color: red') + console.log(colors.red(totalFailing + ' failing')) } console.log('') errors.forEach((error, index) => { console.log(' ' + (index + 1) + ') ' + error.context + ' ' + error.value) console.log('') - console.log(('%c\t error: ' + error.errMsg),'color: red') + console.log(colors.red('\t error: ' + error.errMsg)) }) console.log('') next() }) } - ], function () { + ], () => { }) } diff --git a/remix-tests/src/runTestSources.ts b/remix-tests/src/runTestSources.ts index 34da06f5f6..ee989b11a5 100644 --- a/remix-tests/src/runTestSources.ts +++ b/remix-tests/src/runTestSources.ts @@ -37,7 +37,7 @@ export function runTestSources(contractSources, testCallback, resultCallback, fi compileContractSources(contractSources, importFileCb, opts, next) }, function deployAllContracts (compilationResult, next) { - deployAll(compilationResult, web3, function (err, contracts) { + deployAll(compilationResult, web3, (err, contracts) => { if (err) { next(err) } @@ -58,10 +58,9 @@ export function runTestSources(contractSources, testCallback, resultCallback, fi contractsToTest.push(contractName) }) } - next(null, contractsToTest, contractsToTestDetails, contracts) }, - function runTests (contractsToTest, contractsToTestDetails, contracts, next) { + function runTests(contractsToTest, contractsToTestDetails, contracts, next) { let totalPassing = 0 let totalFailing = 0 let totalTime = 0 diff --git a/remix-tests/src/testRunner.ts b/remix-tests/src/testRunner.ts index b6d38470d2..72ed491331 100644 --- a/remix-tests/src/testRunner.ts +++ b/remix-tests/src/testRunner.ts @@ -2,7 +2,7 @@ import async from 'async' import * as changeCase from 'change-case' import Web3 from 'web3' -interface CbReturnInterface { +export interface TestResultInterface { type: string, value: any, time?: number, @@ -10,13 +10,19 @@ interface CbReturnInterface { errMsg?: string filename?: string } +interface RunListInterface { + name: string, + type: string, + constant: boolean, + signature?: any +} export interface ResultsInterface { passingNum: number, failureNum: number, timePassed: number } export interface TestCbInterface { - (error: Error | null | undefined, result?: CbReturnInterface) : void; + (error: Error | null | undefined, result: TestResultInterface) : void; } export interface ResultCbInterface { (error: Error | null | undefined, result: ResultsInterface) : void; @@ -47,27 +53,27 @@ function getTestFunctions (jsonInterface) { return jsonInterface.filter((x) => specialFunctions.indexOf(x.name) < 0 && x.type === 'function') } -function createRunList (jsonInterface) { +function createRunList(jsonInterface): RunListInterface[] { let availableFunctions = getAvailableFunctions(jsonInterface) let testFunctions = getTestFunctions(jsonInterface) - let runList: any[] = [] + let runList: RunListInterface[] = [] 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) { 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, signature: func.signature, type: 'test', constant: func.constant}) + runList.push({ name: func.name, signature: func.signature, type: 'test', constant: func.constant }) if (availableFunctions.indexOf('afterEach') >= 0) { - runList.push({name: 'afterEach', type: 'internal', constant: false}) + runList.push({ name: 'afterEach', type: 'internal', constant: false }) } } if (availableFunctions.indexOf('afterAll') >= 0) { - runList.push({name: 'afterAll', type: 'internal', constant: false}) + runList.push({ name: 'afterAll', type: 'internal', constant: false }) } return runList @@ -90,7 +96,7 @@ export function runTest(testName, testObject: any, contractDetails: any, opts: a signale.warn('e.g: the following code won\'t work in the current context:') signale.warn('TestsAccounts.getAccount(' + opts.accounts.length + ')') } - const resp: CbReturnInterface = { + const resp: TestResultInterface = { type: 'contract', value: testName, filename: testObject.filename @@ -113,7 +119,7 @@ export function runTest(testName, testObject: any, contractDetails: any, opts: a method.call(sendParams).then((result) => { let time = Math.ceil((Date.now() - startTime) / 1000.0) if (result) { - const resp: CbReturnInterface = { + const resp: TestResultInterface = { type: 'testPass', value: changeCase.sentenceCase(func.name), time: time, @@ -123,7 +129,7 @@ export function runTest(testName, testObject: any, contractDetails: any, opts: a passingNum += 1 timePassed += time } else { - const resp: CbReturnInterface = { + const resp: TestResultInterface = { type: 'testFailure', value: changeCase.sentenceCase(func.name), time: time, @@ -147,7 +153,7 @@ export function runTest(testName, testObject: any, contractDetails: any, opts: a if (event.raw.topics.indexOf(topic) >= 0) { var testEvent = web3.eth.abi.decodeParameters(['bool', 'string'], event.raw.data) if (!testEvent[0]) { - const resp: CbReturnInterface = { + const resp: TestResultInterface = { type: 'testFailure', value: changeCase.sentenceCase(func.name), time: time, @@ -163,7 +169,7 @@ export function runTest(testName, testObject: any, contractDetails: any, opts: a } if (testPassed) { - const resp: CbReturnInterface = { + const resp: TestResultInterface = { type: 'testPass', value: changeCase.sentenceCase(func.name), time: time, @@ -175,8 +181,7 @@ export function runTest(testName, testObject: any, contractDetails: any, opts: a return next() } catch (err) { - console.log('error!') - console.dir(err) + console.error(err) return next(err) } }).on('error', function (err: Error | null | undefined) {