test files created during compilation in SUT

pull/5370/head
Aniket-Engg 3 years ago committed by yann300
parent 211c7fbbfb
commit 4f0e42bf72
  1. 15
      libs/remix-core-plugin/src/lib/compiler-content-imports.ts
  2. 2
      libs/remix-tests/sol/tests_accounts.sol.ts
  3. 19
      libs/remix-tests/src/compiler.ts
  4. 1
      libs/remix-tests/src/index.ts

@ -1,7 +1,8 @@
'use strict' 'use strict'
import { Plugin } from '@remixproject/engine' import { Plugin } from '@remixproject/engine'
import { RemixURLResolver } from '@remix-project/remix-url-resolver' import { RemixURLResolver } from '@remix-project/remix-url-resolver'
const remixTests = require('@remix-project/remix-tests') // const remixTests = require('@remix-project/remix-tests')
var { assertLibCode, getAccountsLib } = require('@remix-project/remix-tests')
const profile = { const profile = {
name: 'contentImport', name: 'contentImport',
@ -106,10 +107,12 @@ export class CompilerImports extends Plugin {
}) })
} }
async importTestFiles () { async importTestFiles (url) {
const provider = await this.call('fileManager', 'getProviderOf', null) const provider = await this.call('fileManager', 'getProviderOf', null)
const content = remixTests.assertLibCode let content
if (provider) provider.addExternal('.deps/remix-tests/remix-tests.sol', content, 'remix_tests.sol') if (url === 'remix_tests.sol' || url === 'tests.sol') content = assertLibCode
else if (url === 'remix_accounts.sol') content = getAccountsLib()
if (provider) provider.addExternal('.deps/remix-tests/' + url, content, url)
return content return content
} }
@ -124,9 +127,7 @@ export class CompilerImports extends Plugin {
* @returns {Promise} - string content * @returns {Promise} - string content
*/ */
async resolveAndSave (url, targetPath) { async resolveAndSave (url, targetPath) {
if (url.indexOf('remix_tests.sol') !== -1) { if (['remix_tests.sol', 'tests.sol', 'remix_accounts.sol'].includes(url)) return await this.importTestFiles(url)
return await this.importTestFiles()
}
try { try {
const provider = await this.call('fileManager', 'getProviderOf', url) const provider = await this.call('fileManager', 'getProviderOf', url)
if (provider) { if (provider) {

@ -3,7 +3,7 @@ module.exports = `// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.4.22 <0.9.0; pragma solidity >=0.4.22 <0.9.0;
library TestsAccounts { library TestsAccounts {
function getAccount(uint index) public returns (address) { function getAccount(uint index) pure public returns (address) {
>accounts< >accounts<
return accounts[index]; return accounts[index];
} }

@ -6,6 +6,11 @@ import { Compiler as RemixCompiler } from '@remix-project/remix-solidity'
import { SrcIfc, CompilerConfiguration, CompilationErrors } from './types' import { SrcIfc, CompilerConfiguration, CompilationErrors } from './types'
const logger = new Log() const logger = new Log()
const log = logger.logger const log = logger.logger
var accountsLibCode
export function getAccountsLib () {
return accountsLibCode
}
function regexIndexOf (inputString: string, regex: RegExp, startpos = 0) { function regexIndexOf (inputString: string, regex: RegExp, startpos = 0) {
const indexOf = inputString.substring(startpos).search(regex) const indexOf = inputString.substring(startpos).search(regex)
@ -18,10 +23,10 @@ function writeTestAccountsContract (accounts: string[]) {
if (!accounts.length) body += ';' if (!accounts.length) body += ';'
else { else {
accounts.map((address, index) => { accounts.map((address, index) => {
body += `\naccounts[${index}] = ${address};\n` body += `\n\t\taccounts[${index}] = ${address};\n`
}) })
} }
return testAccountContract.replace('>accounts<', body) accountsLibCode = testAccountContract.replace('>accounts<', body)
} }
/** /**
@ -87,10 +92,11 @@ const isBrowser = !(typeof (window) === 'undefined' || userAgent.indexOf(' elect
export function compileFileOrFiles (filename: string, isDirectory: boolean, opts: any, compilerConfig: CompilerConfiguration, cb): void { export function compileFileOrFiles (filename: string, isDirectory: boolean, opts: any, compilerConfig: CompilerConfiguration, cb): void {
let compiler: any let compiler: any
const accounts: string[] = opts.accounts || [] const accounts: string[] = opts.accounts || []
writeTestAccountsContract(accounts)
const sources: SrcIfc = { const sources: SrcIfc = {
'tests.sol': { content: require('../sol/tests.sol') }, 'tests.sol': { content: require('../sol/tests.sol') },
'remix_tests.sol': { content: require('../sol/tests.sol') }, 'remix_tests.sol': { content: require('../sol/tests.sol') },
'remix_accounts.sol': { content: writeTestAccountsContract(accounts) } 'remix_accounts.sol': { content: getAccountsLib() }
} }
const filepath: string = (isDirectory ? filename : path.dirname(filename)) const filepath: string = (isDirectory ? filename : path.dirname(filename))
try { try {
@ -173,13 +179,8 @@ export function compileFileOrFiles (filename: string, isDirectory: boolean, opts
export function compileContractSources (sources: SrcIfc, compilerConfig: CompilerConfiguration, importFileCb: any, opts: any, cb): void { export function compileContractSources (sources: SrcIfc, compilerConfig: CompilerConfiguration, importFileCb: any, opts: any, cb): void {
let compiler let compiler
const accounts: string[] = opts.accounts || [] const accounts: string[] = opts.accounts || []
writeTestAccountsContract(accounts)
const filepath = opts.testFilePath || '' const filepath = opts.testFilePath || ''
// Iterate over sources keys. Inject test libraries. Inject test library import statements.
if (!('remix_tests.sol' in sources) && !('tests.sol' in sources)) {
sources['tests.sol'] = { content: require('../sol/tests.sol.js') }
// sources['remix_tests.sol'] = { content: require('../sol/tests.sol.js') }
sources['remix_accounts.sol'] = { content: writeTestAccountsContract(accounts) }
}
const testFileImportRegEx = /^(import)\s['"](remix_tests.sol|tests.sol)['"];/gm const testFileImportRegEx = /^(import)\s['"](remix_tests.sol|tests.sol)['"];/gm
const includeTestLibs = '\nimport \'remix_tests.sol\';\n' const includeTestLibs = '\nimport \'remix_tests.sol\';\n'

@ -3,3 +3,4 @@ export { UnitTestRunner } from './runTestSources'
export { runTest } from './testRunner' export { runTest } from './testRunner'
export * from './types' export * from './types'
export const assertLibCode = require('../sol/tests.sol') export const assertLibCode = require('../sol/tests.sol')
export { getAccountsLib } from './compiler'

Loading…
Cancel
Save