Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions test/fixtures/test-package/esm-export.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { PushActionPlugin } from '@finos/git-proxy/plugin';

// test default export (ESM syntax)
export default new PushActionPlugin(async (req, action) => {
console.log('Dummy plugin: ', action);
return action;
});
18 changes: 18 additions & 0 deletions test/fixtures/test-package/esm-multiple-export.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { PushActionPlugin, PullActionPlugin } from '@finos/git-proxy/plugin';

// test multiple exports (ESM syntax)
export default {
foo: new PushActionPlugin(async (req, action) => {
console.log('PushActionPlugin: ', action);
return action;
}),
bar: new PullActionPlugin(async (req, action) => {
console.log('PullActionPlugin: ', action);
return action;
}),
Comment thread
jescalada marked this conversation as resolved.
baz: {
exec: async (req, action) => {
console.log('not a real plugin object');
},
},
};
14 changes: 14 additions & 0 deletions test/fixtures/test-package/esm-subclass.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { PushActionPlugin } from '@finos/git-proxy/plugin';

class DummyPlugin extends PushActionPlugin {
constructor(exec) {
super();
this.exec = exec;
}
}

// test default export (ESM syntax)
export default new DummyPlugin(async (req, action) => {
console.log('Dummy plugin: ', action);
return action;
});
5 changes: 5 additions & 0 deletions test/fixtures/test-package/multiple-export.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,9 @@ module.exports = {
console.log('PullActionPlugin: ', action);
return action;
}),
baz: {
exec: async (req, action) => {
console.log('not a real plugin object');
},
},
};
99 changes: 66 additions & 33 deletions test/plugin/plugin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,7 @@ import chai from 'chai';
import { spawnSync } from 'child_process';
import { rmSync } from 'fs';
import { join } from 'path';
import {
isCompatiblePlugin,
PullActionPlugin,
PushActionPlugin,
PluginLoader,
} from '../../src/plugin.ts';
import { isCompatiblePlugin, PushActionPlugin, PluginLoader } from '../../src/plugin.ts';

chai.should();

Expand All @@ -22,35 +17,73 @@ describe('loading plugins from packages', function () {
spawnSync('npm', ['install'], { cwd: testPackagePath, timeout: 5000 });
});

it('should load plugins that are the default export (module.exports = pluginObj)', async function () {
const loader = new PluginLoader([join(testPackagePath, 'default-export.js')]);
await loader.load();
expect(loader.pushPlugins.length).to.equal(1);
expect(loader.pushPlugins.every((p) => isCompatiblePlugin(p))).to.be.true;
expect(loader.pushPlugins.every((p) => isCompatiblePlugin(p, 'isGitProxyPushActionPlugin'))).to
.be.true;
}).timeout(10000);
describe('CommonJS syntax', () => {
it('should load plugins that are the default export (module.exports = pluginObj)', async function () {
const loader = new PluginLoader([join(testPackagePath, 'default-export.js')]);
await loader.load();
expect(loader.pushPlugins.length).to.equal(1);
expect(loader.pushPlugins.every((p) => isCompatiblePlugin(p))).to.be.true;
expect(loader.pushPlugins.every((p) => isCompatiblePlugin(p, 'isGitProxyPushActionPlugin')))
.to.be.true;
}).timeout(10000);

it('should load multiple plugins from a module that match the plugin class (module.exports = { pluginFoo, pluginBar })', async function () {
const loader = new PluginLoader([join(testPackagePath, 'multiple-export.js')]);
await loader.load();
expect(loader.pushPlugins.length).to.equal(1);
expect(loader.pullPlugins.length).to.equal(1);
expect(loader.pushPlugins.every((p) => isCompatiblePlugin(p))).to.be.true;
expect(loader.pushPlugins.every((p) => isCompatiblePlugin(p, 'isGitProxyPushActionPlugin'))).to
.be.true;
expect(loader.pullPlugins.every((p) => isCompatiblePlugin(p, 'isGitProxyPullActionPlugin'))).to
.be.true;
}).timeout(10000);
it('should load multiple plugins from a module that match the plugin class (module.exports = { pluginFoo, pluginBar })', async function () {
const loader = new PluginLoader([join(testPackagePath, 'multiple-export.js')]);
await loader.load();

it('should load plugins that are subclassed from plugin classes', async function () {
const loader = new PluginLoader([join(testPackagePath, 'subclass.js')]);
await loader.load();
expect(loader.pushPlugins.length).to.equal(1);
expect(loader.pushPlugins.every((p) => isCompatiblePlugin(p))).to.be.true;
expect(loader.pushPlugins.every((p) => isCompatiblePlugin(p, 'isGitProxyPushActionPlugin'))).to
.be.true;
}).timeout(10000);
// Should load the foo and bar plugins, but not the baz object which isn't a plugin
expect(loader.pushPlugins.length).to.equal(1);
expect(loader.pullPlugins.length).to.equal(1);
expect(loader.pushPlugins.every((p) => isCompatiblePlugin(p))).to.be.true;
expect(loader.pushPlugins.every((p) => isCompatiblePlugin(p, 'isGitProxyPushActionPlugin')))
.to.be.true;
expect(loader.pullPlugins.every((p) => isCompatiblePlugin(p, 'isGitProxyPullActionPlugin')))
.to.be.true;
}).timeout(10000);

it('should load plugins that are subclassed from plugin classes', async function () {
const loader = new PluginLoader([join(testPackagePath, 'subclass.js')]);
await loader.load();
expect(loader.pushPlugins.length).to.equal(1);
expect(loader.pushPlugins.every((p) => isCompatiblePlugin(p))).to.be.true;
expect(loader.pushPlugins.every((p) => isCompatiblePlugin(p, 'isGitProxyPushActionPlugin')))
.to.be.true;
}).timeout(10000);
});

describe('ESM syntax', () => {
it('should load plugins that are the default export (exports default pluginObj)', async function () {
const loader = new PluginLoader([join(testPackagePath, 'esm-export.js')]);
await loader.load();
expect(loader.pushPlugins.length).to.equal(1);
expect(loader.pushPlugins.every((p) => isCompatiblePlugin(p))).to.be.true;
expect(loader.pushPlugins.every((p) => isCompatiblePlugin(p, 'isGitProxyPushActionPlugin')))
.to.be.true;
}).timeout(10000);

it('should load multiple plugins from a module that match the plugin class (exports default { pluginFoo, pluginBar })', async function () {
const loader = new PluginLoader([join(testPackagePath, 'esm-multiple-export.js')]);
await loader.load();

// Should load the foo and bar plugins, but not the baz object which isn't a plugin
expect(loader.pushPlugins.length).to.equal(1);
expect(loader.pullPlugins.length).to.equal(1);
expect(loader.pushPlugins.every((p) => isCompatiblePlugin(p))).to.be.true;
expect(loader.pushPlugins.every((p) => isCompatiblePlugin(p, 'isGitProxyPushActionPlugin')))
.to.be.true;
expect(loader.pullPlugins.every((p) => isCompatiblePlugin(p, 'isGitProxyPullActionPlugin')))
.to.be.true;
}).timeout(10000);

it('should load plugins that are subclassed from plugin classes (exports default class DummyPlugin extends PushActionPlugin {})', async function () {
const loader = new PluginLoader([join(testPackagePath, 'esm-subclass.js')]);
await loader.load();
expect(loader.pushPlugins.length).to.equal(1);
expect(loader.pushPlugins.every((p) => isCompatiblePlugin(p))).to.be.true;
expect(loader.pushPlugins.every((p) => isCompatiblePlugin(p, 'isGitProxyPushActionPlugin')))
.to.be.true;
}).timeout(10000);
});

it('should not load plugins that are not valid modules', async function () {
const loader = new PluginLoader([join(__dirname, './dummy.js')]);
Expand Down
Loading