Create a fake filesystem for unit testing GitBook. @param {Map }
(files)
| 12 | @param {Map<String:String|Map>} |
| 13 | */ |
| 14 | function createMockFS(files) { |
| 15 | files = Immutable.fromJS(files); |
| 16 | var mtime = new Date(); |
| 17 | |
| 18 | function getFile(filePath) { |
| 19 | var parts = path.normalize(filePath).split(path.sep); |
| 20 | return parts.reduce(function(list, part, i) { |
| 21 | if (!list) return null; |
| 22 | |
| 23 | var file; |
| 24 | |
| 25 | if (!part || part === '.') file = list; |
| 26 | else file = list.get(part); |
| 27 | |
| 28 | if (!file) return null; |
| 29 | |
| 30 | if (is.string(file)) { |
| 31 | if (i === (parts.length - 1)) return file; |
| 32 | else return null; |
| 33 | } |
| 34 | |
| 35 | return file; |
| 36 | }, files); |
| 37 | } |
| 38 | |
| 39 | function fsExists(filePath) { |
| 40 | return Boolean(getFile(filePath) !== null); |
| 41 | } |
| 42 | |
| 43 | function fsReadFile(filePath) { |
| 44 | var file = getFile(filePath); |
| 45 | if (!is.string(file)) { |
| 46 | throw error.FileNotFoundError({ |
| 47 | filename: filePath |
| 48 | }); |
| 49 | } |
| 50 | |
| 51 | return new Buffer(file, 'utf8'); |
| 52 | } |
| 53 | |
| 54 | function fsStatFile(filePath) { |
| 55 | var file = getFile(filePath); |
| 56 | if (!file) { |
| 57 | throw error.FileNotFoundError({ |
| 58 | filename: filePath |
| 59 | }); |
| 60 | } |
| 61 | |
| 62 | return { |
| 63 | mtime: mtime |
| 64 | }; |
| 65 | } |
| 66 | |
| 67 | function fsReadDir(filePath) { |
| 68 | var dir = getFile(filePath); |
| 69 | if (!dir || is.string(dir)) { |
| 70 | throw error.FileNotFoundError({ |
| 71 | filename: filePath |
no outgoing calls
searching dependent graphs…