packager: simplify fs.stat mocks

Reviewed By: davidaurelio

Differential Revision: D4620080

fbshipit-source-id: f78b20d2f728ddd32413f27dba85cb825ec7c9a9
This commit is contained in:
Jean Lauliac 2017-02-28 06:10:41 -08:00 committed by Facebook Github Bot
parent a8bbb98805
commit f117533f82
2 changed files with 29 additions and 122 deletions

View File

@ -93,92 +93,55 @@ fs.readFileSync.mockImplementation(function(filepath, encoding) {
return node; return node;
}); });
function makeStatResult(node) {
const isSymlink = node != null && node.SYMLINK != null;
return {
isDirectory: () => node != null && typeof node === 'object' && !isSymlink,
isSymbolicLink: () => isSymlink,
mtime,
};
}
function statSync(filepath) {
const node = getToNode(filepath);
if (node.SYMLINK) {
return statSync(node.SYMLINK);
}
return makeStatResult(node);
}
fs.stat.mockImplementation((filepath, callback) => { fs.stat.mockImplementation((filepath, callback) => {
callback = asyncCallback(callback); callback = asyncCallback(callback);
let node; let result;
try { try {
node = getToNode(filepath); result = statSync(filepath);
} catch (e) { } catch (e) {
callback(e); callback(e);
return; return;
} }
callback(null, result);
if (node.SYMLINK) {
fs.stat(node.SYMLINK, callback);
return;
}
if (node && typeof node === 'object') {
callback(null, {
isDirectory: () => true,
isSymbolicLink: () => false,
mtime,
});
} else {
callback(null, {
isDirectory: () => false,
isSymbolicLink: () => false,
mtime,
});
}
}); });
fs.statSync.mockImplementation((filepath) => { fs.statSync.mockImplementation(statSync);
function lstatSync(filepath) {
const node = getToNode(filepath); const node = getToNode(filepath);
return makeStatResult(node);
if (node.SYMLINK) { }
return fs.statSync(node.SYMLINK);
}
return {
isDirectory: () => node && typeof node === 'object',
isSymbolicLink: () => false,
mtime,
};
});
fs.lstat.mockImplementation((filepath, callback) => { fs.lstat.mockImplementation((filepath, callback) => {
callback = asyncCallback(callback); callback = asyncCallback(callback);
let node; let result;
try { try {
node = getToNode(filepath); result = lstatSync(filepath);
} catch (e) { } catch (e) {
callback(e); callback(e);
return; return;
} }
callback(null, result);
if (node && typeof node === 'object') {
callback(null, {
isDirectory: () => true,
isSymbolicLink: () => false,
mtime,
});
} else {
callback(null, {
isDirectory: () => false,
isSymbolicLink: () => false,
mtime,
});
}
}); });
fs.lstatSync.mockImplementation((filepath) => { fs.lstatSync.mockImplementation(lstatSync);
const node = getToNode(filepath);
if (node.SYMLINK) {
return {
isDirectory: () => false,
isSymbolicLink: () => true,
mtime,
};
}
return {
isDirectory: () => node && typeof node === 'object',
isSymbolicLink: () => false,
mtime,
};
});
fs.open.mockImplementation(function(filepath) { fs.open.mockImplementation(function(filepath) {
const callback = arguments[arguments.length - 1] || noop; const callback = arguments[arguments.length - 1] || noop;
@ -277,7 +240,6 @@ fs.createWriteStream.mockImplementation(file => {
}); });
fs.createWriteStream.mock.returned = []; fs.createWriteStream.mock.returned = [];
fs.__setMockFilesystem = (object) => (filesystem = object); fs.__setMockFilesystem = (object) => (filesystem = object);
function getToNode(filepath) { function getToNode(filepath) {

View File

@ -1239,61 +1239,6 @@ describe('DependencyGraph', function() {
}); });
}); });
it('should work with packages with symlinked subdirs', function() {
var root = '/root';
setMockFileSystem({
'symlinkedPackage': {
'package.json': JSON.stringify({
name: 'aPackage',
main: 'main.js',
}),
'main.js': 'lol',
'subdir': {
'lolynot.js': 'lolynot',
},
},
'root': {
'index.js': [
'/**',
' * @providesModule index',
' */',
'require("aPackage/subdir/lolynot")',
].join('\n'),
'aPackage': { SYMLINK: '/symlinkedPackage' },
},
});
var dgraph = new DependencyGraph({
...defaults,
roots: [root],
});
return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').then(function(deps) {
expect(deps)
.toEqual([
{
id: 'index',
path: '/root/index.js',
dependencies: ['aPackage/subdir/lolynot'],
isAsset: false,
isJSON: false,
isPolyfill: false,
resolution: undefined,
resolveDependency: undefined,
},
{
id: 'aPackage/subdir/lolynot.js',
path: '/root/aPackage/subdir/lolynot.js',
dependencies: [],
isAsset: false,
isJSON: false,
isPolyfill: false,
resolution: undefined,
resolveDependency: undefined,
},
]);
});
});
it('should work with relative modules in packages', function() { it('should work with relative modules in packages', function() {
var root = '/root'; var root = '/root';
setMockFileSystem({ setMockFileSystem({