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