fix: hardcoded '/' in normalized options.fs, fixes #412, #408

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:
Harttle
2021-10-16 12:10:55 +08:00
committed by harttle
parent df6d62fd59
commit 9cfa43b8ae
17 changed files with 124 additions and 54 deletions
+7 -7
View File
@@ -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')
})
})
+4 -4
View File
@@ -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 -2
View File
@@ -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)