Skip to content

Commit

Permalink
feat: add PluginConfig during load
Browse files Browse the repository at this point in the history
  • Loading branch information
mayurkale22 committed Aug 2, 2019
1 parent a387272 commit aac7132
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ interface PluginNames {
[pluginName: string]: string;
}

interface PluginConfig {
// TODO: Consider to add configuration options
[pluginName: string]: boolean;
}

// tslint:disable-next-line:no-any
export type PluginType = Array<Plugin<any>>;

Expand All @@ -54,13 +59,15 @@ export class PluginLoader {
* {@link Plugin} interface and export an instance named as 'plugin'. This
* function will attach a hook to be called the first time the module is
* loaded.
* @param modulesToPatch A list of plugins.
* @param pluginConfig an object to configure the plugin.
*/
load(modulesToPatch: string[]): PluginLoader {
load(pluginConfig: PluginConfig): PluginLoader {
if (this._hookState === HookState.UNINITIALIZED) {
const plugins = modulesToPatch.reduce(
const plugins = Object.keys(pluginConfig).reduce(
(plugins: PluginNames, moduleName: string) => {
plugins[moduleName] = utils.defaultPackageName(moduleName);
if (pluginConfig[moduleName]) {
plugins[moduleName] = utils.defaultPackageName(moduleName);
}
return plugins;
},
{} as PluginNames
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import {
} from '../../src/instrumentation/PluginLoader';

const INSTALLED_PLUGINS_PATH = path.join(__dirname, 'node_modules');
const TEST_MODULES = ['simple-module', 'nonexistent-module', 'http'];

describe('PluginLoader', () => {
const tracer = new NoopTracer();
Expand All @@ -48,31 +47,31 @@ describe('PluginLoader', () => {

it('transitions from UNINITIALIZED to ENABLED', () => {
const pluginLoader = new PluginLoader(tracer, logger);
pluginLoader.load([TEST_MODULES[0]]);
pluginLoader.load({ 'simple-module': true });
assert.strictEqual(pluginLoader['_hookState'], HookState.ENABLED);
pluginLoader.unload();
});

it('transitions from ENABLED to DISABLED', () => {
const pluginLoader = new PluginLoader(tracer, logger);
pluginLoader.load([TEST_MODULES[0]]).unload();
pluginLoader.load({ 'simple-module': true }).unload();
assert.strictEqual(pluginLoader['_hookState'], HookState.DISABLED);
});
});

describe('load()', () => {
it('sanity check', () => {
// Ensure that module fixtures contain values that we expect.
const simpleModule = require(TEST_MODULES[0]);
assert.strictEqual(simpleModule.name(), TEST_MODULES[0]);
const simpleModule = require('simple-module');
assert.strictEqual(simpleModule.name(), 'simple-module');
assert.strictEqual(simpleModule.value(), 0);
assert.throws(() => require(TEST_MODULES[1]));
assert.throws(() => require('nonexistent-module'));
});

it('should load a plugin and patch the target modules', () => {
const pluginLoader = new PluginLoader(tracer, logger);
assert.strictEqual(pluginLoader['_plugins'].length, 0);
pluginLoader.load(['simple-module']);
pluginLoader.load({ 'simple-module': true });
// The hook is only called the first time the module is loaded.
const simpleModule = require('simple-module');
assert.strictEqual(pluginLoader['_plugins'].length, 1);
Expand All @@ -81,17 +80,28 @@ describe('PluginLoader', () => {
pluginLoader.unload();
});

it('should not load a plugin when value is false', () => {
const pluginLoader = new PluginLoader(tracer, logger);
assert.strictEqual(pluginLoader['_plugins'].length, 0);
pluginLoader.load({ 'simple-module': false });
const simpleModule = require('simple-module');
assert.strictEqual(pluginLoader['_plugins'].length, 0);
assert.strictEqual(simpleModule.value(), 0);
assert.strictEqual(simpleModule.name(), 'simple-module');
pluginLoader.unload();
});

it('should not load a non existing plugin', () => {
const pluginLoader = new PluginLoader(tracer, logger);
assert.strictEqual(pluginLoader['_plugins'].length, 0);
pluginLoader.load(['nonexistent-module']);
pluginLoader.load({ 'nonexistent-module': true });
assert.strictEqual(pluginLoader['_plugins'].length, 0);
pluginLoader.unload();
});

it(`doesn't patch modules for which plugins aren't specified`, () => {
const pluginLoader = new PluginLoader(tracer, logger);
pluginLoader.load([]);
pluginLoader.load({});
assert.strictEqual(require('simple-module').value(), 0);
pluginLoader.unload();
});
Expand All @@ -101,7 +111,7 @@ describe('PluginLoader', () => {
it('should unload the plugins and unpatch the target module when unloads', () => {
const pluginLoader = new PluginLoader(tracer, logger);
assert.strictEqual(pluginLoader['_plugins'].length, 0);
pluginLoader.load(['simple-module']);
pluginLoader.load({ 'simple-module': true });
// The hook is only called the first time the module is loaded.
const simpleModule = require('simple-module');
assert.strictEqual(pluginLoader['_plugins'].length, 1);
Expand Down

0 comments on commit aac7132

Please sign in to comment.