diff --git a/desktop/plugin-lib/package.json b/desktop/plugin-lib/package.json index 354357ed1..ba382e5e8 100644 --- a/desktop/plugin-lib/package.json +++ b/desktop/plugin-lib/package.json @@ -24,6 +24,7 @@ "flipper-test-utils": "0.45.0", "globby": "^10", "jest": "^25.1.0", + "mock-fs": "^4.12.0", "rimraf": "^3.0.2", "ts-jest": "^26.0.0", "ts-node": "^8", diff --git a/desktop/plugin-lib/src/__tests__/pluginInstaller.node.ts b/desktop/plugin-lib/src/__tests__/pluginInstaller.node.ts new file mode 100644 index 000000000..602c33885 --- /dev/null +++ b/desktop/plugin-lib/src/__tests__/pluginInstaller.node.ts @@ -0,0 +1,117 @@ +/** + * 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 fs from 'fs-extra'; +import path from 'path'; +import mockfs from 'mock-fs'; +import {consoleMock} from 'flipper-test-utils'; +import {finishPendingPluginInstallations} from '../pluginInstaller'; +import { + pluginPendingInstallationDir, + pluginInstallationDir, +} from '../pluginPaths'; + +jest.mock('../pluginPaths', () => { + return { + pluginInstallationDir: path.join( + '', + 'Users', + 'mock', + '.flipper', + 'thirdparty', + ), + pluginPendingInstallationDir: path.join( + '', + 'Users', + 'mock', + '.flipper', + 'pending', + ), + }; +}); + +describe('pluginInstaller', () => { + const realConsole = global.console; + global.console = consoleMock as any; + + afterAll(() => { + global.console = realConsole; + }); + + beforeEach(() => { + mockfs({}); + }); + + afterEach(() => { + mockfs.restore(); + }); + + test('finish pending plugin installations', async () => { + mockfs({ + [pluginPendingInstallationDir]: { + 'flipper-plugin-test1': { + '1.2.0': { + 'index.ts': '', + 'package.json': '', + }, + }, + 'flipper-plugin-test2': { + '0.3.0': { + 'index.js': '<0.3.0>', + 'package.json': '', + }, + '0.2.0': { + 'index.js': '<0.2.0>', + 'package.json': '', + }, + }, + }, + }); + + await finishPendingPluginInstallations(); + + expect(await fs.readdir(pluginInstallationDir)).toMatchInlineSnapshot(` + Array [ + ".watchmanconfig", + "flipper-plugin-test1", + "flipper-plugin-test2", + ] + `); + expect( + await fs.readdir( + path.join(pluginInstallationDir, 'flipper-plugin-test1'), + ), + ).toMatchInlineSnapshot(` + Array [ + "index.ts", + "package.json", + ] + `); + expect( + await fs.readdir( + path.join(pluginInstallationDir, 'flipper-plugin-test2'), + ), + ).toMatchInlineSnapshot(` + Array [ + "index.js", + "package.json", + ] + `); + expect( + ( + await fs.readFile( + path.join(pluginInstallationDir, 'flipper-plugin-test2', 'index.js'), + ) + ).toString(), + ).toBe('<0.3.0>'); + expect( + await fs.readdir(pluginPendingInstallationDir), + ).toMatchInlineSnapshot(`Array []`); + }); +}); diff --git a/desktop/plugin-lib/src/pluginInstaller.ts b/desktop/plugin-lib/src/pluginInstaller.ts index d45ad1f0d..931364378 100644 --- a/desktop/plugin-lib/src/pluginInstaller.ts +++ b/desktop/plugin-lib/src/pluginInstaller.ts @@ -253,7 +253,7 @@ export async function finishPendingPluginInstallations() { pluginInstallationDir, '.watchmanconfig', ); - if (await fs.pathExists(watchmanConfigPath)) { + if (!(await fs.pathExists(watchmanConfigPath))) { await fs.writeFile(watchmanConfigPath, '{}'); } const pendingPlugins = await fs.readdir(pluginPendingInstallationDir);