Skip to content

Commit

Permalink
bump fs-extra types
Browse files Browse the repository at this point in the history
Summary: `recursive` was dropped in when [dropping](jprichardson/node-fs-extra#886) node v10

Reviewed By: aigoncharov

Differential Revision: D48780242

fbshipit-source-id: 29349590a7f14da85fe8df28b20d9b418e7a8b1d
  • Loading branch information
antonk52 authored and facebook-github-bot committed Sep 5, 2023
1 parent d04abff commit f2ef26c
Show file tree
Hide file tree
Showing 13 changed files with 132 additions and 83 deletions.
2 changes: 1 addition & 1 deletion desktop/babel-transformer/package.json
Expand Up @@ -24,7 +24,7 @@
"@babel/traverse": "^7.20.13",
"@babel/types": "^7.20.7",
"@emotion/babel-plugin": "^11.10.6",
"@types/fs-extra": "^9.0.13",
"@types/fs-extra": "^11.0.0",
"@types/node": "^17.0.31",
"fs-extra": "^11.1.0",
"tslib": "^2.4.1"
Expand Down
2 changes: 1 addition & 1 deletion desktop/pkg-lib/package.json
Expand Up @@ -22,7 +22,7 @@
"p-map": "^4"
},
"devDependencies": {
"@types/fs-extra": "^9.0.13",
"@types/fs-extra": "^11.0.1",
"@types/mock-fs": "^4.13.1",
"@types/node": "^17.0.31",
"@types/npm-packlist": "^1.1.2",
Expand Down
2 changes: 2 additions & 0 deletions desktop/pkg-lib/src/__tests__/getWatchFolders.node.tsx
Expand Up @@ -99,6 +99,7 @@ describe('getWatchFolders', () => {
};
const readReadJson = fs.readJson;
try {
// @ts-expect-error readJson is read only and it is fine, this is a test
fs.readJson = readJsonMock as any;
const resolvedFolders = await getWatchFolders(
path.join(rootDir, 'local_module_2'),
Expand All @@ -115,6 +116,7 @@ describe('getWatchFolders', () => {
]
`);
} finally {
// @ts-expect-error readJson is read only and it is fine, this is a test
fs.readJson = readReadJson;
}
});
Expand Down
2 changes: 1 addition & 1 deletion desktop/pkg/package.json
Expand Up @@ -29,7 +29,7 @@
},
"devDependencies": {
"@oclif/dev-cli": "^1",
"@types/fs-extra": "^9.0.13",
"@types/fs-extra": "^11.0.0",
"@types/inquirer": "^7.3.3",
"@types/node": "^17.0.31",
"@types/recursive-readdir": "^2.2.1",
Expand Down
53 changes: 29 additions & 24 deletions desktop/pkg/src/__tests__/runLint.node.tsx
Expand Up @@ -30,21 +30,28 @@ const validPackageJson = {
},
};

beforeEach(() => {
jest.mock('fs-extra', () => jest.fn());
fs.pathExists = jest.fn().mockResolvedValue(true);
fs.pathExistsSync = jest.fn().mockReturnValue(true);
// Required by some inconsistent node types for rw access.
(fs.lstatSync as any) = jest.fn().mockReturnValue({
isFile: function () {
return true;
},
});
jest.mock('fs-extra', () => {
const mod = {
...jest.requireActual('fs-extra'),
readFile: jest.fn(),
pathExists: jest.fn().mockResolvedValue(true),
pathExistsSync: jest.fn().mockResolvedValue(true),
lstatSync: jest.fn().mockReturnValue({
isFile: function () {
return true;
},
}),
};

return {
...mod,
default: mod,
};
});

test('valid package json', async () => {
const json = JSON.stringify(validPackageJson);
fs.readFile = jest.fn().mockResolvedValue(new Buffer(json));
(fs.readFile as any as jest.Mock).mockReturnValueOnce(new Buffer(json));
const result = await runLint('dir');
expect(result).toBe(null);
});
Expand All @@ -53,7 +60,7 @@ test('valid scoped package json', async () => {
const testPackageJson = Object.assign({}, validPackageJson);
testPackageJson.name = '@test/flipper-plugin-package';
const json = JSON.stringify(testPackageJson);
fs.readFile = jest.fn().mockResolvedValue(new Buffer(json));
(fs.readFile as any as jest.Mock).mockReturnValueOnce(new Buffer(json));
const result = await runLint('dir');
expect(result).toBe(null);
});
Expand All @@ -63,7 +70,7 @@ test('$schema field is required', async () => {
// @ts-ignore cannot delete non-optional fields
delete testPackageJson.$schema;
const json = JSON.stringify(testPackageJson);
fs.readFile = jest.fn().mockResolvedValue(new Buffer(json));
(fs.readFile as any as jest.Mock).mockReturnValueOnce(new Buffer(json));
const result = await runLint('dir');
expect(result).toMatchInlineSnapshot(`
[
Expand All @@ -82,7 +89,7 @@ test('supported schema is required', async () => {
testPackageJson.$schema =
'https://fbflipper.com/schemas/plugin-package/v1.json';
const json = JSON.stringify(testPackageJson);
fs.readFile = jest.fn().mockResolvedValue(new Buffer(json));
(fs.readFile as any as jest.Mock).mockReturnValueOnce(new Buffer(json));
const result = await runLint('dir');
expect(result).toMatchInlineSnapshot(`
[
Expand All @@ -97,7 +104,7 @@ test('name is required', async () => {
// @ts-ignore cannot delete non-optional fields
delete testPackageJson.name;
const json = JSON.stringify(testPackageJson);
fs.readFile = jest.fn().mockResolvedValue(new Buffer(json));
(fs.readFile as any as jest.Mock).mockReturnValueOnce(new Buffer(json));
const result = await runLint('dir');
expect(result).toMatchInlineSnapshot(`
[
Expand All @@ -110,7 +117,7 @@ test('name must start with "flipper-plugin-"', async () => {
const testPackageJson = Object.assign({}, validPackageJson);
testPackageJson.name = 'test-plugin';
const json = JSON.stringify(testPackageJson);
fs.readFile = jest.fn().mockResolvedValue(new Buffer(json));
(fs.readFile as any as jest.Mock).mockReturnValueOnce(new Buffer(json));
const result = await runLint('dir');
expect(result).toMatchInlineSnapshot(`
[
Expand All @@ -123,7 +130,7 @@ test('keywords must contain "flipper-plugin"', async () => {
const testPackageJson = Object.assign({}, validPackageJson);
testPackageJson.keywords = ['flipper', 'network'];
const json = JSON.stringify(testPackageJson);
fs.readFile = jest.fn().mockResolvedValue(new Buffer(json));
(fs.readFile as any as jest.Mock).mockReturnValueOnce(new Buffer(json));
const result = await runLint('dir');
expect(result).toMatchInlineSnapshot(`
[
Expand All @@ -135,13 +142,11 @@ test('keywords must contain "flipper-plugin"', async () => {
test('flippeBundlerEntry must point to an existing file', async () => {
const testPackageJson = Object.assign({}, validPackageJson);
testPackageJson.flipperBundlerEntry = 'unexisting/file';
fs.pathExistsSync = jest
.fn()
.mockImplementation(
(filePath) => !filePath.includes(path.join('unexisting', 'file')),
);
(fs.pathExistsSync as any as jest.Mock).mockImplementation(
(filePath) => !filePath.includes(path.join('unexisting', 'file')),
);
const json = JSON.stringify(testPackageJson);
fs.readFile = jest.fn().mockResolvedValue(new Buffer(json));
(fs.readFile as any as jest.Mock).mockReturnValueOnce(new Buffer(json));
const result = await runLint('dir');
expect(result).toMatchInlineSnapshot(`
[
Expand All @@ -156,7 +161,7 @@ test('multiple validation errors reported', async () => {
// @ts-ignore cannot delete non-optional fields
delete testPackageJson.flipperBundlerEntry;
const json = JSON.stringify(testPackageJson);
fs.readFile = jest.fn().mockResolvedValue(new Buffer(json));
(fs.readFile as any as jest.Mock).mockReturnValueOnce(new Buffer(json));
const result = await runLint('dir');
expect(result).toMatchInlineSnapshot(`
[
Expand Down
80 changes: 47 additions & 33 deletions desktop/pkg/src/__tests__/runMigrate.node.tsx
Expand Up @@ -10,25 +10,6 @@
import runMigrate from '../utils/runMigrate';
import fs from 'fs-extra';

const packageJsonV1 = {
name: 'Fresco',
version: '1.0.0',
main: 'index.tsx',
license: 'MIT',
keywords: ['images'],
dependencies: {
flipper: 'latest',
},
scripts: {
prepack: 'yarn reset && yarn build',
},
title: 'Images',
icon: 'profile',
bugs: {
email: 'example@test.com',
},
};

const packageJsonV2 = {
$schema: 'https://fbflipper.com/schemas/plugin-package/v2.json',
name: 'flipper-plugin-network',
Expand All @@ -53,18 +34,52 @@ const packageJsonV2 = {

let convertedPackageJsonString: string | undefined;

jest.mock('fs-extra', () => {
const packageJsonV1 = {
name: 'Fresco',
version: '1.0.0',
main: 'index.tsx',
license: 'MIT',
keywords: ['images'],
dependencies: {
flipper: 'latest',
},
scripts: {
prepack: 'yarn reset && yarn build',
},
title: 'Images',
icon: 'profile',
bugs: {
email: 'example@test.com',
},
};

const mod = {
...jest.requireActual('fs-extra'),
readFile: jest
.fn()
.mockResolvedValue(new Buffer(JSON.stringify(packageJsonV1))),
pathExists: jest.fn().mockResolvedValue(true),
pathExistsSync: jest.fn().mockResolvedValue(true),
readJson: jest.fn().mockResolvedValue(packageJsonV1),
writeFile: jest.fn(async (_path, content) => {
convertedPackageJsonString = content;
}),
lstatSync: jest.fn().mockReturnValue({
isFile: function () {
return true;
},
}),
};

return {
...mod,
default: mod,
};
});

beforeEach(() => {
jest.mock('fs-extra', () => jest.fn());
fs.pathExists = jest.fn().mockResolvedValue(true);
fs.pathExistsSync = jest.fn().mockReturnValue(true);
fs.readJson = jest.fn().mockResolvedValue(packageJsonV1);
fs.readFile = jest
.fn()
.mockResolvedValue(new Buffer(JSON.stringify(packageJsonV1)));
convertedPackageJsonString = undefined;
fs.writeFile = jest.fn().mockImplementation(async (_path, content) => {
convertedPackageJsonString = content;
});
});

test('converts package.json and adds dependencies', async () => {
Expand Down Expand Up @@ -134,10 +149,9 @@ test('converts package.json without changing dependencies', async () => {
});

test('does not migrate already migrated packages', async () => {
fs.readJson = jest.fn().mockResolvedValue(packageJsonV2);
fs.readFile = jest
.fn()
.mockResolvedValue(new Buffer(JSON.stringify(packageJsonV2)));
(fs.readFile as any as jest.Mock).mockResolvedValue(
new Buffer(JSON.stringify(packageJsonV2)),
);
const error = await runMigrate('dir');
expect(error).toBeUndefined();
expect(convertedPackageJsonString).toBeUndefined();
Expand Down
2 changes: 1 addition & 1 deletion desktop/plugin-lib/package.json
Expand Up @@ -24,7 +24,7 @@
},
"devDependencies": {
"@types/decompress": "4.2.4",
"@types/fs-extra": "^9.0.13",
"@types/fs-extra": "^11.0.1",
"@types/mock-fs": "^4.13.1",
"@types/node": "^17.0.31",
"flipper-test-utils": "0.0.0",
Expand Down
9 changes: 8 additions & 1 deletion desktop/plugin-lib/src/__tests__/getPluginDetails.node.tsx
Expand Up @@ -31,6 +31,7 @@ test('getPluginDetailsV1', async () => {
description: 'Description of Test Plugin',
gatekeeper: 'GK_flipper_plugin_test',
};
// @ts-expect-error this is read only and it is fine, this is a test
fs.readJson = jest.fn().mockImplementation(() => pluginV1);
const details = await getInstalledPluginDetails(pluginPath);
details.dir = normalizePath(details.dir);
Expand Down Expand Up @@ -74,6 +75,7 @@ test('getPluginDetailsV2', async () => {
description: 'Description of Test Plugin',
gatekeeper: 'GK_flipper_plugin_test',
};
// @ts-expect-error this is read only and it is fine, this is a test
fs.readJson = jest.fn().mockImplementation(() => pluginV2);
const details = await getInstalledPluginDetails(pluginPath);
details.dir = normalizePath(details.dir);
Expand Down Expand Up @@ -121,6 +123,7 @@ test('id used as title if the latter omited', async () => {
description: 'Description of Test Plugin',
gatekeeper: 'GK_flipper_plugin_test',
};
// @ts-expect-error this is read only and it is fine, this is a test
fs.readJson = jest.fn().mockImplementation(() => pluginV2);
const details = await getInstalledPluginDetails(pluginPath);
details.dir = normalizePath(details.dir);
Expand Down Expand Up @@ -167,6 +170,7 @@ test('name without "flipper-plugin-" prefix is used as title if the latter omite
description: 'Description of Test Plugin',
gatekeeper: 'GK_flipper_plugin_test',
};
// @ts-expect-error this is read only and it is fine, this is a test
fs.readJson = jest.fn().mockImplementation(() => pluginV2);
const details = await getInstalledPluginDetails(pluginPath);
details.dir = normalizePath(details.dir);
Expand Down Expand Up @@ -216,6 +220,7 @@ test('flipper-plugin-version is parsed', async () => {
'flipper-plugin': '^0.45',
},
};
// @ts-expect-error this is read only and it is fine, this is a test
fs.readJson = jest.fn().mockImplementation(() => pluginV2);
const details = await getInstalledPluginDetails(pluginPath);
details.dir = normalizePath(details.dir);
Expand Down Expand Up @@ -269,6 +274,7 @@ test('plugin type and supported devices parsed', async () => {
description: 'Description of Test Plugin',
gatekeeper: 'GK_flipper_plugin_test',
};
// @ts-expect-error this is read only and it is fine, this is a test
fs.readJson = jest.fn().mockImplementation(() => pluginV2);
const details = await getInstalledPluginDetails(pluginPath);
details.dir = normalizePath(details.dir);
Expand Down Expand Up @@ -338,6 +344,7 @@ test('plugin type and supported apps parsed', async () => {
description: 'Description of Test Plugin',
gatekeeper: 'GK_flipper_plugin_test',
};
// @ts-expect-error this is read only and it is fine, this is a test
fs.readJson = jest.fn().mockImplementation(() => pluginV2);
const details = await getInstalledPluginDetails(pluginPath);
details.dir = normalizePath(details.dir);
Expand Down Expand Up @@ -417,7 +424,7 @@ test('can merge two package.json files', async () => {
},
};
const mockedFs = jest.mocked(fs);
mockedFs.readJson.mockImplementation((file) => {
mockedFs.readJson.mockImplementation((file): any => {
if (file === path.join(pluginPath, 'package.json')) {
return pluginBase;
} else if (file === path.join(pluginPath, 'fb', 'package.json')) {
Expand Down
24 changes: 16 additions & 8 deletions desktop/plugin-lib/src/__tests__/pluginInstaller.node.tsx
Expand Up @@ -119,24 +119,32 @@ function collectFileContent(
}
}

describe('pluginInstaller', () => {
let readJson: any;
beforeEach(() => {
mockfs(installedPluginFiles);
readJson = fs.readJson;
fs.readJson = (file: string) => {
jest.mock('fs-extra', () => {
const mod = {
...jest.requireActual('fs-extra'),
readJson: jest.fn((file: string) => {
const content = fileContent.get(normalizePath(file));
if (content) {
return Promise.resolve(JSON.parse(content));
} else {
return Promise.resolve(undefined);
}
};
}),
};

return {
...mod,
default: mod,
};
});

describe('pluginInstaller', () => {
beforeEach(() => {
mockfs(installedPluginFiles);
});

afterEach(() => {
mockfs.restore();
fs.readJson = readJson;
});

test('getInstalledPlugins', async () => {
Expand Down
1 change: 0 additions & 1 deletion desktop/scripts/build-utils.tsx
Expand Up @@ -57,7 +57,6 @@ export async function prepareDefaultPlugins(isInsidersBuild: boolean = false) {
`⚙️ Copying the provided default plugins dir "${forcedDefaultPluginsDir}"...`,
);
await fs.copy(forcedDefaultPluginsDir, defaultPluginsDir, {
recursive: true,
overwrite: true,
dereference: true,
});
Expand Down
1 change: 0 additions & 1 deletion desktop/scripts/copy-package-with-dependencies.tsx
Expand Up @@ -59,7 +59,6 @@ async function copyPackageWithDependenciesRecursive(
.then((l: Array<string>) => ignore().add(DEFAULT_BUILD_IGNORES.concat(l)));
await fs.copy(packageDir, targetDir, {
dereference: true,
recursive: true,
filter: (src) => {
const relativePath = path.relative(packageDir, src);
return relativePath === '' || !ignores.ignores(relativePath);
Expand Down

0 comments on commit f2ef26c

Please sign in to comment.