Skip to content

Commit

Permalink
Error handling and refactoring pre-undeploy-event-reg hook implementa…
Browse files Browse the repository at this point in the history
…tion (#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
  • Loading branch information
sangeetha5491 committed Jul 20, 2023
1 parent b9cf971 commit 251f7bb
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 18 deletions.
18 changes: 8 additions & 10 deletions src/commands/app/undeploy.js
Expand Up @@ -78,14 +78,21 @@ 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 : <plugin-name> : <error-message>\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)
}

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)
Expand Down Expand Up @@ -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 : <plugin-name> : <error-message>\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)
}
Expand Down
16 changes: 8 additions & 8 deletions test/commands/app/undeploy.test.js
Expand Up @@ -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 () => {
Expand All @@ -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' }]
Expand All @@ -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)
})
})

0 comments on commit 251f7bb

Please sign in to comment.