|
| 1 | +import { Stack, Stage } from '@aws-cdk/core'; |
| 2 | +import { SynthesisMessage } from '@aws-cdk/cx-api'; |
| 3 | +import { Messages } from './private/message'; |
| 4 | +import { findMessage, hasMessage } from './private/messages'; |
| 5 | + |
| 6 | +/** |
| 7 | + * Suite of assertions that can be run on a CDK Stack. |
| 8 | + * Focused on asserting annotations. |
| 9 | + */ |
| 10 | +export class Annotations { |
| 11 | + /** |
| 12 | + * Base your assertions on the messages returned by a synthesized CDK `Stack`. |
| 13 | + * @param stack the CDK Stack to run assertions on |
| 14 | + */ |
| 15 | + public static fromStack(stack: Stack): Annotations { |
| 16 | + return new Annotations(toMessages(stack)); |
| 17 | + } |
| 18 | + |
| 19 | + private readonly _messages: Messages; |
| 20 | + |
| 21 | + private constructor(messages: SynthesisMessage[]) { |
| 22 | + this._messages = convertArrayToMessagesType(messages); |
| 23 | + } |
| 24 | + |
| 25 | + /** |
| 26 | + * Assert that an error with the given message exists in the synthesized CDK `Stack`. |
| 27 | + * |
| 28 | + * @param constructPath the construct path to the error. Provide `'*'` to match all errors in the template. |
| 29 | + * @param message the error message as should be expected. This should be a string or Matcher object. |
| 30 | + */ |
| 31 | + public hasError(constructPath: string, message: any): void { |
| 32 | + const matchError = hasMessage(this._messages, constructPath, constructMessage('error', message)); |
| 33 | + if (matchError) { |
| 34 | + throw new Error(matchError); |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * Get the set of matching errors of a given construct path and message. |
| 40 | + * |
| 41 | + * @param constructPath the construct path to the error. Provide `'*'` to match all errors in the template. |
| 42 | + * @param message the error message as should be expected. This should be a string or Matcher object. |
| 43 | + */ |
| 44 | + public findError(constructPath: string, message: any): SynthesisMessage[] { |
| 45 | + return convertMessagesTypeToArray(findMessage(this._messages, constructPath, constructMessage('error', message)) as Messages); |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * Assert that an warning with the given message exists in the synthesized CDK `Stack`. |
| 50 | + * |
| 51 | + * @param constructPath the construct path to the warning. Provide `'*'` to match all warnings in the template. |
| 52 | + * @param message the warning message as should be expected. This should be a string or Matcher object. |
| 53 | + */ |
| 54 | + public hasWarning(constructPath: string, message: any): void { |
| 55 | + const matchError = hasMessage(this._messages, constructPath, constructMessage('warning', message)); |
| 56 | + if (matchError) { |
| 57 | + throw new Error(matchError); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Get the set of matching warning of a given construct path and message. |
| 63 | + * |
| 64 | + * @param constructPath the construct path to the warning. Provide `'*'` to match all warnings in the template. |
| 65 | + * @param message the warning message as should be expected. This should be a string or Matcher object. |
| 66 | + */ |
| 67 | + public findWarning(constructPath: string, message: any): SynthesisMessage[] { |
| 68 | + return convertMessagesTypeToArray(findMessage(this._messages, constructPath, constructMessage('warning', message)) as Messages); |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Assert that an info with the given message exists in the synthesized CDK `Stack`. |
| 73 | + * |
| 74 | + * @param constructPath the construct path to the info. Provide `'*'` to match all info in the template. |
| 75 | + * @param message the info message as should be expected. This should be a string or Matcher object. |
| 76 | + */ |
| 77 | + public hasInfo(constructPath: string, message: any): void { |
| 78 | + const matchError = hasMessage(this._messages, constructPath, constructMessage('info', message)); |
| 79 | + if (matchError) { |
| 80 | + throw new Error(matchError); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Get the set of matching infos of a given construct path and message. |
| 86 | + * |
| 87 | + * @param constructPath the construct path to the info. Provide `'*'` to match all infos in the template. |
| 88 | + * @param message the info message as should be expected. This should be a string or Matcher object. |
| 89 | + */ |
| 90 | + public findInfo(constructPath: string, message: any): SynthesisMessage[] { |
| 91 | + return convertMessagesTypeToArray(findMessage(this._messages, constructPath, constructMessage('info', message)) as Messages); |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +function constructMessage(type: 'info' | 'warning' | 'error', message: any): {[key:string]: any } { |
| 96 | + return { |
| 97 | + level: type, |
| 98 | + entry: { |
| 99 | + data: message, |
| 100 | + }, |
| 101 | + }; |
| 102 | +} |
| 103 | + |
| 104 | +function convertArrayToMessagesType(messages: SynthesisMessage[]): Messages { |
| 105 | + return messages.reduce((obj, item) => { |
| 106 | + return { |
| 107 | + ...obj, |
| 108 | + [item.id]: item, |
| 109 | + }; |
| 110 | + }, {}) as Messages; |
| 111 | +} |
| 112 | + |
| 113 | +function convertMessagesTypeToArray(messages: Messages): SynthesisMessage[] { |
| 114 | + return Object.values(messages) as SynthesisMessage[]; |
| 115 | +} |
| 116 | + |
| 117 | +function toMessages(stack: Stack): any { |
| 118 | + const root = stack.node.root; |
| 119 | + if (!Stage.isStage(root)) { |
| 120 | + throw new Error('unexpected: all stacks must be part of a Stage or an App'); |
| 121 | + } |
| 122 | + |
| 123 | + // to support incremental assertions (i.e. "expect(stack).toNotContainSomething(); doSomething(); expect(stack).toContainSomthing()") |
| 124 | + const force = true; |
| 125 | + |
| 126 | + const assembly = root.synth({ force }); |
| 127 | + |
| 128 | + return assembly.getStackArtifact(stack.artifactId).messages; |
| 129 | +} |
0 commit comments