1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
|
'use strict'
/* global emitOnexit:true */ // eslint-disable-line no-unused-vars
var path = require('path')
var fs = require('fs')
var stream = require('stream')
var EventEmitter = require('events')
exports.fixturesDir = path.join(__dirname, 'fixtures')
var mustCallChecks = []
if (process.on === process.emit) {
let emitter
const reset = () => {
mustCallChecks = []
emitter = new EventEmitter()
process.on = emitter.on.bind(emitter)
process.once = emitter.once.bind(emitter)
}
emitOnexit = () => {
emitter.emit('exit')
reset()
}
reset()
}
function runCallChecks (exitCode) {
if (exitCode !== 0) return
var failed = mustCallChecks.filter(function (context) {
return context.actual !== context.expected
})
failed.forEach(function (context) {
console.log('Mismatched %s function calls. Expected %d, actual %d.',
context.name,
context.expected,
context.actual)
console.log(context.stack.split('\n').slice(2).join('\n'))
})
if (failed.length) process.exit(1)
}
exports.mustCall = function (fn, expected) {
if (typeof expected !== 'number') expected = 1
var context = {
expected: expected,
actual: 0,
stack: (new Error()).stack,
name: fn.name || '<anonymous>'
}
// add the exit listener only once to avoid listener leak warnings
if (mustCallChecks.length === 0) process.on('exit', runCallChecks)
mustCallChecks.push(context)
return function () {
context.actual++
return fn.apply(this, arguments)
}
}
var testRoot = path.resolve(path.dirname(__filename))
exports.tmpDirName = 'tmp'
exports.tmpDir = path.join(testRoot, exports.tmpDirName)
function rmdirSync (p, originalEr) {
try {
fs.rmdirSync(p)
} catch (e) {
if (e.code === 'ENOTDIR') {
throw originalEr
}
if (e.code === 'ENOTEMPTY' || e.code === 'EEXIST' || e.code === 'EPERM') {
fs.readdirSync(p).forEach(function (f) {
rimrafSync(path.join(p, f))
})
fs.rmdirSync(p)
}
}
}
function rimrafSync (p) {
try {
var st = fs.lstatSync(p)
} catch (e) {
if (e.code === 'ENOENT') {
return
}
}
try {
if (st && st.isDirectory()) {
rmdirSync(p, null)
} else {
fs.unlinkSync(p)
}
} catch (e) {
if (e.code === 'ENOENT') {
return
}
if (e.code === 'EPERM') {
return rmdirSync(p, e)
}
if (e.code !== 'EISDIR') {
throw e
}
rmdirSync(p, e)
}
}
exports.refreshTmpDir = function () {
rimrafSync(exports.tmpDir)
fs.mkdirSync(exports.tmpDir)
}
const rFS = 'readFileSync' // this stops the brfs static analyzer
if (!fs[rFS]) {
// this is to make it work with brfs
const files = {
'elipses.txt': fs.readFileSync(path.resolve('test/fixtures', 'elipses.txt')),
// there is a strange issue like https://github.com/nodejs/node-v0.x-archive/issues/7914,
// even though that should be fixed.
'empty.txt': Buffer.alloc(0),
// 'empty.txt': fs.readFileSync(path.resolve('test/fixtures', 'empty.txt')),
'person.jpg': fs.readFileSync(path.resolve('test/fixtures', 'person.jpg')),
'person.jpg.gz': fs.readFileSync(path.resolve('test/fixtures', 'person.jpg.gz')),
'pseudo-multimember-gzip.gz': fs.readFileSync(path.resolve('test/fixtures', 'pseudo-multimember-gzip.gz')),
'pseudo-multimember-gzip.z': fs.readFileSync(path.resolve('test/fixtures', 'pseudo-multimember-gzip.z'))
}
Object.keys(files).forEach(file => {
files[path.resolve(exports.fixturesDir, file)] = files[file]
})
fs[rFS] = name => {
if (!files[name]) throw new Error(`file "${name}" not found`)
return files[name]
}
const cRS = 'createReadStream'
fs[cRS] = name => {
const s = new stream.Readable()
s.push(fs.readFileSync(name))
s.push(null)
return s
}
fs.createWriteStream = name => {
const s = new stream.Writable()
const chunks = []
s._write = (chunk, encoding, callback) => {
chunks.push(chunk)
callback()
}
s.on('finish', () => {
files[name] = Buffer.concat(chunks)
})
return s
}
exports.refreshTmpDir = () => {}
}
|