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 0b348095b6
commit 5d37bfa605
2 changed files with 303 additions and 289 deletions

View File

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

View File

@ -27,13 +27,8 @@ jasmine.DEFAULT_TIMEOUT_INTERVAL = 10000;
const path = require('path');
const mockStat = {
isDirectory: () => false,
};
beforeEach(() => {
jest.resetModules();
jest.mock('path', () => path);
});
@ -3111,10 +3106,8 @@ describe('DependencyGraph', function() {
filesystem.root['index.js'] = filesystem.root['index.js']
.replace('require("dontWork")', '')
.replace('require("wontWork")', '');
triggerWatchEvent('change', root + '/index.js');
return new Promise(resolve => {
dgraph.once('change', () => {
return resolve(getOrderedDependenciesAsJSON(dgraph, '/root/index.js')
return triggerAndProcessWatchEvent(dgraph, 'change', root + '/index.js')
.then(() => getOrderedDependenciesAsJSON(dgraph, '/root/index.js'))
.then(deps => {
expect(deps).toEqual([
{
@ -3177,8 +3170,6 @@ describe('DependencyGraph', function() {
resolution: undefined,
},
]);
}));
});
});
});
});
@ -4582,8 +4573,9 @@ describe('DependencyGraph', function() {
return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').then(function() {
filesystem.root['index.js'] =
filesystem.root['index.js'].replace('require("foo")', '');
dgraph.processFileChange('change', root + '/index.js', mockStat);
return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').then(function(deps) {
return triggerAndProcessWatchEvent(dgraph, 'change', root + '/index.js')
.then(() => getOrderedDependenciesAsJSON(dgraph, '/root/index.js'))
.then(function(deps) {
expect(deps)
.toEqual([
{
@ -4643,8 +4635,9 @@ describe('DependencyGraph', function() {
return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').then(function() {
filesystem.root['index.js'] =
filesystem.root['index.js'].replace('require("foo")', '');
dgraph.processFileChange('change', root + '/index.js', mockStat);
return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').then(function(deps) {
return triggerAndProcessWatchEvent(dgraph, 'change', root + '/index.js')
.then(() => getOrderedDependenciesAsJSON(dgraph, '/root/index.js'))
.then(function(deps) {
expect(deps)
.toEqual([
{
@ -4671,6 +4664,7 @@ describe('DependencyGraph', function() {
});
it('updates module dependencies on file delete', function() {
expect.assertions(1);
var root = '/root';
var filesystem = setMockFileSystem({
'root': {
@ -4702,16 +4696,15 @@ describe('DependencyGraph', function() {
roots: [root],
});
return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').then(function() {
delete filesystem.root.foo;
dgraph.processFileChange('delete', root + '/foo.js');
return getOrderedDependenciesAsJSON(dgraph, '/root/index.js')
.catch(error => {
expect(error.type).toEqual('UnableToResolveError');
});
delete filesystem.root['foo.js'];
return triggerAndProcessWatchEvent(dgraph, 'change', root + '/foo.js')
.then(() => getOrderedDependenciesAsJSON(dgraph, '/root/index.js'))
.catch(error => expect(error.type).toEqual('UnableToResolveError'));
});
});
it('updates module dependencies on file add', function() {
expect.assertions(1);
var root = '/root';
var filesystem = setMockFileSystem({
'root': {
@ -4749,14 +4742,14 @@ describe('DependencyGraph', function() {
' */',
'require("foo")',
].join('\n');
dgraph.processFileChange('add', root + '/bar.js', mockStat);
return triggerAndProcessWatchEvent(dgraph, 'change', root + '/bar.js');
}).then(() => {
filesystem.root.aPackage['main.js'] = 'require("bar")';
dgraph.processFileChange('change', root + '/aPackage/main.js', mockStat);
return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').then(function(deps) {
expect(deps)
.toEqual([
return triggerAndProcessWatchEvent(dgraph, 'change', root + '/aPackage/main.js');
}).then(
() => getOrderedDependenciesAsJSON(dgraph, '/root/index.js'),
).then(function(deps) {
expect(deps).toEqual([
{
id: 'index',
path: '/root/index.js',
@ -4798,7 +4791,6 @@ describe('DependencyGraph', function() {
]);
});
});
});
it('updates module dependencies on relative asset add', function() {
var root = '/root';
@ -4829,9 +4821,10 @@ describe('DependencyGraph', function() {
).then(() => {
filesystem.root['foo.png'] = '';
dgraph._hasteFS._files[root + '/foo.png'] = ['', 8648460, 1, []];
dgraph.processFileChange('add', root + '/foo.png', mockStat);
return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').then(function(deps2) {
return triggerAndProcessWatchEvent(dgraph, 'change', root + '/foo.png');
}).then(
() => getOrderedDependenciesAsJSON(dgraph, '/root/index.js'),
).then(function(deps2) {
expect(deps2)
.toEqual([
{
@ -4857,9 +4850,9 @@ describe('DependencyGraph', function() {
]);
});
});
});
it('changes to browser field', function() {
expect.assertions(1);
var root = '/root';
var filesystem = setMockFileSystem({
'root': {
@ -4890,11 +4883,11 @@ describe('DependencyGraph', function() {
main: 'main.js',
browser: 'browser.js',
});
dgraph.processFileChange('change', root + '/aPackage/package.json', mockStat);
return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').then(function(deps) {
expect(deps)
.toEqual([
return triggerAndProcessWatchEvent(dgraph, 'change', root + '/aPackage/package.json');
}).then(
() => getOrderedDependenciesAsJSON(dgraph, '/root/index.js'),
).then(function(deps) {
expect(deps).toEqual([
{
id: 'index',
path: '/root/index.js',
@ -4918,7 +4911,6 @@ describe('DependencyGraph', function() {
]);
});
});
});
it('removes old package from cache', function() {
var root = '/root';
@ -4956,12 +4948,15 @@ describe('DependencyGraph', function() {
name: 'bPackage',
main: 'main.js',
});
dgraph.processFileChange('change', root + '/index.js', mockStat);
dgraph.processFileChange('change', root + '/aPackage/package.json', mockStat);
return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').then(function(deps) {
expect(deps)
.toEqual([
return new Promise(resolve => {
dgraph.once('change', () => resolve());
triggerWatchEvent('change', root + '/index.js');
triggerWatchEvent('change', root + '/aPackage/package.json');
});
}).then(
() => getOrderedDependenciesAsJSON(dgraph, '/root/index.js'),
).then(function(deps) {
expect(deps).toEqual([
{
dependencies: ['bPackage'],
id: 'index',
@ -4984,9 +4979,9 @@ describe('DependencyGraph', function() {
]);
});
});
});
it('should update node package changes', function() {
expect.assertions(2);
var root = '/root';
var filesystem = setMockFileSystem({
'root': {
@ -5057,11 +5052,11 @@ describe('DependencyGraph', function() {
]);
filesystem.root.node_modules.foo['main.js'] = 'lol';
dgraph.processFileChange('change', root + '/node_modules/foo/main.js', mockStat);
return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').then(function(deps2) {
expect(deps2)
.toEqual([
return triggerAndProcessWatchEvent(dgraph, 'change', root + '/node_modules/foo/main.js');
}).then(
() => getOrderedDependenciesAsJSON(dgraph, '/root/index.js'),
).then(function(deps2) {
expect(deps2).toEqual([
{
id: 'index',
path: '/root/index.js',
@ -5085,9 +5080,9 @@ describe('DependencyGraph', function() {
]);
});
});
});
it('should update node package main changes', function() {
expect.assertions(1);
var root = '/root';
var filesystem = setMockFileSystem({
'root': {
@ -5120,11 +5115,15 @@ describe('DependencyGraph', function() {
main: 'main.js',
browser: 'browser.js',
});
dgraph.processFileChange('change', root + '/node_modules/foo/package.json', mockStat);
return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').then(function(deps2) {
expect(deps2)
.toEqual([
return triggerAndProcessWatchEvent(
dgraph,
'change',
root + '/node_modules/foo/package.json',
);
}).then(
() => getOrderedDependenciesAsJSON(dgraph, '/root/index.js'),
).then(function(deps2) {
expect(deps2).toEqual([
{
id: 'index',
path: '/root/index.js',
@ -5148,9 +5147,9 @@ describe('DependencyGraph', function() {
]);
});
});
});
it('should not error when the watcher reports a known file as added', function() {
expect.assertions(1);
var root = '/root';
setMockFileSystem({
'root': {
@ -5174,9 +5173,12 @@ describe('DependencyGraph', function() {
roots: [root],
});
return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').then(function() {
dgraph.processFileChange('add', root + '/index.js', mockStat);
return getOrderedDependenciesAsJSON(dgraph, '/root/index.js');
return getOrderedDependenciesAsJSON(dgraph, '/root/index.js').then(
() => triggerAndProcessWatchEvent(dgraph, 'change', 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);
}
function triggerAndProcessWatchEvent(dgraph, eventType, filename) {
return new Promise(resolve => {
dgraph.once('change', () => resolve());
triggerWatchEvent(eventType, filename);
});
}
function triggerWatchEvent(eventType, filename) {
return require('graceful-fs').__triggerWatchEvent(eventType, filename);
}