mirror of
https://github.com/zhigang1992/react-native.git
synced 2026-04-24 04:16:00 +08:00
metro-bundler: add fs#writeFileSync to the mock
Reviewed By: rafeca Differential Revision: D5931412 fbshipit-source-id: 2b51617b57963c424446b04e9381e6500323af56
This commit is contained in:
committed by
Facebook Github Bot
parent
7320ca5c7a
commit
b3fc64285e
@@ -11,9 +11,11 @@
|
||||
|
||||
'use strict';
|
||||
|
||||
const asyncify = require('async/asyncify');
|
||||
const {EventEmitter} = require('events');
|
||||
const {dirname} = require.requireActual('path');
|
||||
const fs = jest.genMockFromModule('fs');
|
||||
const invariant = require('fbjs/lib/invariant');
|
||||
const path = require('path');
|
||||
const stream = require.requireActual('stream');
|
||||
|
||||
@@ -74,8 +76,7 @@ fs.readFile.mockImplementation(function(filepath, encoding, callback) {
|
||||
let node;
|
||||
try {
|
||||
node = getToNode(filepath);
|
||||
// dir check
|
||||
if (node && typeof node === 'object' && node.SYMLINK == null) {
|
||||
if (isDirNode(node)) {
|
||||
callback(new Error('Error readFile a dir: ' + filepath));
|
||||
}
|
||||
if (node == null) {
|
||||
@@ -90,13 +91,51 @@ fs.readFile.mockImplementation(function(filepath, encoding, callback) {
|
||||
|
||||
fs.readFileSync.mockImplementation(function(filepath, encoding) {
|
||||
const node = getToNode(filepath);
|
||||
// dir check
|
||||
if (node && typeof node === 'object' && node.SYMLINK == null) {
|
||||
if (isDirNode(node)) {
|
||||
throw new Error('Error readFileSync a dir: ' + filepath);
|
||||
}
|
||||
return node;
|
||||
});
|
||||
|
||||
fs.writeFile.mockImplementation(asyncify(fs.writeFileSync));
|
||||
|
||||
fs.writeFileSync.mockImplementation((filePath, content, options) => {
|
||||
if (options == null || typeof options === 'string') {
|
||||
options = {encoding: options};
|
||||
}
|
||||
invariant(
|
||||
options.encoding == null || options.encoding === 'utf8',
|
||||
'`options` argument supports only `null` or `"utf8"`',
|
||||
);
|
||||
const dirPath = path.dirname(filePath);
|
||||
const node = getToNode(dirPath);
|
||||
if (!isDirNode(node)) {
|
||||
throw fsError('ENOTDIR', 'not a directory: ' + dirPath);
|
||||
}
|
||||
node[path.basename(filePath)] = content;
|
||||
});
|
||||
|
||||
fs.mkdir.mockImplementation(asyncify(fs.mkdirSync));
|
||||
|
||||
fs.mkdirSync.mockImplementation((dirPath, mode) => {
|
||||
const parentPath = path.dirname(dirPath);
|
||||
const node = getToNode(parentPath);
|
||||
if (!isDirNode(node)) {
|
||||
throw fsError('ENOTDIR', 'not a directory: ' + parentPath);
|
||||
}
|
||||
node[path.basename(dirPath)] = {};
|
||||
});
|
||||
|
||||
function fsError(code, message) {
|
||||
const error = new Error(code + ': ' + message);
|
||||
error.code = code;
|
||||
return error;
|
||||
}
|
||||
|
||||
function isDirNode(node) {
|
||||
return node && typeof node === 'object' && node.SYMLINK == null;
|
||||
}
|
||||
|
||||
function readlinkSync(filepath) {
|
||||
const node = getToNode(filepath);
|
||||
if (node !== null && typeof node === 'object' && !!node.SYMLINK) {
|
||||
@@ -250,7 +289,7 @@ fs.close.mockImplementation((fd, callback = noop) => {
|
||||
callback(null);
|
||||
});
|
||||
|
||||
let filesystem;
|
||||
let filesystem = {};
|
||||
|
||||
fs.createReadStream.mockImplementation(filepath => {
|
||||
if (!filepath.startsWith('/')) {
|
||||
|
||||
Reference in New Issue
Block a user