From 251f7bb62424d52d9eef307a161d28b3ddcc9d9f Mon Sep 17 00:00:00 2001 From: Sangeetha Krishnan Date: Thu, 20 Jul 2023 11:45:56 +0530 Subject: [PATCH] Error handling and refactoring pre-undeploy-event-reg hook implementation (#694) * Error handling and refactoring pre-undeploy-event-reg hook implementation * fix unit tests associated with events undeploy * check for pre-undeploy-events-reg in unit test --- src/commands/app/undeploy.js | 18 ++++++++---------- test/commands/app/undeploy.test.js | 16 ++++++++-------- 2 files changed, 16 insertions(+), 18 deletions(-) diff --git a/src/commands/app/undeploy.js b/src/commands/app/undeploy.js index 912cf413..1701140f 100644 --- a/src/commands/app/undeploy.js +++ b/src/commands/app/undeploy.js @@ -78,6 +78,14 @@ class Undeploy extends BaseCommand { // undeploy try { await runInProcess(config.hooks['pre-app-undeploy'], config) + if (flags['feature-event-hooks'] && flags.events) { + this.log('feature-event-hooks is enabled, running pre-undeploy-event-reg hook') + const hookResults = await this.config.runHook('pre-undeploy-event-reg', { appConfig: config }) + if (hookResults?.failures?.length > 0) { + // output should be "Error : : \n" for each failure + this.error(hookResults.failures.map(f => `${f.plugin.name} : ${f.error.message}`).join('\nError: '), { exit: 1 }) + } + } } catch (err) { this.log(err) } @@ -85,7 +93,6 @@ class Undeploy extends BaseCommand { if (flags.actions) { if (config.app.hasBackend) { try { - this.config.runHook('pre-undeploy-event-reg', { appConfig: config }) const script = await runInProcess(config.hooks['undeploy-actions'], config) if (!script) { await rtLib.undeployActions(config) @@ -117,16 +124,7 @@ class Undeploy extends BaseCommand { } try { - this.config.runHook('post-undeploy-event-reg', { appConfig: config }) await runInProcess(config.hooks['post-app-undeploy'], config) - if (flags['feature-event-hooks'] && flags.events) { - this.log('feature-event-hooks is enabled, running post-undeploy-event-reg hook') - const hookResults = await this.config.runHook('post-undeploy-event-reg', { appConfig: config }) - if (hookResults?.failures?.length > 0) { - // output should be "Error : : \n" for each failure - this.error(hookResults.failures.map(f => `${f.plugin.name} : ${f.error.message}`).join('\nError: '), { exit: 1 }) - } - } } catch (err) { this.log(err) } diff --git a/test/commands/app/undeploy.test.js b/test/commands/app/undeploy.test.js index 73eab710..28b755f5 100644 --- a/test/commands/app/undeploy.test.js +++ b/test/commands/app/undeploy.test.js @@ -410,7 +410,7 @@ describe('run', () => { command.argv = [] await command.run() expect(command.error).not.toHaveBeenCalled() - expect(runHook).not.toHaveBeenCalledWith('post-undeploy-event-reg') + expect(runHook).not.toHaveBeenCalledWith('pre-undeploy-event-reg') }) test('does NOT fire `event` hooks when events flag is false', async () => { @@ -420,25 +420,25 @@ describe('run', () => { command.argv = ['--feature-event-hooks', '--no-events'] await command.run() expect(command.error).not.toHaveBeenCalled() - expect(runHook).not.toHaveBeenCalledWith('post-undeploy-event-reg') + expect(runHook).not.toHaveBeenCalledWith('pre-undeploy-event-reg') }) test('DOES fire `event` hooks when feature flag IS enabled', async () => { const runHook = jest.fn() + .mockResolvedValue({ + successes: ['ok'], + failures: [] + }) command.config = { runHook } command.getAppExtConfigs.mockReturnValueOnce(createAppConfig(command.appConfig)) command.argv = ['--feature-event-hooks'] await command.run() expect(command.error).not.toHaveBeenCalled() - expect(runHook).toHaveBeenCalledWith('post-undeploy-event-reg', expect.any(Object)) + expect(runHook).toHaveBeenCalledWith('pre-undeploy-event-reg', expect.any(Object)) }) test('outputs error if events hook throws', async () => { const runHook = jest.fn() - .mockResolvedValueOnce({ - successes: ['ok'], - failures: [] - }) .mockResolvedValue({ successes: [], failures: [{ plugin: { name: 'ifailedu' }, error: 'some error' }] @@ -447,7 +447,7 @@ describe('run', () => { command.getAppExtConfigs.mockReturnValueOnce(createAppConfig(command.appConfig)) command.argv = ['--feature-event-hooks'] await command.run() - expect(runHook).toHaveBeenCalledWith('post-undeploy-event-reg', expect.any(Object)) + expect(runHook).toHaveBeenCalledWith('pre-undeploy-event-reg', expect.any(Object)) expect(command.error).toHaveBeenCalledTimes(1) }) })