From 3478079dcd31bb479672f39226c167728b0b20fb Mon Sep 17 00:00:00 2001 From: Omkara <0mkar@protonmail.com> Date: Thu, 7 Feb 2019 00:52:22 +0530 Subject: [PATCH] Add more typings --- remix-tests/src/compiler.ts | 23 +++++++++++------------ remix-tests/src/deployer.ts | 15 ++++++++------- remix-tests/src/index.ts | 6 +++--- remix-tests/src/run.ts | 8 +++++--- remix-tests/src/runTestFiles.ts | 6 ++---- remix-tests/src/runTestSources.ts | 13 ++++++------- remix-tests/src/testRunner.ts | 2 +- remix-tests/tests/testRunner.ts | 14 +++++++------- 8 files changed, 43 insertions(+), 44 deletions(-) diff --git a/remix-tests/src/compiler.ts b/remix-tests/src/compiler.ts index cf5f4f4540..90d111cb4d 100644 --- a/remix-tests/src/compiler.ts +++ b/remix-tests/src/compiler.ts @@ -1,10 +1,9 @@ /* eslint no-extend-native: "warn" */ -let fs = require('./fileSystem') +import fs from './fileSystem' var async = require('async') var path = require('path') let RemixCompiler = require('remix-solidity').Compiler - function regexIndexOf (inputString, regex, startpos = 0) { var indexOf = inputString.substring(startpos).search(regex) return (indexOf >= 0) ? (indexOf + (startpos)) : indexOf @@ -26,8 +25,8 @@ var userAgent = (typeof (navigator) !== 'undefined') && navigator.userAgent ? na var isBrowser = !(typeof (window) === 'undefined' || userAgent.indexOf(' electron/') > -1) // TODO: replace this with remix's own compiler code -export function compileFileOrFiles (filename, isDirectory, opts, cb) { - let compiler +export function compileFileOrFiles(filename: string, isDirectory: boolean, opts: any, cb: Function) { + let compiler: any let accounts = opts.accounts || [] const sources = { 'tests.sol': { content: require('../sol/tests.sol.js') }, @@ -43,7 +42,7 @@ export function compileFileOrFiles (filename, isDirectory, opts, cb) { // We should only walk through directory if a directory name is passed try { // walkSync only if it is a directory - fs.walkSync(filepath, foundpath => { + fs.walkSync(filepath, (foundpath: string) => { // only process .sol files if (foundpath.split('.').pop() === 'sol') { let c = fs.readFileSync(foundpath).toString() @@ -59,21 +58,21 @@ export function compileFileOrFiles (filename, isDirectory, opts, cb) { throw e } finally { async.waterfall([ - function loadCompiler (next) { + function loadCompiler(next: Function) { compiler = new RemixCompiler() compiler.onInternalCompilerLoaded() // compiler.event.register('compilerLoaded', this, function (version) { next() // }); }, - function doCompilation (next) { + function doCompilation(next: Function) { // @ts-ignore compiler.event.register('compilationFinished', this, function (success, data, source) { next(null, data) }) compiler.compile(sources, filepath) } - ], function (err, result) { + ], function (err: Error | null | undefined, result) { let errors = (result.errors || []).filter((e) => e.type === 'Error' || e.severity === 'error') if (errors.length > 0) { if (!isBrowser) require('signale').fatal(errors) @@ -84,7 +83,7 @@ export function compileFileOrFiles (filename, isDirectory, opts, cb) { } } -export function compileContractSources (sources, importFileCb, opts, cb) { +export function compileContractSources(sources, importFileCb, opts, cb) { let compiler, filepath let accounts = opts.accounts || [] // Iterate over sources keys. Inject test libraries. Inject test library import statements. @@ -102,21 +101,21 @@ export function compileContractSources (sources, importFileCb, opts, cb) { } async.waterfall([ - function loadCompiler (next) { + function loadCompiler (next: Function) { compiler = new RemixCompiler(importFileCb) compiler.onInternalCompilerLoaded() // compiler.event.register('compilerLoaded', this, function (version) { next() // }); }, - function doCompilation (next) { + function doCompilation (next: Function) { // @ts-ignore compiler.event.register('compilationFinished', this, function (success, data, source) { next(null, data) }) compiler.compile(sources, filepath) } - ], function (err, result) { + ], function (err: Error | null | undefined , result) { let errors = (result.errors || []).filter((e) => e.type === 'Error' || e.severity === 'error') if (errors.length > 0) { if (!isBrowser) require('signale').fatal(errors) diff --git a/remix-tests/src/deployer.ts b/remix-tests/src/deployer.ts index 157926aef6..0c091921cd 100644 --- a/remix-tests/src/deployer.ts +++ b/remix-tests/src/deployer.ts @@ -1,19 +1,20 @@ var async = require('async') var remixLib = require('remix-lib') +import Web3 from 'web3' -export function deployAll (compileResult, web3, callback) { +export function deployAll(compileResult: object, web3: Web3, callback: Function) { let compiledObject = {} let contracts = {} - let accounts = [] + let accounts: string[] = [] async.waterfall([ - function getAccountList (next) { + function getAccountList(next: Function) { web3.eth.getAccounts((_err, _accounts) => { accounts = _accounts next() }) }, - function getContractData (next) { + function getContractData(next: Function) { for (let contractFile in compileResult) { for (let contractName in compileResult[contractFile]) { let contract = compileResult[contractFile][contractName] @@ -38,8 +39,8 @@ export function deployAll (compileResult, web3, callback) { } next() }, - function determineContractsToDeploy (next) { - let contractsToDeploy = ['Assert'] + function determineContractsToDeploy(next: Function) { + let contractsToDeploy: string[] = ['Assert'] let allContracts = Object.keys(compiledObject) for (let contractName of allContracts) { @@ -52,7 +53,7 @@ export function deployAll (compileResult, web3, callback) { } next(null, contractsToDeploy) }, - function deployContracts (contractsToDeploy, next) { + function deployContracts(contractsToDeploy: string[], next: Function) { var deployRunner = (deployObject, contractObject, contractName, filename, callback) => { deployObject.estimateGas().then((gasValue) => { deployObject.send({ diff --git a/remix-tests/src/index.ts b/remix-tests/src/index.ts index 03aec74397..2ac9a00233 100644 --- a/remix-tests/src/index.ts +++ b/remix-tests/src/index.ts @@ -1,6 +1,6 @@ -import runTestFiles from './runTestFiles' -import runTestSources from './runTestSources' -import runTest from './testRunner' +import { runTestFiles } from './runTestFiles' +import { runTestSources } from './runTestSources' +import { runTest } from './testRunner' module.exports = { runTestFiles: runTestFiles, diff --git a/remix-tests/src/run.ts b/remix-tests/src/run.ts index dfc9961d20..217a43e843 100644 --- a/remix-tests/src/run.ts +++ b/remix-tests/src/run.ts @@ -1,7 +1,9 @@ +#!/usr/bin/env ts-node + const commander = require('commander') -const Web3 = require('web3') -import runTestFiles from './runTestFiles' -import fs = require('./fileSystem') +import Web3 from 'web3' +import { runTestFiles } from './runTestFiles' +import fs from './fileSystem' const Provider = require('remix-simulator').Provider import Log = require('./logger') const logger = new Log() diff --git a/remix-tests/src/runTestFiles.ts b/remix-tests/src/runTestFiles.ts index 1e0c200040..b6ea60ee3f 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 fs from './fileSystem' -import runTest from './testRunner' +import { runTest } from './testRunner' require('colors') import Compiler = require('./compiler') import Deployer = require('./deployer') -function runTestFiles(filepath, isDirectory, web3, opts = {}) { +export function runTestFiles(filepath, isDirectory, web3, opts = {}) { opts = opts || {} const { Signale } = require('signale') // signale configuration @@ -129,5 +129,3 @@ function runTestFiles(filepath, isDirectory, web3, opts = {}) { ], function () { }) } - -export = runTestFiles; diff --git a/remix-tests/src/runTestSources.ts b/remix-tests/src/runTestSources.ts index 867b1687c2..34da06f5f6 100644 --- a/remix-tests/src/runTestSources.ts +++ b/remix-tests/src/runTestSources.ts @@ -1,9 +1,9 @@ import async from 'async' require('colors') -import Compiler = require('./compiler.js') -import Deployer = require('./deployer.js') -import runTest from './testRunner' +import { compileContractSources } from './compiler' +import { deployAll } from './deployer' +import { runTest } from './testRunner' import Web3 = require('web3') import Provider from 'remix-simulator' @@ -21,7 +21,7 @@ var createWeb3Provider = function () { return web3 } -function runTestSources(contractSources, testCallback, resultCallback, finalCallback, importFileCb, opts) { +export function runTestSources(contractSources, testCallback, resultCallback, finalCallback, importFileCb, opts) { opts = opts || {} let web3 = opts.web3 || createWeb3Provider() let accounts = opts.accounts || null @@ -34,10 +34,10 @@ function runTestSources(contractSources, testCallback, resultCallback, finalCall }) }, function compile (next) { - Compiler.compileContractSources(contractSources, importFileCb, opts, next) + compileContractSources(contractSources, importFileCb, opts, next) }, function deployAllContracts (compilationResult, next) { - Deployer.deployAll(compilationResult, web3, function (err, contracts) { + deployAll(compilationResult, web3, function (err, contracts) { if (err) { next(err) } @@ -115,4 +115,3 @@ function runTestSources(contractSources, testCallback, resultCallback, finalCall } ], finalCallback) } -export = runTestSources; diff --git a/remix-tests/src/testRunner.ts b/remix-tests/src/testRunner.ts index 8f47424520..b6d38470d2 100644 --- a/remix-tests/src/testRunner.ts +++ b/remix-tests/src/testRunner.ts @@ -73,7 +73,7 @@ function createRunList (jsonInterface) { return runList } -export default function runTest (testName, testObject: any, contractDetails: any, opts: any, testCallback: TestCbInterface, resultsCallback: ResultCbInterface) { +export function runTest(testName, testObject: any, contractDetails: any, opts: any, testCallback: TestCbInterface, resultsCallback: ResultCbInterface) { let runList = createRunList(testObject._jsonInterface) let passingNum: number = 0 diff --git a/remix-tests/tests/testRunner.ts b/remix-tests/tests/testRunner.ts index 129d719b3e..1917d5e76c 100644 --- a/remix-tests/tests/testRunner.ts +++ b/remix-tests/tests/testRunner.ts @@ -3,9 +3,9 @@ import Web3 from 'web3' import * as assert from 'assert' import { Provider } from 'remix-simulator' -let Compiler = require('../dist/compiler.js') -let Deployer = require('../dist/deployer.js') -import runTest, { ResultsInterface, TestCbInterface, ResultCbInterface } from '../dist/testRunner.js' +import { compileFileOrFiles } from '../dist/compiler' +import { deployAll } from '../dist/deployer' +import { runTest, ResultsInterface, TestCbInterface, ResultCbInterface } from '../dist/testRunner' function compileAndDeploy(filename: string, callback: Function) { let web3: Web3 = new Web3() @@ -19,13 +19,13 @@ function compileAndDeploy(filename: string, callback: Function) { next(_err) }) }, - function compile(next: Function) { - Compiler.compileFileOrFiles(filename, false, { accounts }, next) + function compile(next: Function): void { + compileFileOrFiles(filename, false, { accounts }, next) }, function deployAllContracts(compilationResult: object, next: Function): void { try { compilationData = compilationResult - Deployer.deployAll(compilationResult, web3, next) + deployAll(compilationResult, web3, next) } catch (e) { throw e } @@ -39,7 +39,7 @@ function compileAndDeploy(filename: string, callback: Function) { describe('testRunner', () => { describe('#runTest', () => { describe('test with beforeAll', () => { - let filename = 'tests/examples_1/simple_storage_test.sol' + let filename: string = 'tests/examples_1/simple_storage_test.sol' let tests: any[] = [], results: ResultsInterface; before((done) => {