diff --git a/src/root.js b/src/root.js index 9441a7fc3..2776f96e4 100644 --- a/src/root.js +++ b/src/root.js @@ -98,10 +98,10 @@ Root.prototype.load = function load(filename, options, callback) { /* istanbul ignore if */ if (!callback) return; - var cb = callback; - callback = null; if (sync) throw err; + var cb = callback; + callback = null; cb(err, root); } diff --git a/tests/api_root.js b/tests/api_root.js index 6e9814ddb..9125d3aa8 100644 --- a/tests/api_root.js +++ b/tests/api_root.js @@ -67,6 +67,29 @@ tape.test("reflected roots", function(test) { }); }); + test.test(test.name + " - missing import", function(test) { + var root = new Root(); + test.plan(2); + root.load("tests/data/badimport.proto", function(err) { + test.ok(err, "should return an error when an imported file does not exist"); + test.match(err.toString(), /nonexistent\.proto/, "should mention the file name which was not found"); + test.end(); + }); + }); + + test.test(test.name + " - missing import, sync load", function(test) { + var root = new Root(); + test.plan(2); + try { + root.loadSync("tests/data/badimport.proto"); + root.resolveAll(); + } catch (err) { + test.ok(err, "should return an error when an imported file does not exist"); + test.match(err.toString(), /nonexistent\.proto/, "should mention the file name which was not found"); + } + test.end(); + }); + test.test(test.name + " - skipped", function(test) { var root = new Root(); root.resolvePath = function() { diff --git a/tests/data/badimport.proto b/tests/data/badimport.proto new file mode 100644 index 000000000..f11caaf6b --- /dev/null +++ b/tests/data/badimport.proto @@ -0,0 +1,7 @@ +syntax = "proto3"; + +import "nonexistent.proto"; + +message Message { + NonExistent field = 1; +}