Permalink
Browse files

packager: simplify fs.stat mocks

Reviewed By: davidaurelio

Differential Revision: D4620080

fbshipit-source-id: f78b20d2f728ddd32413f27dba85cb825ec7c9a9
  • Loading branch information...
jeanlauliac authored and facebook-github-bot committed Feb 28, 2017
1 parent a8bbb98 commit f117533f8227e0ca1140829e8e3eed09c06494b0
@@ -93,92 +93,55 @@ fs.readFileSync.mockImplementation(function(filepath, encoding) {
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) => {
callback = asyncCallback(callback);
let node;
let result;
try {
node = getToNode(filepath);
result = statSync(filepath);
} catch (e) {
callback(e);
return;
}
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,
});
}
callback(null, result);
});
fs.statSync.mockImplementation((filepath) => {
const node = getToNode(filepath);
if (node.SYMLINK) {
return fs.statSync(node.SYMLINK);
}
fs.statSync.mockImplementation(statSync);
return {
isDirectory: () => node && typeof node === 'object',
isSymbolicLink: () => false,
mtime,
};
});
function lstatSync(filepath) {
const node = getToNode(filepath);
return makeStatResult(node);
}
fs.lstat.mockImplementation((filepath, callback) => {
callback = asyncCallback(callback);
let node;
let result;
try {
node = getToNode(filepath);
result = lstatSync(filepath);
} catch (e) {
callback(e);
return;
}
if (node && typeof node === 'object') {
callback(null, {
isDirectory: () => true,
isSymbolicLink: () => false,
mtime,
});
} else {
callback(null, {
isDirectory: () => false,
isSymbolicLink: () => false,
mtime,
});
}
callback(null, result);
});
fs.lstatSync.mockImplementation((filepath) => {
const node = getToNode(filepath);
if (node.SYMLINK) {
return {
isDirectory: () => false,
isSymbolicLink: () => true,
mtime,
};
}
return {
isDirectory: () => node && typeof node === 'object',
isSymbolicLink: () => false,
mtime,
};
});
fs.lstatSync.mockImplementation(lstatSync);
fs.open.mockImplementation(function(filepath) {
const callback = arguments[arguments.length - 1] || noop;
@@ -277,7 +240,6 @@ fs.createWriteStream.mockImplementation(file => {
});
fs.createWriteStream.mock.returned = [];
fs.__setMockFilesystem = (object) => (filesystem = object);
function getToNode(filepath) {
@@ -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() {
var root = '/root';
setMockFileSystem({

0 comments on commit f117533

Please sign in to comment.