Skip to content

Commit 93704e6

Browse files
authored
Merge pull request #1266 from jescalada/esm-plugin-tests
test: add tests for plugins written in ESM
2 parents 97b6127 + eeb05f2 commit 93704e6

File tree

5 files changed

+110
-33
lines changed

5 files changed

+110
-33
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { PushActionPlugin } from '@finos/git-proxy/plugin';
2+
3+
// test default export (ESM syntax)
4+
export default new PushActionPlugin(async (req, action) => {
5+
console.log('Dummy plugin: ', action);
6+
return action;
7+
});
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { PushActionPlugin, PullActionPlugin } from '@finos/git-proxy/plugin';
2+
3+
// test multiple exports (ESM syntax)
4+
export default {
5+
foo: new PushActionPlugin(async (req, action) => {
6+
console.log('PushActionPlugin: ', action);
7+
return action;
8+
}),
9+
bar: new PullActionPlugin(async (req, action) => {
10+
console.log('PullActionPlugin: ', action);
11+
return action;
12+
}),
13+
baz: {
14+
exec: async (req, action) => {
15+
console.log('not a real plugin object');
16+
},
17+
},
18+
};
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { PushActionPlugin } from '@finos/git-proxy/plugin';
2+
3+
class DummyPlugin extends PushActionPlugin {
4+
constructor(exec) {
5+
super();
6+
this.exec = exec;
7+
}
8+
}
9+
10+
// test default export (ESM syntax)
11+
export default new DummyPlugin(async (req, action) => {
12+
console.log('Dummy plugin: ', action);
13+
return action;
14+
});

test/fixtures/test-package/multiple-export.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,9 @@ module.exports = {
99
console.log('PullActionPlugin: ', action);
1010
return action;
1111
}),
12+
baz: {
13+
exec: async (req, action) => {
14+
console.log('not a real plugin object');
15+
},
16+
},
1217
};

test/plugin/plugin.test.js

Lines changed: 66 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,7 @@ import chai from 'chai';
22
import { spawnSync } from 'child_process';
33
import { rmSync } from 'fs';
44
import { join } from 'path';
5-
import {
6-
isCompatiblePlugin,
7-
PullActionPlugin,
8-
PushActionPlugin,
9-
PluginLoader,
10-
} from '../../src/plugin.ts';
5+
import { isCompatiblePlugin, PushActionPlugin, PluginLoader } from '../../src/plugin.ts';
116

127
chai.should();
138

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

25-
it('should load plugins that are the default export (module.exports = pluginObj)', async function () {
26-
const loader = new PluginLoader([join(testPackagePath, 'default-export.js')]);
27-
await loader.load();
28-
expect(loader.pushPlugins.length).to.equal(1);
29-
expect(loader.pushPlugins.every((p) => isCompatiblePlugin(p))).to.be.true;
30-
expect(loader.pushPlugins.every((p) => isCompatiblePlugin(p, 'isGitProxyPushActionPlugin'))).to
31-
.be.true;
32-
}).timeout(10000);
20+
describe('CommonJS syntax', () => {
21+
it('should load plugins that are the default export (module.exports = pluginObj)', async function () {
22+
const loader = new PluginLoader([join(testPackagePath, 'default-export.js')]);
23+
await loader.load();
24+
expect(loader.pushPlugins.length).to.equal(1);
25+
expect(loader.pushPlugins.every((p) => isCompatiblePlugin(p))).to.be.true;
26+
expect(loader.pushPlugins.every((p) => isCompatiblePlugin(p, 'isGitProxyPushActionPlugin')))
27+
.to.be.true;
28+
}).timeout(10000);
3329

34-
it('should load multiple plugins from a module that match the plugin class (module.exports = { pluginFoo, pluginBar })', async function () {
35-
const loader = new PluginLoader([join(testPackagePath, 'multiple-export.js')]);
36-
await loader.load();
37-
expect(loader.pushPlugins.length).to.equal(1);
38-
expect(loader.pullPlugins.length).to.equal(1);
39-
expect(loader.pushPlugins.every((p) => isCompatiblePlugin(p))).to.be.true;
40-
expect(loader.pushPlugins.every((p) => isCompatiblePlugin(p, 'isGitProxyPushActionPlugin'))).to
41-
.be.true;
42-
expect(loader.pullPlugins.every((p) => isCompatiblePlugin(p, 'isGitProxyPullActionPlugin'))).to
43-
.be.true;
44-
}).timeout(10000);
30+
it('should load multiple plugins from a module that match the plugin class (module.exports = { pluginFoo, pluginBar })', async function () {
31+
const loader = new PluginLoader([join(testPackagePath, 'multiple-export.js')]);
32+
await loader.load();
4533

46-
it('should load plugins that are subclassed from plugin classes', async function () {
47-
const loader = new PluginLoader([join(testPackagePath, 'subclass.js')]);
48-
await loader.load();
49-
expect(loader.pushPlugins.length).to.equal(1);
50-
expect(loader.pushPlugins.every((p) => isCompatiblePlugin(p))).to.be.true;
51-
expect(loader.pushPlugins.every((p) => isCompatiblePlugin(p, 'isGitProxyPushActionPlugin'))).to
52-
.be.true;
53-
}).timeout(10000);
34+
// Should load the foo and bar plugins, but not the baz object which isn't a plugin
35+
expect(loader.pushPlugins.length).to.equal(1);
36+
expect(loader.pullPlugins.length).to.equal(1);
37+
expect(loader.pushPlugins.every((p) => isCompatiblePlugin(p))).to.be.true;
38+
expect(loader.pushPlugins.every((p) => isCompatiblePlugin(p, 'isGitProxyPushActionPlugin')))
39+
.to.be.true;
40+
expect(loader.pullPlugins.every((p) => isCompatiblePlugin(p, 'isGitProxyPullActionPlugin')))
41+
.to.be.true;
42+
}).timeout(10000);
43+
44+
it('should load plugins that are subclassed from plugin classes', async function () {
45+
const loader = new PluginLoader([join(testPackagePath, 'subclass.js')]);
46+
await loader.load();
47+
expect(loader.pushPlugins.length).to.equal(1);
48+
expect(loader.pushPlugins.every((p) => isCompatiblePlugin(p))).to.be.true;
49+
expect(loader.pushPlugins.every((p) => isCompatiblePlugin(p, 'isGitProxyPushActionPlugin')))
50+
.to.be.true;
51+
}).timeout(10000);
52+
});
53+
54+
describe('ESM syntax', () => {
55+
it('should load plugins that are the default export (exports default pluginObj)', async function () {
56+
const loader = new PluginLoader([join(testPackagePath, 'esm-export.js')]);
57+
await loader.load();
58+
expect(loader.pushPlugins.length).to.equal(1);
59+
expect(loader.pushPlugins.every((p) => isCompatiblePlugin(p))).to.be.true;
60+
expect(loader.pushPlugins.every((p) => isCompatiblePlugin(p, 'isGitProxyPushActionPlugin')))
61+
.to.be.true;
62+
}).timeout(10000);
63+
64+
it('should load multiple plugins from a module that match the plugin class (exports default { pluginFoo, pluginBar })', async function () {
65+
const loader = new PluginLoader([join(testPackagePath, 'esm-multiple-export.js')]);
66+
await loader.load();
67+
68+
// Should load the foo and bar plugins, but not the baz object which isn't a plugin
69+
expect(loader.pushPlugins.length).to.equal(1);
70+
expect(loader.pullPlugins.length).to.equal(1);
71+
expect(loader.pushPlugins.every((p) => isCompatiblePlugin(p))).to.be.true;
72+
expect(loader.pushPlugins.every((p) => isCompatiblePlugin(p, 'isGitProxyPushActionPlugin')))
73+
.to.be.true;
74+
expect(loader.pullPlugins.every((p) => isCompatiblePlugin(p, 'isGitProxyPullActionPlugin')))
75+
.to.be.true;
76+
}).timeout(10000);
77+
78+
it('should load plugins that are subclassed from plugin classes (exports default class DummyPlugin extends PushActionPlugin {})', async function () {
79+
const loader = new PluginLoader([join(testPackagePath, 'esm-subclass.js')]);
80+
await loader.load();
81+
expect(loader.pushPlugins.length).to.equal(1);
82+
expect(loader.pushPlugins.every((p) => isCompatiblePlugin(p))).to.be.true;
83+
expect(loader.pushPlugins.every((p) => isCompatiblePlugin(p, 'isGitProxyPushActionPlugin')))
84+
.to.be.true;
85+
}).timeout(10000);
86+
});
5487

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

0 commit comments

Comments
 (0)