Merge branch 'remix-resolve' of github.com:ethereum/remix into remix-resolve

pull/5370/head
0mkar 6 years ago
commit 7b1e49951d
  1. 35
      remix-resolve/src/resolve.ts
  2. 154
      remix-resolve/tests/test.ts

@ -1,4 +1,4 @@
import axios from 'axios'
import axios, { AxiosResponse } from 'axios'
import { Api, ModuleProfile, API } from 'remix-plugin'
@ -42,24 +42,40 @@ export class RemixResolveApi implements API<RemixResolve> {
* @params root The root of the github import statement
* @params filePath path of the file in github
*/
handleGithubCall(root: string, filePath: string) {
return
async handleGithubCall(root: string, filePath: string) {
try {
let req: string = 'https://api.github.com/repos/' + root + '/contents/' + filePath
const response: AxiosResponse = await axios.get(req)
return Buffer.from(response.data.content, 'base64').toString()
} catch(e) {
throw e
}
}
/**
* Handle an import statement based on http
* @params url The url of the import statement
* @params cleanURL
*/
handleHttp(url: string, cleanURL: string) {
return
async handleHttp(url: string, _: string) {
try {
const response: AxiosResponse = await axios.get(url)
return response.data
} catch(e) {
throw e
}
}
/**
* Handle an import statement based on https
* @params url The url of the import statement
* @params cleanURL
*/
handleHttps(url: string, cleanURL: string) {
return
async handleHttps(url: string, _: string) {
try {
const response: AxiosResponse = await axios.get(url)
return response.data
} catch(e) {
throw e
}
}
handleSwarm(url: string, cleanURL: string) {
return
@ -75,15 +91,12 @@ export class RemixResolveApi implements API<RemixResolve> {
const req = 'https://gateway.ipfs.io/' + url
// If you don't find greeter.sol on ipfs gateway use local
// const req = 'http://localhost:8080/' + url
const response = await axios.get(req)
const response: AxiosResponse = await axios.get(req)
return response.data
} catch (e) {
throw e
}
}
handleLocal(root: string, filePath: string) {
return
}
getHandlers(): Handler[] {
return [
{

@ -17,10 +17,10 @@ interface IAppManager {
}
describe('testRunner', () => {
describe('#resolve', () => {
describe('test example_1 [local imports]]', () => {
describe('# RemixResolveApi.resolve()', () => {
// AppManager tests
describe('test with AppManager', () => {
describe('* test with AppManager', () => {
describe('test example_1 [local imports]', () => {
let app: AppManager<IAppManager>
let api: RemixResolveApi
@ -53,19 +53,19 @@ describe('testRunner', () => {
}
]
app['modules'][api.type].api.resolve(fileName, localFSHandler)
.then(sources => {
.then((sources: object) => {
results = sources
done()
})
.catch(e => {
.catch((e: Error) => {
throw e
})
})
it('Plugin should be added to app', () => {
assert.equal(typeof(app.calls[api.type].resolve), 'function')
})
it('should returns contract content of given local path', () => {
it('should return contract content of given local path', () => {
const expt = {
content: 'pragma solidity ^0.5.0;\nimport "./mortal.sol";\n\ncontract Greeter is Mortal {\n /* Define variable greeting of the type string */\n string greeting;\n\n /* This runs when the contract is executed */\n constructor(string memory _greeting) public {\n greeting = _greeting;\n }\n\n /* Main function */\n function greet() public view returns (string memory) {\n return greeting;\n }\n}\n',
cleanURL: '../remix-resolve/tests/example_1/greeter.sol',
@ -74,7 +74,6 @@ describe('testRunner', () => {
assert.deepEqual(results, expt)
})
})
// test IPFShandle
describe('test getting IPFS files', function() {
let app: AppManager<IAppManager>
@ -106,7 +105,7 @@ describe('testRunner', () => {
it('should have 3 items', () => {
assert.equal(Object.keys(results).length, 3)
})
it('should returns contract content of given local path', () => {
it('should return contract content of given IPFS path', () => {
const expt = {
content: 'pragma solidity ^0.5.0;\nimport "./mortal.sol";\n\ncontract Greeter is Mortal {\n /* Define variable greeting of the type string */\n string greeting;\n\n /* This runs when the contract is executed */\n constructor(string memory _greeting) public {\n greeting = _greeting;\n }\n\n /* Main function */\n function greet() public view returns (string memory) {\n return greeting;\n }\n}\n',
cleanURL: 'ipfs://QmeKtwMBqz5Ac7oL8SyTD96mccEzw9X9d39jLb2kgnBYbn',
@ -116,5 +115,142 @@ describe('testRunner', () => {
})
})
})
describe('* test without AppManager', () => {
describe('test example_1 [local imports]', () => {
const remixResolve = new RemixResolveApi()
const fileName: string = '../remix-resolve/tests/example_1/greeter.sol'
let results: object = {}
before(done => {
function handleLocal(pathString: string, filePath: string) {
// if no relative/absolute path given then search in node_modules folder
if (pathString && pathString.indexOf('.') !== 0 && pathString.indexOf('/') !== 0) {
// return handleNodeModulesImport(pathString, filePath, pathString)
return
} else {
const o = { encoding: 'UTF-8' }
const p = pathString ? path.resolve(pathString, filePath) : path.resolve(pathString, filePath)
const content = fs.readFileSync(p, o)
return content
}
}
const localFSHandler = [
{
type: 'local',
match: (url: string) => { return /(^(?!(?:http:\/\/)|(?:https:\/\/)?(?:www.)?(?:github.com)))(^\/*[\w+-_/]*\/)*?(\w+\.sol)/g.exec(url) },
handle: (match: Array<string>) => { return handleLocal(match[2], match[3]) }
}
]
remixResolve.resolve(fileName, localFSHandler)
.then((sources: object) => {
results = sources
done()
})
.catch((e: Error) => {
throw e
})
})
it('should have 3 items', () => {
assert.equal(Object.keys(results).length, 3)
})
it('should return contract content of given local path', () => {
const expt = {
content: 'pragma solidity ^0.5.0;\nimport "./mortal.sol";\n\ncontract Greeter is Mortal {\n /* Define variable greeting of the type string */\n string greeting;\n\n /* This runs when the contract is executed */\n constructor(string memory _greeting) public {\n greeting = _greeting;\n }\n\n /* Main function */\n function greet() public view returns (string memory) {\n return greeting;\n }\n}\n',
cleanURL: '../remix-resolve/tests/example_1/greeter.sol',
type: 'local'
}
assert.deepEqual(results, expt)
})
})
// Test github import
describe('test getting github imports', () => {
const remixResolve = new RemixResolveApi()
const fileName: string = 'github.com/ethereum/populus/docs/assets/Greeter.sol'
let results: object = {}
before(done => {
remixResolve.resolve(fileName)
.then((sources: object) => {
results = sources
done()
})
.catch((e: Error) => {
throw e
})
})
it('should have 3 items', () => {
assert.equal(Object.keys(results).length, 3)
})
it('should return contract content of given github path', () => {
const expt: object = {
cleanURL: 'github.com/ethereum/populus/docs/assets/Greeter.sol',
content: 'pragma solidity ^0.4.0;\n\ncontract Greeter {\n string public greeting;\n\n // TODO: Populus seems to get no bytecode if `internal`\n function Greeter() public {\n greeting = \'Hello\';\n }\n\n function setGreeting(string _greeting) public {\n greeting = _greeting;\n }\n\n function greet() public constant returns (string) {\n return greeting;\n }\n}\n',
type: 'github'
}
assert.deepEqual(results, expt)
})
})
// Test https imports
describe('test getting https imports', () => {
const remixResolve = new RemixResolveApi()
const fileName: string = 'https://gist.githubusercontent.com/roneilr/7901633d7c2f52957d22/raw/d9b9d54760f6e4f4cfbac4b321bee6a6983a1048/greeter.sol'
let results: object = {}
before(done => {
remixResolve.resolve(fileName)
.then((sources: object) => {
results = sources
done()
})
.catch((e: Error) => {
throw e
})
})
it('should have 3 items', () => {
assert.equal(Object.keys(results).length, 3)
})
it('should return contract content from raw github url', () => {
const expt: object = {
content: 'contract mortal {\n /* Define variable owner of the type address*/\n address owner;\n\n /* this function is executed at initialization and sets the owner of the contract */\n function mortal() { owner = msg.sender; }\n\n /* Function to recover the funds on the contract */\n function kill() { if (msg.sender == owner) suicide(owner); }\n}\n\ncontract greeter is mortal {\n /* define variable greeting of the type string */\n string greeting;\n\n /* this runs when the contract is executed */\n function greeter(string _greeting) public {\n greeting = _greeting;\n }\n\n /* main function */\n function greet() constant returns (string) {\n return greeting;\n }\n}',
cleanURL: 'https://gist.githubusercontent.com/roneilr/7901633d7c2f52957d22/raw/d9b9d54760f6e4f4cfbac4b321bee6a6983a1048/greeter.sol',
type: 'https'
}
assert.deepEqual(results, expt)
})
})
// Test http imports
describe('test getting http imports', () => {
const remixResolve = new RemixResolveApi()
const fileName: string = 'http://gist.githubusercontent.com/roneilr/7901633d7c2f52957d22/raw/d9b9d54760f6e4f4cfbac4b321bee6a6983a1048/greeter.sol'
let results: object = {}
before(done => {
remixResolve.resolve(fileName)
.then((sources: object) => {
results = sources
done()
})
.catch((e: Error) => {
throw e
})
})
it('should have 3 items', () => {
assert.equal(Object.keys(results).length, 3)
})
it('should return contract content from raw github url', () => {
const expt: object = {
content: 'contract mortal {\n /* Define variable owner of the type address*/\n address owner;\n\n /* this function is executed at initialization and sets the owner of the contract */\n function mortal() { owner = msg.sender; }\n\n /* Function to recover the funds on the contract */\n function kill() { if (msg.sender == owner) suicide(owner); }\n}\n\ncontract greeter is mortal {\n /* define variable greeting of the type string */\n string greeting;\n\n /* this runs when the contract is executed */\n function greeter(string _greeting) public {\n greeting = _greeting;\n }\n\n /* main function */\n function greet() constant returns (string) {\n return greeting;\n }\n}',
cleanURL: 'http://gist.githubusercontent.com/roneilr/7901633d7c2f52957d22/raw/d9b9d54760f6e4f4cfbac4b321bee6a6983a1048/greeter.sol',
type: 'http'
}
assert.deepEqual(results, expt)
})
})
})
})
})

Loading…
Cancel
Save