mirror of https://github.com/status-im/metro.git
Improve readability of the traverse dependencies tests
Reviewed By: davidaurelio Differential Revision: D7055471 fbshipit-source-id: dfac739159de0591d453511d4f4d7882beacb9bf
This commit is contained in:
parent
ff6f6ef1cf
commit
41e7aa6f4a
|
@ -15,14 +15,81 @@ const {
|
||||||
traverseDependencies,
|
traverseDependencies,
|
||||||
} = require('../traverseDependencies');
|
} = require('../traverseDependencies');
|
||||||
|
|
||||||
const entryModule = createModule({path: '/bundle', name: 'bundle'});
|
|
||||||
const moduleFoo = createModule({path: '/foo', name: 'foo'});
|
|
||||||
const moduleBar = createModule({path: '/bar', name: 'bar'});
|
|
||||||
const moduleBaz = createModule({path: '/baz', name: 'baz'});
|
|
||||||
|
|
||||||
let dependencyGraph;
|
let dependencyGraph;
|
||||||
let mockedDependencies;
|
let mockedDependencies;
|
||||||
let mockedDependencyTree;
|
let mockedDependencyTree;
|
||||||
|
let files = new Set();
|
||||||
|
|
||||||
|
let entryModule;
|
||||||
|
let moduleFoo;
|
||||||
|
let moduleBar;
|
||||||
|
let moduleBaz;
|
||||||
|
|
||||||
|
const Actions = {
|
||||||
|
modifyFile(path) {
|
||||||
|
if (mockedDependencyTree.get(path)) {
|
||||||
|
files.add(path);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
moveFile(from, to) {
|
||||||
|
const module = Actions.createFile(to);
|
||||||
|
Actions.deleteFile(from);
|
||||||
|
|
||||||
|
return module;
|
||||||
|
},
|
||||||
|
|
||||||
|
deleteFile(path) {
|
||||||
|
const dependency = dependencyGraph.getModuleForPath(path);
|
||||||
|
|
||||||
|
if (dependency) {
|
||||||
|
mockedDependencies.delete(dependency);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
createFile(path) {
|
||||||
|
const module = createModule({
|
||||||
|
path,
|
||||||
|
name: path.replace('/', ''),
|
||||||
|
});
|
||||||
|
mockedDependencies.add(module);
|
||||||
|
mockedDependencyTree.set(path, []);
|
||||||
|
|
||||||
|
return module;
|
||||||
|
},
|
||||||
|
|
||||||
|
addDependency(path, dependencyPath, position) {
|
||||||
|
let dependency = dependencyGraph.getModuleForPath(dependencyPath);
|
||||||
|
if (!dependency) {
|
||||||
|
dependency = Actions.createFile(dependencyPath);
|
||||||
|
}
|
||||||
|
|
||||||
|
const deps = mockedDependencyTree.get(path);
|
||||||
|
|
||||||
|
if (position == null) {
|
||||||
|
deps.push(dependency);
|
||||||
|
} else {
|
||||||
|
deps.splice(position, 0, dependency);
|
||||||
|
}
|
||||||
|
|
||||||
|
mockedDependencyTree.set(path, deps);
|
||||||
|
mockedDependencies.add(dependency);
|
||||||
|
|
||||||
|
files.add(path);
|
||||||
|
},
|
||||||
|
|
||||||
|
removeDependency(path, dependencyPath) {
|
||||||
|
const dependency = dependencyGraph.getModuleForPath(dependencyPath);
|
||||||
|
const deps = mockedDependencyTree.get(path);
|
||||||
|
|
||||||
|
if (deps.indexOf(dependency) !== -1) {
|
||||||
|
deps.splice(deps.indexOf(dependency), 1);
|
||||||
|
mockedDependencyTree.set(path, deps);
|
||||||
|
}
|
||||||
|
|
||||||
|
files.add(path);
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
function deferred(value) {
|
function deferred(value) {
|
||||||
let resolve;
|
let resolve;
|
||||||
|
@ -31,15 +98,15 @@ function deferred(value) {
|
||||||
return {promise, resolve: () => resolve(value)};
|
return {promise, resolve: () => resolve(value)};
|
||||||
}
|
}
|
||||||
|
|
||||||
function createModule({path, name, isAsset, isPolyfill}) {
|
function createModule({path, name}) {
|
||||||
return {
|
return {
|
||||||
path,
|
path,
|
||||||
name,
|
name,
|
||||||
isAsset() {
|
isAsset() {
|
||||||
return !!isAsset;
|
return false;
|
||||||
},
|
},
|
||||||
isPolyfill() {
|
isPolyfill() {
|
||||||
return !!isPolyfill;
|
return false;
|
||||||
},
|
},
|
||||||
async read() {
|
async read() {
|
||||||
const deps = mockedDependencyTree.get(path);
|
const deps = mockedDependencyTree.get(path);
|
||||||
|
@ -65,11 +132,8 @@ function getPaths({added, deleted}) {
|
||||||
}
|
}
|
||||||
|
|
||||||
beforeEach(async () => {
|
beforeEach(async () => {
|
||||||
mockedDependencies = new Set([entryModule, moduleFoo, moduleBar, moduleBaz]);
|
mockedDependencies = new Set();
|
||||||
mockedDependencyTree = new Map([
|
mockedDependencyTree = new Map();
|
||||||
[entryModule.path, [moduleFoo]],
|
|
||||||
[moduleFoo.path, [moduleBar, moduleBaz]],
|
|
||||||
]);
|
|
||||||
|
|
||||||
dependencyGraph = {
|
dependencyGraph = {
|
||||||
getAbsolutePath(path) {
|
getAbsolutePath(path) {
|
||||||
|
@ -90,6 +154,18 @@ beforeEach(async () => {
|
||||||
return dependency;
|
return dependency;
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Generate the initial dependency graph.
|
||||||
|
entryModule = Actions.createFile('/bundle');
|
||||||
|
moduleFoo = Actions.createFile('/foo');
|
||||||
|
moduleBar = Actions.createFile('/bar');
|
||||||
|
moduleBaz = Actions.createFile('/baz');
|
||||||
|
|
||||||
|
Actions.addDependency('/bundle', '/foo');
|
||||||
|
Actions.addDependency('/foo', '/bar');
|
||||||
|
Actions.addDependency('/foo', '/baz');
|
||||||
|
|
||||||
|
files = new Set();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should do the initial traversal correctly', async () => {
|
it('should do the initial traversal correctly', async () => {
|
||||||
|
@ -127,11 +203,12 @@ it('should return a removed dependency', async () => {
|
||||||
const edges = new Map();
|
const edges = new Map();
|
||||||
await initialTraverseDependencies('/bundle', dependencyGraph, {}, edges);
|
await initialTraverseDependencies('/bundle', dependencyGraph, {}, edges);
|
||||||
|
|
||||||
// Remove moduleBar
|
Actions.removeDependency('/foo', '/bar');
|
||||||
mockedDependencyTree.set(moduleFoo.path, [moduleBaz]);
|
|
||||||
|
|
||||||
expect(
|
expect(
|
||||||
getPaths(await traverseDependencies(['/foo'], dependencyGraph, {}, edges)),
|
getPaths(
|
||||||
|
await traverseDependencies([...files], dependencyGraph, {}, edges),
|
||||||
|
),
|
||||||
).toEqual({
|
).toEqual({
|
||||||
added: new Set(['/foo']),
|
added: new Set(['/foo']),
|
||||||
deleted: new Set(['/bar']),
|
deleted: new Set(['/bar']),
|
||||||
|
@ -142,13 +219,14 @@ it('should return added/removed dependencies', async () => {
|
||||||
const edges = new Map();
|
const edges = new Map();
|
||||||
await initialTraverseDependencies('/bundle', dependencyGraph, {}, edges);
|
await initialTraverseDependencies('/bundle', dependencyGraph, {}, edges);
|
||||||
|
|
||||||
// Add moduleQux
|
Actions.addDependency('/foo', '/qux');
|
||||||
const moduleQux = createModule({path: '/qux', name: 'qux'});
|
Actions.removeDependency('/foo', '/bar');
|
||||||
mockedDependencyTree.set(moduleFoo.path, [moduleQux]);
|
Actions.removeDependency('/foo', '/baz');
|
||||||
mockedDependencies.add(moduleQux);
|
|
||||||
|
|
||||||
expect(
|
expect(
|
||||||
getPaths(await traverseDependencies(['/foo'], dependencyGraph, {}, edges)),
|
getPaths(
|
||||||
|
await traverseDependencies([...files], dependencyGraph, {}, edges),
|
||||||
|
),
|
||||||
).toEqual({
|
).toEqual({
|
||||||
added: new Set(['/foo', '/qux']),
|
added: new Set(['/foo', '/qux']),
|
||||||
deleted: new Set(['/bar', '/baz']),
|
deleted: new Set(['/bar', '/baz']),
|
||||||
|
@ -159,7 +237,7 @@ it('should retry to traverse the dependencies as it was after getting an error',
|
||||||
const edges = new Map();
|
const edges = new Map();
|
||||||
await initialTraverseDependencies('/bundle', dependencyGraph, {}, edges);
|
await initialTraverseDependencies('/bundle', dependencyGraph, {}, edges);
|
||||||
|
|
||||||
mockedDependencies.delete(moduleBar);
|
Actions.deleteFile(moduleBar.path);
|
||||||
|
|
||||||
await expect(
|
await expect(
|
||||||
traverseDependencies(['/foo'], dependencyGraph, {}, edges),
|
traverseDependencies(['/foo'], dependencyGraph, {}, edges),
|
||||||
|
@ -177,21 +255,13 @@ describe('edge cases', () => {
|
||||||
const edges = new Map();
|
const edges = new Map();
|
||||||
await initialTraverseDependencies('/bundle', dependencyGraph, {}, edges);
|
await initialTraverseDependencies('/bundle', dependencyGraph, {}, edges);
|
||||||
|
|
||||||
// Change the dependencies of /path, removing /baz and adding /qux.
|
Actions.removeDependency('/foo', '/baz');
|
||||||
const moduleQux = createModule({path: '/qux', name: 'qux'});
|
Actions.moveFile('/baz', '/qux');
|
||||||
mockedDependencyTree.set(moduleFoo.path, [moduleQux, moduleBar]);
|
Actions.addDependency('/foo', '/qux');
|
||||||
mockedDependencies.add(moduleQux);
|
|
||||||
|
|
||||||
// Call traverseDependencies with /foo, /qux and /baz, simulating that the
|
|
||||||
// user has modified the 3 files.
|
|
||||||
expect(
|
expect(
|
||||||
getPaths(
|
getPaths(
|
||||||
await traverseDependencies(
|
await traverseDependencies([...files], dependencyGraph, {}, edges),
|
||||||
['/foo', '/qux', '/baz'],
|
|
||||||
dependencyGraph,
|
|
||||||
{},
|
|
||||||
edges,
|
|
||||||
),
|
|
||||||
),
|
),
|
||||||
).toEqual({
|
).toEqual({
|
||||||
added: new Set(['/foo', '/qux']),
|
added: new Set(['/foo', '/qux']),
|
||||||
|
@ -204,20 +274,16 @@ describe('edge cases', () => {
|
||||||
await initialTraverseDependencies('/bundle', dependencyGraph, {}, edges);
|
await initialTraverseDependencies('/bundle', dependencyGraph, {}, edges);
|
||||||
|
|
||||||
// Rename /foo to /foo-renamed, but keeping all its dependencies.
|
// Rename /foo to /foo-renamed, but keeping all its dependencies.
|
||||||
const moduleFooRenamed = createModule({
|
Actions.addDependency('/bundle', '/foo-renamed');
|
||||||
path: '/foo-renamed',
|
Actions.removeDependency('/bundle', '/foo');
|
||||||
name: 'foo-renamed',
|
|
||||||
});
|
Actions.moveFile('/foo', '/foo-renamed');
|
||||||
mockedDependencyTree.set(entryModule.path, [moduleFooRenamed]);
|
Actions.addDependency('/foo-renamed', '/bar');
|
||||||
mockedDependencyTree.set(moduleFooRenamed.path, [moduleBar, moduleBaz]);
|
Actions.addDependency('/foo-renamed', '/baz');
|
||||||
mockedDependencies.add(moduleFooRenamed);
|
|
||||||
mockedDependencies.delete(moduleFoo);
|
|
||||||
|
|
||||||
// Call traverseDependencies with /foo, /qux and /baz, simulating that the
|
|
||||||
// user has modified the 3 files.
|
|
||||||
expect(
|
expect(
|
||||||
getPaths(
|
getPaths(
|
||||||
await traverseDependencies(['/bundle'], dependencyGraph, {}, edges),
|
await traverseDependencies([...files], dependencyGraph, {}, edges),
|
||||||
),
|
),
|
||||||
).toEqual({
|
).toEqual({
|
||||||
added: new Set(['/bundle', '/foo-renamed']),
|
added: new Set(['/bundle', '/foo-renamed']),
|
||||||
|
@ -231,15 +297,12 @@ describe('edge cases', () => {
|
||||||
|
|
||||||
mockedDependencyTree.set(moduleFoo.path, [moduleBar]);
|
mockedDependencyTree.set(moduleFoo.path, [moduleBar]);
|
||||||
|
|
||||||
// Modify /foo and /baz, and then remove the dependency from /foo to /baz
|
Actions.modifyFile('/baz');
|
||||||
|
Actions.removeDependency('/foo', '/baz');
|
||||||
|
|
||||||
expect(
|
expect(
|
||||||
getPaths(
|
getPaths(
|
||||||
await traverseDependencies(
|
await traverseDependencies([...files], dependencyGraph, {}, edges),
|
||||||
['/baz', '/foo'],
|
|
||||||
dependencyGraph,
|
|
||||||
{},
|
|
||||||
edges,
|
|
||||||
),
|
|
||||||
),
|
),
|
||||||
).toEqual({
|
).toEqual({
|
||||||
added: new Set(['/foo']),
|
added: new Set(['/foo']),
|
||||||
|
@ -251,12 +314,9 @@ describe('edge cases', () => {
|
||||||
const edges = new Map();
|
const edges = new Map();
|
||||||
await initialTraverseDependencies('/bundle', dependencyGraph, {}, edges);
|
await initialTraverseDependencies('/bundle', dependencyGraph, {}, edges);
|
||||||
|
|
||||||
const moduleBazMoved = createModule({path: '/baz-moved', name: 'baz'});
|
Actions.addDependency('/foo', '/baz-moved');
|
||||||
mockedDependencyTree.set(moduleFoo.path, [moduleBar, moduleBazMoved]);
|
Actions.removeDependency('/foo', '/baz');
|
||||||
mockedDependencies.add(moduleBazMoved);
|
|
||||||
mockedDependencies.delete(moduleBaz);
|
|
||||||
|
|
||||||
// Modify /baz, rename it to /qux and modify it again.
|
|
||||||
expect(
|
expect(
|
||||||
getPaths(
|
getPaths(
|
||||||
await traverseDependencies(['/foo'], dependencyGraph, {}, edges),
|
await traverseDependencies(['/foo'], dependencyGraph, {}, edges),
|
||||||
|
@ -271,9 +331,7 @@ describe('edge cases', () => {
|
||||||
const edges = new Map();
|
const edges = new Map();
|
||||||
await initialTraverseDependencies('/bundle', dependencyGraph, {}, edges);
|
await initialTraverseDependencies('/bundle', dependencyGraph, {}, edges);
|
||||||
|
|
||||||
const moduleQux = createModule({path: '/qux', name: 'qux'});
|
Actions.addDependency('/foo', '/qux', 0);
|
||||||
mockedDependencyTree.set(moduleFoo.path, [moduleQux, moduleBar, moduleBaz]);
|
|
||||||
mockedDependencies.add(moduleQux);
|
|
||||||
|
|
||||||
expect(
|
expect(
|
||||||
getPaths(
|
getPaths(
|
||||||
|
@ -285,9 +343,9 @@ describe('edge cases', () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
expect([...edges.get(moduleFoo.path).dependencies]).toEqual([
|
expect([...edges.get(moduleFoo.path).dependencies]).toEqual([
|
||||||
['qux', moduleQux.path],
|
['qux', '/qux'],
|
||||||
['bar', moduleBar.path],
|
['bar', '/bar'],
|
||||||
['baz', moduleBaz.path],
|
['baz', '/baz'],
|
||||||
]);
|
]);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue