From 8f2c450ce78d625c4a1ec36406131b0521bbad8a Mon Sep 17 00:00:00 2001 From: aniket-engg Date: Mon, 18 Jan 2021 17:51:25 +0530 Subject: [PATCH] custom compiler in progress --- libs/remix-solidity/src/compiler/compiler.ts | 35 +++++++++++++++++++ libs/remix-tests/package.json | 2 +- libs/remix-tests/src/compiler.ts | 9 +++-- libs/remix-tests/src/run.ts | 2 +- libs/remix-tests/tests/testRunner.cli.spec.ts | 14 ++++++++ 5 files changed, 55 insertions(+), 7 deletions(-) diff --git a/libs/remix-solidity/src/compiler/compiler.ts b/libs/remix-solidity/src/compiler/compiler.ts index 95e1f92195..5b45a8c41b 100644 --- a/libs/remix-solidity/src/compiler/compiler.ts +++ b/libs/remix-solidity/src/compiler/compiler.ts @@ -163,6 +163,41 @@ export class Compiler { } } + /** + * @dev Load compiler using given version (used by remix-tests CLI) + * @param version compiler version + */ + + loadRemoteVersion (version: string): void { + console.log('Loading remote solc version ' + version) + const compiler: any = require('solc') + compiler.loadRemoteVersion(version, (err, remoteCompiler) => { + if (err) { + console.error('Error in loading remote solc compiler: ', err) + } else { + this.state.compileJSON = (source: SourceWithTarget) => { + const missingInputs: string[] = [] + const missingInputsCallback = (path: string) => { + missingInputs.push(path) + return { error: 'Deferred import' } + } + let result: CompilationResult = {} + try { + if(source && source.sources) { + const {optimize, runs, evmVersion, language} = this.state + const input = compilerInput(source.sources, {optimize, runs, evmVersion, language}) + result = JSON.parse(remoteCompiler.compile(input, { import: missingInputsCallback })) + } + } catch (exception) { + result = { error: { formattedMessage: 'Uncaught JavaScript exception:\n' + exception, severity: 'error', mode: 'panic' } } + } + this.onCompilationFinished(result, missingInputs, source) + } + this.onCompilerLoaded(version) + } + }) + } + /** * @dev Load compiler using given URL (used by IDE) * @param usingWorker if true, load compiler using worker diff --git a/libs/remix-tests/package.json b/libs/remix-tests/package.json index eb9062c841..334f7a402d 100644 --- a/libs/remix-tests/package.json +++ b/libs/remix-tests/package.json @@ -37,7 +37,7 @@ "dependencies": { "@remix-project/remix-lib": "^0.4.31", "@remix-project/remix-simulator": "^0.1.9-beta.8", - "@remix-project/remix-solidity": "^0.3.32", + "@remix-project/remix-solidity": "file:../remix-solidity", "ansi-gray": "^0.1.1", "async": "^2.6.0", "change-case": "^3.0.1", diff --git a/libs/remix-tests/src/compiler.ts b/libs/remix-tests/src/compiler.ts index 49b58d6e15..0c6c3a0f27 100644 --- a/libs/remix-tests/src/compiler.ts +++ b/libs/remix-tests/src/compiler.ts @@ -130,11 +130,10 @@ export function compileFileOrFiles(filename: string, isDirectory: boolean, opts: evmVersion ? compiler.set('evmVersion', evmVersion) : optimize ? compiler.set('optimize', optimize) : runs ? compiler.set('runs', runs) : - currentCompilerUrl ? compiler.loadVersion('false', currentCompilerUrl) : - compiler.onInternalCompilerLoaded() - // compiler.event.register('compilerLoaded', this, function (version) { - next() - // }); + currentCompilerUrl ? compiler.loadRemoteVersion(currentCompilerUrl) : compiler.onInternalCompilerLoaded() + compiler.event.register('compilerLoaded', this, function (version) { + next() + }); }, function doCompilation(next) { // @ts-ignore diff --git a/libs/remix-tests/src/run.ts b/libs/remix-tests/src/run.ts index d300807c66..1ecc09c879 100644 --- a/libs/remix-tests/src/run.ts +++ b/libs/remix-tests/src/run.ts @@ -89,7 +89,7 @@ commander process.exit() } else { log.info(`Compiler version set to ${compVersion}. Latest version is ${latestRelease}`) - compilerConfig.currentCompilerUrl = baseURL + '/' + compString + compilerConfig.currentCompilerUrl = compString.replace('soljson-', '').replace('.js', '') } } diff --git a/libs/remix-tests/tests/testRunner.cli.spec.ts b/libs/remix-tests/tests/testRunner.cli.spec.ts index 952a1623c8..9347988a50 100644 --- a/libs/remix-tests/tests/testRunner.cli.spec.ts +++ b/libs/remix-tests/tests/testRunner.cli.spec.ts @@ -50,5 +50,19 @@ Commands: expect(res.stdout.toString().trim()).toMatch(/expected value to be ok to: true/) expect(res.stdout.toString().trim()).toMatch(/returned: false/) }) + + test('remix-tests running a test file with custom compiler version', () => { + const res = spawnSync(executablePath, ['--compiler', '0.7.4', resolve(__dirname + '/examples_0/assert_ok_test.sol')]) + console.log('res.stdout.toString().trim()---------->', res.stdout.toString().trim()) + // match initial lines + expect(res.stdout.toString().trim()).toMatch(/Loading remote solc version v0.7.4+commit.3f05b770/) + expect(res.stdout.toString().trim()).toMatch(/:: Running remix-tests - Unit testing for solidity ::/) + expect(res.stdout.toString().trim()).toMatch(/creation of library remix_tests.sol:Assert pending.../) + // match test result + expect(res.stdout.toString().trim()).toMatch(/Ok pass test/) + expect(res.stdout.toString().trim()).toMatch(/Ok fail test/) + // macth fail test details + expect(res.stdout.toString().trim()).toMatch(/error: okFailTest fails/) + }) }) }) \ No newline at end of file