diff options
Diffstat (limited to 'packages/gitbook/src/fs/mock.js')
-rw-r--r-- | packages/gitbook/src/fs/mock.js | 95 |
1 files changed, 95 insertions, 0 deletions
diff --git a/packages/gitbook/src/fs/mock.js b/packages/gitbook/src/fs/mock.js new file mode 100644 index 0000000..611b2ab --- /dev/null +++ b/packages/gitbook/src/fs/mock.js @@ -0,0 +1,95 @@ +const path = require('path'); +const is = require('is'); +const Buffer = require('buffer').Buffer; +const Immutable = require('immutable'); + +const FS = require('../models/fs'); +const error = require('../utils/error'); + +/** + * Create a fake filesystem for unit testing GitBook. + * @param {Map<String:String|Map>} + * @return {FS} + */ +function createMockFS(files, root = '') { + files = Immutable.fromJS(files); + const mtime = new Date(); + + function getFile(filePath) { + const parts = path.normalize(filePath).split(path.sep); + return parts.reduce(function(list, part, i) { + if (!list) return null; + + let file; + + if (!part || part === '.') file = list; + else file = list.get(part); + + if (!file) return null; + + if (is.string(file)) { + if (i === (parts.length - 1)) return file; + else return null; + } + + return file; + }, files); + } + + function fsExists(filePath) { + return Boolean(getFile(filePath) !== null); + } + + function fsReadFile(filePath) { + const file = getFile(filePath); + if (!is.string(file)) { + throw error.FileNotFoundError({ + filename: filePath + }); + } + + return new Buffer(file, 'utf8'); + } + + function fsStatFile(filePath) { + const file = getFile(filePath); + if (!file) { + throw error.FileNotFoundError({ + filename: filePath + }); + } + + return { + mtime + }; + } + + function fsReadDir(filePath) { + const dir = getFile(filePath); + if (!dir || is.string(dir)) { + throw error.FileNotFoundError({ + filename: filePath + }); + } + + return dir + .map(function(content, name) { + if (!is.string(content)) { + name = name + '/'; + } + + return name; + }) + .valueSeq(); + } + + return FS.create({ + root: '', + fsExists, + fsReadFile, + fsStatFile, + fsReadDir + }); +} + +module.exports = createMockFS; |