refacotr how we handle files already parsed

This commit is contained in:
Jonathan Rainville 2018-04-20 10:03:03 -04:00
parent abc89b2015
commit 9bf06aebce
2 changed files with 34 additions and 16 deletions

View File

@ -15,11 +15,16 @@ class File {
} }
parseFileForImport(content, isHttpContract, callback) { parseFileForImport(content, isHttpContract, callback) {
const self = this;
if (typeof isHttpContract === 'function') { if (typeof isHttpContract === 'function') {
callback = isHttpContract; callback = isHttpContract;
isHttpContract = false; isHttpContract = false;
} }
const self = this; if (self.parsedImports) {
// We already parsed this file
return callback();
}
self.parsedImports = true;
if (self.filename.indexOf('.sol') < 0) { if (self.filename.indexOf('.sol') < 0) {
// Only supported in Solidity // Only supported in Solidity
return callback(); return callback();
@ -98,20 +103,15 @@ class File {
content = fs.readFileSync(this.path).toString(); content = fs.readFileSync(this.path).toString();
} else if (this.type === File.types.custom) { } else if (this.type === File.types.custom) {
return this.resolver((theContent) => { return this.resolver((theContent) => {
if (!this.parsedImports) { this.parseFileForImport(content, () => {
this.parsedImports = true;
return this.parseFileForImport(content, () => {
callback(theContent); callback(theContent);
}); });
}
callback(theContent);
}); });
} else if (this.type === File.types.http) { } else if (this.type === File.types.http) {
return this.downloadFile(this.filename, this.path, (content) => { return this.downloadFile(this.filename, this.path, (content) => {
if (!content) { if (!content) {
return callback(content); return callback(content);
} }
this.parsedImports = true;
this.path = this.filename; this.path = this.filename;
this.type = File.types.dapp_file; this.type = File.types.dapp_file;
callback(content); callback(content);
@ -119,14 +119,10 @@ class File {
} else { } else {
throw new Error("unknown file: " + this.filename); throw new Error("unknown file: " + this.filename);
} }
if (!this.parsedImports) {
this.parsedImports = true;
return this.parseFileForImport(content, () => { return this.parseFileForImport(content, () => {
callback(content); callback(content);
}); });
} }
callback(content);
}
} }

View File

@ -59,5 +59,27 @@ describe('embark.File', function () {
done(); 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();
});
});
});
}); });
}); });