chore(TypeScript): remove mock-fs

This commit is contained in:
harttle
2019-02-17 21:37:13 +08:00
parent aa49b4f117
commit f432aade6a
11 changed files with 57 additions and 57 deletions
+3 -5
View File
@@ -1,8 +1,8 @@
import { expect } from 'chai'
import * as request from 'supertest'
import * as express from 'express'
import * as mock from 'mock-fs'
import Liquid from '../../dist/liquid.common.js'
import { mock, restore } from 'test/stub/mockfs'
import Liquid from '../..'
describe('express()', function () {
var app, engine
@@ -24,9 +24,7 @@ describe('express()', function () {
file: req.params.file
}))
})
after(function () {
mock.restore()
})
after(restore)
it('should render express views', function (done) {
mock({ '/views/name.html': 'My name is {{name}}.' })
app.set('views', ['/views'])
+3 -7
View File
@@ -1,6 +1,6 @@
import { expect } from 'chai'
import * as mock from 'mock-fs'
import Liquid from '../..'
import { mock, restore } from '../stub/mockfs'
describe('#renderFile()', function () {
var engine
@@ -13,14 +13,10 @@ describe('#renderFile()', function () {
'/root/files/bar': 'bar',
'/root/files/foo.html': 'foo',
'/root/files/name.html': 'My name is {{name}}.',
'/un-readable.html': mock.file({
mode: '0000'
})
'/un-readable.html': { mode: '0000' }
})
})
afterEach(function () {
mock.restore()
})
afterEach(restore)
it('should render file', async function () {
const html = await engine.renderFile('/root/files/foo.html', {})
return expect(html).to.equal('foo')
+31
View File
@@ -0,0 +1,31 @@
import { fs } from 'src/parser/template'
import { isString } from 'src/util/underscore'
const readFile = fs.readFile
const stat = fs.stat
type fileDescriptor = { mode: string, content: string }
export function mock (files: { [path: string]: (string | fileDescriptor) }) {
for (const [key, val] of Object.entries(files)) {
files[key] = isString(val)
? { mode: '33188', content: val as string }
: val
}
fs.readFile = async function (path) {
const file = files[path] as fileDescriptor
if (file === undefined) throw new Error('ENOENT')
if (file.mode === '000') throw new Error('EACCES')
return file.content
}
fs.stat = async function (path) {
const file = files[path] as fileDescriptor
if (file === undefined) throw new Error('ENOENT')
return file
}
}
export function restore () {
fs.readFile = readFile
fs.stat = stat
}
+2 -4
View File
@@ -1,6 +1,6 @@
import Liquid from '../../src/liquid'
import * as mock from 'mock-fs'
import * as chai from 'chai'
import { mock, restore } from 'test/stub/mockfs'
const expect = chai.expect
@@ -36,9 +36,7 @@ describe('Liquid', function () {
'/root/foo': 'foo'
})
})
after(function () {
mock.restore()
})
after(restore)
it('should render single template', function (done) {
render.call({ root: '.' }, 'foo', null, (err, result) => {
if (err) return done(err)
+3 -5
View File
@@ -1,6 +1,6 @@
import { expect } from 'chai'
import * as mock from 'mock-fs'
import Liquid from '../../../src/liquid'
import Liquid from 'src/liquid'
import { mock, restore } from 'test/stub/mockfs'
describe('LiquidOptions#cache', function () {
let engine
@@ -11,9 +11,7 @@ describe('LiquidOptions#cache', function () {
})
mock({ '/root/files/foo.html': 'foo' })
})
afterEach(function () {
mock.restore()
})
afterEach(restore)
it('should be disabled by default', function () {
return engine.renderFile('files/foo')
.then(x => expect(x).to.equal('foo'))
+2 -4
View File
@@ -1,6 +1,6 @@
import Liquid from 'src/liquid'
import { expect } from 'chai'
import * as mock from 'mock-fs'
import { mock, restore } from 'test/stub/mockfs'
describe('tags/include', function () {
let liquid
@@ -10,9 +10,7 @@ describe('tags/include', function () {
extname: '.html'
})
})
afterEach(function () {
mock.restore()
})
afterEach(restore)
it('should support include', async function () {
mock({
'/current.html': 'bar{% include "bar/foo.html" %}bar',
+2 -4
View File
@@ -1,6 +1,6 @@
import Liquid from 'src/liquid'
import { expect } from 'chai'
import * as mock from 'mock-fs'
import { mock, restore } from 'test/stub/mockfs'
describe('tags/layout', function () {
let liquid
@@ -10,9 +10,7 @@ describe('tags/layout', function () {
extname: '.html'
})
})
afterEach(function () {
mock.restore()
})
afterEach(restore)
it('should throw when block not closed', function () {
mock({
+4 -18
View File
@@ -1,29 +1,15 @@
import { resolve } from '../../src/parser/template'
import * as path from 'path'
import { fs } from 'src/parser/template'
import { expect, use } from 'chai'
import * as chaiAsPromised from 'chai-as-promised'
import { mock, restore } from '../stub/mockfs'
use(chaiAsPromised)
describe('template', function () {
let readFile, stat
before(function () {
readFile = fs.readFile
stat = fs.stat
fs.readFile = async function (file) {
if (file === '/foo/bar.html') return 'bar'
throw new Error('NOENT')
}
fs.stat = async function (file) {
if (file === '/foo/bar.html') return { type: 'file' }
throw new Error('NOENT')
}
})
after(function () {
fs.readFile = readFile
fs.stat = stat
})
before(() => mock({ '/foo/bar.html': 'bar' }))
after(restore)
describe('#resolve()', function () {
it('should resolve based on root', async function () {
const filepath = await resolve('bar.html', '/foo', { root: [] })
+5 -7
View File
@@ -1,7 +1,7 @@
import { expect } from 'chai'
import Liquid from 'src/liquid'
import * as mock from 'mock-fs'
import * as path from 'path'
import { mock, restore } from 'test/stub/mockfs'
let engine = new Liquid()
const strictEngine = new Liquid({
@@ -10,9 +10,7 @@ const strictEngine = new Liquid({
})
describe('error', function () {
afterEach(function () {
mock.restore()
})
afterEach(restore)
describe('TokenizationError', function () {
it('should throw TokenizationError when tag illegal', async function () {
@@ -66,7 +64,7 @@ describe('error', function () {
'/foo.html': html
})
const err = await expect(engine.renderFile('/foo.html')).be.rejected
mock.restore()
restore()
expect(err.name).to.equal('TokenizationError')
expect(err.file).to.equal(path.resolve('/foo.html'))
})
@@ -207,7 +205,7 @@ describe('error', function () {
'/foo.html': html
})
const err = await expect(engine.renderFile('/foo.html')).be.rejected
mock.restore()
restore()
console.log(err, err.name)
expect(err.name).to.equal('RenderError')
expect(err.file).to.equal(path.resolve('/foo.html'))
@@ -299,7 +297,7 @@ describe('error', function () {
'/foo.html': html
})
const err = await expect(engine.renderFile('/foo.html')).be.rejected
mock.restore()
restore()
expect(err.name).to.equal('ParseError')
expect(err.file).to.equal(path.resolve('/foo.html'))
})