ruby-cdk 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/CHANGELOG.md +87 -0
- data/LICENSE +21 -0
- data/README.md +303 -0
- data/THIRD_PARTY_NOTICES.md +327 -0
- data/data/spec/META.json +33 -0
- data/data/spec/model.json.gz +0 -0
- data/exe/ruby-cdk +9 -0
- data/lib/ruby-cdk.rb +4 -0
- data/lib/ruby_cdk/assets/builder.rb +122 -0
- data/lib/ruby_cdk/assets/hasher.rb +175 -0
- data/lib/ruby_cdk/assets/record.rb +49 -0
- data/lib/ruby_cdk/assets/uploader.rb +139 -0
- data/lib/ruby_cdk/assets/zipper.rb +108 -0
- data/lib/ruby_cdk/assets.rb +116 -0
- data/lib/ruby_cdk/bootstrap.rb +289 -0
- data/lib/ruby_cdk/cfn/resource.rb +93 -0
- data/lib/ruby_cdk/cfn.rb +10 -0
- data/lib/ruby_cdk/cli/arguments.rb +41 -0
- data/lib/ruby_cdk/cli/aws.rb +35 -0
- data/lib/ruby_cdk/cli/bootstrap.rb +63 -0
- data/lib/ruby_cdk/cli/diff.rb +98 -0
- data/lib/ruby_cdk/cli/failure.rb +80 -0
- data/lib/ruby_cdk/cli/io.rb +70 -0
- data/lib/ruby_cdk/cli/list.rb +46 -0
- data/lib/ruby_cdk/cli/new.rb +313 -0
- data/lib/ruby_cdk/cli/operation.rb +124 -0
- data/lib/ruby_cdk/cli/pairs.rb +32 -0
- data/lib/ruby_cdk/cli/report.rb +118 -0
- data/lib/ruby_cdk/cli/selection.rb +134 -0
- data/lib/ruby_cdk/cli/session.rb +290 -0
- data/lib/ruby_cdk/cli/shell.rb +30 -0
- data/lib/ruby_cdk/cli/synth.rb +58 -0
- data/lib/ruby_cdk/cli/validate.rb +101 -0
- data/lib/ruby_cdk/cli.rb +242 -0
- data/lib/ruby_cdk/constructs/bucket.rb +191 -0
- data/lib/ruby_cdk/constructs/function.rb +163 -0
- data/lib/ruby_cdk/constructs/grant.rb +90 -0
- data/lib/ruby_cdk/constructs/log_group.rb +115 -0
- data/lib/ruby_cdk/constructs/policy_statement.rb +161 -0
- data/lib/ruby_cdk/constructs/queue.rb +118 -0
- data/lib/ruby_cdk/constructs/role.rb +282 -0
- data/lib/ruby_cdk/constructs/table.rb +160 -0
- data/lib/ruby_cdk/constructs/topic.rb +88 -0
- data/lib/ruby_cdk/constructs.rb +56 -0
- data/lib/ruby_cdk/core/app.rb +146 -0
- data/lib/ruby_cdk/core/construct.rb +105 -0
- data/lib/ruby_cdk/core/elements.rb +107 -0
- data/lib/ruby_cdk/core/ids.rb +117 -0
- data/lib/ruby_cdk/core/stack.rb +291 -0
- data/lib/ruby_cdk/core/stateful.rb +156 -0
- data/lib/ruby_cdk/core.rb +15 -0
- data/lib/ruby_cdk/deploy/capabilities.rb +44 -0
- data/lib/ruby_cdk/deploy/deployer.rb +805 -0
- data/lib/ruby_cdk/deploy/errors.rb +72 -0
- data/lib/ruby_cdk/deploy/fakes.rb +546 -0
- data/lib/ruby_cdk/deploy/output/base.rb +167 -0
- data/lib/ruby_cdk/deploy/output/human.rb +37 -0
- data/lib/ruby_cdk/deploy/output/json.rb +80 -0
- data/lib/ruby_cdk/deploy/output/quiet.rb +20 -0
- data/lib/ruby_cdk/deploy/parameters.rb +86 -0
- data/lib/ruby_cdk/deploy/poller.rb +72 -0
- data/lib/ruby_cdk/deploy/ports.rb +205 -0
- data/lib/ruby_cdk/deploy/sdk_adapters.rb +608 -0
- data/lib/ruby_cdk/deploy/states.rb +62 -0
- data/lib/ruby_cdk/deploy.rb +25 -0
- data/lib/ruby_cdk/diff/compare.rb +141 -0
- data/lib/ruby_cdk/diff/normalizer.rb +83 -0
- data/lib/ruby_cdk/diff/render.rb +242 -0
- data/lib/ruby_cdk/diff/security.rb +322 -0
- data/lib/ruby_cdk/diff/statement.rb +256 -0
- data/lib/ruby_cdk/diff/template.rb +524 -0
- data/lib/ruby_cdk/diff.rb +62 -0
- data/lib/ruby_cdk/env.rb +42 -0
- data/lib/ruby_cdk/errors.rb +38 -0
- data/lib/ruby_cdk/fn.rb +409 -0
- data/lib/ruby_cdk/lazy.rb +55 -0
- data/lib/ruby_cdk/nag/config.rb +303 -0
- data/lib/ruby_cdk/nag/engine.rb +152 -0
- data/lib/ruby_cdk/nag/report.rb +86 -0
- data/lib/ruby_cdk/nag/rule.rb +171 -0
- data/lib/ruby_cdk/nag/rules/api_gateway_authorization.rb +91 -0
- data/lib/ruby_cdk/nag/rules/dynamodb_point_in_time_recovery.rb +45 -0
- data/lib/ruby_cdk/nag/rules/ebs_volume_encrypted.rb +41 -0
- data/lib/ruby_cdk/nag/rules/ec2_ingress_http.rb +48 -0
- data/lib/ruby_cdk/nag/rules/ec2_ingress_other_ports.rb +46 -0
- data/lib/ruby_cdk/nag/rules/iam_policy_admin_access.rb +60 -0
- data/lib/ruby_cdk/nag/rules/iam_policy_full_access.rb +55 -0
- data/lib/ruby_cdk/nag/rules/iam_user_credentials.rb +61 -0
- data/lib/ruby_cdk/nag/rules/lambda_runtime_supported.rb +88 -0
- data/lib/ruby_cdk/nag/rules/lambda_secret_environment.rb +61 -0
- data/lib/ruby_cdk/nag/rules/log_group_retention.rb +40 -0
- data/lib/ruby_cdk/nag/rules/policy_statements.rb +128 -0
- data/lib/ruby_cdk/nag/rules/rds_storage_encrypted.rb +65 -0
- data/lib/ruby_cdk/nag/rules/resource_policy_wildcard_principal.rb +58 -0
- data/lib/ruby_cdk/nag/rules/s3_public_access_block.rb +56 -0
- data/lib/ruby_cdk/nag/rules/security_group_ingress.rb +136 -0
- data/lib/ruby_cdk/nag/rules/stateful_deletion_policy.rb +46 -0
- data/lib/ruby_cdk/nag/rules.rb +51 -0
- data/lib/ruby_cdk/nag/values.rb +305 -0
- data/lib/ruby_cdk/nag.rb +60 -0
- data/lib/ruby_cdk/schema/normalizer.rb +164 -0
- data/lib/ruby_cdk/schema/property_schema.rb +22 -0
- data/lib/ruby_cdk/schema/schema.rb +195 -0
- data/lib/ruby_cdk/schema/sources/compiler.rb +56 -0
- data/lib/ruby_cdk/schema/sources/descriptor.rb +21 -0
- data/lib/ruby_cdk/schema/sources/download.rb +86 -0
- data/lib/ruby_cdk/schema/sources/provider_schemas.rb +47 -0
- data/lib/ruby_cdk/schema/sources/provider_type.rb +630 -0
- data/lib/ruby_cdk/schema/sources/regions.rb +86 -0
- data/lib/ruby_cdk/schema/sources/resource_specification.rb +94 -0
- data/lib/ruby_cdk/schema/sources/specification_type.rb +159 -0
- data/lib/ruby_cdk/schema/sources/update.rb +178 -0
- data/lib/ruby_cdk/schema/sources.rb +20 -0
- data/lib/ruby_cdk/schema/type_schema.rb +109 -0
- data/lib/ruby_cdk/schema/validator/corpus_check.rb +81 -0
- data/lib/ruby_cdk/schema/validator/policies.rb +48 -0
- data/lib/ruby_cdk/schema/validator/refs.rb +489 -0
- data/lib/ruby_cdk/schema/validator/report.rb +92 -0
- data/lib/ruby_cdk/schema/validator/rule.rb +119 -0
- data/lib/ruby_cdk/schema/validator/scans.rb +146 -0
- data/lib/ruby_cdk/schema/validator/structure.rb +469 -0
- data/lib/ruby_cdk/schema/validator/template.rb +203 -0
- data/lib/ruby_cdk/schema/validator/template_rules.rb +70 -0
- data/lib/ruby_cdk/schema/validator/walk.rb +122 -0
- data/lib/ruby_cdk/schema/validator.rb +159 -0
- data/lib/ruby_cdk/schema.rb +19 -0
- data/lib/ruby_cdk/synth/manifest.rb +128 -0
- data/lib/ruby_cdk/synth/resolver.rb +326 -0
- data/lib/ruby_cdk/synth/rules.rb +83 -0
- data/lib/ruby_cdk/synth/serializer.rb +120 -0
- data/lib/ruby_cdk/synth/writer.rb +31 -0
- data/lib/ruby_cdk/synth.rb +255 -0
- data/lib/ruby_cdk/test/capture.rb +133 -0
- data/lib/ruby_cdk/test/match.rb +426 -0
- data/lib/ruby_cdk/test/matcher.rb +290 -0
- data/lib/ruby_cdk/test/private/absent.rb +34 -0
- data/lib/ruby_cdk/test/private/conditions.rb +39 -0
- data/lib/ruby_cdk/test/private/cyclic.rb +160 -0
- data/lib/ruby_cdk/test/private/error.rb +48 -0
- data/lib/ruby_cdk/test/private/js.rb +326 -0
- data/lib/ruby_cdk/test/private/mappings.rb +39 -0
- data/lib/ruby_cdk/test/private/outputs.rb +39 -0
- data/lib/ruby_cdk/test/private/parameters.rb +39 -0
- data/lib/ruby_cdk/test/private/resources.rb +116 -0
- data/lib/ruby_cdk/test/private/section.rb +98 -0
- data/lib/ruby_cdk/test/private/sorting.rb +40 -0
- data/lib/ruby_cdk/test/private/sparse_matrix.rb +35 -0
- data/lib/ruby_cdk/test/private/type.rb +23 -0
- data/lib/ruby_cdk/test/template.rb +364 -0
- data/lib/ruby_cdk/test.rb +116 -0
- data/lib/ruby_cdk/values.rb +8 -0
- data/lib/ruby_cdk/version.rb +5 -0
- data/lib/ruby_cdk.rb +47 -0
- metadata +256 -0
|
@@ -0,0 +1,326 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Ported from the AWS CDK.
|
|
4
|
+
#
|
|
5
|
+
# Origin: packages/aws-cdk-lib/matching/lib (the messages of the matcher engine)
|
|
6
|
+
# and packages/aws-cdk-lib/assertions/lib/private (the messages of the template)
|
|
7
|
+
# Commit: 025a6f67fec3d37a6ffdc6168f8a751a04918f66 (aws/aws-cdk, main)
|
|
8
|
+
# Licence: Apache-2.0
|
|
9
|
+
# Changed: the CDK has no file to translate here. Its engine is JavaScript and
|
|
10
|
+
# builds its messages with typeof, String(), JSON.stringify, === and
|
|
11
|
+
# localeCompare; this file reproduces those five in Ruby, so that a failure
|
|
12
|
+
# reads here as it reads there. UNDEFINED is this tool's own: Ruby needs a way
|
|
13
|
+
# to write what JavaScript writes as undefined, which is not null.
|
|
14
|
+
|
|
15
|
+
require "json"
|
|
16
|
+
|
|
17
|
+
module RubyCdk
|
|
18
|
+
module Test
|
|
19
|
+
# Stands where JavaScript writes `undefined`: a key that is present without a
|
|
20
|
+
# value, an index past the end of an array. It is not nil, which is JSON's
|
|
21
|
+
# null. The CDK tells the two apart (`Match.absent()` accepts one and refuses
|
|
22
|
+
# the other), so a template written by hand in Ruby can say which one it means.
|
|
23
|
+
#
|
|
24
|
+
# Not in the CDK.
|
|
25
|
+
UNDEFINED = Object.new
|
|
26
|
+
UNDEFINED.define_singleton_method(:to_s) { "undefined" }
|
|
27
|
+
UNDEFINED.define_singleton_method(:inspect) { "undefined" }
|
|
28
|
+
UNDEFINED.freeze
|
|
29
|
+
|
|
30
|
+
module Private
|
|
31
|
+
# Internal to the matcher engine and to the template. Not part of the public API.
|
|
32
|
+
module Js
|
|
33
|
+
module_function
|
|
34
|
+
|
|
35
|
+
# What JSON.stringify returns nothing for.
|
|
36
|
+
NO_JSON = Object.new.freeze
|
|
37
|
+
private_constant :NO_JSON
|
|
38
|
+
|
|
39
|
+
# JavaScript `typeof`, quirk included: null is 'object' there.
|
|
40
|
+
def typeof(value)
|
|
41
|
+
if value.equal?(UNDEFINED)
|
|
42
|
+
"undefined"
|
|
43
|
+
elsif value.nil?
|
|
44
|
+
"object"
|
|
45
|
+
elsif value == true || value == false
|
|
46
|
+
"boolean"
|
|
47
|
+
elsif value.is_a?(Numeric)
|
|
48
|
+
"number"
|
|
49
|
+
elsif value.is_a?(String)
|
|
50
|
+
"string"
|
|
51
|
+
elsif value.is_a?(Symbol)
|
|
52
|
+
"symbol"
|
|
53
|
+
elsif value.is_a?(Proc) || value.is_a?(Method)
|
|
54
|
+
"function"
|
|
55
|
+
else
|
|
56
|
+
"object"
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def nullish?(value)
|
|
61
|
+
value.nil? || value.equal?(UNDEFINED)
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
# JavaScript: `value && typeof value === 'object'`. True of neither null nor undefined.
|
|
65
|
+
def object?(value)
|
|
66
|
+
!nullish?(value) && !primitive?(value)
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
def primitive?(value)
|
|
70
|
+
value.nil? || value.equal?(UNDEFINED) || value.instance_of?(TrueClass) || value.instance_of?(FalseClass) ||
|
|
71
|
+
value.is_a?(Numeric) || value.is_a?(String) || value.is_a?(Symbol)
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
# JavaScript `===`: primitives by value, everything else by identity.
|
|
75
|
+
def strict_equal?(left, right)
|
|
76
|
+
return left.equal?(right) unless primitive?(left)
|
|
77
|
+
|
|
78
|
+
left == right
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
# JavaScript `String(value)`.
|
|
82
|
+
def string(value)
|
|
83
|
+
if value.is_a?(String)
|
|
84
|
+
value
|
|
85
|
+
elsif value.equal?(UNDEFINED)
|
|
86
|
+
"undefined"
|
|
87
|
+
elsif value.nil?
|
|
88
|
+
"null"
|
|
89
|
+
elsif value == true || value == false
|
|
90
|
+
value.to_s
|
|
91
|
+
elsif value.is_a?(Integer)
|
|
92
|
+
value.to_s
|
|
93
|
+
elsif value.is_a?(Float)
|
|
94
|
+
number_string(value)
|
|
95
|
+
elsif value.is_a?(Symbol)
|
|
96
|
+
value.to_s
|
|
97
|
+
elsif value.is_a?(Array)
|
|
98
|
+
value.map { |element| nullish?(element) ? "" : string(element) }.join(",")
|
|
99
|
+
else
|
|
100
|
+
"[object Object]"
|
|
101
|
+
end
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
# How JavaScript prints a number: 2.0 is "2", and nothing Ruby writes as
|
|
105
|
+
# 1.0e+21 keeps the ".0". Only used in messages.
|
|
106
|
+
def number_string(value)
|
|
107
|
+
return value.to_s unless value.finite?
|
|
108
|
+
return value.to_i.to_s if (value % 1).zero? && value.abs < 1e21
|
|
109
|
+
|
|
110
|
+
sign = value.negative? ? "-" : ""
|
|
111
|
+
digits, exponent = shortest_digits(value.abs)
|
|
112
|
+
if exponent > 20 || exponent < -6
|
|
113
|
+
fraction = digits.length > 1 ? ".#{digits[1..]}" : ""
|
|
114
|
+
"#{sign}#{digits[0]}#{fraction}e#{exponent.negative? ? "-" : "+"}#{exponent.abs}"
|
|
115
|
+
elsif exponent >= digits.length - 1
|
|
116
|
+
"#{sign}#{digits}#{"0" * (exponent - digits.length + 1)}"
|
|
117
|
+
elsif exponent >= 0
|
|
118
|
+
"#{sign}#{digits[0..exponent]}.#{digits[(exponent + 1)..]}"
|
|
119
|
+
else
|
|
120
|
+
"#{sign}0.#{"0" * (-exponent - 1)}#{digits}"
|
|
121
|
+
end
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
# The shortest digits that still round-trip, and the power of ten of the first.
|
|
125
|
+
def shortest_digits(magnitude)
|
|
126
|
+
match = magnitude.to_s.match(/\A(\d)(?:\.(\d+))?(?:e([+-]\d+))?\z/)
|
|
127
|
+
return [ magnitude.to_s, 0 ] unless match
|
|
128
|
+
|
|
129
|
+
whole, fraction, power = match.captures
|
|
130
|
+
if power
|
|
131
|
+
digits = "#{whole}#{fraction}".sub(/0+\z/, "")
|
|
132
|
+
exponent = power.to_i
|
|
133
|
+
elsif whole == "0"
|
|
134
|
+
digits = fraction.sub(/\A0+/, "")
|
|
135
|
+
exponent = digits.length - fraction.length - 1
|
|
136
|
+
else
|
|
137
|
+
digits = "#{whole}#{fraction}".sub(/0+\z/, "")
|
|
138
|
+
exponent = whole.length - 1
|
|
139
|
+
end
|
|
140
|
+
[ digits, exponent ]
|
|
141
|
+
end
|
|
142
|
+
|
|
143
|
+
# JavaScript `JSON.stringify(value)`: compact text, or indented text when
|
|
144
|
+
# an indent is given. nil where JavaScript returns undefined.
|
|
145
|
+
def json(value, indent = nil)
|
|
146
|
+
converted = native(value)
|
|
147
|
+
return nil if converted.equal?(NO_JSON)
|
|
148
|
+
|
|
149
|
+
indent ? JSON.pretty_generate(converted, indent: " " * indent) : JSON.generate(converted)
|
|
150
|
+
end
|
|
151
|
+
|
|
152
|
+
# JSON.stringify, with 'undefined' where JavaScript prints nothing.
|
|
153
|
+
def jsonify(value)
|
|
154
|
+
json(value) || "undefined"
|
|
155
|
+
end
|
|
156
|
+
|
|
157
|
+
# The value as Ruby's JSON library prints it as JavaScript would: a Float
|
|
158
|
+
# that is a whole number is an Integer there ("2", not "2.0"), a number
|
|
159
|
+
# that is not finite is null, and what JavaScript leaves out is NO_JSON.
|
|
160
|
+
def native(value)
|
|
161
|
+
case value
|
|
162
|
+
when Hash
|
|
163
|
+
object_keys(value).each_with_object({}) do |key, converted_hash|
|
|
164
|
+
converted = native(value[key])
|
|
165
|
+
next if converted.equal?(NO_JSON)
|
|
166
|
+
|
|
167
|
+
converted_hash[key.to_s] = converted
|
|
168
|
+
end
|
|
169
|
+
when Array
|
|
170
|
+
value.map do |item|
|
|
171
|
+
converted = native(item)
|
|
172
|
+
converted.equal?(NO_JSON) ? nil : converted
|
|
173
|
+
end
|
|
174
|
+
when Float
|
|
175
|
+
return nil unless value.finite?
|
|
176
|
+
|
|
177
|
+
(value % 1).zero? && value.abs < 1e21 ? value.to_i : value
|
|
178
|
+
when UNDEFINED, Proc, Method, Symbol
|
|
179
|
+
NO_JSON
|
|
180
|
+
when Numeric, String, TrueClass, FalseClass, NilClass
|
|
181
|
+
value
|
|
182
|
+
else
|
|
183
|
+
# Any other object is a JSON object with the keys JavaScript would list:
|
|
184
|
+
# those of its to_h, or none.
|
|
185
|
+
value.respond_to?(:to_h) ? native(value.to_h) : {}
|
|
186
|
+
end
|
|
187
|
+
end
|
|
188
|
+
|
|
189
|
+
# A template or a pattern as JavaScript would have held it: a Symbol is
|
|
190
|
+
# Ruby's way of writing a JSON string, so it becomes one, as a key or as a
|
|
191
|
+
# value. Matchers are left alone. Nothing of the caller's is modified.
|
|
192
|
+
#
|
|
193
|
+
# Not in the CDK: JavaScript has no symbols.
|
|
194
|
+
def normalize(value)
|
|
195
|
+
case value
|
|
196
|
+
when Hash
|
|
197
|
+
value.to_h { |key, item| [ key.is_a?(Symbol) ? key.to_s : key, normalize(item) ] }
|
|
198
|
+
when Array
|
|
199
|
+
value.map { |item| normalize(item) }
|
|
200
|
+
when Symbol
|
|
201
|
+
value.to_s
|
|
202
|
+
else
|
|
203
|
+
value
|
|
204
|
+
end
|
|
205
|
+
end
|
|
206
|
+
|
|
207
|
+
# A copy of the arrays and hashes of a value. Everything else is shared.
|
|
208
|
+
def deep_copy(value)
|
|
209
|
+
case value
|
|
210
|
+
when Hash
|
|
211
|
+
value.to_h { |key, item| [ key, deep_copy(item) ] }
|
|
212
|
+
when Array
|
|
213
|
+
value.map { |item| deep_copy(item) }
|
|
214
|
+
else
|
|
215
|
+
value
|
|
216
|
+
end
|
|
217
|
+
end
|
|
218
|
+
|
|
219
|
+
# JavaScript `Object.keys`: own keys in order, the ones that read as array
|
|
220
|
+
# indexes first and by number. Throws where JavaScript throws.
|
|
221
|
+
def object_keys(value)
|
|
222
|
+
if value.is_a?(Hash)
|
|
223
|
+
indexed, other = value.keys.partition { |key| key.is_a?(String) && key.match?(/\A(?:0|[1-9]\d*)\z/) }
|
|
224
|
+
indexed.sort_by(&:to_i) + other
|
|
225
|
+
elsif value.is_a?(Array)
|
|
226
|
+
(0...value.length).map(&:to_s)
|
|
227
|
+
else
|
|
228
|
+
Kernel.raise TypeError, "Cannot convert undefined or null to object" if nullish?(value)
|
|
229
|
+
|
|
230
|
+
[]
|
|
231
|
+
end
|
|
232
|
+
end
|
|
233
|
+
|
|
234
|
+
# JavaScript `Object.entries`.
|
|
235
|
+
def entries(value)
|
|
236
|
+
object_keys(value).map do |key|
|
|
237
|
+
item = value.is_a?(Hash) ? value[key] : value[key.to_i]
|
|
238
|
+
[ key, item ]
|
|
239
|
+
end
|
|
240
|
+
end
|
|
241
|
+
|
|
242
|
+
# JavaScript `value[key]`: UNDEFINED when there is nothing there.
|
|
243
|
+
def get(value, key)
|
|
244
|
+
if value.is_a?(Hash)
|
|
245
|
+
value.key?(key) ? value[key] : UNDEFINED
|
|
246
|
+
elsif value.is_a?(Array)
|
|
247
|
+
index = key.is_a?(String) && key.match?(/\A\d+\z/) ? key.to_i : nil
|
|
248
|
+
index && index < value.length ? value[index] : UNDEFINED
|
|
249
|
+
else
|
|
250
|
+
UNDEFINED
|
|
251
|
+
end
|
|
252
|
+
end
|
|
253
|
+
|
|
254
|
+
# JavaScript `key in value`. Throws where JavaScript throws.
|
|
255
|
+
def in?(key, value)
|
|
256
|
+
if value.is_a?(Hash)
|
|
257
|
+
value.key?(key)
|
|
258
|
+
elsif value.is_a?(Array)
|
|
259
|
+
key.is_a?(String) && key.match?(/\A\d+\z/) && key.to_i < value.length
|
|
260
|
+
else
|
|
261
|
+
Kernel.raise TypeError, "Cannot use 'in' operator to search for '#{key}' in #{string(value)}"
|
|
262
|
+
end
|
|
263
|
+
end
|
|
264
|
+
|
|
265
|
+
def trim_end(value)
|
|
266
|
+
value.gsub(/\s+\z/, "")
|
|
267
|
+
end
|
|
268
|
+
|
|
269
|
+
# An abridged version of a value: a long array is [ ... ], an object with
|
|
270
|
+
# more than one key is { ... }. One of renderMismatch()'s inner functions.
|
|
271
|
+
def render_abridged(value)
|
|
272
|
+
if value.is_a?(Array)
|
|
273
|
+
case value.length
|
|
274
|
+
when 0
|
|
275
|
+
"[]"
|
|
276
|
+
when 1
|
|
277
|
+
"[ #{render_abridged(value[0])} ]"
|
|
278
|
+
when 2
|
|
279
|
+
if value.all? { |element| [ "number", "boolean", "string" ].include?(typeof(element)) }
|
|
280
|
+
"[ #{value.map { |element| render_abridged(element) }.join(", ")} ]"
|
|
281
|
+
else
|
|
282
|
+
"[ ... ]"
|
|
283
|
+
end
|
|
284
|
+
else
|
|
285
|
+
"[ ... ]"
|
|
286
|
+
end
|
|
287
|
+
elsif object?(value)
|
|
288
|
+
keys = object_keys(value)
|
|
289
|
+
case keys.length
|
|
290
|
+
when 0
|
|
291
|
+
"{}"
|
|
292
|
+
when 1
|
|
293
|
+
"{ #{json(keys[0])}: #{render_abridged(get(value, keys[0]))} }"
|
|
294
|
+
else
|
|
295
|
+
"{ ... }"
|
|
296
|
+
end
|
|
297
|
+
else
|
|
298
|
+
jsonify(value)
|
|
299
|
+
end
|
|
300
|
+
end
|
|
301
|
+
|
|
302
|
+
# Move `!!` in front of the indentation of every line that starts with it.
|
|
303
|
+
def move_markers_to_front(value)
|
|
304
|
+
value.gsub(/^(\s+)!!/) do
|
|
305
|
+
spaces = Regexp.last_match(1) || ""
|
|
306
|
+
"!!#{spaces[0, [ spaces.length - 2, 0 ].max]}"
|
|
307
|
+
end
|
|
308
|
+
end
|
|
309
|
+
|
|
310
|
+
# String.prototype.localeCompare as Node does it with no locale given
|
|
311
|
+
# (ICU's root collation), as far as pure Ruby gets: letters compare without
|
|
312
|
+
# regard to case, and among equal ones the lower-case spelling comes
|
|
313
|
+
# first. Digits come after punctuation and before letters, as there.
|
|
314
|
+
def locale_compare(left, right)
|
|
315
|
+
result = collation_key(left) <=> collation_key(right)
|
|
316
|
+
result.zero? ? left <=> right : result
|
|
317
|
+
end
|
|
318
|
+
|
|
319
|
+
def collation_key(value)
|
|
320
|
+
primary = value.downcase.chars.map { |char| char.match?(/[a-z0-9]/) || char.ord < 0x30 ? char : "/" }.join
|
|
321
|
+
[ primary, value.chars.map { |char| char == char.downcase ? 0 : 1 } ]
|
|
322
|
+
end
|
|
323
|
+
end
|
|
324
|
+
end
|
|
325
|
+
end
|
|
326
|
+
end
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Ported from the AWS CDK.
|
|
4
|
+
#
|
|
5
|
+
# Origin: packages/aws-cdk-lib/assertions/lib/private/mappings.ts
|
|
6
|
+
# Commit: 025a6f67fec3d37a6ffdc6168f8a751a04918f66 (aws/aws-cdk, main)
|
|
7
|
+
# Licence: Apache-2.0
|
|
8
|
+
# Changed: camelCase to snake_case, and nothing in behaviour. The functions are
|
|
9
|
+
# module functions of Private.
|
|
10
|
+
|
|
11
|
+
require_relative "js"
|
|
12
|
+
require_relative "section"
|
|
13
|
+
|
|
14
|
+
module RubyCdk
|
|
15
|
+
module Test
|
|
16
|
+
module Private
|
|
17
|
+
module_function
|
|
18
|
+
|
|
19
|
+
def find_mappings(template, logical_id, props = {})
|
|
20
|
+
result = match_section(filter_logical_id(mappings_of(template), logical_id), props)
|
|
21
|
+
return {} unless result.match
|
|
22
|
+
|
|
23
|
+
result.matches
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def has_mapping(template, logical_id, props)
|
|
27
|
+
result = match_section(filter_logical_id(mappings_of(template), logical_id), props)
|
|
28
|
+
return if result.match
|
|
29
|
+
|
|
30
|
+
format_section_match_failure("mappings with logicalId #{logical_id}", result)
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def mappings_of(template)
|
|
34
|
+
section = Js.get(template, "Mappings")
|
|
35
|
+
Js.nullish?(section) ? {} : section
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Ported from the AWS CDK.
|
|
4
|
+
#
|
|
5
|
+
# Origin: packages/aws-cdk-lib/assertions/lib/private/outputs.ts
|
|
6
|
+
# Commit: 025a6f67fec3d37a6ffdc6168f8a751a04918f66 (aws/aws-cdk, main)
|
|
7
|
+
# Licence: Apache-2.0
|
|
8
|
+
# Changed: camelCase to snake_case, and nothing in behaviour. The functions are
|
|
9
|
+
# module functions of Private.
|
|
10
|
+
|
|
11
|
+
require_relative "js"
|
|
12
|
+
require_relative "section"
|
|
13
|
+
|
|
14
|
+
module RubyCdk
|
|
15
|
+
module Test
|
|
16
|
+
module Private
|
|
17
|
+
module_function
|
|
18
|
+
|
|
19
|
+
def find_outputs(template, logical_id, props = {})
|
|
20
|
+
result = match_section(filter_logical_id(outputs_of(template), logical_id), props)
|
|
21
|
+
return {} unless result.match
|
|
22
|
+
|
|
23
|
+
result.matches
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def has_output(template, logical_id, props)
|
|
27
|
+
result = match_section(filter_logical_id(outputs_of(template), logical_id), props)
|
|
28
|
+
return if result.match
|
|
29
|
+
|
|
30
|
+
format_section_match_failure("outputs named #{logical_id}", result)
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def outputs_of(template)
|
|
34
|
+
section = Js.get(template, "Outputs")
|
|
35
|
+
Js.nullish?(section) ? {} : section
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Ported from the AWS CDK.
|
|
4
|
+
#
|
|
5
|
+
# Origin: packages/aws-cdk-lib/assertions/lib/private/parameters.ts
|
|
6
|
+
# Commit: 025a6f67fec3d37a6ffdc6168f8a751a04918f66 (aws/aws-cdk, main)
|
|
7
|
+
# Licence: Apache-2.0
|
|
8
|
+
# Changed: camelCase to snake_case, and nothing in behaviour. The functions are
|
|
9
|
+
# module functions of Private.
|
|
10
|
+
|
|
11
|
+
require_relative "js"
|
|
12
|
+
require_relative "section"
|
|
13
|
+
|
|
14
|
+
module RubyCdk
|
|
15
|
+
module Test
|
|
16
|
+
module Private
|
|
17
|
+
module_function
|
|
18
|
+
|
|
19
|
+
def find_parameters(template, logical_id, props = {})
|
|
20
|
+
result = match_section(filter_logical_id(parameters_of(template), logical_id), props)
|
|
21
|
+
return {} unless result.match
|
|
22
|
+
|
|
23
|
+
result.matches
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def has_parameter(template, logical_id, props)
|
|
27
|
+
result = match_section(filter_logical_id(parameters_of(template), logical_id), props)
|
|
28
|
+
return if result.match
|
|
29
|
+
|
|
30
|
+
format_section_match_failure("parameters with logicalId '#{logical_id}'", result)
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def parameters_of(template)
|
|
34
|
+
section = Js.get(template, "Parameters")
|
|
35
|
+
Js.nullish?(section) ? {} : section
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Ported from the AWS CDK.
|
|
4
|
+
#
|
|
5
|
+
# Origin: packages/aws-cdk-lib/assertions/lib/private/resources.ts
|
|
6
|
+
# Commit: 025a6f67fec3d37a6ffdc6168f8a751a04918f66 (aws/aws-cdk, main)
|
|
7
|
+
# Licence: Apache-2.0
|
|
8
|
+
# Changed: camelCase to snake_case. getResourceId returns [ resource id, match
|
|
9
|
+
# error ] where the CDK returns an object with one of the two. The deep copy
|
|
10
|
+
# that the CDK takes with a round trip through JSON is Js.deep_copy: the
|
|
11
|
+
# template may hold a Capture, which JSON would destroy. A section or a resource
|
|
12
|
+
# that is not an object reads as empty instead of throwing.
|
|
13
|
+
|
|
14
|
+
require_relative "../match"
|
|
15
|
+
require_relative "../matcher"
|
|
16
|
+
require_relative "absent"
|
|
17
|
+
require_relative "js"
|
|
18
|
+
require_relative "section"
|
|
19
|
+
|
|
20
|
+
module RubyCdk
|
|
21
|
+
module Test
|
|
22
|
+
module Private
|
|
23
|
+
module_function
|
|
24
|
+
|
|
25
|
+
def find_resources(template, type, props = {})
|
|
26
|
+
result = match_section(filter_type(resources_of(template), type), props)
|
|
27
|
+
return {} unless result.match
|
|
28
|
+
|
|
29
|
+
result.matches
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
# [ resource id, match error ]: one of the two is nil.
|
|
33
|
+
def get_resource_id(template, type, props = {})
|
|
34
|
+
result = match_section(filter_type(resources_of(template), type), props)
|
|
35
|
+
|
|
36
|
+
return [ nil, format_section_match_failure("resources with type #{type}", result) ] unless result.match
|
|
37
|
+
|
|
38
|
+
resource_ids = result.matches.keys
|
|
39
|
+
if resource_ids.length != 1
|
|
40
|
+
return [ nil, [ "Template has #{resource_ids.length} matches, expected only one.", format_all_matches(result.matches) ].join("\n") ]
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
[ resource_ids[0], nil ]
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
# The failure message, or nil when every resource of the type matches.
|
|
47
|
+
def all_resources(template, type, props)
|
|
48
|
+
result = match_section(filter_type(resources_of(template), type), props)
|
|
49
|
+
if result.match
|
|
50
|
+
match_count = result.matches.keys.length
|
|
51
|
+
if result.analyzed_count > match_count
|
|
52
|
+
return [ "Template has #{result.analyzed_count} resource(s) with type #{type}, but only #{match_count} match as expected.",
|
|
53
|
+
format_all_mismatches(result.analyzed, result.matches) ].join("\n")
|
|
54
|
+
end
|
|
55
|
+
nil
|
|
56
|
+
else
|
|
57
|
+
[ "Template has #{result.analyzed_count} resource(s) with type #{type}, but none match as expected.",
|
|
58
|
+
format_all_mismatches(result.analyzed) ].join("\n")
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
def all_resources_properties(template, type, props)
|
|
63
|
+
all_resources(with_empty_properties(template, props), type, Match.object_like("Properties" => props))
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
def has_resource(template, type, props)
|
|
67
|
+
result = match_section(filter_type(resources_of(template), type), props)
|
|
68
|
+
return if result.match
|
|
69
|
+
|
|
70
|
+
format_section_match_failure("resources with type #{type}", result)
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
def has_resource_properties(template, type, props)
|
|
74
|
+
has_resource(with_empty_properties(template, props), type, Match.object_like("Properties" => props))
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
def count_resources(template, type)
|
|
78
|
+
filter_type(resources_of(template), type).length
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
def count_resources_properties(template, type, props)
|
|
82
|
+
amended = with_empty_properties(template, props)
|
|
83
|
+
result = match_section(filter_type(resources_of(amended), type), Match.object_like("Properties" => props))
|
|
84
|
+
|
|
85
|
+
result.match ? result.matches.keys.length : 0
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
# A copy of the template in which every resource has a Properties key, so
|
|
89
|
+
# that a Properties pattern applies to all of them. Not for Match.absent():
|
|
90
|
+
# adding an empty Properties object would change what it evaluates.
|
|
91
|
+
def with_empty_properties(template, props)
|
|
92
|
+
return template if Matcher.matcher?(props) && props.is_a?(AbsentMatch)
|
|
93
|
+
|
|
94
|
+
# The copy must be deep, to avoid modifying the template.
|
|
95
|
+
amended = Js.deep_copy(template)
|
|
96
|
+
section = resources_of(amended)
|
|
97
|
+
Js.object_keys(section).each do |key|
|
|
98
|
+
resource = section[key]
|
|
99
|
+
resource["Properties"] = {} if resource.is_a?(Hash) && !resource.key?("Properties")
|
|
100
|
+
end
|
|
101
|
+
amended
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
def resources_of(template)
|
|
105
|
+
section = Js.get(template, "Resources")
|
|
106
|
+
Js.nullish?(section) ? {} : section
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
def filter_type(section, type)
|
|
110
|
+
return {} unless section.is_a?(Hash)
|
|
111
|
+
|
|
112
|
+
section.select { |_key, resource| resource.is_a?(Hash) && Js.strict_equal?(Js.get(resource, "Type"), type) }
|
|
113
|
+
end
|
|
114
|
+
end
|
|
115
|
+
end
|
|
116
|
+
end
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Ported from the AWS CDK.
|
|
4
|
+
#
|
|
5
|
+
# Origin: packages/aws-cdk-lib/assertions/lib/private/section.ts
|
|
6
|
+
# Commit: 025a6f67fec3d37a6ffdc6168f8a751a04918f66 (aws/aws-cdk, main)
|
|
7
|
+
# Licence: Apache-2.0
|
|
8
|
+
# Changed: camelCase to snake_case. matchSection returns one value, a
|
|
9
|
+
# SectionMatch, where the CDK returns a MatchSuccess or a MatchFailure: matches
|
|
10
|
+
# is nil on a failure and closest_results on a success. The functions are module
|
|
11
|
+
# functions of Private.
|
|
12
|
+
|
|
13
|
+
require_relative "../match"
|
|
14
|
+
require_relative "../matcher"
|
|
15
|
+
require_relative "js"
|
|
16
|
+
require_relative "sorting"
|
|
17
|
+
|
|
18
|
+
module RubyCdk
|
|
19
|
+
module Test
|
|
20
|
+
module Private
|
|
21
|
+
# What came out of matching every entry of one section of a template against
|
|
22
|
+
# the same matcher.
|
|
23
|
+
#
|
|
24
|
+
# match: whether at least one entry matched
|
|
25
|
+
# matches: on a success, the entries that matched, by logical ID
|
|
26
|
+
# closest_results: on a failure, the three entries that came closest, each
|
|
27
|
+
# with its MatchResult
|
|
28
|
+
# analyzed: every entry the matcher was run against
|
|
29
|
+
# analyzed_count: how many
|
|
30
|
+
SectionMatch = Data.define(:match, :matches, :closest_results, :analyzed, :analyzed_count)
|
|
31
|
+
|
|
32
|
+
module_function
|
|
33
|
+
|
|
34
|
+
def match_section(section, props)
|
|
35
|
+
matcher = Matcher.matcher?(props) ? props : Match.object_like(props)
|
|
36
|
+
matching = {}
|
|
37
|
+
analyzed = {}
|
|
38
|
+
failures = []
|
|
39
|
+
|
|
40
|
+
each_entry_in_section(section) do |logical_id, entry|
|
|
41
|
+
analyzed[logical_id] = entry
|
|
42
|
+
result = matcher.test(entry)
|
|
43
|
+
result.finished
|
|
44
|
+
if !result.has_failed?
|
|
45
|
+
matching[logical_id] = entry
|
|
46
|
+
else
|
|
47
|
+
failures << [ logical_id, result ]
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
if matching.keys.length > 0
|
|
52
|
+
SectionMatch.new(match: true, matches: matching, closest_results: nil, analyzed_count: analyzed.keys.length, analyzed: analyzed)
|
|
53
|
+
else
|
|
54
|
+
# Sort by cost, use logicalId as a tie breaker. Take the 3 closest
|
|
55
|
+
# matches (helps debugging in case we get the top pick wrong).
|
|
56
|
+
failures.sort!(&sort_key_comparator { |logical_id, result| [ result.fail_cost, logical_id ] })
|
|
57
|
+
SectionMatch.new(match: false, matches: nil, closest_results: failures.first(3).to_h, analyzed_count: analyzed.keys.length, analyzed: analyzed)
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
def format_all_matches(matches)
|
|
62
|
+
left_pad(Js.json(matches, 2))
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
def format_all_mismatches(analyzed, matches = {})
|
|
66
|
+
[ "The following resources do not match the given definition:", *Js.object_keys(analyzed).reject { |id| Js.in?(id, matches) }.map { |id| "\t#{id}" } ].join("\n")
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
def format_section_match_failure(qualifier, result, what = "Template")
|
|
70
|
+
"#{what} has #{result.analyzed_count} #{qualifier}#{result.analyzed_count > 0 ? ", but none match as expected" : ""}.\n#{format_failure(result.closest_results)}"
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
def format_failure(closest_results)
|
|
74
|
+
keys = Js.object_keys(closest_results)
|
|
75
|
+
return "No matches found" if keys.empty?
|
|
76
|
+
|
|
77
|
+
[ "The #{keys.length} closest matches:", *keys.map { |key| "#{key} :: #{closest_results[key].render_mismatch}" } ].join("\n")
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
# default signal for all logicalIds is '*'
|
|
81
|
+
def filter_logical_id(section, logical_id)
|
|
82
|
+
return {} unless section.is_a?(Hash)
|
|
83
|
+
return section if logical_id == "*"
|
|
84
|
+
|
|
85
|
+
section.select { |key, _value| key == logical_id }
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
def each_entry_in_section(section)
|
|
89
|
+
Js.object_keys(section || {}).each { |logical_id| yield logical_id, section[logical_id] }
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
def left_pad(value, indent = 2)
|
|
93
|
+
pad = " " * indent
|
|
94
|
+
pad + value.split("\n", -1).join("\n#{pad}")
|
|
95
|
+
end
|
|
96
|
+
end
|
|
97
|
+
end
|
|
98
|
+
end
|