Skip to content

Commit

Permalink
Allow undef parameter passing with nil
Browse files Browse the repository at this point in the history
Now, when `nil` is used as a param value, its `inspect` method is not
called to construct the Puppet parameter value. Instead, the unquoted
string `'undef'` is used in the generated resource declaration. So now
`let(:params) { { :foo => nil } }` leads to the desired resource
declaration `class { 'test': foo => undef }`.

Puppet `undef` is nearly equivalent to Ruby `nil`, and there is no `nil`
in Puppet, so it makes sense to do this translation.

Before this change, it was impossible to pass `undef` as a parameter.
Using `nil`, `:undef`, or `'undef'` all failed for various reasons.

Credit to the discussion at [1] for identifying the issue and proposing
an alternate solution.

Fixes rodjek#279.

[1] <https://groups.google.com/forum/#!topic/puppet-users/6nL2eROH8is>
  • Loading branch information
jmoldow committed Apr 30, 2015
1 parent 4ff4fd2 commit 5989cbd
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 2 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,12 @@ let(:title) { 'foo' }
let(:params) { {:ensure => 'present', ...} }
```

##### Passing Puppet's `undef` as a parameter value

```ruby
let(:params) { {:user => nil, ...} }
```

#### Specifying the FQDN of the test node

If the manifest you're testing expects to run on host with a particular name,
Expand Down
6 changes: 4 additions & 2 deletions lib/rspec-puppet/support.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,10 @@ def facts_hash(node)

def param_str
params.keys.map do |r|
param_val = escape_special_chars(params[r].inspect)
"#{r.to_s} => #{param_val}"
param_val = params[r]
is_undef = param_val.nil?
param_val_str = escape_special_chars(is_undef ? 'undef' : param_val.inspect)
"#{r.to_s} => #{param_val_str}"
end.join(', ')
end

Expand Down
18 changes: 18 additions & 0 deletions spec/classes/test_undef_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
require 'spec_helper'

describe 'test_undef' do
it { should compile.with_all_deps }

shared_examples 'exec echo' do
it { should contain_exec('/bin/echo foo').with_user(nil) }
end

context 'with user => nil' do
let(:params) { { :user => nil } }
include_examples 'exec echo'
end

context 'with params unset' do
include_examples 'exec echo'
end
end
20 changes: 20 additions & 0 deletions spec/defines/test_undef_def_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
require 'spec_helper'

describe 'test_undef::def' do
let(:title) { '/bin/echo foo' }

it { should compile.with_all_deps }

shared_examples 'exec echo' do
it { should contain_exec('/bin/echo foo').with_user(nil) }
end

context 'with user => nil' do
let(:params) { { :user => nil } }
include_examples 'exec echo'
end

context 'with params unset' do
include_examples 'exec echo'
end
end
5 changes: 5 additions & 0 deletions spec/fixtures/modules/test_undef/manifests/def.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
define test_undef::def($user = undef) {
exec { '/bin/echo foo' :
user => $user,
}
}
5 changes: 5 additions & 0 deletions spec/fixtures/modules/test_undef/manifests/init.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class test_undef($user = undef) {
exec { '/bin/echo foo':
user => $user,
}
}

0 comments on commit 5989cbd

Please sign in to comment.