|
| 1 | +/** |
| 2 | + * Copyright (c) 2015-present, Facebook, Inc. |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * This source code is licensed under the BSD-style license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. An additional grant |
| 7 | + * of patent rights can be found in the PATENTS file in the same directory. |
| 8 | + */ |
| 9 | +'use strict'; |
| 10 | + |
| 11 | +const fs = jest.genMockFromModule('fs'); |
| 12 | +const noop = () => {}; |
| 13 | + |
| 14 | +function asyncCallback(cb) { |
| 15 | + return function() { |
| 16 | + setImmediate(() => cb.apply(this, arguments)); |
| 17 | + }; |
| 18 | +} |
| 19 | + |
| 20 | +const mtime = { |
| 21 | + getTime: () => Math.ceil(Math.random() * 10000000), |
| 22 | +}; |
| 23 | + |
| 24 | +fs.realpath.mockImpl((filepath, callback) => { |
| 25 | + callback = asyncCallback(callback); |
| 26 | + let node; |
| 27 | + try { |
| 28 | + node = getToNode(filepath); |
| 29 | + } catch (e) { |
| 30 | + return callback(e); |
| 31 | + } |
| 32 | + if (node && typeof node === 'object' && node.SYMLINK != null) { |
| 33 | + return callback(null, node.SYMLINK); |
| 34 | + } |
| 35 | + callback(null, filepath); |
| 36 | +}); |
| 37 | + |
| 38 | +fs.readdirSync.mockImpl(filepath => Object.keys(getToNode(filepath))); |
| 39 | + |
| 40 | +fs.readdir.mockImpl((filepath, callback) => { |
| 41 | + callback = asyncCallback(callback); |
| 42 | + let node; |
| 43 | + try { |
| 44 | + node = getToNode(filepath); |
| 45 | + if (node && typeof node === 'object' && node.SYMLINK != null) { |
| 46 | + node = getToNode(node.SYMLINK); |
| 47 | + } |
| 48 | + } catch (e) { |
| 49 | + return callback(e); |
| 50 | + } |
| 51 | + |
| 52 | + if (!(node && typeof node === 'object' && node.SYMLINK == null)) { |
| 53 | + return callback(new Error(filepath + ' is not a directory.')); |
| 54 | + } |
| 55 | + |
| 56 | + callback(null, Object.keys(node)); |
| 57 | +}); |
| 58 | + |
| 59 | +fs.readFile.mockImpl(function(filepath, encoding, callback) { |
| 60 | + callback = asyncCallback(callback); |
| 61 | + if (arguments.length === 2) { |
| 62 | + callback = encoding; |
| 63 | + encoding = null; |
| 64 | + } |
| 65 | + |
| 66 | + let node; |
| 67 | + try { |
| 68 | + node = getToNode(filepath); |
| 69 | + // dir check |
| 70 | + if (node && typeof node === 'object' && node.SYMLINK == null) { |
| 71 | + callback(new Error('Error readFile a dir: ' + filepath)); |
| 72 | + } |
| 73 | + return callback(null, node); |
| 74 | + } catch (e) { |
| 75 | + return callback(e); |
| 76 | + } |
| 77 | +}); |
| 78 | + |
| 79 | +fs.stat.mockImpl((filepath, callback) => { |
| 80 | + callback = asyncCallback(callback); |
| 81 | + let node; |
| 82 | + try { |
| 83 | + node = getToNode(filepath); |
| 84 | + } catch (e) { |
| 85 | + callback(e); |
| 86 | + return; |
| 87 | + } |
| 88 | + |
| 89 | + if (node.SYMLINK) { |
| 90 | + fs.stat(node.SYMLINK, callback); |
| 91 | + return; |
| 92 | + } |
| 93 | + |
| 94 | + if (node && typeof node === 'object') { |
| 95 | + callback(null, { |
| 96 | + isDirectory: () => true, |
| 97 | + isSymbolicLink: () => false, |
| 98 | + mtime, |
| 99 | + }); |
| 100 | + } else { |
| 101 | + callback(null, { |
| 102 | + isDirectory: () => false, |
| 103 | + isSymbolicLink: () => false, |
| 104 | + mtime, |
| 105 | + }); |
| 106 | + } |
| 107 | +}); |
| 108 | + |
| 109 | +fs.statSync.mockImpl(filepath => { |
| 110 | + const node = getToNode(filepath); |
| 111 | + |
| 112 | + if (node.SYMLINK) { |
| 113 | + return fs.statSync(node.SYMLINK); |
| 114 | + } |
| 115 | + |
| 116 | + return { |
| 117 | + isDirectory: () => node && typeof node === 'object', |
| 118 | + isSymbolicLink: () => false, |
| 119 | + mtime, |
| 120 | + }; |
| 121 | +}); |
| 122 | + |
| 123 | +fs.lstatSync.mockImpl(filepath => { |
| 124 | + const node = getToNode(filepath); |
| 125 | + |
| 126 | + if (node.SYMLINK) { |
| 127 | + return { |
| 128 | + isDirectory: () => false, |
| 129 | + isSymbolicLink: () => true, |
| 130 | + mtime, |
| 131 | + }; |
| 132 | + } |
| 133 | + |
| 134 | + return { |
| 135 | + isDirectory: () => node && typeof node === 'object', |
| 136 | + isSymbolicLink: () => false, |
| 137 | + mtime, |
| 138 | + }; |
| 139 | +}); |
| 140 | + |
| 141 | +fs.open.mockImpl(function(path) { |
| 142 | + const callback = arguments[arguments.length - 1] || noop; |
| 143 | + let data, error, fd; |
| 144 | + try { |
| 145 | + data = getToNode(path); |
| 146 | + } catch (e) { |
| 147 | + error = e; |
| 148 | + } |
| 149 | + |
| 150 | + if (error || data == null) { |
| 151 | + error = Error(`ENOENT: no such file or directory, open ${path}`); |
| 152 | + } |
| 153 | + if (data != null) { |
| 154 | + /* global Buffer: true */ |
| 155 | + fd = {buffer: new Buffer(data, 'utf8'), position: 0}; |
| 156 | + } |
| 157 | + |
| 158 | + callback(error, fd); |
| 159 | +}); |
| 160 | + |
| 161 | +fs.read.mockImpl((fd, buffer, writeOffset, length, position, callback = noop) => { |
| 162 | + let bytesWritten; |
| 163 | + try { |
| 164 | + if (position == null || position < 0) { |
| 165 | + ({position} = fd); |
| 166 | + } |
| 167 | + bytesWritten = fd.buffer.copy(buffer, writeOffset, position, position + length); |
| 168 | + fd.position = position + bytesWritten; |
| 169 | + } catch (e) { |
| 170 | + callback(Error('invalid argument')); |
| 171 | + return; |
| 172 | + } |
| 173 | + callback(null, bytesWritten, buffer); |
| 174 | +}); |
| 175 | + |
| 176 | +fs.close.mockImpl((fd, callback = noop) => { |
| 177 | + try { |
| 178 | + fd.buffer = fs.position = undefined; |
| 179 | + } catch (e) { |
| 180 | + callback(Error('invalid argument')); |
| 181 | + return; |
| 182 | + } |
| 183 | + callback(null); |
| 184 | +}); |
| 185 | + |
| 186 | +let filesystem; |
| 187 | + |
| 188 | +fs.__setMockFilesystem = object => filesystem = object; |
| 189 | + |
| 190 | +function getToNode(filepath) { |
| 191 | + // Ignore the drive for Windows paths. |
| 192 | + if (filepath.match(/^[a-zA-Z]:\\/)) { |
| 193 | + filepath = filepath.substring(2); |
| 194 | + } |
| 195 | + |
| 196 | + const parts = filepath.split(/[\/\\]/); |
| 197 | + if (parts[0] !== '') { |
| 198 | + throw new Error('Make sure all paths are absolute.'); |
| 199 | + } |
| 200 | + let node = filesystem; |
| 201 | + parts.slice(1).forEach(part => { |
| 202 | + if (node && node.SYMLINK) { |
| 203 | + node = getToNode(node.SYMLINK); |
| 204 | + } |
| 205 | + node = node[part]; |
| 206 | + }); |
| 207 | + |
| 208 | + return node; |
| 209 | +} |
| 210 | + |
| 211 | +module.exports = fs; |
0 commit comments