From fcff184a0011b2bdabd7d12c9f09d4fe6326c344 Mon Sep 17 00:00:00 2001 From: 0mkar <0mkar@protonmail.com> Date: Tue, 4 Dec 2018 16:21:15 +0530 Subject: [PATCH] try to implement getFile --- remix-resolve/src/combineSource.js | 10 +++--- remix-resolve/src/getFile.js | 5 +++ remix-resolve/src/index.js | 4 ++- remix-resolve/tests/test.js | 55 ++++++++++++++++++++++++++++-- 4 files changed, 65 insertions(+), 9 deletions(-) create mode 100644 remix-resolve/src/getFile.js diff --git a/remix-resolve/src/combineSource.js b/remix-resolve/src/combineSource.js index 68f1947082..f08b8b2ee4 100644 --- a/remix-resolve/src/combineSource.js +++ b/remix-resolve/src/combineSource.js @@ -5,7 +5,7 @@ const resolve = require('./resolve.js') combineSource(//basepath, //sources object) */ const combineSource = async function (rootpath, sources) { - let fn, importLine, ir + let fn, ir var matches = [] ir = /^(?:import){1}(.+){0,1}\s['"](.+)['"];/gm let match = null @@ -15,8 +15,8 @@ const combineSource = async function (rootpath, sources) { matches.push(match) } for (let match of matches) { - importLine = match[0] - const extra = match[1] ? match[1] : '' + // importLine = match[0] + // const extra = match[1] ? match[1] : '' if (validUrl.isUri(rootpath)) { fn = url.resolve(rootpath, match[2]) } else { @@ -27,8 +27,8 @@ const combineSource = async function (rootpath, sources) { if (fn.localeCompare('remix_tests.sol') !== 0 && fn.localeCompare('tests.sol') !== 0) { let subSorce = {} const response = await resolve(rootpath, fn) - sources[fileName].content = sources[fileName].content.replace(importLine, 'import' + extra + ' \'' + response.filename + '\';') - subSorce[response.filename] = { content: response.content } + // sources[fileName].content = sources[fileName].content.replace(importLine, 'import' + extra + ' \'' + response.filename + '\';') + subSorce[fn] = { content: response.content } sources = Object.assign(await combineSource(response.rootpath, subSorce), sources) } } catch (e) { diff --git a/remix-resolve/src/getFile.js b/remix-resolve/src/getFile.js new file mode 100644 index 0000000000..92cf8f1d6e --- /dev/null +++ b/remix-resolve/src/getFile.js @@ -0,0 +1,5 @@ +const getFile = function (path, sources) { + return sources[path].content +} + +module.exports = getFile diff --git a/remix-resolve/src/index.js b/remix-resolve/src/index.js index eaa3b1d1ed..2329d0cbf7 100644 --- a/remix-resolve/src/index.js +++ b/remix-resolve/src/index.js @@ -5,8 +5,10 @@ const input = rr.combineSource({ 'greeter.sol': content }) */ const resolve = require('./resolve.js') const combineSource = require('./combineSource.js') +const getFile = require('./getFile.js') module.exports = { resolve: resolve, - combineSource: combineSource + combineSource: combineSource, + getFile: getFile } diff --git a/remix-resolve/tests/test.js b/remix-resolve/tests/test.js index 42e7de4667..7ea08b2111 100644 --- a/remix-resolve/tests/test.js +++ b/remix-resolve/tests/test.js @@ -7,7 +7,7 @@ describe('testRunner', function () { describe('#combineSource', function() { describe('test example_1 [local imports]]', function () { let filename = '../remix-resolve/tests/example_1/greeter.sol' - let tests = [], results = {} + let results = {} before(function (done) { const content = fs.readFileSync(filename).toString() @@ -35,7 +35,7 @@ describe('testRunner', function () { describe('test example_2 [github imports]', function () { let filename = '../remix-resolve/tests/example_2/github_import.sol' - let tests = [], results = {} + let results = {} before(function (done) { const content = fs.readFileSync(filename).toString() @@ -65,7 +65,7 @@ describe('testRunner', function () { // Test successful compile after combineSource describe('test example_1 for successful compile', function() { let filename = '../remix-resolve/tests/example_1/greeter.sol' - let tests = [], results = {} + let results = {} before(function (done) { const content = fs.readFileSync(filename).toString() @@ -135,4 +135,53 @@ describe('testRunner', function () { assert.notDeepStrictEqual(results, msg) }) }) + + // Test handleImportCb + describe('test github imports with callback', function() { + let filename = '../remix-resolve/tests/example_1/greeter.sol' + let sources = {}, results = {} + + before(function (done) { + const outputSelection = { + // Enable the metadata and bytecode outputs of every single contract. + '*': { + '': ['ast', 'legacyAST'], + '*': ['abi', 'evm.bytecode.object', 'devdoc', 'userdoc', 'evm.gasEstimates'] + } + } + const settings = { + optimizer: { enabled: true, runs: 500 }, + evmVersion: 'byzantium', + outputSelection + } + + const content = fs.readFileSync(filename).toString() + var sources = [] + sources['greeter.sol'] = { 'content': content } + rr.combineSource('../remix-resolve/tests/example_1/', sources) + .then(combinedSources => { + console.log(combinedSources) + const input = { language: 'Solidity', sources: combinedSources, settings } + const findImportsSync = function (path) { + console.log(path) + console.log(sources) + // return rr.getFile(path, sources) + } + try { + results = solc.compile(JSON.stringify(input), findImportsSync) + done() + } catch (e) { + throw e + } + }) + .catch(e => { + throw e + }) + }) + it('should not match file not found error msg', function () { + console.log(results) + const msg = "{\"contracts\":{},\"errors\":[{\"component\":\"general\",\"formattedMessage\":\"github_import.sol:2:1: ParserError: Source \\\"https://github.com/OpenZeppelin/zeppelin-solidity/contracts/math/SafeMath.sol\\\" not found: File not found.\\nimport 'https://github.com/OpenZeppelin/zeppelin-solidity/contracts/math/SafeMath.sol';\\n^-------------------------------------------------------------------------------------^\\n\",\"message\":\"Source \\\"https://github.com/OpenZeppelin/zeppelin-solidity/contracts/math/SafeMath.sol\\\" not found: File not found.\",\"severity\":\"error\",\"sourceLocation\":{\"end\":111,\"file\":\"github_import.sol\",\"start\":24},\"type\":\"ParserError\"}],\"sources\":{}}" + assert.notDeepStrictEqual(results, msg) + }) + }) })