Permalink
Browse files

Other: Root#resolvePath skips files when returning null, see #368

  • Loading branch information...
dcodeIO committed Jan 26, 2017
1 parent de89035 commit 2d81864fa5c4dac75913456d582e0bea9cf0dd80
Showing with 34 additions and 6 deletions.
  1. +9 −5 src/root.js
  2. +25 −1 tests/api_root.js
@@ -56,7 +56,7 @@ Root.fromJSON = function fromJSON(json, root) {
* @function
* @param {string} origin The file name of the importing file
* @param {string} target The file name being imported
* @returns {string} Resolved path to `target`
* @returns {?string} Resolved path to `target` or `null` to skip the file
*/
Root.prototype.resolvePath = util.path.resolve;
@@ -104,13 +104,16 @@ Root.prototype.load = function load(filename, options, callback) {
else {
parse.filename = filename;
var parsed = parse(source, self, options),
resolved,
i = 0;
if (parsed.imports)
for (; i < parsed.imports.length; ++i)
fetch(self.resolvePath(filename, parsed.imports[i]));
if (resolved = self.resolvePath(filename, parsed.imports[i]))
fetch(resolved);
if (parsed.weakImports)
for (i = 0; i < parsed.weakImports.length; ++i)
fetch(self.resolvePath(filename, parsed.weakImports[i]), true);
if (resolved = self.resolvePath(filename, parsed.weakImports[i]))
fetch(resolved, true);
}
} catch (err) {
finish(err);
@@ -184,8 +187,9 @@ Root.prototype.load = function load(filename, options, callback) {
// references anymore, so we can load everything in parallel
if (util.isString(filename))
filename = [ filename ];
for (var i = 0; i < filename.length; ++i)
fetch(self.resolvePath("", filename[i]));
for (var i = 0, resolved; i < filename.length; ++i)
if (resolved = self.resolvePath("", filename[i]))
fetch(resolved);
if (sync)
return self;
@@ -20,7 +20,7 @@ tape.test("reflected roots", function(test) {
if (typeof Promise !== "undefined")
test.ok(root.load("tests/data/common.proto") instanceof Promise, "should return a Promise when loading without a callback");
var count = 0, total = 4;
var count = 0, total = 6;
root = new Root();
root.load("tests/data/common.json", function(err, root) {
@@ -50,4 +50,28 @@ tape.test("reflected roots", function(test) {
if (++count === total)
test.end();
});
var root3 = new Root();
root3.resolvePath = function() {
return null;
};
root3.load("tests/data/NOTFOUND2", function(err, root) {
test.notOk(err, "should skip files without error when resolvePath returns null");
test.equal(root, root3, "should still return itself");
if (++count === total)
test.end();
});
var root4 = new Root();
root4.resolvePath = function(origin, target) {
if (/weak\.proto$/.test(target))
return protobuf.util.path.resolve(origin, target);
return null;
};
root4.load("tests/data/weak.proto", function(err, root) {
test.notOk(err, "should skip files without error when resolvePath returns null");
test.equal(root, root4, "should still return itself");
if (++count === total)
test.end();
});
});

0 comments on commit 2d81864

Please sign in to comment.