mirror of
https://github.com/status-im/react-native.git
synced 2025-01-13 02:54:42 +00:00
e327f88f02
Summary: @public These tests are using a mock memory FS to start with, so there is no reason at all they should depend on the host OS or filesystem details. This changeset fixes that so that we fully mock the `fs` and `path` modules dependending on the mock platform (not the host platform). I also added an example of how we can test both platforms (regardless of the host platform) in `findPackageClassName`. Follow up changeset will be to do the same for all the other affected tests. Related to https://github.com/facebook/react-native/issues/20260. Reviewed By: mjesun Differential Revision: D9771024 fbshipit-source-id: b368b43e8e54292d33b6183eec9a9ea69f2e6e76
354 lines
8.8 KiB
JavaScript
354 lines
8.8 KiB
JavaScript
/**
|
|
* 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
|
|
* @emails oncall+javascript_foundation
|
|
*/
|
|
|
|
jest.mock('path');
|
|
jest.mock('fs');
|
|
|
|
const fs = require('fs');
|
|
const findSymlinkedModules = require('../findSymlinkedModules');
|
|
|
|
describe('findSymlinksForProjectRoot', () => {
|
|
it('correctly finds normal module symlinks', () => {
|
|
fs.__setMockFilesystem({
|
|
root: {
|
|
projectA: {
|
|
'package.json': JSON.stringify({
|
|
name: 'projectA',
|
|
main: 'main.js',
|
|
}),
|
|
node_modules: {
|
|
depFoo: {
|
|
'package.json': JSON.stringify({
|
|
name: 'depFoo',
|
|
main: 'main.js',
|
|
}),
|
|
},
|
|
projectB: {
|
|
SYMLINK: '/root/projectB',
|
|
},
|
|
},
|
|
},
|
|
projectB: {
|
|
'package.json': JSON.stringify({
|
|
name: 'projectB',
|
|
main: 'main.js',
|
|
}),
|
|
node_modules: {
|
|
depBar: {
|
|
'package.json': JSON.stringify({
|
|
name: 'depBar',
|
|
main: 'main.js',
|
|
}),
|
|
},
|
|
},
|
|
},
|
|
},
|
|
});
|
|
|
|
const symlinkedModules = findSymlinkedModules('/root/projectA', []);
|
|
expect(symlinkedModules).toEqual(['/root/projectB']);
|
|
});
|
|
|
|
it('correctly finds scoped module symlinks', () => {
|
|
fs.__setMockFilesystem({
|
|
root: {
|
|
projectA: {
|
|
'package.json': JSON.stringify({
|
|
name: 'projectA',
|
|
main: 'main.js',
|
|
}),
|
|
node_modules: {
|
|
depFoo: {
|
|
'package.json': JSON.stringify({
|
|
name: 'depFoo',
|
|
main: 'main.js',
|
|
}),
|
|
},
|
|
'@scoped': {
|
|
projectC: {
|
|
SYMLINK: '/root/@scoped/projectC',
|
|
},
|
|
},
|
|
projectB: {
|
|
SYMLINK: '/root/projectB',
|
|
},
|
|
},
|
|
},
|
|
projectB: {
|
|
'package.json': JSON.stringify({
|
|
name: 'projectB',
|
|
main: 'main.js',
|
|
}),
|
|
node_modules: {
|
|
depBar: {
|
|
'package.json': JSON.stringify({
|
|
name: 'depBar',
|
|
main: 'main.js',
|
|
}),
|
|
},
|
|
},
|
|
},
|
|
'@scoped': {
|
|
projectC: {
|
|
'package.json': JSON.stringify({
|
|
name: '@scoped/projectC',
|
|
main: 'main.js',
|
|
}),
|
|
},
|
|
},
|
|
},
|
|
});
|
|
|
|
const symlinkedModules = findSymlinkedModules('/root/projectA', []);
|
|
expect(symlinkedModules).toEqual([
|
|
'/root/@scoped/projectC',
|
|
'/root/projectB',
|
|
]);
|
|
});
|
|
|
|
it('correctly finds module symlinks within other module symlinks', () => {
|
|
fs.__setMockFilesystem({
|
|
root: {
|
|
projectA: {
|
|
'package.json': JSON.stringify({
|
|
name: 'projectA',
|
|
main: 'main.js',
|
|
}),
|
|
node_modules: {
|
|
depFoo: {
|
|
'package.json': JSON.stringify({
|
|
name: 'depFoo',
|
|
main: 'main.js',
|
|
}),
|
|
},
|
|
'@scoped': {
|
|
projectC: {
|
|
SYMLINK: '/root/@scoped/projectC',
|
|
},
|
|
},
|
|
projectB: {
|
|
SYMLINK: '/root/projectB',
|
|
},
|
|
},
|
|
},
|
|
projectB: {
|
|
'package.json': JSON.stringify({
|
|
name: 'projectB',
|
|
main: 'main.js',
|
|
}),
|
|
node_modules: {
|
|
depBar: {
|
|
'package.json': JSON.stringify({
|
|
name: 'depBar',
|
|
main: 'main.js',
|
|
}),
|
|
},
|
|
projectD: {
|
|
SYMLINK: '/root/projectD',
|
|
},
|
|
},
|
|
},
|
|
'@scoped': {
|
|
projectC: {
|
|
'package.json': JSON.stringify({
|
|
name: '@scoped/projectC',
|
|
main: 'main.js',
|
|
}),
|
|
},
|
|
},
|
|
projectD: {
|
|
'package.json': JSON.stringify({
|
|
name: 'projectD',
|
|
main: 'main.js',
|
|
}),
|
|
},
|
|
},
|
|
});
|
|
|
|
const symlinkedModules = findSymlinkedModules('/root/projectA', []);
|
|
expect(symlinkedModules).toEqual([
|
|
'/root/@scoped/projectC',
|
|
'/root/projectB',
|
|
'/root/projectD',
|
|
]);
|
|
});
|
|
|
|
it('correctly handles duplicate symlink paths', () => {
|
|
// projectA ->
|
|
// -> projectC
|
|
// -> projectB -> projectC
|
|
// Final list should only contain projectC once
|
|
fs.__setMockFilesystem({
|
|
root: {
|
|
projectA: {
|
|
'package.json': JSON.stringify({
|
|
name: 'projectA',
|
|
main: 'main.js',
|
|
}),
|
|
node_modules: {
|
|
depFoo: {
|
|
'package.json': JSON.stringify({
|
|
name: 'depFoo',
|
|
main: 'main.js',
|
|
}),
|
|
},
|
|
'@scoped': {
|
|
projectC: {
|
|
SYMLINK: '/root/@scoped/projectC',
|
|
},
|
|
},
|
|
projectB: {
|
|
SYMLINK: '/root/projectB',
|
|
},
|
|
},
|
|
},
|
|
projectB: {
|
|
'package.json': JSON.stringify({
|
|
name: 'projectB',
|
|
main: 'main.js',
|
|
}),
|
|
node_modules: {
|
|
depBar: {
|
|
'package.json': JSON.stringify({
|
|
name: 'depBar',
|
|
main: 'main.js',
|
|
}),
|
|
},
|
|
'@scoped': {
|
|
projectC: {
|
|
SYMLINK: '/root/@scoped/projectC',
|
|
},
|
|
},
|
|
},
|
|
},
|
|
'@scoped': {
|
|
projectC: {
|
|
'package.json': JSON.stringify({
|
|
name: '@scoped/projectC',
|
|
main: 'main.js',
|
|
}),
|
|
},
|
|
},
|
|
},
|
|
});
|
|
|
|
const symlinkedModules = findSymlinkedModules('/root/projectA', []);
|
|
expect(symlinkedModules).toEqual([
|
|
'/root/@scoped/projectC',
|
|
'/root/projectB',
|
|
]);
|
|
});
|
|
|
|
it('correctly handles symlink recursion', () => {
|
|
// projectA ->
|
|
// -> projectC -> projectD -> projectA
|
|
// -> projectB -> projectC -> projectA
|
|
// -> projectD -> projectC -> projectA
|
|
// Should not infinite loop, should not contain projectA
|
|
fs.__setMockFilesystem({
|
|
root: {
|
|
projectA: {
|
|
'package.json': JSON.stringify({
|
|
name: 'projectA',
|
|
main: 'main.js',
|
|
}),
|
|
node_modules: {
|
|
depFoo: {
|
|
'package.json': JSON.stringify({
|
|
name: 'depFoo',
|
|
main: 'main.js',
|
|
}),
|
|
},
|
|
'@scoped': {
|
|
projectC: {
|
|
SYMLINK: '/root/@scoped/projectC',
|
|
},
|
|
},
|
|
projectB: {
|
|
SYMLINK: '/root/projectB',
|
|
},
|
|
},
|
|
},
|
|
projectB: {
|
|
'package.json': JSON.stringify({
|
|
name: 'projectB',
|
|
main: 'main.js',
|
|
}),
|
|
node_modules: {
|
|
depBar: {
|
|
'package.json': JSON.stringify({
|
|
name: 'depBar',
|
|
main: 'main.js',
|
|
}),
|
|
},
|
|
projectD: {
|
|
SYMLINK: '/root/projectD',
|
|
},
|
|
'@scoped': {
|
|
projectC: {
|
|
SYMLINK: '/root/@scoped/projectC',
|
|
},
|
|
},
|
|
},
|
|
},
|
|
'@scoped': {
|
|
projectC: {
|
|
'package.json': JSON.stringify({
|
|
name: '@scoped/projectC',
|
|
main: 'main.js',
|
|
}),
|
|
node_modules: {
|
|
projectA: {
|
|
SYMLINK: '/root/projectA',
|
|
},
|
|
projectD: {
|
|
SYMLINK: '/root/projectD',
|
|
},
|
|
projectE: {
|
|
SYMLINK: '/root/projectE',
|
|
},
|
|
},
|
|
},
|
|
},
|
|
projectD: {
|
|
'package.json': JSON.stringify({
|
|
name: 'projectD',
|
|
main: 'main.js',
|
|
}),
|
|
node_modules: {
|
|
'@scoped': {
|
|
projectC: {
|
|
SYMLINK: '/root/@scoped/projectC',
|
|
},
|
|
},
|
|
projectE: {
|
|
SYMLINK: '/root/projectE',
|
|
},
|
|
},
|
|
},
|
|
projectE: {
|
|
'package.json': JSON.stringify({
|
|
name: 'projectD',
|
|
main: 'main.js',
|
|
}),
|
|
},
|
|
},
|
|
});
|
|
|
|
const symlinkedModules = findSymlinkedModules('/root/projectA');
|
|
expect(symlinkedModules).toEqual([
|
|
'/root/@scoped/projectC',
|
|
'/root/projectB',
|
|
'/root/projectD',
|
|
'/root/projectE',
|
|
]);
|
|
});
|
|
});
|