mirror of
https://github.com/harttle/liquidjs.git
synced 2026-09-15 04:10:40 -07:00
Changes including: - will not call fs.resolve when normalizing `fs` - removed hardcoded `/` - mandatory `fs.dirname` for relative reference - mandatory `fs.sep`, defaults to '/'
This commit is contained in:
@@ -114,4 +114,19 @@ describe('Issues', function () {
|
||||
const html = await engine.render(tpl, { date: '2021-10-06T15:31:00+08:00' })
|
||||
expect(html).to.equal('2021-10-06 17:31 PM +1000')
|
||||
})
|
||||
it('#412 Pass root as it is to `resolve`', async () => {
|
||||
const engine = new Liquid({
|
||||
root: '/tmp',
|
||||
fs: {
|
||||
readFileSync: (file: string) => file,
|
||||
async readFile (file: string) { return 'foo' },
|
||||
existsSync (file: string) { return true },
|
||||
async exists (file: string) { return true },
|
||||
resolve: (dir: string, file: string) => dir + '/' + file
|
||||
}
|
||||
})
|
||||
const tpl = engine.parse('{% include "foo.liquid" %}')
|
||||
const html = await engine.renderSync(tpl)
|
||||
expect(html).to.equal('/tmp/foo.liquid')
|
||||
})
|
||||
})
|
||||
|
||||
@@ -6,16 +6,16 @@ use(chaiAsPromised)
|
||||
describe('LiquidOptions#fs', function () {
|
||||
let engine: Liquid
|
||||
const fs = {
|
||||
exists: (x: string) => Promise.resolve(x.match(/^\/root\/files\//)),
|
||||
existsSync: (x: string) => x.match(/^\/root\/files\//),
|
||||
exists: (x: string) => Promise.resolve(!x.match(/not-exist/)),
|
||||
existsSync: (x: string) => !x.match(/not-exist/),
|
||||
readFile: (x: string) => Promise.resolve(`content for ${x}`),
|
||||
readFileSync: (x: string) => `content for ${x}`,
|
||||
fallback: (x: string) => '/root/files/fallback',
|
||||
resolve: (base: string, path: string) => base + path
|
||||
resolve: (base: string, path: string) => base + '/' + path
|
||||
}
|
||||
beforeEach(function () {
|
||||
engine = new Liquid({
|
||||
root: '/root/',
|
||||
root: '/root',
|
||||
fs
|
||||
} as any)
|
||||
})
|
||||
@@ -25,12 +25,12 @@ describe('LiquidOptions#fs', function () {
|
||||
})
|
||||
|
||||
it('should support fallback', async function () {
|
||||
const html = await engine.renderFile('notexist/foo')
|
||||
const html = await engine.renderFile('not-exist/foo')
|
||||
expect(html).to.equal('content for /root/files/fallback')
|
||||
})
|
||||
|
||||
it('should support renderSync', function () {
|
||||
const html = engine.renderFileSync('notexist/foo')
|
||||
const html = engine.renderFileSync('not-exist/foo')
|
||||
expect(html).to.equal('content for /root/files/fallback')
|
||||
})
|
||||
|
||||
@@ -39,7 +39,7 @@ describe('LiquidOptions#fs', function () {
|
||||
root: '/root/',
|
||||
fs: { ...fs, fallback: undefined }
|
||||
} as any)
|
||||
return expect(engine.renderFile('notexist/foo'))
|
||||
return expect(engine.renderFile('not-exist/foo'))
|
||||
.to.be.rejectedWith('Failed to lookup')
|
||||
})
|
||||
})
|
||||
|
||||
@@ -75,7 +75,7 @@ describe('Liquid', function () {
|
||||
extname: '.html'
|
||||
})
|
||||
return expect(engine.renderFile('/not/exist.html')).to
|
||||
.be.rejectedWith(/Failed to lookup "\/not\/exist.html" in "\/boo\/,\/root\/"/)
|
||||
.be.rejectedWith(/Failed to lookup "\/not\/exist.html" in "\/boo,\/root\/"/)
|
||||
})
|
||||
})
|
||||
describe('#parseFile', function () {
|
||||
@@ -85,7 +85,7 @@ describe('Liquid', function () {
|
||||
extname: '.html'
|
||||
})
|
||||
return expect(engine.parseFile('/not/exist.html')).to
|
||||
.be.rejectedWith(/Failed to lookup "\/not\/exist.html" in "\/boo\/,\/root\/"/)
|
||||
.be.rejectedWith(/Failed to lookup "\/not\/exist.html" in "\/boo,\/root\/"/)
|
||||
})
|
||||
it('should fallback to require.resolve in Node.js', async function () {
|
||||
const engine = new Liquid({
|
||||
@@ -144,7 +144,7 @@ describe('Liquid', function () {
|
||||
extname: '.html'
|
||||
})
|
||||
return expect(() => engine.parseFileSync('/not/exist.html'))
|
||||
.to.throw(/Failed to lookup "\/not\/exist.html" in "\/boo\/,\/root\/"/)
|
||||
.to.throw(/Failed to lookup "\/not\/exist.html" in "\/boo,\/root\/"/)
|
||||
})
|
||||
it('should throw with lookup list when file not exist', function () {
|
||||
const engine = new Liquid({
|
||||
@@ -152,7 +152,7 @@ describe('Liquid', function () {
|
||||
extname: '.html'
|
||||
})
|
||||
return expect(() => engine.parseFileSync('/not/exist.html'))
|
||||
.to.throw(/Failed to lookup "\/not\/exist.html" in "\/boo\/,\/root\/"/)
|
||||
.to.throw(/Failed to lookup "\/not\/exist.html" in "\/boo,\/root\/"/)
|
||||
})
|
||||
})
|
||||
describe('#enderToNodeStream', function () {
|
||||
|
||||
@@ -1,12 +1,11 @@
|
||||
import { normalize } from '../../../src/liquid-options'
|
||||
import { expect } from 'chai'
|
||||
import { resolve } from 'path'
|
||||
|
||||
describe('LiquidOptions#root', function () {
|
||||
describe('#normalize ()', function () {
|
||||
it('should normalize string typed root array', function () {
|
||||
const options = normalize({ root: 'foo' })
|
||||
expect(options.root).to.eql([resolve('foo') + '/'])
|
||||
expect(options.root).to.eql(['foo'])
|
||||
})
|
||||
it('should normalize null typed root as empty array', function () {
|
||||
const options = normalize({ root: null } as any)
|
||||
|
||||
+23
-16
@@ -6,23 +6,23 @@ import * as chaiAsPromised from 'chai-as-promised'
|
||||
use(chaiAsPromised)
|
||||
|
||||
describe('fs/browser', function () {
|
||||
if (+(process.version.match(/^v(\d+)/) as RegExpMatchArray)[1] < 8) {
|
||||
console.info('jsdom not supported, skipping template-browser...')
|
||||
return
|
||||
}
|
||||
const JSDOM = require('jsdom').JSDOM
|
||||
beforeEach(function () {
|
||||
const dom = new JSDOM(``, {
|
||||
url: 'https://example.com/foo/bar/',
|
||||
contentType: 'text/html',
|
||||
includeNodeLocations: true
|
||||
});
|
||||
(global as any).document = dom.window.document
|
||||
})
|
||||
afterEach(function () {
|
||||
delete (global as any).document
|
||||
})
|
||||
describe('#resolve()', function () {
|
||||
if (+(process.version.match(/^v(\d+)/) as RegExpMatchArray)[1] < 8) {
|
||||
console.info('jsdom not supported, skipping template-browser...')
|
||||
return
|
||||
}
|
||||
const JSDOM = require('jsdom').JSDOM
|
||||
beforeEach(function () {
|
||||
const dom = new JSDOM(``, {
|
||||
url: 'https://example.com/foo/bar/',
|
||||
contentType: 'text/html',
|
||||
includeNodeLocations: true
|
||||
});
|
||||
(global as any).document = dom.window.document
|
||||
})
|
||||
afterEach(function () {
|
||||
delete (global as any).document
|
||||
})
|
||||
it('should support relative root', function () {
|
||||
expect(fs.resolve('./views/', 'foo', '')).to.equal('https://example.com/foo/bar/views/foo')
|
||||
})
|
||||
@@ -52,6 +52,13 @@ describe('fs/browser', function () {
|
||||
})
|
||||
})
|
||||
|
||||
describe('#dirname()', () => {
|
||||
it('should return dirname of file', async function () {
|
||||
const val = fs.dirname('https://example.com/views/foo/bar')
|
||||
expect(val).to.equal('https://example.com/views/foo/')
|
||||
})
|
||||
})
|
||||
|
||||
describe('#exists()', () => {
|
||||
it('should always return true', async function () {
|
||||
const val = await fs.exists('/foo/bar')
|
||||
|
||||
@@ -12,5 +12,15 @@ describe('fs/loader', function () {
|
||||
const candidates = [...loader.candidates('./foo/bar', ['/root', '/root/foo'], '/root/current', true)]
|
||||
expect(candidates).to.contain('/root/foo/bar')
|
||||
})
|
||||
it('should not include out of root candidates', async function () {
|
||||
const loader = new Loader({ relativeReference: true, fs, extname: '' } as any)
|
||||
const candidates = [...loader.candidates('../foo/bar', ['/root'], '/root/current', true)]
|
||||
expect(candidates).to.have.lengthOf(0)
|
||||
})
|
||||
it('should treat root as a terminated path', async function () {
|
||||
const loader = new Loader({ relativeReference: true, fs, extname: '' } as any)
|
||||
const candidates = [...loader.candidates('../root-dir/bar', ['/root'], '/root/current', true)]
|
||||
expect(candidates).to.have.lengthOf(0)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user