@contract-case/case-core
Version:
Core functionality for the ContractCase contract testing suite
83 lines • 5.85 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.loadPlugin = void 0;
const case_plugin_base_1 = require("@contract-case/case-plugin-base");
const versionString_1 = require("../../entities/versionString");
const MatcherExecutors_1 = require("../matching/MatcherExecutors");
const mockTypeToPluginHumanName = {};
const loadedPluginVersions = {};
const isCorePlugin = (plugin) => plugin.description.uniqueMachineName.startsWith(case_plugin_base_1.CORE_PLUGIN_PREFIX);
const isCoreType = (type) => type.startsWith('_case:');
const IN_PROGRESS = 'LOAD_IN_PROGRESS';
const loadPlugin = (MockExecutors, context, plugin) => {
const { description } = plugin;
if (description.version === IN_PROGRESS) {
throw new case_plugin_base_1.CaseConfigurationError(`The plugin '${description.humanReadableName}' reported its version to be LOAD_IN_PROGRESS, which is not valid. Contact the plugin authors to fix this.`, 'DONT_ADD_LOCATION', 'UNDOCUMENTED');
}
if (loadedPluginVersions[description.uniqueMachineName] != null) {
if (plugin.description.version !==
loadedPluginVersions[description.uniqueMachineName]) {
throw new case_plugin_base_1.CaseConfigurationError(`Trying to load plugin '${description.humanReadableName}' at version '${description.version}', but it was previously loaded as version '${loadedPluginVersions[description.uniqueMachineName]}'.`, 'DONT_ADD_LOCATION', 'UNDOCUMENTED');
}
context.logger.deepMaintainerDebug(`Plugin '${description.humanReadableName}' at version '${description.version}' has been previously loaded, skipping`);
return;
}
// We record this at the start, as otherwise failed plugins cause errors every time
loadedPluginVersions[description.uniqueMachineName] =
plugin.description.version;
if (isCorePlugin(plugin)) {
if (plugin.description.version !== versionString_1.caseVersion) {
throw new case_plugin_base_1.CaseCoreError(`Core plugin '${description.humanReadableName}' is at version
'${description.version}', but this is Core version ${versionString_1.caseVersion}.
These versions are supposed to match, and the ContractCase build process is supposed to prevent this happening.`);
}
context.logger.deepMaintainerDebug(`Loading core plugin '${description.humanReadableName}'`, description);
}
else {
context.logger.debug(`Loading plugin '${description.humanReadableName}' version ${description.version}`);
}
Object.entries(plugin.setupMocks).forEach(([mockType, setup]) => {
if (mockType in MockExecutors) {
throw new case_plugin_base_1.CaseConfigurationError(`Plugin '${description.humanReadableName}' @ ${plugin.description.version}
attempted to load a mock setup function for '${mockType}',
but one had already been loaded by plugin '${mockTypeToPluginHumanName[mockType]}'.`, 'DONT_ADD_LOCATION', 'UNDOCUMENTED');
}
if (isCorePlugin(plugin)) {
if (!isCoreType(mockType)) {
throw new case_plugin_base_1.CaseCoreError(`Core plugin '${description.humanReadableName}' @ ${plugin.description.version}' tried to load a non-core mock, '${mockType}'`);
}
context.logger.deepMaintainerDebug(`Core plugin '${description.humanReadableName}' @ ${plugin.description.version}' registered a mock setup function with type '${mockType}'`);
}
else {
if (isCoreType(mockType)) {
throw new case_plugin_base_1.CaseConfigurationError(`Non-core plugin '${description.humanReadableName}' @ ${plugin.description.version} tried to load a core mock, '${mockType}'. This is an error in the plugin definition, please contact the plugin's authors`, 'DONT_ADD_LOCATION', 'UNDOCUMENTED');
}
context.logger.debug(`Plugin '${description.humanReadableName}' @ ${plugin.description.version} registered a mock setup function with type '${mockType}'`);
}
// We cheat the type system here - we can't actually
// do any type checking so we just tell typescript it's any known type.
// Additionally, we disable param-reassign, because here we intend to do it
// eslint-disable-next-line no-param-reassign
MockExecutors[mockType] = setup;
mockTypeToPluginHumanName[mockType] = description.humanReadableName;
});
Object.entries(plugin.matcherExecutors).forEach(([pluginExecutorType, pluginExecutor]) => {
if (pluginExecutorType in MatcherExecutors_1.MatcherExecutors) {
throw new case_plugin_base_1.CaseConfigurationError(`Plugin '${description.humanReadableName}' @ ${plugin.description.version} attempted to load a matcher executor for '${pluginExecutorType}', but one had already been loaded by plugin '${mockTypeToPluginHumanName[pluginExecutorType]}'.`, 'DONT_ADD_LOCATION', 'UNDOCUMENTED');
}
if (pluginExecutorType.startsWith(`_case:`)) {
context.logger.deepMaintainerDebug(`Core plugin '${description.humanReadableName}' @ ${plugin.description.version} registered a matcher executor with type '${pluginExecutorType}'`);
}
else {
context.logger.debug(`Plugin '${description.humanReadableName}' @ ${plugin.description.version} registered a matcher executor with type '${pluginExecutorType}'`);
}
// We cheat the type system here - we can't actually
// do any type checking so we just tell typescript it's any known type.
MatcherExecutors_1.MatcherExecutors[pluginExecutorType] =
pluginExecutor;
});
loadedPluginVersions[plugin.description.uniqueMachineName] =
plugin.description.version;
};
exports.loadPlugin = loadPlugin;
//# sourceMappingURL=loadPlugin.js.map