Skip to content

Commit

Permalink
Other: Root#resolvePath skips files when returning null, see #368
Browse files Browse the repository at this point in the history
  • Loading branch information
dcodeIO committed Jan 26, 2017
1 parent de89035 commit 2d81864
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 6 deletions.
14 changes: 9 additions & 5 deletions src/root.js
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down
26 changes: 25 additions & 1 deletion tests/api_root.js
Expand Up @@ -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) {
Expand Down Expand Up @@ -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.