Skip to content

Commit c1a8157

Browse files
Jean Lauliacfacebook-github-bot
Jean Lauliac
authored andcommitted
packager: simplify fs.stat mocks
Reviewed By: davidaurelio Differential Revision: D4620080 fbshipit-source-id: f78b20d2f728ddd32413f27dba85cb825ec7c9a9
1 parent 84ee9a4 commit c1a8157

File tree

2 files changed

+29
-122
lines changed

2 files changed

+29
-122
lines changed

packages/metro-bundler/src/node-haste/__mocks__/graceful-fs.js

+29-67
Original file line numberDiff line numberDiff line change
@@ -93,92 +93,55 @@ fs.readFileSync.mockImplementation(function(filepath, encoding) {
9393
return node;
9494
});
9595

96+
function makeStatResult(node) {
97+
const isSymlink = node != null && node.SYMLINK != null;
98+
return {
99+
isDirectory: () => node != null && typeof node === 'object' && !isSymlink,
100+
isSymbolicLink: () => isSymlink,
101+
mtime,
102+
};
103+
}
104+
105+
function statSync(filepath) {
106+
const node = getToNode(filepath);
107+
if (node.SYMLINK) {
108+
return statSync(node.SYMLINK);
109+
}
110+
return makeStatResult(node);
111+
}
112+
96113
fs.stat.mockImplementation((filepath, callback) => {
97114
callback = asyncCallback(callback);
98-
let node;
115+
let result;
99116
try {
100-
node = getToNode(filepath);
117+
result = statSync(filepath);
101118
} catch (e) {
102119
callback(e);
103120
return;
104121
}
105-
106-
if (node.SYMLINK) {
107-
fs.stat(node.SYMLINK, callback);
108-
return;
109-
}
110-
111-
if (node && typeof node === 'object') {
112-
callback(null, {
113-
isDirectory: () => true,
114-
isSymbolicLink: () => false,
115-
mtime,
116-
});
117-
} else {
118-
callback(null, {
119-
isDirectory: () => false,
120-
isSymbolicLink: () => false,
121-
mtime,
122-
});
123-
}
122+
callback(null, result);
124123
});
125124

126-
fs.statSync.mockImplementation((filepath) => {
127-
const node = getToNode(filepath);
128-
129-
if (node.SYMLINK) {
130-
return fs.statSync(node.SYMLINK);
131-
}
125+
fs.statSync.mockImplementation(statSync);
132126

133-
return {
134-
isDirectory: () => node && typeof node === 'object',
135-
isSymbolicLink: () => false,
136-
mtime,
137-
};
138-
});
127+
function lstatSync(filepath) {
128+
const node = getToNode(filepath);
129+
return makeStatResult(node);
130+
}
139131

140132
fs.lstat.mockImplementation((filepath, callback) => {
141133
callback = asyncCallback(callback);
142-
let node;
134+
let result;
143135
try {
144-
node = getToNode(filepath);
136+
result = lstatSync(filepath);
145137
} catch (e) {
146138
callback(e);
147139
return;
148140
}
149-
150-
if (node && typeof node === 'object') {
151-
callback(null, {
152-
isDirectory: () => true,
153-
isSymbolicLink: () => false,
154-
mtime,
155-
});
156-
} else {
157-
callback(null, {
158-
isDirectory: () => false,
159-
isSymbolicLink: () => false,
160-
mtime,
161-
});
162-
}
141+
callback(null, result);
163142
});
164143

165-
fs.lstatSync.mockImplementation((filepath) => {
166-
const node = getToNode(filepath);
167-
168-
if (node.SYMLINK) {
169-
return {
170-
isDirectory: () => false,
171-
isSymbolicLink: () => true,
172-
mtime,
173-
};
174-
}
175-
176-
return {
177-
isDirectory: () => node && typeof node === 'object',
178-
isSymbolicLink: () => false,
179-
mtime,
180-
};
181-
});
144+
fs.lstatSync.mockImplementation(lstatSync);
182145

183146
fs.open.mockImplementation(function(filepath) {
184147
const callback = arguments[arguments.length - 1] || noop;
@@ -277,7 +240,6 @@ fs.createWriteStream.mockImplementation(file => {
277240
});
278241
fs.createWriteStream.mock.returned = [];
279242

280-
281243
fs.__setMockFilesystem = (object) => (filesystem = object);
282244

283245
function getToNode(filepath) {

packages/metro-bundler/src/node-haste/__tests__/DependencyGraph-test.js

-55
Original file line numberDiff line numberDiff line change
@@ -1239,61 +1239,6 @@ describe('DependencyGraph', function() {
12391239
});
12401240
});
12411241

1242-
it('should work with packages with symlinked subdirs', function() {
1243-
var root = '/root';
1244-
setMockFileSystem({
1245-
'symlinkedPackage': {
1246-
'package.json': JSON.stringify({
1247-
name: 'aPackage',
1248-
main: 'main.js',
1249-
}),
1250-
'main.js': 'lol',
1251-
'subdir': {
1252-
'lolynot.js': 'lolynot',
1253-
},
1254-
},
1255-
'root': {
1256-
'index.js': [
1257-
'/**',
1258-
' * @providesModule index',
1259-
' */',
1260-
'require("aPackage/subdir/lolynot")',
1261-
].join('\n'),
1262-
'aPackage': { SYMLINK: '/symlinkedPackage' },
1263-
},
1264-
});
1265-
1266-
var dgraph = new DependencyGraph({
1267-
...defaults,
1268-
roots: [root],
1269-
});
1270-
return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').then(function(deps) {
1271-
expect(deps)
1272-
.toEqual([
1273-
{
1274-
id: 'index',
1275-
path: '/root/index.js',
1276-
dependencies: ['aPackage/subdir/lolynot'],
1277-
isAsset: false,
1278-
isJSON: false,
1279-
isPolyfill: false,
1280-
resolution: undefined,
1281-
resolveDependency: undefined,
1282-
},
1283-
{
1284-
id: 'aPackage/subdir/lolynot.js',
1285-
path: '/root/aPackage/subdir/lolynot.js',
1286-
dependencies: [],
1287-
isAsset: false,
1288-
isJSON: false,
1289-
isPolyfill: false,
1290-
resolution: undefined,
1291-
resolveDependency: undefined,
1292-
},
1293-
]);
1294-
});
1295-
});
1296-
12971242
it('should work with relative modules in packages', function() {
12981243
var root = '/root';
12991244
setMockFileSystem({

0 commit comments

Comments
 (0)