packager: DependencyGraph-test: fix tests

Reviewed By: davidaurelio

Differential Revision: D4635348

fbshipit-source-id: 0130496e20d54f5ddf6546823b7a6cba3118917d
This commit is contained in:
Jean Lauliac 2017-03-02 08:33:02 -08:00 committed by Facebook Github Bot
parent 20da966546
commit 17c6ca3484
2 changed files with 303 additions and 289 deletions

View File

@ -110,7 +110,7 @@ function makeStatResult(node) {
function statSync(filepath) { function statSync(filepath) {
const node = getToNode(filepath); const node = getToNode(filepath);
if (node.SYMLINK) { if (node != null && node.SYMLINK) {
return statSync(node.SYMLINK); return statSync(node.SYMLINK);
} }
return makeStatResult(node); return makeStatResult(node);
@ -295,6 +295,11 @@ function getToNode(filepath) {
node = getToNode(node.SYMLINK); node = getToNode(node.SYMLINK);
} }
node = node[part]; node = node[part];
if (node == null) {
const err = new Error('ENOENT: no such file or directory');
err.code = 'ENOENT';
throw err;
}
}); });
return node; return node;

View File

@ -27,13 +27,8 @@ jasmine.DEFAULT_TIMEOUT_INTERVAL = 10000;
const path = require('path'); const path = require('path');
const mockStat = {
isDirectory: () => false,
};
beforeEach(() => { beforeEach(() => {
jest.resetModules(); jest.resetModules();
jest.mock('path', () => path); jest.mock('path', () => path);
}); });
@ -3111,74 +3106,70 @@ describe('DependencyGraph', function() {
filesystem.root['index.js'] = filesystem.root['index.js'] filesystem.root['index.js'] = filesystem.root['index.js']
.replace('require("dontWork")', '') .replace('require("dontWork")', '')
.replace('require("wontWork")', ''); .replace('require("wontWork")', '');
triggerWatchEvent('change', root + '/index.js'); return triggerAndProcessWatchEvent(dgraph, 'change', root + '/index.js')
return new Promise(resolve => { .then(() => getOrderedDependenciesAsJSON(dgraph, '/root/index.js'))
dgraph.once('change', () => { .then(deps => {
return resolve(getOrderedDependenciesAsJSON(dgraph, '/root/index.js') expect(deps).toEqual([
.then(deps => { {
expect(deps).toEqual([ id: 'index',
{ path: '/root/index.js',
id: 'index', dependencies: [
path: '/root/index.js', 'shouldWork',
dependencies: [ 'ember',
'shouldWork', 'internalVendoredPackage',
'ember', 'anotherIndex',
'internalVendoredPackage', ],
'anotherIndex', isAsset: false,
], isJSON: false,
isAsset: false, isPolyfill: false,
isJSON: false, resolution: undefined,
isPolyfill: false, },
resolution: undefined, {
}, id: 'shouldWork',
{ path: '/root/node_modules/react-haste/main.js',
id: 'shouldWork', dependencies: ['submodule'],
path: '/root/node_modules/react-haste/main.js', isAsset: false,
dependencies: ['submodule'], isJSON: false,
isAsset: false, isPolyfill: false,
isJSON: false, resolution: undefined,
isPolyfill: false, },
resolution: undefined, {
}, id: 'submodule/main.js',
{ path: '/root/node_modules/react-haste/node_modules/submodule/main.js',
id: 'submodule/main.js', dependencies: [],
path: '/root/node_modules/react-haste/node_modules/submodule/main.js', isAsset: false,
dependencies: [], isJSON: false,
isAsset: false, isPolyfill: false,
isJSON: false, resolution: undefined,
isPolyfill: false, },
resolution: undefined, {
}, id: 'ember/main.js',
{ path: '/root/node_modules/ember/main.js',
id: 'ember/main.js', dependencies: [],
path: '/root/node_modules/ember/main.js', isAsset: false,
dependencies: [], isJSON: false,
isAsset: false, isPolyfill: false,
isJSON: false, resolution: undefined,
isPolyfill: false, },
resolution: undefined, {
}, id: 'internalVendoredPackage',
{ path: '/root/vendored_modules/a-vendored-package/main.js',
id: 'internalVendoredPackage', dependencies: [],
path: '/root/vendored_modules/a-vendored-package/main.js', isAsset: false,
dependencies: [], isJSON: false,
isAsset: false, isPolyfill: false,
isJSON: false, resolution: undefined,
isPolyfill: false, },
resolution: undefined, {
}, id: 'anotherIndex',
{ path: '/anotherRoot/index.js',
id: 'anotherIndex', dependencies: [],
path: '/anotherRoot/index.js', isAsset: false,
dependencies: [], isJSON: false,
isAsset: false, isPolyfill: false,
isJSON: false, resolution: undefined,
isPolyfill: false, },
resolution: undefined, ]);
},
]);
}));
});
}); });
}); });
}); });
@ -4582,30 +4573,31 @@ describe('DependencyGraph', function() {
return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').then(function() { return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').then(function() {
filesystem.root['index.js'] = filesystem.root['index.js'] =
filesystem.root['index.js'].replace('require("foo")', ''); filesystem.root['index.js'].replace('require("foo")', '');
dgraph.processFileChange('change', root + '/index.js', mockStat); return triggerAndProcessWatchEvent(dgraph, 'change', root + '/index.js')
return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').then(function(deps) { .then(() => getOrderedDependenciesAsJSON(dgraph, '/root/index.js'))
expect(deps) .then(function(deps) {
.toEqual([ expect(deps)
{ .toEqual([
id: 'index', {
path: '/root/index.js', id: 'index',
dependencies: ['aPackage'], path: '/root/index.js',
isAsset: false, dependencies: ['aPackage'],
isJSON: false, isAsset: false,
isPolyfill: false, isJSON: false,
resolution: undefined, isPolyfill: false,
}, resolution: undefined,
{ },
id: 'aPackage/main.js', {
path: '/root/aPackage/main.js', id: 'aPackage/main.js',
dependencies: [], path: '/root/aPackage/main.js',
isAsset: false, dependencies: [],
isJSON: false, isAsset: false,
isPolyfill: false, isJSON: false,
resolution: undefined, isPolyfill: false,
}, resolution: undefined,
]); },
}); ]);
});
}); });
}); });
@ -4643,34 +4635,36 @@ describe('DependencyGraph', function() {
return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').then(function() { return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').then(function() {
filesystem.root['index.js'] = filesystem.root['index.js'] =
filesystem.root['index.js'].replace('require("foo")', ''); filesystem.root['index.js'].replace('require("foo")', '');
dgraph.processFileChange('change', root + '/index.js', mockStat); return triggerAndProcessWatchEvent(dgraph, 'change', root + '/index.js')
return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').then(function(deps) { .then(() => getOrderedDependenciesAsJSON(dgraph, '/root/index.js'))
expect(deps) .then(function(deps) {
.toEqual([ expect(deps)
{ .toEqual([
id: 'index', {
path: '/root/index.js', id: 'index',
dependencies: ['aPackage'], path: '/root/index.js',
isAsset: false, dependencies: ['aPackage'],
isJSON: false, isAsset: false,
isPolyfill: false, isJSON: false,
resolution: undefined, isPolyfill: false,
}, resolution: undefined,
{ },
id: 'aPackage/main.js', {
path: '/root/aPackage/main.js', id: 'aPackage/main.js',
dependencies: [], path: '/root/aPackage/main.js',
isAsset: false, dependencies: [],
isJSON: false, isAsset: false,
isPolyfill: false, isJSON: false,
resolution: undefined, isPolyfill: false,
}, resolution: undefined,
]); },
}); ]);
});
}); });
}); });
it('updates module dependencies on file delete', function() { it('updates module dependencies on file delete', function() {
expect.assertions(1);
var root = '/root'; var root = '/root';
var filesystem = setMockFileSystem({ var filesystem = setMockFileSystem({
'root': { 'root': {
@ -4702,16 +4696,15 @@ describe('DependencyGraph', function() {
roots: [root], roots: [root],
}); });
return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').then(function() { return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').then(function() {
delete filesystem.root.foo; delete filesystem.root['foo.js'];
dgraph.processFileChange('delete', root + '/foo.js'); return triggerAndProcessWatchEvent(dgraph, 'change', root + '/foo.js')
return getOrderedDependenciesAsJSON(dgraph, '/root/index.js') .then(() => getOrderedDependenciesAsJSON(dgraph, '/root/index.js'))
.catch(error => { .catch(error => expect(error.type).toEqual('UnableToResolveError'));
expect(error.type).toEqual('UnableToResolveError');
});
}); });
}); });
it('updates module dependencies on file add', function() { it('updates module dependencies on file add', function() {
expect.assertions(1);
var root = '/root'; var root = '/root';
var filesystem = setMockFileSystem({ var filesystem = setMockFileSystem({
'root': { 'root': {
@ -4749,54 +4742,53 @@ describe('DependencyGraph', function() {
' */', ' */',
'require("foo")', 'require("foo")',
].join('\n'); ].join('\n');
dgraph.processFileChange('add', root + '/bar.js', mockStat); return triggerAndProcessWatchEvent(dgraph, 'change', root + '/bar.js');
}).then(() => {
filesystem.root.aPackage['main.js'] = 'require("bar")'; filesystem.root.aPackage['main.js'] = 'require("bar")';
dgraph.processFileChange('change', root + '/aPackage/main.js', mockStat); return triggerAndProcessWatchEvent(dgraph, 'change', root + '/aPackage/main.js');
}).then(
return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').then(function(deps) { () => getOrderedDependenciesAsJSON(dgraph, '/root/index.js'),
expect(deps) ).then(function(deps) {
.toEqual([ expect(deps).toEqual([
{ {
id: 'index', id: 'index',
path: '/root/index.js', path: '/root/index.js',
dependencies: ['aPackage', 'foo'], dependencies: ['aPackage', 'foo'],
isAsset: false, isAsset: false,
isJSON: false, isJSON: false,
isPolyfill: false, isPolyfill: false,
resolution: undefined, resolution: undefined,
}, },
{ {
id: 'aPackage/main.js', id: 'aPackage/main.js',
path: '/root/aPackage/main.js', path: '/root/aPackage/main.js',
dependencies: ['bar'], dependencies: ['bar'],
isAsset: false, isAsset: false,
isJSON: false, isJSON: false,
isPolyfill: false, isPolyfill: false,
resolution: undefined, resolution: undefined,
}, },
{ {
id: 'bar', id: 'bar',
path: '/root/bar.js', path: '/root/bar.js',
dependencies: ['foo'], dependencies: ['foo'],
isAsset: false, isAsset: false,
isJSON: false, isJSON: false,
isPolyfill: false, isPolyfill: false,
resolution: undefined, resolution: undefined,
resolveDependency: undefined, resolveDependency: undefined,
}, },
{ {
id: 'foo', id: 'foo',
path: '/root/foo.js', path: '/root/foo.js',
dependencies: ['aPackage'], dependencies: ['aPackage'],
isAsset: false, isAsset: false,
isJSON: false, isJSON: false,
isPolyfill: false, isPolyfill: false,
resolution: undefined, resolution: undefined,
resolveDependency: undefined, resolveDependency: undefined,
}, },
]); ]);
});
}); });
}); });
@ -4829,9 +4821,10 @@ describe('DependencyGraph', function() {
).then(() => { ).then(() => {
filesystem.root['foo.png'] = ''; filesystem.root['foo.png'] = '';
dgraph._hasteFS._files[root + '/foo.png'] = ['', 8648460, 1, []]; dgraph._hasteFS._files[root + '/foo.png'] = ['', 8648460, 1, []];
dgraph.processFileChange('add', root + '/foo.png', mockStat); return triggerAndProcessWatchEvent(dgraph, 'change', root + '/foo.png');
}).then(
return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').then(function(deps2) { () => getOrderedDependenciesAsJSON(dgraph, '/root/index.js'),
).then(function(deps2) {
expect(deps2) expect(deps2)
.toEqual([ .toEqual([
{ {
@ -4855,11 +4848,11 @@ describe('DependencyGraph', function() {
resolveDependency: undefined, resolveDependency: undefined,
}, },
]); ]);
});
}); });
}); });
it('changes to browser field', function() { it('changes to browser field', function() {
expect.assertions(1);
var root = '/root'; var root = '/root';
var filesystem = setMockFileSystem({ var filesystem = setMockFileSystem({
'root': { 'root': {
@ -4890,33 +4883,32 @@ describe('DependencyGraph', function() {
main: 'main.js', main: 'main.js',
browser: 'browser.js', browser: 'browser.js',
}); });
dgraph.processFileChange('change', root + '/aPackage/package.json', mockStat); return triggerAndProcessWatchEvent(dgraph, 'change', root + '/aPackage/package.json');
}).then(
return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').then(function(deps) { () => getOrderedDependenciesAsJSON(dgraph, '/root/index.js'),
expect(deps) ).then(function(deps) {
.toEqual([ expect(deps).toEqual([
{ {
id: 'index', id: 'index',
path: '/root/index.js', path: '/root/index.js',
dependencies: ['aPackage'], dependencies: ['aPackage'],
isAsset: false, isAsset: false,
isJSON: false, isJSON: false,
isPolyfill: false, isPolyfill: false,
resolution: undefined, resolution: undefined,
resolveDependency: undefined, resolveDependency: undefined,
}, },
{ {
id: 'aPackage/browser.js', id: 'aPackage/browser.js',
path: '/root/aPackage/browser.js', path: '/root/aPackage/browser.js',
dependencies: [], dependencies: [],
isAsset: false, isAsset: false,
isJSON: false, isJSON: false,
isPolyfill: false, isPolyfill: false,
resolution: undefined, resolution: undefined,
resolveDependency: undefined, resolveDependency: undefined,
}, },
]); ]);
});
}); });
}); });
@ -4956,37 +4948,40 @@ describe('DependencyGraph', function() {
name: 'bPackage', name: 'bPackage',
main: 'main.js', main: 'main.js',
}); });
dgraph.processFileChange('change', root + '/index.js', mockStat); return new Promise(resolve => {
dgraph.processFileChange('change', root + '/aPackage/package.json', mockStat); dgraph.once('change', () => resolve());
triggerWatchEvent('change', root + '/index.js');
return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').then(function(deps) { triggerWatchEvent('change', root + '/aPackage/package.json');
expect(deps)
.toEqual([
{
dependencies: ['bPackage'],
id: 'index',
isAsset: false,
isJSON: false,
isPolyfill: false,
path: '/root/index.js',
resolution: undefined,
resolveDependency: undefined,
},
{
dependencies: [],
id: 'aPackage/main.js',
isAsset: false,
isJSON: false,
isPolyfill: false,
path: '/root/aPackage/main.js',
resolution: undefined,
},
]);
}); });
}).then(
() => getOrderedDependenciesAsJSON(dgraph, '/root/index.js'),
).then(function(deps) {
expect(deps).toEqual([
{
dependencies: ['bPackage'],
id: 'index',
isAsset: false,
isJSON: false,
isPolyfill: false,
path: '/root/index.js',
resolution: undefined,
resolveDependency: undefined,
},
{
dependencies: [],
id: 'aPackage/main.js',
isAsset: false,
isJSON: false,
isPolyfill: false,
path: '/root/aPackage/main.js',
resolution: undefined,
},
]);
}); });
}); });
it('should update node package changes', function() { it('should update node package changes', function() {
expect.assertions(2);
var root = '/root'; var root = '/root';
var filesystem = setMockFileSystem({ var filesystem = setMockFileSystem({
'root': { 'root': {
@ -5057,37 +5052,37 @@ describe('DependencyGraph', function() {
]); ]);
filesystem.root.node_modules.foo['main.js'] = 'lol'; filesystem.root.node_modules.foo['main.js'] = 'lol';
dgraph.processFileChange('change', root + '/node_modules/foo/main.js', mockStat); return triggerAndProcessWatchEvent(dgraph, 'change', root + '/node_modules/foo/main.js');
}).then(
return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').then(function(deps2) { () => getOrderedDependenciesAsJSON(dgraph, '/root/index.js'),
expect(deps2) ).then(function(deps2) {
.toEqual([ expect(deps2).toEqual([
{ {
id: 'index', id: 'index',
path: '/root/index.js', path: '/root/index.js',
dependencies: ['foo'], dependencies: ['foo'],
isAsset: false, isAsset: false,
isJSON: false, isJSON: false,
isPolyfill: false, isPolyfill: false,
resolution: undefined, resolution: undefined,
resolveDependency: undefined, resolveDependency: undefined,
}, },
{ {
id: 'foo/main.js', id: 'foo/main.js',
path: '/root/node_modules/foo/main.js', path: '/root/node_modules/foo/main.js',
dependencies: [], dependencies: [],
isAsset: false, isAsset: false,
isJSON: false, isJSON: false,
isPolyfill: false, isPolyfill: false,
resolution: undefined, resolution: undefined,
resolveDependency: undefined, resolveDependency: undefined,
}, },
]); ]);
});
}); });
}); });
it('should update node package main changes', function() { it('should update node package main changes', function() {
expect.assertions(1);
var root = '/root'; var root = '/root';
var filesystem = setMockFileSystem({ var filesystem = setMockFileSystem({
'root': { 'root': {
@ -5120,37 +5115,41 @@ describe('DependencyGraph', function() {
main: 'main.js', main: 'main.js',
browser: 'browser.js', browser: 'browser.js',
}); });
dgraph.processFileChange('change', root + '/node_modules/foo/package.json', mockStat); return triggerAndProcessWatchEvent(
dgraph,
return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').then(function(deps2) { 'change',
expect(deps2) root + '/node_modules/foo/package.json',
.toEqual([ );
{ }).then(
id: 'index', () => getOrderedDependenciesAsJSON(dgraph, '/root/index.js'),
path: '/root/index.js', ).then(function(deps2) {
dependencies: ['foo'], expect(deps2).toEqual([
isAsset: false, {
isJSON: false, id: 'index',
isPolyfill: false, path: '/root/index.js',
resolution: undefined, dependencies: ['foo'],
resolveDependency: undefined, isAsset: false,
}, isJSON: false,
{ isPolyfill: false,
id: 'foo/browser.js', resolution: undefined,
path: '/root/node_modules/foo/browser.js', resolveDependency: undefined,
dependencies: [], },
isAsset: false, {
isJSON: false, id: 'foo/browser.js',
isPolyfill: false, path: '/root/node_modules/foo/browser.js',
resolution: undefined, dependencies: [],
resolveDependency: undefined, isAsset: false,
}, isJSON: false,
]); isPolyfill: false,
}); resolution: undefined,
resolveDependency: undefined,
},
]);
}); });
}); });
it('should not error when the watcher reports a known file as added', function() { it('should not error when the watcher reports a known file as added', function() {
expect.assertions(1);
var root = '/root'; var root = '/root';
setMockFileSystem({ setMockFileSystem({
'root': { 'root': {
@ -5174,9 +5173,12 @@ describe('DependencyGraph', function() {
roots: [root], roots: [root],
}); });
return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').then(function() { return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').then(
dgraph.processFileChange('add', root + '/index.js', mockStat); () => triggerAndProcessWatchEvent(dgraph, 'change', root + '/index.js'),
return getOrderedDependenciesAsJSON(dgraph, '/root/index.js'); ).then(
() => getOrderedDependenciesAsJSON(dgraph, '/root/index.js'),
).then(deps => {
expect(deps).toBeDefined();
}); });
}); });
}); });
@ -5460,6 +5462,13 @@ describe('DependencyGraph', function() {
return require('graceful-fs').__setMockFilesystem(object); return require('graceful-fs').__setMockFilesystem(object);
} }
function triggerAndProcessWatchEvent(dgraph, eventType, filename) {
return new Promise(resolve => {
dgraph.once('change', () => resolve());
triggerWatchEvent(eventType, filename);
});
}
function triggerWatchEvent(eventType, filename) { function triggerWatchEvent(eventType, filename) {
return require('graceful-fs').__triggerWatchEvent(eventType, filename); return require('graceful-fs').__triggerWatchEvent(eventType, filename);
} }