diff --git a/lib/core/file.js b/lib/core/file.js index d81a90897..d799b50db 100644 --- a/lib/core/file.js +++ b/lib/core/file.js @@ -15,11 +15,16 @@ class File { } parseFileForImport(content, isHttpContract, callback) { + const self = this; if (typeof isHttpContract === 'function') { callback = isHttpContract; isHttpContract = false; } - const self = this; + if (self.parsedImports) { + // We already parsed this file + return callback(); + } + self.parsedImports = true; if (self.filename.indexOf('.sol') < 0) { // Only supported in Solidity return callback(); @@ -98,20 +103,15 @@ class File { content = fs.readFileSync(this.path).toString(); } else if (this.type === File.types.custom) { return this.resolver((theContent) => { - if (!this.parsedImports) { - this.parsedImports = true; - return this.parseFileForImport(content, () => { - callback(theContent); - }); - } - callback(theContent); + this.parseFileForImport(content, () => { + callback(theContent); + }); }); } else if (this.type === File.types.http) { return this.downloadFile(this.filename, this.path, (content) => { if (!content) { return callback(content); } - this.parsedImports = true; this.path = this.filename; this.type = File.types.dapp_file; callback(content); @@ -119,13 +119,9 @@ class File { } else { throw new Error("unknown file: " + this.filename); } - if (!this.parsedImports) { - this.parsedImports = true; - return this.parseFileForImport(content, () => { - callback(content); - }); - } - callback(content); + return this.parseFileForImport(content, () => { + callback(content); + }); } } diff --git a/test/file.js b/test/file.js index db913710c..bd261c45f 100644 --- a/test/file.js +++ b/test/file.js @@ -59,5 +59,27 @@ describe('embark.File', function () { done(); }); }); + + it('should find all the imports but only once if called twice', function (done) { + const contract = fs.readFileSync('./test/contracts/contract_with_http_import.sol').toString(); + const file = new File({filename: '.embark/contracts/embark-framework/embark/master/test_app/app/contracts/simple_storage.sol', + path: 'https://raw.githubusercontent.com/embark-framework/embark/develop/test_apps/test_app/app/contracts/simple_storage.sol'}); + const downloadFileStub = sinon.stub(file, 'downloadFile') + .callsFake((path, url, cb) => { + cb(); + }); + + file.parseFileForImport(contract, () => { + // Parse again + file.parseFileForImport(contract, () => { + assert.strictEqual(downloadFileStub.callCount, 1); + assert.strictEqual(downloadFileStub.firstCall.args[0], + '.embark/contracts/embark-framework/embark/develop/test_apps/contracts_app/contracts/contract_args.sol'); + assert.strictEqual(downloadFileStub.firstCall.args[1], + 'https://raw.githubusercontent.com/embark-framework/embark/develop/test_apps/contracts_app/contracts/contract_args.sol'); + done(); + }); + }); + }); }); });