diff --git a/index.js b/index.js index a851d78f0..f17d4b551 100644 --- a/index.js +++ b/index.js @@ -63,6 +63,9 @@ var _engine = { }) .catch((e) => { e.file = filepath; + if (e.code === 'ENOENT') { + e.message = `Failed to lookup ${filepath} in: ${this.options.root}`; + } throw e; }); }, diff --git a/test/express.js b/test/express.js index 2a99ef3a6..970887d77 100644 --- a/test/express.js +++ b/test/express.js @@ -48,9 +48,14 @@ describe('engine#express()', function() { var file = '/not-exist.html'; var ctx = {}; engine.express().call(view, file, ctx, function(err) { - expect(err.code).to.equal('ENOENT'); - console.log(err.message); - done(); + try{ + expect(err.code).to.equal('ENOENT'); + expect(err.message).to.match(/Failed to lookup/); + done(); + } + catch(e){ + done(e); + } }); }); it('should respect root option when lookup', function(done) { diff --git a/test/liquid.js b/test/liquid.js index a2fecd5e5..e478df69a 100644 --- a/test/liquid.js +++ b/test/liquid.js @@ -21,7 +21,10 @@ describe('liquid', function() { }); mock({ '/root/files/foo.html': 'foo', - '/root/files/name.html': 'My name is {{name}}.' + '/root/files/name.html': 'My name is {{name}}.', + '/un-readable.html': mock.file({ + mode: '0000' + }) }); }); afterEach(function() { @@ -99,18 +102,17 @@ describe('liquid', function() { it('should use default extname', function() { return engine.renderFile('files/name', ctx).should.eventually.equal('My name is harttle.'); }); - it('should accept root with no trailing slash', function() { + it('should throw with lookup list when file not exist', function() { engine = Liquid({ - root: '/root', + root: ['/boo', '/root/'], extname: '.html' }); - return expect(engine.renderFile('files/foo.html')).to.eventually.equal('foo'); + return expect(engine.renderFile('/not/exist.html')).to + .be.rejectedWith(/failed to lookup \/not\/exist.html in: \/boo,\/root\//i); }); - it('should accept dot path', function() { - return expect(engine.renderFile('./files/foo.html')).to.eventually.equal('foo'); - }); - it('should accept double-dot path', function() { - return expect(engine.renderFile('files/foo/../foo.html')).to.eventually.equal('foo'); + it('should throw when file not readable', function() { + return expect(engine.renderFile('/un-readable.html')).to + .be.rejectedWith(/EACCES/); }); }); describe('strict', function() { diff --git a/test/util/fs.js b/test/util/fs.js new file mode 100644 index 000000000..433d353bc --- /dev/null +++ b/test/util/fs.js @@ -0,0 +1,22 @@ +const chai = require("chai"); +const expect = chai.expect; + +const fs = require('../../src/util/fs.js'); +const pathResolve = fs.pathResolve; + +describe('fs', function() { + describe('.pathResolve(root, path)', function() { + it('should accept root with no trailing slash', function() { + expect(pathResolve('/root', 'files/foo.html')).to.equal('/root/files/foo.html'); + }); + it('should accept root with trailing slash', function() { + expect(pathResolve('/root/', 'files/foo.html')).to.equal('/root/files/foo.html'); + }); + it('should accept dot path', function() { + expect(pathResolve('/root', './foo.html')).to.equal('/root/foo.html'); + }); + it('should accept double-dot path', function() { + expect(pathResolve('/root', 'files/../foo.html')).to.equal('/root/foo.html'); + }); + }); +});