react-native: make automated fs-based tests platform-independent

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
This commit is contained in:
Jean Lauliac 2018-09-17 05:43:43 -07:00 committed by Facebook Github Bot
parent 470a958e83
commit e327f88f02
16 changed files with 105 additions and 39 deletions

View File

@ -9,18 +9,21 @@
'use strict';
const fs = new (require('metro-memory-fs'))({cwd: process.cwd});
const path = require('path');
const MemoryFS = require('metro-memory-fs');
let fs;
function setMockFilesystem(object) {
fs.reset();
const root = process.platform === 'win32' ? 'c:\\' : '/';
function setMockFilesystem(object, platform) {
reset(platform);
const root = platform === 'win32' ? 'c:\\' : '/';
mockDir(root, {...object});
return root;
}
function mockDir(dirPath, desc) {
for (const entName in desc) {
const ent = desc[entName];
const entPath = require('path').join(dirPath, entName);
const entPath = path.join(dirPath, entName);
if (typeof ent === 'string' || ent instanceof Buffer) {
fs.writeFileSync(entPath, ent);
continue;
@ -37,7 +40,22 @@ function mockDir(dirPath, desc) {
}
}
fs.__setMockFilesystem = setMockFilesystem;
fs.mock = {clear: () => fs.reset()};
function reset(platform) {
if (path.mock == null) {
throw new Error(
'to use this "fs" module mock, you must also mock the "path" module',
);
}
path.mock.reset(platform);
const cwd = () => (platform === 'win32' ? 'c:\\' : '/');
fs = new MemoryFS({platform, cwd});
Object.assign(mockFs, fs);
}
module.exports = fs;
const mockFs = {};
mockFs.__setMockFilesystem = setMockFilesystem;
mockFs.mock = {clear: reset};
reset('posix');
module.exports = mockFs;

View File

@ -0,0 +1,20 @@
/**
* 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
*/
'use strict';
const mockPath = {};
function reset(platform) {
Object.assign(mockPath, jest.requireActual('path')[platform]);
}
mockPath.mock = {reset};
module.exports = mockPath;

View File

@ -1,7 +1,14 @@
/** @format */
/**
* 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
*/
const fs = require.requireActual('fs');
const path = require('path');
const path = require.requireActual('path');
const manifest = fs.readFileSync(
path.join(__dirname, './files/AndroidManifest.xml'),

View File

@ -10,6 +10,7 @@
'use strict';
jest.mock('path');
jest.mock('fs');
const fs = require('fs');

View File

@ -10,6 +10,7 @@
'use strict';
jest.mock('path');
jest.mock('fs');
const findManifest = require('../../android/findManifest');

View File

@ -10,40 +10,49 @@
'use strict';
jest.mock('path');
jest.mock('fs');
const mocks = require('../../__fixtures__/android');
const findPackageClassName = require('../../android/findPackageClassName');
const fs = require('fs');
const mocks = require('../../__fixtures__/android');
describe('android::findPackageClassName', () => {
beforeAll(() => {
fs.__setMockFilesystem({
empty: {},
flatJava: {
android: mocks.valid,
},
flatKotlin: {
android: mocks.validKotlin,
},
['posix', 'win32'].forEach(platform => {
let root;
describe(`android::findPackageClassName (${platform})`, () => {
beforeAll(() => {
root = fs.__setMockFilesystem(
{
empty: {},
flatJava: {
android: mocks.valid,
},
flatKotlin: {
android: mocks.validKotlin,
},
},
platform,
);
});
it('returns manifest content if file exists in the folder', () => {
expect(typeof findPackageClassName(root + 'flatJava')).toBe('string');
});
it('returns the name of the java class implementing ReactPackage', () => {
expect(findPackageClassName(root + 'flatJava')).toBe(
'SomeExampleJavaPackage',
);
});
it('returns the name of the kotlin class implementing ReactPackage', () => {
expect(findPackageClassName(root + 'flatKotlin')).toBe(
'SomeExampleKotlinPackage',
);
});
it('returns `null` if there are no matches', () => {
expect(findPackageClassName(root + 'empty')).toBeNull();
});
});
it('returns manifest content if file exists in the folder', () => {
expect(typeof findPackageClassName('/flatJava')).toBe('string');
});
it('returns the name of the java class implementing ReactPackage', () => {
expect(findPackageClassName('/flatJava')).toBe('SomeExampleJavaPackage');
});
it('returns the name of the kotlin class implementing ReactPackage', () => {
expect(findPackageClassName('/flatKotlin')).toBe(
'SomeExampleKotlinPackage',
);
});
it('returns `null` if there are no matches', () => {
expect(findPackageClassName('/empty')).toBeNull();
});
});

View File

@ -10,6 +10,7 @@
'use strict';
jest.mock('path');
jest.mock('fs');
const getDependencyConfig = require('../../android').dependencyConfig;

View File

@ -10,6 +10,7 @@
'use strict';
jest.mock('path');
jest.mock('fs');
const getProjectConfig = require('../../android').projectConfig;

View File

@ -10,6 +10,7 @@
'use strict';
jest.mock('path');
jest.mock('fs');
const findManifest = require('../../android/findManifest');

View File

@ -10,6 +10,7 @@
'use strict';
jest.mock('path');
jest.mock('fs');
const findAssets = require('../findAssets');

View File

@ -10,6 +10,7 @@
'use strict';
jest.mock('path');
jest.mock('fs');
const findPodfilePath = require('../../ios/findPodfilePath');

View File

@ -10,6 +10,7 @@
'use strict';
jest.mock('path');
jest.mock('fs');
const findPodspecName = require('../../ios/findPodspecName');

View File

@ -10,6 +10,7 @@
'use strict';
jest.mock('path');
jest.mock('fs');
const findProject = require('../../ios/findProject');

View File

@ -10,6 +10,7 @@
'use strict';
jest.mock('path');
jest.mock('fs');
const getProjectConfig = require('../../ios').projectConfig;

View File

@ -10,6 +10,7 @@
'use strict';
jest.mock('path');
jest.mock('fs');
let plistPath = null;

View File

@ -8,6 +8,7 @@
* @emails oncall+javascript_foundation
*/
jest.mock('path');
jest.mock('fs');
const fs = require('fs');