diff --git a/remix-resolve/package.json b/remix-resolve/package.json index 1e2a6544b0..fd1e51fa7f 100644 --- a/remix-resolve/package.json +++ b/remix-resolve/package.json @@ -9,7 +9,7 @@ "scripts": { "build": "tsc", "lint": "standard", - "test": "standard && mocha tests/ -t 300000" + "test": "standard && mocha --require ts-node/register tests/*.ts -t 300000" }, "repository": { "type": "git", @@ -35,8 +35,14 @@ "valid-url": "^1.0.9" }, "devDependencies": { + "@types/chai": "^4.1.7", + "@types/mocha": "^5.2.5", + "@types/node": "^10.12.18", + "chai": "^4.2.0", "mocha": "^5.1.0", + "remix-plugin": "0.0.1-alpha.2", "standard": "^12.0.1", + "ts-node": "^7.0.1", "tslint": "^5.11.0", "typescript": "^3.1.6" } diff --git a/remix-resolve/src/resolve.ts b/remix-resolve/src/resolve.ts index ca67d272be..b790cfbe5f 100644 --- a/remix-resolve/src/resolve.ts +++ b/remix-resolve/src/resolve.ts @@ -1,5 +1,5 @@ import axios from 'axios' -interface Imported { +export interface Imported { content: string; cleanURL: string; type: string; @@ -96,7 +96,7 @@ export class ImportResolver { } ] } - async resolve(filePath: string, customHandlers?: Handler[]) { + async resolve(filePath: string, customHandlers?: Handler[]): Promise { var imported: Imported = this.previouslyHandled[filePath] if(imported) { return imported diff --git a/remix-resolve/tests/test.js b/remix-resolve/tests/test.js deleted file mode 100644 index 8d31ab0de7..0000000000 --- a/remix-resolve/tests/test.js +++ /dev/null @@ -1,85 +0,0 @@ -const rr = require('../dist/index.js') -const assert = require('assert') -const fs = require('fs') -const path = require('path') - -describe('testRunner', function () { - describe('#resolve', function() { - describe('test example_1 [local imports]]', function () { - const fileName = '../remix-resolve/tests/example_1/greeter.sol' - let results = {} - - before(function (done) { - const resolver = new rr.ImportResolver() - var sources = [] - function handleLocal(pathString, filePath) { - // 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) - } 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) => { return /(^(?!(?:http:\/\/)|(?:https:\/\/)?(?:www.)?(?:github.com)))(^\/*[\w+-_/]*\/)*?(\w+\.sol)/g.exec(url) }, - handle: (match) => { return handleLocal(match[2], match[3]) } - } - ] - resolver.resolve(fileName, localFSHandler) - .then(sources => { - results = sources - done() - }) - .catch(e => { - throw e - }) - }) - - it('should have 3 items', function () { - assert.equal(Object.keys(results).length, 3) - }) - - it('should returns contract content of given local path', function () { - 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 IPFShandle - describe('test getting IPFS files', function() { - const fileName = 'ipfs://QmeKtwMBqz5Ac7oL8SyTD96mccEzw9X9d39jLb2kgnBYbn' - let results = [] - - before(async function() { - try { - const resolver = new rr.ImportResolver() - var sources = await resolver.resolve(fileName) - results = sources - return - } catch (e) { - throw e - } - }) - - it('should have 3 items', function () { - assert.equal(Object.keys(results).length, 3) - }) - it('should returns contract content of given local path', function () { - 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', - type: 'ipfs' - } - assert.deepEqual(results, expt) - }) - }) - }) -}) diff --git a/remix-resolve/tests/test.ts b/remix-resolve/tests/test.ts new file mode 100644 index 0000000000..d842908fb2 --- /dev/null +++ b/remix-resolve/tests/test.ts @@ -0,0 +1,89 @@ +import { expect } from 'chai' +import { ImportResolver, Imported } from '../src' +import * as fs from 'fs' +import * as path from 'path' +import * as assert from 'assert' +import { AppManager } from 'remix-plugin' + +describe('testRunner', () => { + describe('#resolve', () => { + describe('test example_1 [local imports]]', () => { + const fileName = '../remix-resolve/tests/example_1/greeter.sol' + let results = {} + + before(done => { + let resolver: ImportResolver = new ImportResolver() + + 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) => { return handleLocal(match[2], match[3]) } + } + ] + resolver.resolve(fileName, localFSHandler) + .then(sources => { + results = sources + done() + }) + .catch(e => { + throw e + }) + }) + + it('should have 3 items', () => { + assert.equal(Object.keys(results).length, 3) + }) + + it('should returns 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 IPFShandle + describe('test getting IPFS files', () => { + const fileName = 'ipfs://QmeKtwMBqz5Ac7oL8SyTD96mccEzw9X9d39jLb2kgnBYbn' + let results:Imported + + before(async function() { + try { + const resolver: ImportResolver = new ImportResolver() + var sources: Imported = await resolver.resolve(fileName) + results = sources + return + } catch (e) { + throw e + } + }) + + it('should have 3 items', () => { + assert.equal(Object.keys(results).length, 3) + }) + it('should returns 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: 'ipfs://QmeKtwMBqz5Ac7oL8SyTD96mccEzw9X9d39jLb2kgnBYbn', + type: 'ipfs' + } + assert.deepEqual(results, expt) + }) + }) + }) + }) +})