|
1 |
| -import { App, CfnMapping, CfnOutput, CfnParameter, CfnResource, NestedStack, Stack } from '@aws-cdk/core'; |
| 1 | +import { App, CfnCondition, CfnMapping, CfnOutput, CfnParameter, CfnResource, Fn, NestedStack, Stack } from '@aws-cdk/core'; |
2 | 2 | import { Construct } from 'constructs';
|
3 | 3 | import { Capture, Match, Template } from '../lib';
|
4 | 4 |
|
@@ -940,6 +940,150 @@ describe('Template', () => {
|
940 | 940 | expect(Object.keys(result).length).toEqual(0);
|
941 | 941 | });
|
942 | 942 | });
|
| 943 | + |
| 944 | + describe('hasCondition', () => { |
| 945 | + test('matching', () => { |
| 946 | + const stack = new Stack(); |
| 947 | + new CfnCondition(stack, 'Foo', { |
| 948 | + expression: Fn.conditionEquals('Bar', 'Baz'), |
| 949 | + }); |
| 950 | + |
| 951 | + const inspect = Template.fromStack(stack); |
| 952 | + expect(() => inspect.hasCondition('*', { 'Fn::Equals': ['Bar', 'Baz'] })).not.toThrow(); |
| 953 | + }); |
| 954 | + |
| 955 | + test('not matching', (done) => { |
| 956 | + const stack = new Stack(); |
| 957 | + new CfnCondition(stack, 'Foo', { |
| 958 | + expression: Fn.conditionEquals('Bar', 'Baz'), |
| 959 | + }); |
| 960 | + |
| 961 | + new CfnCondition(stack, 'Qux', { |
| 962 | + expression: Fn.conditionNot(Fn.conditionEquals('Quux', 'Quuz')), |
| 963 | + }); |
| 964 | + |
| 965 | + const inspect = Template.fromStack(stack); |
| 966 | + expectToThrow( |
| 967 | + () => inspect.hasCondition('*', { |
| 968 | + 'Fn::Equals': ['Baz', 'Bar'], |
| 969 | + }), |
| 970 | + [ |
| 971 | + /2 conditions/, |
| 972 | + /Missing key/, |
| 973 | + ], |
| 974 | + done, |
| 975 | + ); |
| 976 | + done(); |
| 977 | + }); |
| 978 | + |
| 979 | + test('matching specific outputName', () => { |
| 980 | + const stack = new Stack(); |
| 981 | + new CfnCondition(stack, 'Foo', { |
| 982 | + expression: Fn.conditionEquals('Bar', 'Baz'), |
| 983 | + }); |
| 984 | + |
| 985 | + const inspect = Template.fromStack(stack); |
| 986 | + expect(() => inspect.hasCondition('Foo', { 'Fn::Equals': ['Bar', 'Baz'] })).not.toThrow(); |
| 987 | + }); |
| 988 | + |
| 989 | + test('not matching specific outputName', (done) => { |
| 990 | + const stack = new Stack(); |
| 991 | + new CfnCondition(stack, 'Foo', { |
| 992 | + expression: Fn.conditionEquals('Baz', 'Bar'), |
| 993 | + }); |
| 994 | + |
| 995 | + const inspect = Template.fromStack(stack); |
| 996 | + expectToThrow( |
| 997 | + () => inspect.hasCondition('Foo', { |
| 998 | + 'Fn::Equals': ['Bar', 'Baz'], |
| 999 | + }), |
| 1000 | + [ |
| 1001 | + /1 conditions/, |
| 1002 | + /Expected Baz but received Bar/, |
| 1003 | + ], |
| 1004 | + done, |
| 1005 | + ); |
| 1006 | + done(); |
| 1007 | + }); |
| 1008 | + }); |
| 1009 | + |
| 1010 | + describe('findConditions', () => { |
| 1011 | + test('matching', () => { |
| 1012 | + const stack = new Stack(); |
| 1013 | + new CfnCondition(stack, 'Foo', { |
| 1014 | + expression: Fn.conditionEquals('Bar', 'Baz'), |
| 1015 | + }); |
| 1016 | + |
| 1017 | + new CfnCondition(stack, 'Qux', { |
| 1018 | + expression: Fn.conditionNot(Fn.conditionEquals('Quux', 'Quuz')), |
| 1019 | + }); |
| 1020 | + |
| 1021 | + const inspect = Template.fromStack(stack); |
| 1022 | + const firstCondition = inspect.findConditions('Foo'); |
| 1023 | + expect(firstCondition).toEqual({ |
| 1024 | + Foo: { |
| 1025 | + 'Fn::Equals': [ |
| 1026 | + 'Bar', |
| 1027 | + 'Baz', |
| 1028 | + ], |
| 1029 | + }, |
| 1030 | + }); |
| 1031 | + |
| 1032 | + const secondCondition = inspect.findConditions('Qux'); |
| 1033 | + expect(secondCondition).toEqual({ |
| 1034 | + Qux: { |
| 1035 | + 'Fn::Not': [ |
| 1036 | + { |
| 1037 | + 'Fn::Equals': [ |
| 1038 | + 'Quux', |
| 1039 | + 'Quuz', |
| 1040 | + ], |
| 1041 | + }, |
| 1042 | + ], |
| 1043 | + }, |
| 1044 | + }); |
| 1045 | + }); |
| 1046 | + |
| 1047 | + test('not matching', () => { |
| 1048 | + const stack = new Stack(); |
| 1049 | + new CfnCondition(stack, 'Foo', { |
| 1050 | + expression: Fn.conditionEquals('Bar', 'Baz'), |
| 1051 | + }); |
| 1052 | + |
| 1053 | + const inspect = Template.fromStack(stack); |
| 1054 | + const result = inspect.findMappings('Bar'); |
| 1055 | + expect(Object.keys(result).length).toEqual(0); |
| 1056 | + }); |
| 1057 | + |
| 1058 | + test('matching with specific outputName', () => { |
| 1059 | + const stack = new Stack(); |
| 1060 | + new CfnCondition(stack, 'Foo', { |
| 1061 | + expression: Fn.conditionEquals('Bar', 'Baz'), |
| 1062 | + }); |
| 1063 | + |
| 1064 | + const inspect = Template.fromStack(stack); |
| 1065 | + const result = inspect.findConditions('Foo', { 'Fn::Equals': ['Bar', 'Baz'] }); |
| 1066 | + expect(result).toEqual({ |
| 1067 | + Foo: { |
| 1068 | + 'Fn::Equals': [ |
| 1069 | + 'Bar', |
| 1070 | + 'Baz', |
| 1071 | + ], |
| 1072 | + }, |
| 1073 | + }); |
| 1074 | + }); |
| 1075 | + |
| 1076 | + test('not matching specific output name', () => { |
| 1077 | + const stack = new Stack(); |
| 1078 | + new CfnCondition(stack, 'Foo', { |
| 1079 | + expression: Fn.conditionEquals('Bar', 'Baz'), |
| 1080 | + }); |
| 1081 | + |
| 1082 | + const inspect = Template.fromStack(stack); |
| 1083 | + const result = inspect.findConditions('Foo', { 'Fn::Equals': ['Bar', 'Qux'] }); |
| 1084 | + expect(Object.keys(result).length).toEqual(0); |
| 1085 | + }); |
| 1086 | + }); |
943 | 1087 | });
|
944 | 1088 |
|
945 | 1089 | function expectToThrow(fn: () => void, msgs: (RegExp | string)[], done: jest.DoneCallback): void {
|
|
0 commit comments