Files
flipper/desktop/pkg-lib/src/__tests__/getWatchFolders.node.ts
Anton Nikolaev f436f192eb Fix getWatchFolder script to resolve workspaces transitive dependencies (#1289)
Summary:
Pull Request resolved: https://github.com/facebook/flipper/pull/1289

getWatchFolder script updated to resolve symlinked transitive dependencies. Before this change the script only resolved 1st direct symlinked dependencies.

Reviewed By: mweststrate

Differential Revision: D22161469

fbshipit-source-id: c30802a413259021aaca99c08743dc762ed877eb
2020-06-23 03:55:41 -07:00

112 lines
3.4 KiB
TypeScript

/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
*/
import mockfs from 'mock-fs';
import path from 'path';
import {consoleMock, normalizePath} from 'flipper-test-utils';
import getWatchFolders from '../getWatchFolders';
import fs from 'fs-extra';
describe('getWatchFolders', () => {
const realConsole = global.console;
global.console = consoleMock as any;
afterAll(() => {
global.console = realConsole;
});
beforeEach(() => {});
afterEach(() => {
mockfs.restore();
});
test('getWatchFolders correctly resolves symlinked packages', async () => {
const rootDir =
process.platform === 'win32' ? 'C:\\test\\root' : '/test/root';
const files = {
[rootDir]: {
node_modules: {
installed_module_1: {},
local_module_1: mockfs.symlink({path: '../local_module_1'}),
local_module_2: mockfs.symlink({path: '../local_module_2'}),
plugin_module_1: mockfs.symlink({path: '../plugins/plugin_module_1'}),
plugin_module_2: mockfs.symlink({path: '../plugins/plugin_module_2'}),
fb_plugin_module_1: mockfs.symlink({
path: '../plugins/fb/fb_plugin_module_1',
}),
fb_plugin_module_2: mockfs.symlink({
path: '../plugins/fb/fb_plugin_module_2',
}),
},
local_module_1: {
'package.json': '{"dependencies": {"installed_module_1": "1.0.0"}}',
},
local_module_2: {
'package.json':
'{"dependencies": {"fb_plugin_module_1": "1.0.0", "plugin_module_1": "1.0.0"}}',
},
plugins: {
plugin_module_1: {
'package.json': '{"dependencies": {"local_module_2": "1.0.0"}}',
},
plugin_module_2: {
'package.json': '{"dependencies": {"fb_plugin_module_1": "1.0.0"}}',
},
fb: {
node_modules: {
installed_module_2: {},
},
fb_plugin_module_1: {
'package.json': '{"dependencies": {"plugin_module_2": "1.0.0"}}',
},
fb_plugin_module_2: {
'package.json': '{"dependencies": {}}',
},
},
},
},
};
mockfs(files);
const readJsonMock = async (file: string) => {
if (!file.startsWith(rootDir)) {
throw new Error('File not found: ' + file);
}
const parts = file.substring(rootDir.length + 1).split(path.sep);
let cur = <any>files[rootDir];
for (const part of parts) {
cur = cur[part];
if (!cur) {
throw new Error(`File part "${part}" not found: ${file}`);
}
}
return JSON.parse(cur);
};
const readReadJson = fs.readJson;
try {
fs.readJson = <any>readJsonMock;
const resolvedFolders = await getWatchFolders(
path.join(rootDir, 'local_module_2'),
);
expect(resolvedFolders.map(normalizePath)).toMatchInlineSnapshot(`
Array [
"/test/root/local_module_2",
"/test/root/node_modules",
"/test/root/plugins/fb/fb_plugin_module_1",
"/test/root/plugins/fb/node_modules",
"/test/root/plugins/plugin_module_1",
"/test/root/plugins/plugin_module_2",
]
`);
} finally {
fs.readJson = readReadJson;
}
});
});