Skip to content

Commit 84792b8

Browse files
committed
Log a warning when multiple plugins share the same name
1 parent 864f3d0 commit 84792b8

File tree

2 files changed

+51
-7
lines changed

2 files changed

+51
-7
lines changed

src/globalPlug.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,22 @@ export class GlobalPlug implements Plug {
4646

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

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

54+
if (initialized.includes(pluginName)) {
55+
logger.warn(
56+
`Multiple plugins registered with name "${pluginName}" `
57+
+ 'which may cause unexpected errors.',
58+
);
59+
}
60+
5361
logger.debug(`Initializing plugin "${pluginName}"...`);
5462

63+
initialized.push(pluginName);
64+
5565
const controller = plugin.initialize({
5666
tracker: sdk.tracker,
5767
evaluator: sdk.evaluator,

test/index.test.ts

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
1-
import {SdkFacade, SdkFacadeConfiguration} from '@croct-tech/sdk';
1+
import {Logger, SdkFacade, SdkFacadeConfiguration} from '@croct-tech/sdk';
22
import croct, {Configuration, Plugin, PluginController, PluginSdk} from '../src/index';
33

4+
function createLoggerMock(): Logger {
5+
return {
6+
debug: jest.fn(),
7+
info: jest.fn(),
8+
warn: jest.fn(),
9+
error: jest.fn(),
10+
};
11+
}
12+
413
describe('The Croct plug', () => {
514
const appId = '7e9d59a9-e4b3-45d4-b1c7-48287f1e5e8a';
615

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

82+
test('should log a warn message if multiple plugins share the same name', async () => {
83+
const fooPlugin: Plugin = {
84+
getName(): string {
85+
return 'foo';
86+
},
87+
initialize(): void {
88+
// does nothing
89+
},
90+
};
91+
92+
const logger: Logger = createLoggerMock();
93+
94+
croct.plug({
95+
appId: appId,
96+
plugins: [fooPlugin, fooPlugin],
97+
logger: logger,
98+
});
99+
100+
await croct.plugged;
101+
102+
expect(logger.warn).toHaveBeenCalledWith(
103+
expect.stringContaining('Multiple plugins registered with name "foo" which may cause unexpected errors'),
104+
);
105+
});
106+
73107
test('should handle failures enabling plugins', async () => {
74108
const fooController: PluginController = {
75109
enable: jest.fn().mockReturnValue(Promise.reject(new Error('Failure'))),
@@ -80,7 +114,7 @@ describe('The Croct plug', () => {
80114
return 'foo';
81115
},
82116
initialize(): PluginController {
83-
return fooController
117+
return fooController;
84118
},
85119
};
86120

@@ -107,7 +141,7 @@ describe('The Croct plug', () => {
107141
initialize(): PluginController {
108142
return {
109143
enable: fooEnable,
110-
}
144+
};
111145
},
112146
};
113147

@@ -120,7 +154,7 @@ describe('The Croct plug', () => {
120154
initialize(): PluginController {
121155
return {
122156
enable: barEnable,
123-
}
157+
};
124158
},
125159
};
126160

@@ -455,7 +489,7 @@ describe('The Croct plug', () => {
455489
initialize(): PluginController {
456490
return {
457491
disable: fooDisable,
458-
}
492+
};
459493
},
460494
};
461495

@@ -468,7 +502,7 @@ describe('The Croct plug', () => {
468502
initialize(): PluginController {
469503
return {
470504
disable: barDisable,
471-
}
505+
};
472506
},
473507
};
474508

@@ -529,7 +563,7 @@ describe('The Croct plug', () => {
529563
return 'foo';
530564
},
531565
initialize(): PluginController {
532-
return fooController
566+
return fooController;
533567
},
534568
};
535569

0 commit comments

Comments
 (0)