Skip to content

Commit 81b95de

Browse files
authored
Merge pull request #890 from binford2k/pick_undefined_vars
Allow pick() to work with strict variables
2 parents 7ac5b90 + 227a231 commit 81b95de

2 files changed

Lines changed: 16 additions & 1 deletion

File tree

lib/puppet/parser/functions/pick.rb

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,21 @@ 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+
# look up the values of any strings that look like '$variables' w/o mutating args
28+
args = args.map do |item|
29+
next item unless item.is_a? String
30+
item.start_with?('$') ? call_function('getvar', [item.slice(1..-1)]) : item
31+
end
32+
args.compact!
2133
args.delete(:undef)
2234
args.delete(:undefined)
2335
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)