rspec-terraform 0.1.0.pre.7 → 0.1.0.pre.10
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 +4 -4
- data/Gemfile.lock +1 -1
- data/Rakefile +1 -1
- data/lib/rspec/terraform/matchers/include_resource_change.rb +140 -49
- data/lib/rspec/terraform/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f8c0ef1824e2c32907a06a34ab775ab0004a9ddf57ab22c5b2baeb40812eb4df
|
4
|
+
data.tar.gz: af62f1d916db8ea95f186bb44903bd52641787c270b6c0e1fa7bf6073482204e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3a2638bb48081a61c55f716459873c66e7bb3e730e80561ddff0605bcbbd48e67cc56498ce7a343747d08d80bdccd6536203b53728163562fd272ca0d4debc37
|
7
|
+
data.tar.gz: 26e7621f75e736abc0e0ba5a036513c4f6d32b2ae3cfd16ba357fe3c956b566773b2756963fba41cae6c57e9537b6af4d124db93beb5f6c3f4971d2e213e8fae
|
data/Gemfile.lock
CHANGED
data/Rakefile
CHANGED
@@ -8,10 +8,11 @@ require 'rspec/matchers/built_in/count_expectation'
|
|
8
8
|
module RSpec
|
9
9
|
module Terraform
|
10
10
|
module Matchers
|
11
|
+
# rubocop:disable Metrics/ClassLength
|
11
12
|
class IncludeResourceChange
|
12
13
|
include RSpec::Matchers::BuiltIn::CountExpectation
|
13
14
|
|
14
|
-
attr_reader :definition
|
15
|
+
attr_reader :definition, :attributes, :plan
|
15
16
|
|
16
17
|
def initialize(definition = {})
|
17
18
|
@definition = definition
|
@@ -19,6 +20,7 @@ module RSpec
|
|
19
20
|
end
|
20
21
|
|
21
22
|
def matches?(plan)
|
23
|
+
@plan = plan
|
22
24
|
matches = attribute_matches(plan)
|
23
25
|
|
24
26
|
match_count = matches.count
|
@@ -36,6 +38,16 @@ module RSpec
|
|
36
38
|
self
|
37
39
|
end
|
38
40
|
|
41
|
+
def failure_message
|
42
|
+
"\nexpected: #{positive_expected_line}" \
|
43
|
+
"\n got: #{positive_got_line}"
|
44
|
+
end
|
45
|
+
|
46
|
+
def failure_message_when_negated
|
47
|
+
"\nexpected: a plan including no resource changes" \
|
48
|
+
"\n got: a plan including at least one resource change"
|
49
|
+
end
|
50
|
+
|
39
51
|
private
|
40
52
|
|
41
53
|
def definition_matches(plan)
|
@@ -70,55 +82,134 @@ module RSpec
|
|
70
82
|
|
71
83
|
actual
|
72
84
|
end
|
85
|
+
|
86
|
+
def positive_expected_line
|
87
|
+
maybe_with_expected_attributes(
|
88
|
+
maybe_with_definition(
|
89
|
+
positive_plan_line
|
90
|
+
)
|
91
|
+
)
|
92
|
+
end
|
93
|
+
|
94
|
+
def positive_plan_line
|
95
|
+
cardinality = cardinality_fragment
|
96
|
+
plurality = expected_count.nil? || expected_count == 1 ? '' : 's'
|
97
|
+
|
98
|
+
"a plan including #{cardinality} resource change#{plurality}"
|
99
|
+
end
|
100
|
+
|
101
|
+
def maybe_with_definition(expected_line)
|
102
|
+
unless @definition.empty?
|
103
|
+
expected_line =
|
104
|
+
"#{expected_line} matching definition:\n#{definition_lines}"
|
105
|
+
end
|
106
|
+
expected_line
|
107
|
+
end
|
108
|
+
|
109
|
+
def maybe_with_expected_attributes(expected_line)
|
110
|
+
unless @attributes.empty?
|
111
|
+
expected_line =
|
112
|
+
"#{expected_line}\n with attribute values after " \
|
113
|
+
"the resource change is applied of:\n#{expected_attribute_lines}"
|
114
|
+
end
|
115
|
+
expected_line
|
116
|
+
end
|
117
|
+
|
118
|
+
# rubocop:disable Metrics/MethodLength
|
119
|
+
def positive_got_line
|
120
|
+
if plan.resource_changes.empty?
|
121
|
+
'a plan including no resource changes.'
|
122
|
+
else
|
123
|
+
count = attribute_matches(plan).count
|
124
|
+
amount = count.zero? ? 'no' : cardinality_amount(count)
|
125
|
+
plurality = count == 1 ? '' : 's'
|
126
|
+
got_line =
|
127
|
+
"a plan including #{amount} matching " \
|
128
|
+
"resource change#{plurality}."
|
129
|
+
|
130
|
+
unless attributes.empty?
|
131
|
+
got_line =
|
132
|
+
"#{got_line}\n relevant resource changes are:" \
|
133
|
+
"\n#{relevant_resource_change_lines}"
|
134
|
+
end
|
135
|
+
|
136
|
+
"#{got_line}\n available resource changes are:" \
|
137
|
+
"\n#{available_resource_change_lines}"
|
138
|
+
end
|
139
|
+
end
|
140
|
+
# rubocop:enable Metrics/MethodLength
|
141
|
+
|
142
|
+
def cardinality_fragment
|
143
|
+
qualifier = cardinality_qualifier
|
144
|
+
amount = cardinality_amount(expected_count)
|
145
|
+
"#{qualifier} #{amount}"
|
146
|
+
end
|
147
|
+
|
148
|
+
def cardinality_qualifier
|
149
|
+
case count_expectation_type
|
150
|
+
when :<= then 'at most'
|
151
|
+
when nil, :>= then 'at least'
|
152
|
+
when :== then 'exactly'
|
153
|
+
when :<=> then 'between'
|
154
|
+
end
|
155
|
+
end
|
156
|
+
|
157
|
+
def cardinality_amount(count)
|
158
|
+
case count
|
159
|
+
when Range then "#{count.first} and #{count.last}"
|
160
|
+
when nil, 1 then 'one'
|
161
|
+
when 2 then 'two'
|
162
|
+
when 3 then 'three'
|
163
|
+
else count.to_s
|
164
|
+
end
|
165
|
+
end
|
166
|
+
|
167
|
+
def definition_lines
|
168
|
+
indent = ' '
|
169
|
+
definition
|
170
|
+
.collect { |k, v| "#{indent}#{k} = #{v.inspect}" }
|
171
|
+
.join("\n")
|
172
|
+
end
|
173
|
+
|
174
|
+
def expected_attribute_lines
|
175
|
+
indent = ' '
|
176
|
+
attribute_fragments = attributes.collect do |a|
|
177
|
+
"#{indent}#{render(a[:path])} = #{a[:value].inspect}"
|
178
|
+
end
|
179
|
+
attribute_fragments.join("\n")
|
180
|
+
end
|
181
|
+
|
182
|
+
# rubocop:disable Metrics/MethodLength
|
183
|
+
def relevant_resource_change_lines
|
184
|
+
relevant_lines = definition_matches(plan).collect do |rc|
|
185
|
+
address = rc.address
|
186
|
+
actions = rc.change.actions.join(', ')
|
187
|
+
attributes = rc.change.after_object
|
188
|
+
attribute_lines = attributes.collect do |key, value|
|
189
|
+
" #{key} = #{value.inspect}"
|
190
|
+
end
|
191
|
+
attribute_lines = attribute_lines.join("\n")
|
192
|
+
" - #{address} (#{actions})\n#{attribute_lines}"
|
193
|
+
end
|
194
|
+
relevant_lines.join("\n")
|
195
|
+
end
|
196
|
+
# rubocop:enable Metrics/MethodLength
|
197
|
+
|
198
|
+
def available_resource_change_lines
|
199
|
+
available_lines = plan.resource_changes.collect do |rc|
|
200
|
+
address = rc.address
|
201
|
+
actions = rc.change.actions.join(', ')
|
202
|
+
" - #{address} (#{actions})"
|
203
|
+
end
|
204
|
+
available_lines.join("\n")
|
205
|
+
end
|
206
|
+
|
207
|
+
def render(path)
|
208
|
+
path.collect { |elem| elem.to_s }.join(',')
|
209
|
+
end
|
73
210
|
end
|
211
|
+
|
212
|
+
# rubocop:enable Metrics/ClassLength
|
74
213
|
end
|
75
214
|
end
|
76
215
|
end
|
77
|
-
|
78
|
-
# RSpec::Matchers.define :include_resource_creation do |type|
|
79
|
-
# match do |plan|
|
80
|
-
# resource_changes = plan.resource_changes_with_type(type)
|
81
|
-
# resource_creations = resource_changes.filter(&:create?)
|
82
|
-
#
|
83
|
-
# return false if @count && resource_creations.length != @count
|
84
|
-
# return false if resource_creations.empty?
|
85
|
-
#
|
86
|
-
# pp plan.to_h
|
87
|
-
#
|
88
|
-
# if @arguments
|
89
|
-
# return resource_creations.any? do |resource_creation|
|
90
|
-
# @arguments.all? do |name, value|
|
91
|
-
# resource_creation.change.after[name] == value
|
92
|
-
# end
|
93
|
-
# end
|
94
|
-
# end
|
95
|
-
#
|
96
|
-
# return true
|
97
|
-
# end
|
98
|
-
#
|
99
|
-
# chain :count do |count|
|
100
|
-
# @count = count
|
101
|
-
# end
|
102
|
-
#
|
103
|
-
# chain :with_argument_value do |name, value|
|
104
|
-
# @arguments = (@arguments || {}).merge(name => value)
|
105
|
-
# end
|
106
|
-
#
|
107
|
-
# failure_message do |plan|
|
108
|
-
# resource_creations = plan.resource_creations.map do |resource_creation|
|
109
|
-
# "#{resource_creation.type}.#{resource_creation.name}"
|
110
|
-
# end
|
111
|
-
# "\nexpected: a plan with a resource creation for type: #{type}" \
|
112
|
-
# "\n got: a plan with resource creations:" \
|
113
|
-
# "\n - #{resource_creations.join("\n - ")}"
|
114
|
-
# end
|
115
|
-
#
|
116
|
-
# failure_message_when_negated do |plan|
|
117
|
-
# resource_creations = plan.resource_creations.map do |resource_creation|
|
118
|
-
# "#{resource_creation.type}.#{resource_creation.name}"
|
119
|
-
# end
|
120
|
-
# "\nexpected: a plan without a resource creation for type: #{type}" \
|
121
|
-
# "\n got: a plan with resource creations:" \
|
122
|
-
# "\n - #{resource_creations.join("\n - ")}"
|
123
|
-
# end
|
124
|
-
# end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rspec-terraform
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.0.pre.
|
4
|
+
version: 0.1.0.pre.10
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- InfraBlocks Maintainers
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-07-
|
11
|
+
date: 2022-07-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec
|