Skip to content

Commit

Permalink
Fix bug that broke template caching. fixes mozilla#1074
Browse files Browse the repository at this point in the history
  • Loading branch information
fdintino committed Feb 23, 2018
1 parent eb02308 commit 2f646c9
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 6 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -6,6 +6,8 @@ master (unreleased)

* Fix regression to make `chokidar` an optional dependency again.
* Fix issue when running `npm install nunjucks` with the `--no-bin-links` flag
* Fix regression that broke template caching. Fixes
[#1074](https://github.com/mozilla/nunjucks/issues/1074)

3.1.0 (Feb 19 2018)
-------------------
Expand Down
3 changes: 3 additions & 0 deletions nunjucks/index.js
Expand Up @@ -59,6 +59,9 @@ module.exports = {
nodes: nodes,
installJinjaCompat: installJinjaCompat,
configure: configure,
reset() {
e = undefined;
},
compile(src, env, path, eagerCompile) {
if (!e) {
configure();
Expand Down
19 changes: 13 additions & 6 deletions nunjucks/src/environment.js
Expand Up @@ -233,18 +233,25 @@ class Environment extends Obj {
if (err) {
if (cb) {
cb(err);
return;
} else {
throw err;
}
}
let newTmpl;
if (!info) {
newTmpl = new Template(noopTmplSrc, this, '', eagerCompile);
} else {
info = info || {src: noopTmplSrc, path: ''};
const newTmpl = new Template(info.src, this, info.path, eagerCompile);
if (cb) {
cb(null, newTmpl);
} else {
syncResult = newTmpl;
newTmpl = new Template(info.src, this, info.path, eagerCompile);
if (!info.noCache) {
info.loader.cache[name] = newTmpl;
}
}
if (cb) {
cb(null, newTmpl);
} else {
syncResult = newTmpl;
}
};

lib.asyncIter(this.loaders, (loader, i, next, done) => {
Expand Down
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -26,6 +26,7 @@
"eslint-plugin-import": "^2.8.0",
"expect.js": "*",
"express": "4.x",
"fs-extra": "^5.0.0",
"get-port": "^3.2.0",
"mocha": "*",
"mocha-phantomjs-core": "^2.1.2",
Expand Down
76 changes: 76 additions & 0 deletions tests/core.js
@@ -0,0 +1,76 @@
(function() {
'use strict';

var expect,
nunjucks,
fs,
os,
path;

if (typeof require !== 'undefined') {
expect = require('expect.js');
nunjucks = require('../nunjucks/index');
fs = require('fs-extra');
path = require('path');
os = require('os');
} else {
expect = window.expect;
nunjucks = window.nunjucks;
}

function rmdir(dirPath) {
fs.emptyDirSync(dirPath);
fs.rmdir(dirPath);
}

describe('nunjucks.configure', function() {
var tempdir;

before(function() {
if (fs && path && os) {
try {
tempdir = fs.mkdtempSync(path.join(os.tmpdir(), 'templates'));
fs.emptyDirSync(tempdir);
} catch (e) {
rmdir(tempdir);
throw e;
}
}
});

after(function() {
nunjucks.reset();
if (typeof tempdir !== 'undefined') {
rmdir(tempdir);
}
});

it('should cache templates by default', function() {
if (typeof fs === 'undefined') {
this.skip();
return;
}
nunjucks.configure(tempdir);

fs.writeFileSync(tempdir + '/test.html', '{{ name }}', 'utf-8');
expect(nunjucks.render('test.html', {name: 'foo'})).to.be('foo');

fs.writeFileSync(tempdir + '/test.html', '{{ name }}-changed', 'utf-8');
expect(nunjucks.render('test.html', {name: 'foo'})).to.be('foo');
});

it('should not cache templates with {noCache: true}', function() {
if (typeof fs === 'undefined') {
this.skip();
return;
}
nunjucks.configure(tempdir, {noCache: true});

fs.writeFileSync(tempdir + '/test.html', '{{ name }}', 'utf-8');
expect(nunjucks.render('test.html', {name: 'foo'})).to.be('foo');

fs.writeFileSync(tempdir + '/test.html', '{{ name }}-changed', 'utf-8');
expect(nunjucks.render('test.html', {name: 'foo'})).to.be('foo-changed');
});
});
}());

0 comments on commit 2f646c9

Please sign in to comment.