pull/741/head
aniket-engg 4 years ago
parent 9c0bb0fb1b
commit a6b0e14036
  1. 20
      libs/remix-tests/src/compiler.ts

@ -45,15 +45,15 @@ function isRemixTestFile(path: string) {
*/ */
function processFile(filePath: string, sources: SrcIfc, isRoot = false) { function processFile(filePath: string, sources: SrcIfc, isRoot = false) {
const importRegEx = /import ['"](.+?)['"];/g; const importRegEx = /import ['"](.+?)['"];/g
let group: RegExpExecArray| null = null; let group: RegExpExecArray| null = null
const isFileAlreadyInSources: boolean = Object.keys(sources).includes(filePath) const isFileAlreadyInSources: boolean = Object.keys(sources).includes(filePath)
// Return if file is a remix test file or already processed // Return if file is a remix test file or already processed
if(isRemixTestFile(filePath) || isFileAlreadyInSources) if(isRemixTestFile(filePath) || isFileAlreadyInSources)
return return
let content: string = fs.readFileSync(filePath, { encoding: 'utf-8' }); let content: string = fs.readFileSync(filePath, { encoding: 'utf-8' })
const testFileImportRegEx = /^(import)\s['"](remix_tests.sol|tests.sol)['"];/gm const testFileImportRegEx = /^(import)\s['"](remix_tests.sol|tests.sol)['"];/gm
// import 'remix_tests.sol', if file is a root test contract file and doesn't already have it // import 'remix_tests.sol', if file is a root test contract file and doesn't already have it
@ -61,13 +61,13 @@ function processFile(filePath: string, sources: SrcIfc, isRoot = false) {
const includeTestLibs = '\nimport \'remix_tests.sol\';\n' const includeTestLibs = '\nimport \'remix_tests.sol\';\n'
content = includeTestLibs.concat(content) content = includeTestLibs.concat(content)
} }
sources[filePath] = {content}; sources[filePath] = {content}
importRegEx.exec(''); // Resetting state of RegEx importRegEx.exec('') // Resetting state of RegEx
// Process each 'import' in file content // Process each 'import' in file content
while ((group = importRegEx.exec(content))) { while ((group = importRegEx.exec(content))) {
const importedFile: string = group[1]; const importedFile: string = group[1]
const importedFilePath: string = path.join(path.dirname(filePath), importedFile); const importedFilePath: string = path.join(path.dirname(filePath), importedFile)
processFile(importedFilePath, sources) processFile(importedFilePath, sources)
} }
} }
@ -103,11 +103,11 @@ export function compileFileOrFiles(filename: string, isDirectory: boolean, opts:
} }
} else { } else {
// walkSync only if it is a directory // walkSync only if it is a directory
let testFileCount = 0; let testFileCount = 0
fs.walkSync(filepath, (foundpath: string) => { fs.walkSync(filepath, (foundpath: string) => {
// only process .sol files // only process .sol files
if (foundpath.split('.').pop() === 'sol' && foundpath.endsWith('_test.sol')) { if (foundpath.split('.').pop() === 'sol' && foundpath.endsWith('_test.sol')) {
testFileCount++; testFileCount++
processFile(foundpath, sources, true) processFile(foundpath, sources, true)
} }
}) })
@ -135,7 +135,7 @@ export function compileFileOrFiles(filename: string, isDirectory: boolean, opts:
compiler.loadRemoteVersion(currentCompilerUrl) compiler.loadRemoteVersion(currentCompilerUrl)
compiler.event.register('compilerLoaded', this, function (version) { compiler.event.register('compilerLoaded', this, function (version) {
next() next()
}); })
} else { } else {
compiler.onInternalCompilerLoaded() compiler.onInternalCompilerLoaded()
next() next()

Loading…
Cancel
Save