Skip to content

Commit

Permalink
Merge pull request #1049 from devoidfury/fix-multiple-callback-calls
Browse files Browse the repository at this point in the history
Fix multiple callback calls, issue #1029
  • Loading branch information
fdintino committed Jan 15, 2018
2 parents 77f0e2f + 9605652 commit d1bdf30
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 3 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Expand Up @@ -5,8 +5,11 @@ Changelog
master (unreleased)
-------------------

* Fix calling render callback twice when a conditional import throws an error.
Solves [#1029](https://github.com/mozilla/nunjucks/issues/1029)

* Support objects created with Object.create(null). fixes [#468](https://github.com/mozilla/nunjucks/issues/468)


3.0.1 (May 24 2017)
-------------------
Expand Down
10 changes: 8 additions & 2 deletions src/environment.js
Expand Up @@ -152,12 +152,12 @@ var Environment = Obj.extend({
}
return this.filters[name];
},

addTest: function(name, func) {
this.tests[name] = func;
return this;
},

getTest: function(name) {
if(!this.tests[name]) {
throw new Error('test not found: ' + name);
Expand Down Expand Up @@ -495,15 +495,21 @@ Template = Obj.extend({
var frame = parentFrame ? parentFrame.push(true) : new Frame();
frame.topLevel = true;
var syncResult = null;
var didError = false;

_this.rootRenderFunc(
_this.env,
context,
frame || new Frame(),
runtime,
function(err, res) {
if (didError) {
// prevent multiple calls to cb
return;
}
if(err) {
err = lib.prettifyError(_this.path, _this.env.opts.dev, err);
didError = true;
}

if(cb) {
Expand Down
11 changes: 11 additions & 0 deletions tests/api.js
Expand Up @@ -44,6 +44,17 @@
expect(util.normEOL(test.render())).to.be('Test1\nTest2');
});

it('should only call the callback once when conditional import fails', function(done) {
var env = new Environment(new Loader(templatesPath));
var called = 0;
env.render('broken-conditional-include.njk',
function() {
expect(++called).to.be(1);
}
);
setTimeout(done, 0);
});

it('should handle correctly relative paths in renderString', function() {
var env = new Environment(new Loader(templatesPath));
expect(env.renderString('{% extends "./relative/test1.njk" %}{% block block1 %}Test3{% endblock %}', {}, {
Expand Down
3 changes: 3 additions & 0 deletions tests/templates/broken-conditional-include.njk
@@ -0,0 +1,3 @@
{% if not whatever %}
{% include "throws.njk" %}
{% endif %}
1 change: 1 addition & 0 deletions tests/templates/throws.njk
@@ -0,0 +1 @@
{{ nonExistentFn() }}

0 comments on commit d1bdf30

Please sign in to comment.