fix: cache ongoing parseFile() calls, fixes #416

This commit is contained in:
Harttle
2021-10-16 21:07:48 +08:00
committed by harttle
parent c58a116513
commit 8894cbfe6e
8 changed files with 63 additions and 32 deletions
+24
View File
@@ -1,9 +1,12 @@
import { Liquid } from '../../src/liquid'
import { expect, use } from 'chai'
import * as chaiAsPromised from 'chai-as-promised'
import * as sinon from 'sinon'
import * as sinonChai from 'sinon-chai'
const LiquidUMD = require('../../dist/liquid.browser.umd.js').Liquid
use(chaiAsPromised)
use(sinonChai)
describe('Issues', function () {
it('#221 unicode blanks are not properly treated', async () => {
@@ -129,4 +132,25 @@ describe('Issues', function () {
const html = await engine.renderSync(tpl)
expect(html).to.equal('/tmp/foo.liquid')
})
it('#416 Templates imported by {% render %} not cached for concurrent async render', async () => {
const readFile = sinon.spy(() => Promise.resolve('HELLO'))
const exists = sinon.spy(() => 'HELLO')
const engine = new Liquid({
cache: true,
extname: '.liquid',
root: '~',
fs: {
exists,
resolve: (root: string, file: string, ext: string) => root + '#' + file + ext,
sep: '#',
readFile
} as any
})
await Promise.all(Array(5).fill(0).map(
x => engine.parseAndRender("{% render 'template' %}")
))
expect(exists).to.be.calledOnce
expect(readFile).to.be.calledOnce
})
})
-3
View File
@@ -155,10 +155,7 @@ describe('LiquidOptions#cache', function () {
cache: true
})
mock({ '/root/foo.html': 'foo' })
mock({ '/root/bar.html': 'bar' })
expect(engine.renderFileSync('foo')).to.equal('foo')
expect(engine.renderFileSync('bar')).to.equal('bar')
mock({ '/root/foo.html': 'bar' })
expect(engine.renderFileSync('foo')).to.equal('foo')
})