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
-2
View File
@@ -58,7 +58,6 @@
"express": "^4.16.4", "express": "^4.16.4",
"jsdom": "^13.2.0", "jsdom": "^13.2.0",
"mocha": "^5.2.0", "mocha": "^5.2.0",
"mock-fs": "^4.7.0",
"nyc": "^13.1.0", "nyc": "^13.1.0",
"regenerator-runtime": "^0.12.1", "regenerator-runtime": "^0.12.1",
"rollup": "^1.1.2", "rollup": "^1.1.2",
@@ -68,7 +67,6 @@
"semantic-release": "^15.13.3", "semantic-release": "^15.13.3",
"sinon": "^7.2.3", "sinon": "^7.2.3",
"sinon-chai": "^3.3.0", "sinon-chai": "^3.3.0",
"source-map-support": "^0.5.10",
"supertest": "^3.4.2", "supertest": "^3.4.2",
"ts-node": "^8.0.2", "ts-node": "^8.0.2",
"tsconfig-paths": "^3.8.0", "tsconfig-paths": "^3.8.0",
+3 -5
View File
@@ -1,8 +1,8 @@
import { expect } from 'chai' import { expect } from 'chai'
import * as request from 'supertest' import * as request from 'supertest'
import * as express from 'express' import * as express from 'express'
import * as mock from 'mock-fs' import { mock, restore } from 'test/stub/mockfs'
import Liquid from '../../dist/liquid.common.js' import Liquid from '../..'
describe('express()', function () { describe('express()', function () {
var app, engine var app, engine
@@ -24,9 +24,7 @@ describe('express()', function () {
file: req.params.file file: req.params.file
})) }))
}) })
after(function () { after(restore)
mock.restore()
})
it('should render express views', function (done) { it('should render express views', function (done) {
mock({ '/views/name.html': 'My name is {{name}}.' }) mock({ '/views/name.html': 'My name is {{name}}.' })
app.set('views', ['/views']) app.set('views', ['/views'])
+3 -7
View File
@@ -1,6 +1,6 @@
import { expect } from 'chai' import { expect } from 'chai'
import * as mock from 'mock-fs'
import Liquid from '../..' import Liquid from '../..'
import { mock, restore } from '../stub/mockfs'
describe('#renderFile()', function () { describe('#renderFile()', function () {
var engine var engine
@@ -13,14 +13,10 @@ describe('#renderFile()', function () {
'/root/files/bar': 'bar', '/root/files/bar': 'bar',
'/root/files/foo.html': 'foo', '/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({ '/un-readable.html': { mode: '0000' }
mode: '0000'
})
}) })
}) })
afterEach(function () { afterEach(restore)
mock.restore()
})
it('should render file', async function () { it('should render file', async function () {
const html = await engine.renderFile('/root/files/foo.html', {}) const html = await engine.renderFile('/root/files/foo.html', {})
return expect(html).to.equal('foo') 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 Liquid from '../../src/liquid'
import * as mock from 'mock-fs'
import * as chai from 'chai' import * as chai from 'chai'
import { mock, restore } from 'test/stub/mockfs'
const expect = chai.expect const expect = chai.expect
@@ -36,9 +36,7 @@ describe('Liquid', function () {
'/root/foo': 'foo' '/root/foo': 'foo'
}) })
}) })
after(function () { after(restore)
mock.restore()
})
it('should render single template', function (done) { it('should render single template', function (done) {
render.call({ root: '.' }, 'foo', null, (err, result) => { render.call({ root: '.' }, 'foo', null, (err, result) => {
if (err) return done(err) if (err) return done(err)
+3 -5
View File
@@ -1,6 +1,6 @@
import { expect } from 'chai' 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 () { describe('LiquidOptions#cache', function () {
let engine let engine
@@ -11,9 +11,7 @@ describe('LiquidOptions#cache', function () {
}) })
mock({ '/root/files/foo.html': 'foo' }) mock({ '/root/files/foo.html': 'foo' })
}) })
afterEach(function () { afterEach(restore)
mock.restore()
})
it('should be disabled by default', function () { it('should be disabled by default', function () {
return engine.renderFile('files/foo') return engine.renderFile('files/foo')
.then(x => expect(x).to.equal('foo')) .then(x => expect(x).to.equal('foo'))
+2 -4
View File
@@ -1,6 +1,6 @@
import Liquid from 'src/liquid' import Liquid from 'src/liquid'
import { expect } from 'chai' import { expect } from 'chai'
import * as mock from 'mock-fs' import { mock, restore } from 'test/stub/mockfs'
describe('tags/include', function () { describe('tags/include', function () {
let liquid let liquid
@@ -10,9 +10,7 @@ describe('tags/include', function () {
extname: '.html' extname: '.html'
}) })
}) })
afterEach(function () { afterEach(restore)
mock.restore()
})
it('should support include', async function () { it('should support include', async function () {
mock({ mock({
'/current.html': 'bar{% include "bar/foo.html" %}bar', '/current.html': 'bar{% include "bar/foo.html" %}bar',
+2 -4
View File
@@ -1,6 +1,6 @@
import Liquid from 'src/liquid' import Liquid from 'src/liquid'
import { expect } from 'chai' import { expect } from 'chai'
import * as mock from 'mock-fs' import { mock, restore } from 'test/stub/mockfs'
describe('tags/layout', function () { describe('tags/layout', function () {
let liquid let liquid
@@ -10,9 +10,7 @@ describe('tags/layout', function () {
extname: '.html' extname: '.html'
}) })
}) })
afterEach(function () { afterEach(restore)
mock.restore()
})
it('should throw when block not closed', function () { it('should throw when block not closed', function () {
mock({ mock({
+4 -18
View File
@@ -1,29 +1,15 @@
import { resolve } from '../../src/parser/template' import { resolve } from '../../src/parser/template'
import * as path from 'path' import * as path from 'path'
import { fs } from 'src/parser/template'
import { expect, use } from 'chai' import { expect, use } from 'chai'
import * as chaiAsPromised from 'chai-as-promised' import * as chaiAsPromised from 'chai-as-promised'
import { mock, restore } from '../stub/mockfs'
use(chaiAsPromised) use(chaiAsPromised)
describe('template', function () { describe('template', function () {
let readFile, stat before(() => mock({ '/foo/bar.html': 'bar' }))
before(function () { after(restore)
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
})
describe('#resolve()', function () { describe('#resolve()', function () {
it('should resolve based on root', async function () { it('should resolve based on root', async function () {
const filepath = await resolve('bar.html', '/foo', { root: [] }) const filepath = await resolve('bar.html', '/foo', { root: [] })
+5 -7
View File
@@ -1,7 +1,7 @@
import { expect } from 'chai' import { expect } from 'chai'
import Liquid from 'src/liquid' import Liquid from 'src/liquid'
import * as mock from 'mock-fs'
import * as path from 'path' import * as path from 'path'
import { mock, restore } from 'test/stub/mockfs'
let engine = new Liquid() let engine = new Liquid()
const strictEngine = new Liquid({ const strictEngine = new Liquid({
@@ -10,9 +10,7 @@ const strictEngine = new Liquid({
}) })
describe('error', function () { describe('error', function () {
afterEach(function () { afterEach(restore)
mock.restore()
})
describe('TokenizationError', function () { describe('TokenizationError', function () {
it('should throw TokenizationError when tag illegal', async function () { it('should throw TokenizationError when tag illegal', async function () {
@@ -66,7 +64,7 @@ describe('error', function () {
'/foo.html': html '/foo.html': html
}) })
const err = await expect(engine.renderFile('/foo.html')).be.rejected const err = await expect(engine.renderFile('/foo.html')).be.rejected
mock.restore() restore()
expect(err.name).to.equal('TokenizationError') expect(err.name).to.equal('TokenizationError')
expect(err.file).to.equal(path.resolve('/foo.html')) expect(err.file).to.equal(path.resolve('/foo.html'))
}) })
@@ -207,7 +205,7 @@ describe('error', function () {
'/foo.html': html '/foo.html': html
}) })
const err = await expect(engine.renderFile('/foo.html')).be.rejected const err = await expect(engine.renderFile('/foo.html')).be.rejected
mock.restore() restore()
console.log(err, err.name) console.log(err, err.name)
expect(err.name).to.equal('RenderError') expect(err.name).to.equal('RenderError')
expect(err.file).to.equal(path.resolve('/foo.html')) expect(err.file).to.equal(path.resolve('/foo.html'))
@@ -299,7 +297,7 @@ describe('error', function () {
'/foo.html': html '/foo.html': html
}) })
const err = await expect(engine.renderFile('/foo.html')).be.rejected const err = await expect(engine.renderFile('/foo.html')).be.rejected
mock.restore() restore()
expect(err.name).to.equal('ParseError') expect(err.name).to.equal('ParseError')
expect(err.file).to.equal(path.resolve('/foo.html')) expect(err.file).to.equal(path.resolve('/foo.html'))
}) })
+2 -1
View File
@@ -11,7 +11,8 @@
"baseUrl": ".", "baseUrl": ".",
"paths": { "paths": {
"template": ["src/parser/template"], "template": ["src/parser/template"],
"src/*": ["src/*"] "src/*": ["src/*"],
"test/*": ["test/*"]
} }
}, },
"include": [ "src", "test" ], "include": [ "src", "test" ],