Skip to content

Commit

Permalink
packager: simplify fs.stat mocks
Browse files Browse the repository at this point in the history
Reviewed By: davidaurelio

Differential Revision: D4620080

fbshipit-source-id: f78b20d2f728ddd32413f27dba85cb825ec7c9a9
  • Loading branch information
Jean Lauliac authored and facebook-github-bot committed Feb 28, 2017
1 parent 84ee9a4 commit c1a8157
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 122 deletions.
96 changes: 29 additions & 67 deletions packages/metro-bundler/src/node-haste/__mocks__/graceful-fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -277,7 +240,6 @@ fs.createWriteStream.mockImplementation(file => {
});
fs.createWriteStream.mock.returned = [];


fs.__setMockFilesystem = (object) => (filesystem = object);

function getToNode(filepath) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down

0 comments on commit c1a8157

Please sign in to comment.