Skip to content

Commit 2a493ad

Browse files
committed
Allow pick() to work with strict variables
If `strict_variables` is enabled, then the standard usage of `pick()` will fail. This simply adds the calling convention that strings that look like variables will be resolved if they exist and ignored if they don't.
1 parent d5ddf85 commit 2a493ad

2 files changed

Lines changed: 17 additions & 1 deletion

File tree

lib/puppet/parser/functions/pick.rb

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,22 @@ module Puppet::Parser::Functions
1515
called 'jenkins_version' (note that parameters set in the Puppet Dashboard/
1616
Enterprise Console are brought into Puppet as top-scope variables), and,
1717
failing that, will use a default value of 1.449.
18+
19+
If you have `strict_variables` turned on, then wrap your variable in single
20+
quotes to prevent interpolation and this function will check to see if that
21+
variable exists.
22+
23+
$real_jenkins_version = pick('$::jenkins_version', '1.449')
24+
1825
DOC
1926
) do |args|
20-
args = args.compact
27+
28+
# look up the values of any strings that look like '$variables'
29+
args.map! do |item|
30+
next unless item.is_a? String
31+
item.start_with?('$') ? function_getvar([item.slice(1..-1)]) : item
32+
end
33+
args.compact!
2134
args.delete(:undef)
2235
args.delete(:undefined)
2336
args.delete('')

spec/functions/pick_spec.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
it { is_expected.to run.with_params(:undef, 'two').and_return('two') }
1010
it { is_expected.to run.with_params(:undefined, 'two').and_return('two') }
1111
it { is_expected.to run.with_params(nil, 'two').and_return('two') }
12+
it { is_expected.to run.with_params('$foo', 'two').and_return('two') }
13+
it { is_expected.to run.with_params('$puppetversion', 'two').and_return(Puppet.version) }
14+
it { is_expected.to run.with_params('$::puppetversion', 'two').and_return(Puppet.version) }
1215

1316
context 'with UTF8 and double byte characters' do
1417
it { is_expected.to run.with_params(nil, 'このテキスト').and_return('このテキスト') }

0 commit comments

Comments
 (0)