try to implement getFile

pull/7/head
0mkar 6 years ago
parent b12bfeff41
commit fcff184a00
  1. 10
      remix-resolve/src/combineSource.js
  2. 5
      remix-resolve/src/getFile.js
  3. 4
      remix-resolve/src/index.js
  4. 55
      remix-resolve/tests/test.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) {

@ -0,0 +1,5 @@
const getFile = function (path, sources) {
return sources[path].content
}
module.exports = getFile

@ -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
}

@ -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)
})
})
})

Loading…
Cancel
Save