feat(plugin.test): plugins can be tested (#446)

See vatesfr/xo-web#1749
This commit is contained in:
badrAZ
2016-11-09 14:58:19 +01:00
committed by Julien Fontanet
parent 6383104796
commit 77ce2ff6d1
3 changed files with 64 additions and 4 deletions

View File

@@ -102,3 +102,22 @@ purgeConfiguration.params = {
}
purgeConfiguration.permission = 'admin'
// ---------------------------------------------------------------------
export async function test ({ id, data }) {
await this.testPlugin(id, data)
}
test.description = 'Test a plugin with its current configuration'
test.params = {
id: {
type: 'string'
},
data: {}
}
test.permission = 'admin'
// ---------------------------------------------------------------------

View File

@@ -20,7 +20,7 @@ export default {
},
unloadable: {
type: 'boolean',
default: 'true',
default: true,
description: 'whether or not this plugin can be unloaded'
},
configuration: {
@@ -30,6 +30,14 @@ export default {
configurationSchema: {
$ref: 'http://json-schema.org/draft-04/schema#',
description: 'configuration schema for this plugin (not present if not configurable)'
},
testable: {
type: 'boolean',
description: 'whether or not this plugin can be tested'
},
testSchema: {
$ref: 'http://json-schema.org/draft-04/schema#',
description: 'test schema for this plugin'
}
},
required: [

View File

@@ -51,17 +51,20 @@ export default class {
name,
instance,
configurationSchema,
testSchema,
configurationPresets,
version
) {
const id = name
const plugin = this._plugins[id] = {
configured: !configurationSchema,
configurationPresets,
configurationSchema,
configured: !configurationSchema,
id,
instance,
name,
testable: isFunction(instance.test),
testSchema,
unloadable: isFunction(instance.unload),
version
}
@@ -69,7 +72,6 @@ export default class {
const metadata = await this._getPluginMetadata(id)
let autoload = true
let configuration
if (metadata) {
({
autoload,
@@ -108,6 +110,8 @@ export default class {
configurationSchema,
loaded,
name,
testable,
testSchema,
unloadable,
version
} = this._getRawPlugin(id)
@@ -125,7 +129,9 @@ export default class {
version,
configuration,
configurationPresets,
configurationSchema
configurationSchema,
testable,
testSchema
}
}
@@ -220,4 +226,31 @@ export default class {
async purgePluginConfiguration (id) {
await this._pluginsMetadata.merge(id, { configuration: undefined })
}
async testPlugin (id, data) {
const plugin = this._getRawPlugin(id)
if (!plugin.testable) {
throw invalidParameters('plugin not testable')
}
if (!plugin.loaded) {
throw invalidParameters('plugin not loaded')
}
const { testSchema } = plugin
if (testSchema) {
if (data == null) {
throw invalidParameters([{
field: 'data',
message: 'is the wrong type'
}])
}
const validate = createJsonSchemaValidator(testSchema)
if (!validate(data)) {
throw invalidParameters(validate.errors)
}
}
await plugin.instance.test(data)
}
}