Skip to content

Commit ab1a564

Browse files
Merge pull request #294 from seanmil/array_autorequires
Add array support to autorequire variable expansion
2 parents cef4c86 + 2de2466 commit ab1a564

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

lib/puppet/resource_api.rb

+4-1
Original file line numberDiff line numberDiff line change
@@ -456,14 +456,17 @@ def self.parse_title_patterns(patterns)
456456
definition[auto].each do |type, values|
457457
Puppet.debug("Registering #{auto} for #{type}: #{values.inspect}")
458458
send(auto, type.downcase.to_sym) do
459-
[values].flatten.map do |v|
459+
resolved = [values].flatten.map do |v|
460460
match = %r{\A\$(.*)\Z}.match(v) if v.is_a? String
461461
if match.nil?
462462
v
463463
else
464464
self[match[1].to_sym]
465465
end
466466
end
467+
# Flatten to handle any resolved array properties and filter any nil
468+
# values resulting from unspecified optional parameters:
469+
resolved.flatten.reject { |v| v.nil? }
467470
end
468471
end
469472
end

spec/puppet/resource_api_spec.rb

+8-4
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,10 @@
111111
type: 'Optional[String]',
112112
desc: 'a optional string value',
113113
},
114+
test_array: {
115+
type: 'Array',
116+
desc: 'an array value',
117+
},
114118
},
115119
autorequire: {
116120
var: '$test_string',
@@ -122,7 +126,7 @@
122126
list: %w[foo bar],
123127
},
124128
autonotify: {
125-
mixed: [10, '$test_integer'],
129+
mixed: [10, '$test_integer', '$test_optional_string', '$test_array'],
126130
},
127131
}
128132
end
@@ -145,7 +149,7 @@ def extract_values(function)
145149
# rely on the fact that the resource api is doing `self[]` internally to find the value
146150
# see https://github.com/puppetlabs/puppet/blob/9f2c143962803a72c68f35be3462944e851bcdce/lib/puppet/type.rb#L2143
147151
# for details
148-
result += { test_string: 'foo', test_integer: 100 }.instance_eval(&values)
152+
result += { test_string: 'foo', test_integer: 100, test_array: %w[a b c] }.instance_eval(&values)
149153
end
150154
result
151155
end
@@ -173,8 +177,8 @@ def extract_values(function)
173177

174178
describe 'autonotify' do
175179
it('yields the block for `mixed`') { expect { |b| type.eachautonotify(&b) }.to yield_with_args(:mixed, be_a(Proc)) }
176-
it('the yielded block returns multiple integer values') do
177-
expect(extract_values(:eachautonotify)).to eq [10, 100]
180+
it('the yielded block returns multiple integer values, the flattened array results, and no nils') do
181+
expect(extract_values(:eachautonotify)).to eq [10, 100, 'a', 'b', 'c']
178182
end
179183
end
180184
end

0 commit comments

Comments
 (0)