Skip to content

Commit 5b85e7c

Browse files
committed
Merge branch 'DavidS-broaden-pick-arguments2'
* DavidS-broaden-pick-arguments2: (PUP-638) Add a pick_default() function that always returns a value. (PUP-636) Ignore generated file
2 parents a2e9d00 + 52fcef5 commit 5b85e7c

3 files changed

Lines changed: 94 additions & 0 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ spec/fixtures/
66
Gemfile.lock
77
.bundle/
88
vendor/bundle/
9+
/metadata.json
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
module Puppet::Parser::Functions
2+
newfunction(:pick_default, :type => :rvalue, :doc => <<-EOS
3+
4+
This function is similar to a coalesce function in SQL in that it will return
5+
the first value in a list of values that is not undefined or an empty string
6+
(two things in Puppet that will return a boolean false value). If no value is
7+
found, it will return the last argument.
8+
9+
Typically, this function is used to check for a value in the Puppet
10+
Dashboard/Enterprise Console, and failover to a default value like the
11+
following:
12+
13+
$real_jenkins_version = pick_default($::jenkins_version, '1.449')
14+
15+
The value of $real_jenkins_version will first look for a top-scope variable
16+
called 'jenkins_version' (note that parameters set in the Puppet Dashboard/
17+
Enterprise Console are brought into Puppet as top-scope variables), and,
18+
failing that, will use a default value of 1.449.
19+
20+
Note that, contrary to the pick() function, the pick_default does not fail if
21+
all arguments are empty. This allows pick_default to use an empty value as
22+
default.
23+
24+
EOS
25+
) do |args|
26+
fail "Must receive at least one argument." if args.empty?
27+
default = args.last
28+
args = args[0..-2].compact
29+
args.delete(:undef)
30+
args.delete(:undefined)
31+
args.delete("")
32+
args << default
33+
return args[0]
34+
end
35+
end
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#!/usr/bin/env ruby -S rspec
2+
require 'spec_helper'
3+
4+
describe "the pick_default function" do
5+
let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
6+
7+
it "should exist" do
8+
Puppet::Parser::Functions.function("pick_default").should == "function_pick_default"
9+
end
10+
11+
it 'should return the correct value' do
12+
scope.function_pick_default(['first', 'second']).should == 'first'
13+
end
14+
15+
it 'should return the correct value if the first value is empty' do
16+
scope.function_pick_default(['', 'second']).should == 'second'
17+
end
18+
19+
it 'should skip empty string values' do
20+
scope.function_pick_default(['', 'first']).should == 'first'
21+
end
22+
23+
it 'should skip :undef values' do
24+
scope.function_pick_default([:undef, 'first']).should == 'first'
25+
end
26+
27+
it 'should skip :undefined values' do
28+
scope.function_pick_default([:undefined, 'first']).should == 'first'
29+
end
30+
31+
it 'should return the empty string if it is the last possibility' do
32+
scope.function_pick_default([:undef, :undefined, '']).should == ''
33+
end
34+
35+
it 'should return :undef if it is the last possibility' do
36+
scope.function_pick_default(['', :undefined, :undef]).should == :undef
37+
end
38+
39+
it 'should return :undefined if it is the last possibility' do
40+
scope.function_pick_default([:undef, '', :undefined]).should == :undefined
41+
end
42+
43+
it 'should return the empty string if it is the only possibility' do
44+
scope.function_pick_default(['']).should == ''
45+
end
46+
47+
it 'should return :undef if it is the only possibility' do
48+
scope.function_pick_default([:undef]).should == :undef
49+
end
50+
51+
it 'should return :undefined if it is the only possibility' do
52+
scope.function_pick_default([:undefined]).should == :undefined
53+
end
54+
55+
it 'should error if no values are passed' do
56+
expect { scope.function_pick_default([]) }.to raise_error(Puppet::Error, /Must receive at least one argument./)
57+
end
58+
end

0 commit comments

Comments
 (0)