Skip to content

Commit b3fc642

Browse files
Jean Lauliacfacebook-github-bot
authored andcommitted
metro-bundler: add fs#writeFileSync to the mock
Reviewed By: rafeca Differential Revision: D5931412 fbshipit-source-id: 2b51617b57963c424446b04e9381e6500323af56
1 parent 7320ca5 commit b3fc642

File tree

2 files changed

+93
-5
lines changed

2 files changed

+93
-5
lines changed

local-cli/util/__mocks__/fs.js

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@
1111

1212
'use strict';
1313

14+
const asyncify = require('async/asyncify');
1415
const {EventEmitter} = require('events');
1516
const {dirname} = require.requireActual('path');
1617
const fs = jest.genMockFromModule('fs');
18+
const invariant = require('fbjs/lib/invariant');
1719
const path = require('path');
1820
const stream = require.requireActual('stream');
1921

@@ -74,8 +76,7 @@ fs.readFile.mockImplementation(function(filepath, encoding, callback) {
7476
let node;
7577
try {
7678
node = getToNode(filepath);
77-
// dir check
78-
if (node && typeof node === 'object' && node.SYMLINK == null) {
79+
if (isDirNode(node)) {
7980
callback(new Error('Error readFile a dir: ' + filepath));
8081
}
8182
if (node == null) {
@@ -90,13 +91,51 @@ fs.readFile.mockImplementation(function(filepath, encoding, callback) {
9091

9192
fs.readFileSync.mockImplementation(function(filepath, encoding) {
9293
const node = getToNode(filepath);
93-
// dir check
94-
if (node && typeof node === 'object' && node.SYMLINK == null) {
94+
if (isDirNode(node)) {
9595
throw new Error('Error readFileSync a dir: ' + filepath);
9696
}
9797
return node;
9898
});
9999

100+
fs.writeFile.mockImplementation(asyncify(fs.writeFileSync));
101+
102+
fs.writeFileSync.mockImplementation((filePath, content, options) => {
103+
if (options == null || typeof options === 'string') {
104+
options = {encoding: options};
105+
}
106+
invariant(
107+
options.encoding == null || options.encoding === 'utf8',
108+
'`options` argument supports only `null` or `"utf8"`',
109+
);
110+
const dirPath = path.dirname(filePath);
111+
const node = getToNode(dirPath);
112+
if (!isDirNode(node)) {
113+
throw fsError('ENOTDIR', 'not a directory: ' + dirPath);
114+
}
115+
node[path.basename(filePath)] = content;
116+
});
117+
118+
fs.mkdir.mockImplementation(asyncify(fs.mkdirSync));
119+
120+
fs.mkdirSync.mockImplementation((dirPath, mode) => {
121+
const parentPath = path.dirname(dirPath);
122+
const node = getToNode(parentPath);
123+
if (!isDirNode(node)) {
124+
throw fsError('ENOTDIR', 'not a directory: ' + parentPath);
125+
}
126+
node[path.basename(dirPath)] = {};
127+
});
128+
129+
function fsError(code, message) {
130+
const error = new Error(code + ': ' + message);
131+
error.code = code;
132+
return error;
133+
}
134+
135+
function isDirNode(node) {
136+
return node && typeof node === 'object' && node.SYMLINK == null;
137+
}
138+
100139
function readlinkSync(filepath) {
101140
const node = getToNode(filepath);
102141
if (node !== null && typeof node === 'object' && !!node.SYMLINK) {
@@ -250,7 +289,7 @@ fs.close.mockImplementation((fd, callback = noop) => {
250289
callback(null);
251290
});
252291

253-
let filesystem;
292+
let filesystem = {};
254293

255294
fs.createReadStream.mockImplementation(filepath => {
256295
if (!filepath.startsWith('/')) {
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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+
* @emails oncall+javascript_foundation
10+
* @flow
11+
* @format
12+
*/
13+
14+
'use strict';
15+
16+
/* eslint-disable no-unclear-flowtypes */
17+
18+
declare var jest: any;
19+
declare var describe: any;
20+
declare var it: any;
21+
22+
jest.mock('fs');
23+
24+
const fs = require('fs');
25+
26+
describe('fs mock', () => {
27+
describe('writeFileSync()', () => {
28+
it('stores content correctly', () => {
29+
fs.writeFileSync('/test', 'foobar', 'utf8');
30+
const content = fs.readFileSync('/test', 'utf8');
31+
expect(content).toEqual('foobar');
32+
});
33+
34+
it('fails on missing path', () => {
35+
expect(() =>
36+
fs.writeFileSync('/dir/test', 'foobar', 'utf8'),
37+
).toThrowError('ENOENT: no such file or directory');
38+
});
39+
});
40+
41+
describe('mkdirSync()', () => {
42+
it('creates folders that we can write files in', () => {
43+
fs.mkdirSync('/dir', 0o777);
44+
fs.writeFileSync('/dir/test', 'foobar', 'utf8');
45+
const content = fs.readFileSync('/dir/test', 'utf8');
46+
expect(content).toEqual('foobar');
47+
});
48+
});
49+
});

0 commit comments

Comments
 (0)