Skip to content

Commit

Permalink
Log a warning when multiple plugins share the same name
Browse files Browse the repository at this point in the history
  • Loading branch information
marcospassos committed May 10, 2020
1 parent 864f3d0 commit 84792b8
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 7 deletions.
10 changes: 10 additions & 0 deletions src/globalPlug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,22 @@ export class GlobalPlug implements Plug {

const logger = this.instance.getLogger();
const pending: Promise<void>[] = [];
const initialized: string[] = [];

for (const plugin of plugins ?? []) {
const pluginName = plugin.getName();

if (initialized.includes(pluginName)) {
logger.warn(
`Multiple plugins registered with name "${pluginName}" `
+ 'which may cause unexpected errors.',
);
}

logger.debug(`Initializing plugin "${pluginName}"...`);

initialized.push(pluginName);

const controller = plugin.initialize({
tracker: sdk.tracker,
evaluator: sdk.evaluator,
Expand Down
48 changes: 41 additions & 7 deletions test/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
import {SdkFacade, SdkFacadeConfiguration} from '@croct-tech/sdk';
import {Logger, SdkFacade, SdkFacadeConfiguration} from '@croct-tech/sdk';
import croct, {Configuration, Plugin, PluginController, PluginSdk} from '../src/index';

function createLoggerMock(): Logger {
return {
debug: jest.fn(),
info: jest.fn(),
warn: jest.fn(),
error: jest.fn(),
};
}

describe('The Croct plug', () => {
const appId = '7e9d59a9-e4b3-45d4-b1c7-48287f1e5e8a';

Expand Down Expand Up @@ -70,6 +79,31 @@ describe('The Croct plug', () => {
expect(barPlugin.initialize).toBeCalled();
});

test('should log a warn message if multiple plugins share the same name', async () => {
const fooPlugin: Plugin = {
getName(): string {
return 'foo';
},
initialize(): void {
// does nothing
},
};

const logger: Logger = createLoggerMock();

croct.plug({
appId: appId,
plugins: [fooPlugin, fooPlugin],
logger: logger,
});

await croct.plugged;

expect(logger.warn).toHaveBeenCalledWith(
expect.stringContaining('Multiple plugins registered with name "foo" which may cause unexpected errors'),
);
});

test('should handle failures enabling plugins', async () => {
const fooController: PluginController = {
enable: jest.fn().mockReturnValue(Promise.reject(new Error('Failure'))),
Expand All @@ -80,7 +114,7 @@ describe('The Croct plug', () => {
return 'foo';
},
initialize(): PluginController {
return fooController
return fooController;
},
};

Expand All @@ -107,7 +141,7 @@ describe('The Croct plug', () => {
initialize(): PluginController {
return {
enable: fooEnable,
}
};
},
};

Expand All @@ -120,7 +154,7 @@ describe('The Croct plug', () => {
initialize(): PluginController {
return {
enable: barEnable,
}
};
},
};

Expand Down Expand Up @@ -455,7 +489,7 @@ describe('The Croct plug', () => {
initialize(): PluginController {
return {
disable: fooDisable,
}
};
},
};

Expand All @@ -468,7 +502,7 @@ describe('The Croct plug', () => {
initialize(): PluginController {
return {
disable: barDisable,
}
};
},
};

Expand Down Expand Up @@ -529,7 +563,7 @@ describe('The Croct plug', () => {
return 'foo';
},
initialize(): PluginController {
return fooController
return fooController;
},
};

Expand Down

0 comments on commit 84792b8

Please sign in to comment.