From 89cd12756035a146bbcc4db78cd3cd64f2f3d88d Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Fri, 19 Jul 2019 17:30:41 +0200 Subject: Add hook filters tests --- shared/core-utils/plugins/hooks.ts | 35 ++++++++++++++++++++--------------- shared/extra-utils/server/plugins.ts | 7 ++++++- shared/extra-utils/server/servers.ts | 3 ++- 3 files changed, 28 insertions(+), 17 deletions(-) (limited to 'shared') diff --git a/shared/core-utils/plugins/hooks.ts b/shared/core-utils/plugins/hooks.ts index 047c04f7b..60f4130f5 100644 --- a/shared/core-utils/plugins/hooks.ts +++ b/shared/core-utils/plugins/hooks.ts @@ -8,25 +8,30 @@ function getHookType (hookName: string) { return HookType.STATIC } -async function internalRunHook (handler: Function, hookType: HookType, param: any, onError: (err: Error) => void) { - let result = param - +async function internalRunHook (handler: Function, hookType: HookType, result: T, params: any, onError: (err: Error) => void) { try { - const p = handler(result) + if (hookType === HookType.FILTER) { + const p = handler(result, params) + + if (isPromise(p)) result = await p + else result = p + + return result + } - switch (hookType) { - case HookType.FILTER: - if (isPromise(p)) result = await p - else result = p - break + // Action/static hooks do not have result value + const p = handler(params) + + if (hookType === HookType.STATIC) { + if (isPromise(p)) await p + + return undefined + } - case HookType.STATIC: - if (isPromise(p)) await p - break + if (hookType === HookType.ACTION) { + if (isCatchable(p)) p.catch(err => onError(err)) - case HookType.ACTION: - if (isCatchable(p)) p.catch(err => onError(err)) - break + return undefined } } catch (err) { onError(err) diff --git a/shared/extra-utils/server/plugins.ts b/shared/extra-utils/server/plugins.ts index 7a5c5344b..2302208a8 100644 --- a/shared/extra-utils/server/plugins.ts +++ b/shared/extra-utils/server/plugins.ts @@ -201,6 +201,10 @@ function getPluginPackageJSON (server: ServerInfo, npmName: string) { return readJSON(path) } +function getPluginTestPath (suffix = '') { + return join(root(), 'server', 'tests', 'fixtures', 'peertube-plugin-test' + suffix) +} + export { listPlugins, listAvailablePlugins, @@ -213,5 +217,6 @@ export { getPluginRegisteredSettings, getPackageJSONPath, updatePluginPackageJSON, - getPluginPackageJSON + getPluginPackageJSON, + getPluginTestPath } diff --git a/shared/extra-utils/server/servers.ts b/shared/extra-utils/server/servers.ts index 9167ebe5b..40cf7f0f3 100644 --- a/shared/extra-utils/server/servers.ts +++ b/shared/extra-utils/server/servers.ts @@ -171,7 +171,8 @@ async function runServer (server: ServerInfo, configOverrideArg?: any, args = [] thumbnails: `test${server.internalServerNumber}/thumbnails/`, torrents: `test${server.internalServerNumber}/torrents/`, captions: `test${server.internalServerNumber}/captions/`, - cache: `test${server.internalServerNumber}/cache/` + cache: `test${server.internalServerNumber}/cache/`, + plugins: `test${server.internalServerNumber}/plugins/` }, admin: { email: `admin${server.internalServerNumber}@example.com` -- cgit v1.2.3