mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-17 23:33:12 +09:00
Validate plugin entrypoint
Check that Master/Instance/Control plugins export the correct class and that this class is a subclass of the correct base class. This gives a better user experience when plugins are incorrectly set up and loads a duplicated clusterio/lib, and when accidentally using the wrong name when developing plugins.
This commit is contained in:
1 parent
6d7a2d46d6
commit
033de0e20a
2 files changed
+61
-6
No files matched your search
@@ -64,4 +64,45 @@ describe("lib/plugin_loader", function() {
|
||||
);
|
||||
});
|
||||
});
|
||||
describe("loadMasterPluginClass()", function() {
|
||||
let baseDir = path.join("temp", "test", "plugin");
|
||||
let missingClass = path.join(baseDir, "missing_class_plugin");
|
||||
let wrongParentClass = path.join(baseDir, "wrong_parent_class_plugin");
|
||||
before(async function() {
|
||||
async function writeEntrypoint(pluginPath, content) {
|
||||
await fs.outputFile(path.join(pluginPath, "master.js"), content);
|
||||
}
|
||||
|
||||
await writeEntrypoint(missingClass, "");
|
||||
await writeEntrypoint(wrongParentClass, "class MasterPlugin {}\n module.exports = { MasterPlugin };\n");
|
||||
});
|
||||
it("should throw if class is missing from entrypoint", async function() {
|
||||
await assert.rejects(
|
||||
libPluginLoader.loadMasterPluginClass({
|
||||
name: "test",
|
||||
masterEntrypoint: "master",
|
||||
requirePath: path.resolve(missingClass),
|
||||
}),
|
||||
{
|
||||
message:
|
||||
`PluginError: Expected ${path.resolve(missingClass, "master")} ` +
|
||||
"to export a class named MasterPlugin",
|
||||
}
|
||||
);
|
||||
});
|
||||
it("should throw if class is not a subclass of BaseMasterPlugin", async function() {
|
||||
await assert.rejects(
|
||||
libPluginLoader.loadMasterPluginClass({
|
||||
name: "test",
|
||||
masterEntrypoint: "master",
|
||||
requirePath: path.resolve(wrongParentClass),
|
||||
}),
|
||||
{
|
||||
message:
|
||||
"PluginError: Expected MasterPlugin exported from " +
|
||||
`${path.resolve(wrongParentClass, "master")} to be a subclass of BaseMasterPlugin`,
|
||||
}
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user