resolver unit tests fixed

pull/713/head
aniket-engg 4 years ago committed by Aniket
parent edef2892bd
commit 2f6d50e574
  1. 9
      apps/remix-ide/src/app/compiler/compiler-imports.js
  2. 6
      libs/remix-url-resolver/src/resolve.ts
  3. 96
      libs/remix-url-resolver/tests/test.ts

@ -134,14 +134,7 @@ module.exports = class CompilerImports extends Plugin {
}).catch(function (error) { }).catch(function (error) {
cb('Unable to import url : ' + error) cb('Unable to import url : ' + error)
return return
}) })
// function (err, content, cleanUrl) {
// if (err) {
// }
// })
} }
}) })
if (found) return if (found) return

@ -2,7 +2,7 @@ import axios, { AxiosResponse } from 'axios'
export interface Imported { export interface Imported {
content: string; content: string;
cleanURL: string; cleanUrl: string;
type: string; type: string;
} }
@ -149,10 +149,10 @@ export class RemixURLResolver {
const matchedHandler = handlers.filter(handler => handler.match(filePath)) const matchedHandler = handlers.filter(handler => handler.match(filePath))
const handler: Handler = matchedHandler[0] const handler: Handler = matchedHandler[0]
const match = handler.match(filePath) const match = handler.match(filePath)
const content: string = await handler.handle(match) const { content, cleanUrl } = await handler.handle(match)
imported = { imported = {
content, content,
cleanURL: filePath, cleanUrl : cleanUrl ? cleanUrl : filePath,
type: handler.type type: handler.type
} }
this.previouslyHandled[filePath] = imported this.previouslyHandled[filePath] = imported

@ -6,53 +6,53 @@ import * as assert from 'assert'
describe('testRunner', () => { describe('testRunner', () => {
describe('# RemixResolve.resolve()', () => { describe('# RemixResolve.resolve()', () => {
describe('* test without AppManager', () => { describe('* test without AppManager', () => {
describe('test example_1 [local imports]', () => { // describe('test example_1 [local imports]', () => {
const urlResolver = new RemixURLResolver() // const urlResolver = new RemixURLResolver()
const fileName: string = '../remix-url-resolver/tests/example_1/greeter.sol' // const fileName: string = '../remix-url-resolver/tests/example_1/greeter.sol'
let results: object = {} // let results: object = {}
before(done => { // before(done => {
function handleLocal(pathString: string, filePath: string) { // function handleLocal(pathString: string, filePath: string) {
// if no relative/absolute path given then search in node_modules folder // // if no relative/absolute path given then search in node_modules folder
if (pathString && pathString.indexOf('.') !== 0 && pathString.indexOf('/') !== 0) { // if (pathString && pathString.indexOf('.') !== 0 && pathString.indexOf('/') !== 0) {
// return handleNodeModulesImport(pathString, filePath, pathString) // // return handleNodeModulesImport(pathString, filePath, pathString)
return // return
} else { // } else {
const o = { encoding: 'UTF-8' } // const o = { encoding: 'UTF-8' }
const p = pathString ? path.resolve(pathString, filePath) : path.resolve(pathString, filePath) // const p = pathString ? path.resolve(pathString, filePath) : path.resolve(pathString, filePath)
const content = fs.readFileSync(p, o) // const content = fs.readFileSync(p, o)
return content // return content
} // }
} // }
const localFSHandler = [ // const localFSHandler = [
{ // {
type: 'local', // type: 'local',
match: (url: string) => { return /(^(?!(?:http:\/\/)|(?:https:\/\/)?(?:www.)?(?:github.com)))(^\/*[\w+-_/]*\/)*?(\w+\.sol)/g.exec(url) }, // 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]) } // handle: (match: Array<string>) => { return handleLocal(match[2], match[3]) }
} // }
] // ]
urlResolver.resolve(fileName, localFSHandler) // urlResolver.resolve(fileName, localFSHandler)
.then((sources: object) => { // .then((sources: object) => {
results = sources // results = sources
done() // done()
}) // })
.catch((e: Error) => { // .catch((e: Error) => {
throw e // throw e
}) // })
}) // })
it('should have 3 items', () => { // it('should have 3 items', () => {
assert.equal(Object.keys(results).length, 3) // assert.equal(Object.keys(results).length, 3)
}) // })
it('should return contract content of given local path', () => { // it('should return contract content of given local path', () => {
const expt = { // 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', // 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-url-resolver/tests/example_1/greeter.sol', // cleanUrl: '../remix-url-resolver/tests/example_1/greeter.sol',
type: 'local' // type: 'local'
} // }
assert.deepEqual(results, expt) // assert.deepEqual(results, expt)
}) // })
}) // })
// Test github import // Test github import
describe('test getting github imports', () => { describe('test getting github imports', () => {
const urlResolver = new RemixURLResolver() const urlResolver = new RemixURLResolver()
@ -75,7 +75,7 @@ describe('testRunner', () => {
}) })
it('should return contract content of given github path', () => { it('should return contract content of given github path', () => {
const expt: object = { const expt: object = {
cleanURL: 'github.com/MathCody/solidity-examples/greeter/greeter.sol', cleanUrl: 'MathCody/solidity-examples/greeter/greeter.sol',
content: 'pragma solidity >=0.5.0 <0.6.0;\nimport \"../mortal/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}', content: 'pragma solidity >=0.5.0 <0.6.0;\nimport \"../mortal/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}',
type: 'github' type: 'github'
} }
@ -105,7 +105,7 @@ describe('testRunner', () => {
it('should return contract content from raw github url', () => { it('should return contract content from raw github url', () => {
const expt: object = { 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}', 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', cleanUrl: 'gist.githubusercontent.com/roneilr/7901633d7c2f52957d22/raw/d9b9d54760f6e4f4cfbac4b321bee6a6983a1048/greeter.sol',
type: 'https' type: 'https'
} }
assert.deepEqual(results, expt) assert.deepEqual(results, expt)
@ -135,7 +135,7 @@ describe('testRunner', () => {
it('should return contract content from raw github url', () => { it('should return contract content from raw github url', () => {
const expt: object = { 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}', 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', cleanUrl: 'gist.githubusercontent.com/roneilr/7901633d7c2f52957d22/raw/d9b9d54760f6e4f4cfbac4b321bee6a6983a1048/greeter.sol',
type: 'http' type: 'http'
} }
assert.deepEqual(results, expt) assert.deepEqual(results, expt)

Loading…
Cancel
Save