diff options
author | Chocobozzz <me@florianbigard.com> | 2019-07-19 17:30:41 +0200 |
---|---|---|
committer | Chocobozzz <chocobozzz@cpy.re> | 2019-07-24 10:58:16 +0200 |
commit | 89cd12756035a146bbcc4db78cd3cd64f2f3d88d (patch) | |
tree | 896cd9fca1e0baa969b1f7a5b398ec1602761661 /shared | |
parent | 09071200c73f5358e1d0bfb61a274e4f2c4ec52b (diff) | |
download | PeerTube-89cd12756035a146bbcc4db78cd3cd64f2f3d88d.tar.gz PeerTube-89cd12756035a146bbcc4db78cd3cd64f2f3d88d.tar.zst PeerTube-89cd12756035a146bbcc4db78cd3cd64f2f3d88d.zip |
Add hook filters tests
Diffstat (limited to 'shared')
-rw-r--r-- | shared/core-utils/plugins/hooks.ts | 35 | ||||
-rw-r--r-- | shared/extra-utils/server/plugins.ts | 7 | ||||
-rw-r--r-- | shared/extra-utils/server/servers.ts | 3 |
3 files changed, 28 insertions, 17 deletions
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) { | |||
8 | return HookType.STATIC | 8 | return HookType.STATIC |
9 | } | 9 | } |
10 | 10 | ||
11 | async function internalRunHook (handler: Function, hookType: HookType, param: any, onError: (err: Error) => void) { | 11 | async function internalRunHook <T>(handler: Function, hookType: HookType, result: T, params: any, onError: (err: Error) => void) { |
12 | let result = param | ||
13 | |||
14 | try { | 12 | try { |
15 | const p = handler(result) | 13 | if (hookType === HookType.FILTER) { |
14 | const p = handler(result, params) | ||
15 | |||
16 | if (isPromise(p)) result = await p | ||
17 | else result = p | ||
18 | |||
19 | return result | ||
20 | } | ||
16 | 21 | ||
17 | switch (hookType) { | 22 | // Action/static hooks do not have result value |
18 | case HookType.FILTER: | 23 | const p = handler(params) |
19 | if (isPromise(p)) result = await p | 24 | |
20 | else result = p | 25 | if (hookType === HookType.STATIC) { |
21 | break | 26 | if (isPromise(p)) await p |
27 | |||
28 | return undefined | ||
29 | } | ||
22 | 30 | ||
23 | case HookType.STATIC: | 31 | if (hookType === HookType.ACTION) { |
24 | if (isPromise(p)) await p | 32 | if (isCatchable(p)) p.catch(err => onError(err)) |
25 | break | ||
26 | 33 | ||
27 | case HookType.ACTION: | 34 | return undefined |
28 | if (isCatchable(p)) p.catch(err => onError(err)) | ||
29 | break | ||
30 | } | 35 | } |
31 | } catch (err) { | 36 | } catch (err) { |
32 | onError(err) | 37 | 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) { | |||
201 | return readJSON(path) | 201 | return readJSON(path) |
202 | } | 202 | } |
203 | 203 | ||
204 | function getPluginTestPath (suffix = '') { | ||
205 | return join(root(), 'server', 'tests', 'fixtures', 'peertube-plugin-test' + suffix) | ||
206 | } | ||
207 | |||
204 | export { | 208 | export { |
205 | listPlugins, | 209 | listPlugins, |
206 | listAvailablePlugins, | 210 | listAvailablePlugins, |
@@ -213,5 +217,6 @@ export { | |||
213 | getPluginRegisteredSettings, | 217 | getPluginRegisteredSettings, |
214 | getPackageJSONPath, | 218 | getPackageJSONPath, |
215 | updatePluginPackageJSON, | 219 | updatePluginPackageJSON, |
216 | getPluginPackageJSON | 220 | getPluginPackageJSON, |
221 | getPluginTestPath | ||
217 | } | 222 | } |
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 = [] | |||
171 | thumbnails: `test${server.internalServerNumber}/thumbnails/`, | 171 | thumbnails: `test${server.internalServerNumber}/thumbnails/`, |
172 | torrents: `test${server.internalServerNumber}/torrents/`, | 172 | torrents: `test${server.internalServerNumber}/torrents/`, |
173 | captions: `test${server.internalServerNumber}/captions/`, | 173 | captions: `test${server.internalServerNumber}/captions/`, |
174 | cache: `test${server.internalServerNumber}/cache/` | 174 | cache: `test${server.internalServerNumber}/cache/`, |
175 | plugins: `test${server.internalServerNumber}/plugins/` | ||
175 | }, | 176 | }, |
176 | admin: { | 177 | admin: { |
177 | email: `admin${server.internalServerNumber}@example.com` | 178 | email: `admin${server.internalServerNumber}@example.com` |