Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hook errors#665 #666

Merged
merged 5 commits into from
May 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/TemplatesCommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,8 @@ class TemplatesCommand extends AddCommand {

if (templates.length <= 0) {
aioLogger.debug('installTemplates: standalone-app')
// technically runHook can quietly fail, but we are choosing to ignore it as these are telemetry events
// and not mission critical
await this.config.runHook('telemetry', { data: 'installTemplates:standalone-app' })
} else {
aioLogger.debug(`installTemplates: ${templates}`)
Expand Down
18 changes: 15 additions & 3 deletions src/commands/app/deploy.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,11 @@ class Deploy extends BuildCommand {

if (flags['feature-event-hooks']) {
this.log('feature-event-hooks is enabled, running pre-deploy-event-reg hook')
await this.config.runHook('pre-deploy-event-reg', { appConfig: config })
const hookResults = await this.config.runHook('pre-deploy-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 })
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want to extract this into a function in the class? Since it is duplicated thrice

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

its not exactly the same all 3 times
if we need to write it 5 times I'll consider it, or if it expands into more than 1 line.

}
}
} catch (err) {
this.log(err)
Expand All @@ -163,11 +167,15 @@ class Deploy extends BuildCommand {
try {
const script = await runScript(config.hooks['deploy-actions'])
if (!script) {
await this.config.runHook('deploy-actions', {
const hookResults = await this.config.runHook('deploy-actions', {
appConfig: config,
filterEntities: filterActions || [],
isLocalDev: false
})
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 })
}
deployedRuntimeEntities = await rtLib.deployActions(config, { filterEntities }, onProgress)
}

Expand Down Expand Up @@ -246,7 +254,11 @@ class Deploy extends BuildCommand {
await runScript(config.hooks['post-app-deploy'])
if (flags['feature-event-hooks']) {
this.log('feature-event-hooks is enabled, running post-deploy-event-reg hook')
await this.config.runHook('post-deploy-event-reg', { appConfig: config })
const hookResults = await this.config.runHook('post-deploy-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
7 changes: 6 additions & 1 deletion src/lib/deploy-actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,16 @@ module.exports = async (config, isLocalDev = false, log = () => {}, filter = fal
}
if (inprocHook) {
const hookFilterEntities = Array.isArray(filter) ? filter : []
await inprocHook('deploy-actions', {
const hookResults = await inprocHook('deploy-actions', {
appConfig: config,
filterEntities: hookFilterEntities,
isLocalDev
})
if (hookResults?.failures?.length > 0) {
// output should be "Error : <plugin-name> : <error-message>\n" for each failure
log('Error: ' + hookResults.failures.map(f => `${f.plugin.name} : ${f.error.message}`).join('\nError: '))
throw new Error(`Hook 'deploy-actions' failed with ${hookResults.failures[0].error}`)
}
}
const entities = await deployActions(config, deployConfig, log)
if (entities.actions) {
Expand Down
57 changes: 57 additions & 0 deletions test/commands/app/deploy.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1053,4 +1053,61 @@ describe('run', () => {
expect(runHook).toHaveBeenCalledWith('pre-deploy-event-reg', expect.any(Object))
expect(runHook).toHaveBeenCalledWith('post-deploy-event-reg', expect.any(Object))
})

test('handles error and exits when first hook fails', async () => {
const runHook = jest.fn().mockResolvedValueOnce({
successes: [],
failures: [{ plugin: { name: 'ifailedu' }, error: 'some error' }]
})
command.config = { runHook }
command.getAppExtConfigs.mockReturnValueOnce(createAppConfig(command.appConfig))
command.argv = ['--feature-event-hooks']
await command.run()
expect(runHook).toHaveBeenCalledWith('pre-deploy-event-reg', expect.any(Object))
// technically, I think only the first hook should be called -jm
expect(runHook).toHaveBeenCalledWith('post-deploy-event-reg', expect.any(Object))
expect(command.error).toHaveBeenCalledTimes(1)
})

test('handles error and exits when second hook fails', async () => {
const runHook = jest.fn()
.mockResolvedValueOnce({
successes: [{ plugin: { name: 'imsuccess' }, result: 'some string' }],
failures: []
})
.mockResolvedValueOnce({
successes: [],
failures: [{ plugin: { name: 'ifailedu' }, error: 'some error' }]
})
command.config = { runHook }
command.getAppExtConfigs.mockReturnValueOnce(createAppConfig(command.appConfig))
command.argv = ['--feature-event-hooks']
await command.run()
expect(runHook).toHaveBeenCalledWith('pre-deploy-event-reg', expect.any(Object))
expect(runHook).toHaveBeenCalledWith('post-deploy-event-reg', expect.any(Object))
expect(command.error).toHaveBeenCalledTimes(1)
})

test('handles error and exits when third hook fails', async () => {
const runHook = jest.fn()
.mockResolvedValueOnce({
successes: [{ plugin: { name: 'imsuccess' }, result: 'some string' }],
failures: []
})
.mockResolvedValueOnce({
successes: [{ plugin: { name: 'imsuccess' }, result: 'some string' }],
failures: []
})
.mockResolvedValueOnce({
successes: [],
failures: [{ plugin: { name: 'ifailedu' }, error: 'some error' }]
})
command.config = { runHook }
command.getAppExtConfigs.mockReturnValueOnce(createAppConfig(command.appConfig))
command.argv = ['--feature-event-hooks']
await command.run()
expect(runHook).toHaveBeenCalledWith('pre-deploy-event-reg', expect.any(Object))
expect(runHook).toHaveBeenCalledWith('post-deploy-event-reg', expect.any(Object))
expect(command.error).toHaveBeenCalledTimes(1)
})
})
17 changes: 17 additions & 0 deletions test/commands/lib/deploy-actions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,23 @@ test('call inprocHook with filter : isLocalDev true', async () => {
expect(utils.runScript).toHaveBeenNthCalledWith(3, undefined) // post-app-deploy
})

test('throws if hook returns failures', async () => {
const mockHook = jest.fn().mockResolvedValueOnce({
successes: [],
failures: [{ plugin: { name: 'ifailedu' }, error: 'some error' }]
})
const mockLog = jest.fn()
await expect(deployActions({ hooks: {} }, true, mockLog, ['action-1', 'action-2'], mockHook)).rejects.toThrow('Hook \'deploy-actions\' failed with some error')
expect(mockHook).toHaveBeenCalledWith('deploy-actions', expect.objectContaining({
appConfig: { hooks: {} },
filterEntities: ['action-1', 'action-2'],
isLocalDev: true
}))
expect(rtDeployActions).not.toHaveBeenCalled()
expect(mockLog).toHaveBeenCalled()
expect(utils.runScript).toHaveBeenCalledTimes(2)
})

test('use default parameters (coverage)', async () => {
rtDeployActions.mockResolvedValue({
actions: [
Expand Down